index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <view class="kPayPopIdx">
  3. <u-popup :customStyle="{ backgroundColor: '#f4f4f4' }" :show="popShow" :round="10" mode="bottom" @close="popShow = false" closeable>
  4. <view class="o-plr-30 o-ptb-20" style="height: 20vh;">
  5. <view class="l-flex-center c-text-20">请选择支付方式</view>
  6. <view class="c-bg-f o-mt-20 c-radius-16" style="border: 1px solid #eee;">
  7. <u-cell-group :border="false">
  8. <u-cell
  9. @click="payClick"
  10. :border="false"
  11. :iconStyle="{ color: '#02be04', fontSize: '24px' }"
  12. icon="weixin-circle-fill"
  13. isLink
  14. rightIcon="arrow-right"
  15. title="微信支付"
  16. ></u-cell>
  17. </u-cell-group>
  18. </view>
  19. </view>
  20. </u-popup>
  21. </view>
  22. </template>
  23. <script>
  24. /**
  25. * payPop 支付弹窗组件
  26. * @description 本组件主要是支付弹窗。
  27. *
  28. * @property {Boolean} popShow 是否展示弹窗
  29. *
  30. */
  31. // 导入接口
  32. import { API_repayment, API_getUserInfo } from '@/common/api.js';
  33. // 日志打印
  34. import reportErr from '@/util/errorReport/log.js';
  35. export default {
  36. data() {
  37. return {
  38. // 是否展示弹窗
  39. popShow: false,
  40. // 要支付的商品
  41. payParam: {},
  42. // 1是从首页,2是从购物车页面,3从单个商品详情进来
  43. isCar: 1,
  44. // 优惠券的码集合
  45. coupons: ''
  46. };
  47. },
  48. methods: {
  49. // 开启弹窗
  50. popOpen(payParam, isCar, couponList) {
  51. console.log(payParam,isCar,111111111)
  52. this.payParam = payParam;
  53. this.isCar = isCar;
  54. this.coupons = '';
  55. // 优惠券的码集合,只有从优惠券的弹窗进来才会有的
  56. if (couponList) {
  57. console.log('couponList', couponList);
  58. this.coupons = couponList.join(',');
  59. }
  60. this.popShow = true;
  61. },
  62. // 点击支付
  63. async payClick() {
  64. // 获取购物车数据
  65. let shopCarts = this.payParam.shopCarts;
  66. console.log('shopCarts', shopCarts);
  67. // 如果传进来是空数据
  68. if (JSON.stringify(shopCarts) === '{}') {
  69. uni.$u.toast('支付失败');
  70. return;
  71. }
  72. // 商品信息接口参数
  73. const payObj = {};
  74. // 组合成接口数据格式
  75. for (let key in shopCarts) {
  76. let par = shopCarts[key]['imgName'] + '-' + shopCarts[key]['nums'];
  77. // 如果是从首页进来的,不用打折
  78. if (this.isCar === 1) {
  79. // 根据商品数量添加优惠码
  80. const list = [];
  81. for(let i=1;i<=shopCarts[key]['nums'];i++){
  82. list.push(0);
  83. }
  84. payObj[par] = JSON.stringify(list);
  85. } else {
  86. payObj[par] = JSON.stringify(shopCarts[key]['codeList']);
  87. }
  88. }
  89. // 这个参数放大请求body
  90. let paramBody = JSON.stringify(payObj);
  91. // 这个参数放到url的
  92. let paramUrl = {
  93. clientId: this.payParam.clientId,
  94. id: this.payParam.id,
  95. coupons: this.coupons
  96. };
  97. console.log(paramBody,'paramBody')
  98. console.log(paramUrl,'paramUrl')
  99. let res = await API_repayment(paramBody, paramUrl);
  100. // 跟后端约定好了,比如如果是1元的商品,花了折扣价1元,就是实际付0元,就会返回“支付成功”信息,不用再去调微信支付接口了
  101. if (res === '支付成功') {
  102. // 获取优惠券列表
  103. await this.$M_Dp('ACTI_SETCOUPONLIST');
  104. console.log('res11111',res)
  105. uni.$u.toast('支付成功');
  106. // 重新获取用户积分
  107. this.getIntegral();
  108. // 支付成功后在购物车中删除对应的商品
  109. this.delCarPayGoods();
  110. // 支付成功后跳转到成功页
  111. this.$M_Rl(`/otherPages/mine/orderRecord/paySucess`);
  112. return;
  113. }
  114. let payParam = JSON.parse(res.rc_Result);
  115. wx.requestPayment({
  116. timeStamp: payParam.timeStamp,
  117. nonceStr: payParam.nonceStr,
  118. package: payParam.package,
  119. signType: payParam.signType,
  120. paySign: payParam.paySign,
  121. success: async payRes => {
  122. // 获取优惠券列表
  123. await this.$M_Dp('ACTI_SETCOUPONLIST');
  124. console.log('payRes2222',payRes)
  125. uni.$u.toast('支付成功');
  126. // 重新获取用户积分
  127. this.getIntegral();
  128. // 支付成功后在购物车中删除对应的商品
  129. this.delCarPayGoods();
  130. // 支付成功后跳转到成功页
  131. this.$M_Rl(`/otherPages/mine/orderRecord/paySucess?sn=${res.sn}`);
  132. },
  133. fail: err => {
  134. // 错误上报
  135. reportErr('支付失败!!!', err);
  136. uni.$u.toast('支付失败');
  137. }
  138. });
  139. },
  140. // 获取用户积分
  141. async getIntegral() {
  142. let userInfo = this.$M_GS('common', '$S_app_loginInfos');
  143. let res = await API_getUserInfo({
  144. id: userInfo.id
  145. });
  146. this.$M_Cm('MUTA_GETOPENID', res);
  147. },
  148. // 删除购物车商品
  149. delCarPayGoods() {
  150. const shopCarts = this.$M_GS('common', '$S_tabbar_shopCarts');
  151. const payCarts = this.payParam.shopCarts;
  152. // 比较购物车数据和当前支付成功数据,找出没有支付的商品
  153. if(payCarts.constructor === Array) {
  154. for(let i = 0; i < payCarts.length; i++) {
  155. delete shopCarts[payCarts[i]['no']];
  156. }
  157. } else {
  158. for (let key in payCarts) {
  159. delete shopCarts[key];
  160. }
  161. }
  162. // 储存没有支付的商品数据到vuex
  163. this.$M_Dp('ACTI_SHOPCARTS', shopCarts);
  164. }
  165. }
  166. };
  167. </script>