pay.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <template>
  2. <view class="app">
  3. <view class="price-box">
  4. <text>支付金额</text>
  5. <text class="price">{{ money }}</text>
  6. </view>
  7. <view class="pay-type-list">
  8. <!-- #ifndef APP-PLUS -->
  9. <view class="type-item b-b" @click="changePayType(1)">
  10. <text class="icon iconfont iconweixin"></text>
  11. <view class="con">
  12. <text class="tit">微信支付</text>
  13. <text>推荐使用微信支付</text>
  14. </view>
  15. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 1"></radio></label>
  16. </view>
  17. <!-- #endif -->
  18. <view class="type-item b-b" @click="changePayType(1)">
  19. <text class="icon iconfont iconweixin"></text>
  20. <view class="con">
  21. <text class="tit">微信支付</text>
  22. <text>推荐使用微信支付</text>
  23. </view>
  24. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 1"></radio></label>
  25. </view>
  26. <view class="type-item b-b" @click="changePayType(2)">
  27. <text class="icon iconfont iconzhifubao"></text>
  28. <view class="con"><text class="tit">支付宝支付</text></view>
  29. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 2"></radio></label>
  30. </view>
  31. <!-- #ifdef APP-PLUS -->
  32. <!-- #endif -->
  33. <view class="type-item" @click="changePayType(3)">
  34. <text class="icon iconfont iconyue"></text>
  35. <view class="con">
  36. <text class="tit">余额支付</text>
  37. <text>可用余额 ¥{{ now_money }}</text>
  38. </view>
  39. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 3"></radio></label>
  40. </view>
  41. </view>
  42. <text class="mix-btn" :class="{ clickbg: payLoding }" @click="!payLoding ? confirm() : ''">确认支付</text>
  43. </view>
  44. </template>
  45. <script>
  46. import { balance } from '@/api/wallet.js';
  47. import { createOrderkey, computedOrderkey, orderPay } from '@/api/order.js';
  48. import { getUserInfo } from '@/api/user.js';
  49. // #ifdef H5
  50. import weixinObj from '@/plugin/jweixin-module/index.js';
  51. // #endif
  52. import { mapState, mapMutations } from 'vuex';
  53. export default {
  54. data() {
  55. return {
  56. // #ifdef H5
  57. payName: 'weixin',
  58. payType: 1, //支付类型
  59. // #endif
  60. // #ifdef MP-WEIXIN
  61. payName: 'weixin',
  62. payType: 1, //支付类型
  63. // #endif
  64. // #ifdef APP-PLUS
  65. payType: 3,
  66. payName: 'yue',
  67. // #endif
  68. // orderInfo: {},
  69. money: 0.0, //订单金额
  70. now_money: 0.0, //余额
  71. orderKey: '',
  72. orderId: '', //保存订单id
  73. payLoding: false, //判断是否支付中
  74. type: '', //判断是否从订单中进入
  75. // #ifdef H5
  76. froms: '', //保存h5中数据来源对象
  77. // #endif
  78. pinkid: '', //保存拼团商品id
  79. checkedAuto: 0, //判断是否为自动拼团
  80. topify: '',
  81. };
  82. },
  83. computed: {
  84. // #ifdef H5
  85. ...mapState(['weichatObj']),
  86. // #endif
  87. ...mapState('user', ['userInfo', 'orderInfo', 'hasLogin'])
  88. },
  89. onLoad(options) {
  90. this.checkedAuto = options.checked || 0;
  91. this.topify = options.topify || 0;
  92. if (options.type == 1) {
  93. this.type = 1;
  94. this.orderId = options.ordid;
  95. this.money = options.money;
  96. } else {
  97. this.orderKey = options.key;
  98. let prepage = this.$api.prePage();
  99. computedOrderkey({
  100. orderkey: this.orderKey,
  101. couponId: prepage.couponChecked.id, //优惠券编号
  102. addressId: prepage.addressData.id, //地址编号
  103. useIntegral: prepage.checkedPoints ? 1 : 0
  104. }).then(({ data }) => {
  105. // 获取支付金额
  106. this.money = data.result.pay_price;
  107. });
  108. }
  109. // 保存pinkid
  110. if (options.pinkid) {
  111. this.pinkid = options.pinkid;
  112. }
  113. // 载入余额
  114. if ('now_money' in this.userInfo) {
  115. this.now_money = this.userInfo.now_money;
  116. } else {
  117. balance({}).then(({ data }) => {
  118. // 获取余额
  119. this.now_money = data.now_money;
  120. });
  121. }
  122. },
  123. methods: {
  124. ...mapMutations('user', ['setUserInfo', 'setOrderInfo']),
  125. //选择支付方式
  126. changePayType(type) {
  127. this.payType = type;
  128. if (this.payType == 1) {
  129. this.payName = 'weixin';
  130. }
  131. if (this.payType == 2) {
  132. this.payName = 'ali';
  133. }
  134. if (this.payType == 3) {
  135. this.payName = 'yue';
  136. }
  137. // if (this.payType == 4) {
  138. // this.payName = 'consumer';
  139. // }
  140. },
  141. // 支付金额
  142. orderMoneyPay() {
  143. let obj = this;
  144. orderPay({
  145. uni: obj.orderId,
  146. // #ifdef H5
  147. from: obj.froms ? 'weixin' : 'H5', //来源
  148. // #endif
  149. // #ifdef MP-WEIXIN
  150. from: 'routine', //来源
  151. // #endif
  152. // #ifdef APP-PLUS
  153. from: 'app', //来源
  154. // #endif
  155. paytype: obj.payName ,//支付类型 weixin-微信 yue-余额
  156. })
  157. .then(e => {
  158. console.log(e,'zhifu+++++++++')
  159. // 判断是否为余额支付
  160. if (obj.payName == 'yue' && e.data.status == 'SUCCESS') {
  161. if (e.status == 200) {
  162. obj.paySuccessTo();
  163. } else {
  164. obj.$api.msg(msg);
  165. }
  166. }
  167. // #ifdef APP-PLUS
  168. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  169. let da = e.data.result.jsConfig;
  170. console.log('--da--', da);
  171. let data = {
  172. appid:da.appid,
  173. noncestr: da.noncestr,
  174. package: da.package,
  175. partnerid:da.partnerid,
  176. prepayid:da.prepayid,
  177. timestamp: da.timestamp,
  178. sign:da.sign
  179. };
  180. console.log('--data--', data);
  181. uni.requestPayment({
  182. provider: 'wxpay',
  183. orderInfo: data,
  184. success(res) {
  185. console.log(res)
  186. },
  187. fail(res) {
  188. console.log(res,'失败')
  189. }
  190. })
  191. }
  192. if (obj.payName == 'ali') {
  193. const url = e.data.result.jsConfig;
  194. console.log(url, 'url');
  195. uni.requestPayment({
  196. provider: 'alipay',
  197. orderInfo: url,
  198. success: res => {
  199. console.log(res);
  200. uni.showToast({
  201. title: '支付成功',
  202. duration: 2000
  203. });
  204. },
  205. fail: e => {
  206. console.log(e);
  207. },
  208. complete: () => {}
  209. });
  210. obj.payLoding = false;
  211. }
  212. // #endif
  213. // #ifndef APP-PLUS
  214. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  215. let da = e.data.result.jsConfig;
  216. let data = {
  217. // #ifdef H5
  218. timestamp: da.timestamp,
  219. // #endif
  220. // #ifdef MP
  221. timeStamp: da.timestamp,
  222. // #endif
  223. nonceStr: da.nonceStr,
  224. package: da.package,
  225. signType: da.signType,
  226. paySign: da.paySign,
  227. success: function(res) {
  228. obj.paySuccessTo();
  229. },
  230. fail: () => {
  231. uni.navigateTo({
  232. url: '/pages/order/order?state=0'
  233. });
  234. }
  235. };
  236. // #ifdef H5
  237. if (obj.payName == 'weixin') {
  238. weixinObj.chooseWXPay(data);
  239. }
  240. // #endif
  241. // #ifdef MP-WEIXIN
  242. if (obj.payName == 'routine') {
  243. wx.requestPayment(data);
  244. }
  245. // #endif
  246. }
  247. // #endif
  248. uni.hideLoading();
  249. obj.payLoding = false;
  250. })
  251. .catch(e => {
  252. console.log(e);
  253. // 支付完成
  254. uni.hideLoading();
  255. obj.payLoding = false;
  256. });
  257. },
  258. // 支付成功跳转
  259. paySuccessTo() {
  260. getUserInfo({})
  261. .then(({ data }) => {
  262. console.log(data,"data")
  263. this.setUserInfo(data);
  264. })
  265. .catch(e => {
  266. console.log(e);
  267. });
  268. uni.hideLoading();
  269. uni.redirectTo({
  270. url: '/pages/money/paySuccess?orderid=' + this.orderId
  271. });
  272. },
  273. //确认支付
  274. confirm: async function() {
  275. let obj = this;
  276. uni.showLoading({
  277. title: '支付中',
  278. mask: true
  279. });
  280. // 判断是否余额不足
  281. if (obj.payName == 'yue' && +obj.now_money < obj.money) {
  282. uni.hideLoading();
  283. uni.showModal({
  284. title: '提示',
  285. content: '账户余额不足!',
  286. showCancel: false,
  287. success: res => {},
  288. fail: () => {},
  289. complete: () => {}
  290. });
  291. return;
  292. }
  293. // 支付中
  294. obj.payLoding = true;
  295. // #ifdef H5
  296. // 获取当前是否为微信浏览器
  297. obj.froms = uni.getStorageSync('weichatBrowser') || '';
  298. // #endif
  299. // 判断是否为未支付订单中跳转进入
  300. if (obj.type != 1) {
  301. // 初次生成订单
  302. obj.firstCreateOrder();
  303. } else {
  304. console.log("重新提交")
  305. // 已经生成订单未支付
  306. obj.orderMoneyPay();
  307. }
  308. },
  309. // 初次订单创建
  310. firstCreateOrder() {
  311. let obj = this;
  312. // 获取下单页面数据
  313. let prepage = obj.$api.prePage();
  314. let data = {
  315. real_name: prepage.addressData.real_name, //联系人名称
  316. phone: prepage.addressData.phone, //联系人号码
  317. couponId: prepage.couponChecked.id, //优惠券编号
  318. addressId: prepage.addressData.id, //支付地址id
  319. useIntegral: prepage.checkedPoints ? 1 : 0, //是否积分抵扣1为是0为否
  320. payType: obj.payName, //支付类型 weixin-微信 yue-余额
  321. mark: prepage.desc, //备注
  322. // #ifdef H5
  323. from: obj.froms ? 'weixin' : 'H5', //来源
  324. // #endif
  325. // #ifdef MP-WEIXIN
  326. from: 'routine', //来源
  327. // #endif
  328. // #ifdef APP-PLUS
  329. from: 'app', //来源
  330. // #endif
  331. shipping_type: 1, //提货方式 1 快递 2自提
  332. automatic: obj.checkedAuto,
  333. topify: this.topify
  334. };
  335. // 判断是否拼团商品
  336. if (obj.pinkid) {
  337. data.pinkId = obj.pinkid;
  338. }
  339. // 生成订单
  340. createOrderkey(data, obj.orderKey)
  341. .then(({ data, status, msg }) => {
  342. console.log(data,status, msg,"123456");
  343. // 判断是否支付失败
  344. if (data.status == 'ORDER_EXIST') {
  345. uni.showModal({
  346. title: '提示',
  347. content: msg,
  348. showCancel: false
  349. });
  350. uni.hideLoading();
  351. obj.payLoding = false;
  352. return;
  353. }
  354. console.log(data,'dingdan')
  355. // 保存订单号
  356. obj.orderId = data.result.orderId;
  357. // 判断是否为余额支付
  358. if (obj.payName == 'yue') {
  359. if (status == 200 && data.status == 'SUCCESS') {
  360. obj.paySuccessTo();
  361. } else {
  362. obj.$api.msg(msg);
  363. }
  364. } else {
  365. // 立即支付
  366. console.log(1111)
  367. obj.orderMoneyPay();
  368. }
  369. })
  370. .catch(e => {
  371. uni.hideLoading();
  372. obj.payLoding = false;
  373. console.log(e);
  374. });
  375. }
  376. }
  377. };
  378. </script>
  379. <style lang="scss">
  380. .app {
  381. width: 100%;
  382. }
  383. .price-box {
  384. background-color: #fff;
  385. height: 265upx;
  386. display: flex;
  387. flex-direction: column;
  388. justify-content: center;
  389. align-items: center;
  390. font-size: 28upx;
  391. color: #909399;
  392. .price {
  393. font-size: 50upx;
  394. color: #303133;
  395. margin-top: 12upx;
  396. &:before {
  397. content: '¥';
  398. font-size: 40upx;
  399. }
  400. }
  401. }
  402. .pay-type-list {
  403. margin-top: 20upx;
  404. background-color: #fff;
  405. padding-left: 60upx;
  406. .type-item {
  407. height: 120upx;
  408. padding: 20upx 0;
  409. display: flex;
  410. justify-content: space-between;
  411. align-items: center;
  412. padding-right: 60upx;
  413. font-size: 30upx;
  414. position: relative;
  415. }
  416. .icon {
  417. width: 100upx;
  418. font-size: 52upx;
  419. }
  420. .iconyue {
  421. color: #fe8e2e;
  422. }
  423. .iconweixin {
  424. color: #36cb59;
  425. }
  426. .iconzhifubao {
  427. color: #01aaef;
  428. }
  429. .tit {
  430. font-size: $font-lg;
  431. color: $font-color-dark;
  432. margin-bottom: 4upx;
  433. }
  434. .con {
  435. flex: 1;
  436. display: flex;
  437. flex-direction: column;
  438. font-size: $font-sm;
  439. color: $font-color-light;
  440. }
  441. }
  442. .mix-btn {
  443. display: flex;
  444. align-items: center;
  445. justify-content: center;
  446. width: 630upx;
  447. height: 80upx;
  448. margin: 80upx auto 30upx;
  449. font-size: $font-lg;
  450. color: #fff;
  451. background-color: $base-color;
  452. border-radius: 10upx;
  453. /* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4); */
  454. }
  455. .clickbg {
  456. background-color: $color-gray !important;
  457. }
  458. </style>