Pārlūkot izejas kodu

✨ 微信支付:增加 weixin 未绑定的提示引导

YunaiV 1 gadu atpakaļ
vecāks
revīzija
f90096e20a

+ 1 - 1
pages/pay/index.vue

@@ -38,7 +38,7 @@
               </view>
               <view class="check-box ss-flex ss-col-center ss-p-l-10">
                 <view class="userInfo-money ss-m-r-10" v-if="item.value === 'wallet'">
-                  余额: {{ userInfo.money }}元
+                  余额: {{ fen2yuan(userInfo.money) }}元
                 </view>
                 <radio
                   :value="item.value"

+ 2 - 2
sheep/components/s-wallet-card/s-wallet-card.vue

@@ -1,11 +1,10 @@
 <!-- 装修用户组件:用户资产 -->
 <template>
-  <!-- TODO @惠智造:代码合并有问题,可以看看 -->
 	<view class="ss-wallet-menu-wrap ss-flex ss-col-center">
 		<view class="menu-item ss-flex-1 ss-flex-col ss-row-center ss-col-center"
 			@tap="sheep.$router.go('/pages/user/wallet/money')">
 			<view class="value-box ss-flex ss-col-bottom">
-				<view class="value-text ss-line-1">{{ userInfo.money || '0.00' }}</view>
+				<view class="value-text ss-line-1">{{ fen2yuan(userInfo.money) || '0.00' }}</view>
 				<view class="unit-text ss-m-l-6">元</view>
 			</view>
 			<view class="menu-title ss-m-t-28">账户余额</view>
@@ -47,6 +46,7 @@
 		ref
 	} from 'vue';
 	import sheep from '@/sheep';
+  import { fen2yuan } from '../../hooks/useGoods';
 
 	const userInfo = computed(() => sheep.$store('user').userInfo);
 	const numData = computed(() => sheep.$store('user').numData);

+ 39 - 24
sheep/platform/pay.js

@@ -85,56 +85,55 @@ export default class SheepPay {
 
   // 预支付
   prepay(channel) {
-    return new Promise((resolve, reject) => {
+    return new Promise(async (resolve, reject) => {
       let data = {
         id: this.id,
         channelCode: channel,
         channelExtras: {}
       };
-      if (uni.getStorageSync('openid')) {
-        data.openid = uni.getStorageSync('openid');
+      // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid
+      if (['wx_pub'].includes(channel)) {
+        const openid = await sheep.$platform.useProvider('wechat').getOpenid();
+        // 如果获取不到 openid,微信无法发起支付,此时需要引导
+        if (!openid) {
+          this.bindWeixin();
+          return;
+        }
+        data.channelExtras.openid = openid;
       }
       PayOrderApi.submitOrder(data).then((res) => {
         // 成功时
         res.code === 0 && resolve(res);
         // 失败时
+        // TODO 芋艿:这块需要在测试下哈;
         if (res.code !== 0 && res.msg === 'miss_openid') {
-          uni.showModal({
-            title: '微信支付',
-            content: '请先绑定微信再使用微信支付',
-            success: function (res) {
-              if (res.confirm) {
-                sheep.$platform.useProvider('wechat').bind();
-              }
-            },
-          });
+          this.bindWeixin();
         }
       });
     });
   }
   // #ifdef H5
-  // 微信公众号JSSDK支付 TODO 芋艿:待接入
+  // 微信公众号 JSSDK 支付
   async wechatOfficialAccountPay() {
-    let that = this;
-    let { error, data, msg } = await this.prepay();
-    if (error !== 0) {
-      console.log('支付错误', msg);
+    let { code, data } = await this.prepay('wx_pub');
+    if (code !== 0) {
       return;
     }
-    $wxsdk.wxpay(data.pay_data, {
+    // let that = this;
+    $wxsdk.wxpay(data, {
       success: () => {
-        that.payResult('success');
+        this.payResult('success');
       },
       cancel: () => {
-        sheep.$helper.toast('支付已手动取消');
+        this.$helper.toast('支付已手动取消');
       },
       fail: () => {
-        that.payResult('fail');
+        this.payResult('fail');
       },
     });
   }
 
-  //浏览器微信H5支付 TODO 芋艿:待接入
+  // 浏览器微信H5支付 TODO 芋艿:待接入
   async wechatWapPay() {
     const { error, data } = await this.prepay();
     if (error === 0) {
@@ -255,6 +254,20 @@ export default class SheepPay {
       payState: resultType
     });
   }
+
+  // 引导绑定微信
+  bindWeixin() {
+    uni.showModal({
+      title: '微信支付',
+      content: '请先绑定微信再使用微信支付',
+      success: function (res) {
+        if (res.confirm) {
+          sheep.$platform.useProvider('wechat').bind();
+        }
+      },
+    });
+  }
+
 }
 
 export function getPayMethods(channels) {
@@ -295,10 +308,12 @@ export function getPayMethods(channels) {
   // 1. 处理【微信支付】
   const wechatMethod = payMethods[0];
   if ((platform === 'WechatOfficialAccount' && channels.includes('wx_pub'))
-    || platform === 'WechatMiniProgram' && channels.includes('wx_lite')
-    || platform === 'App' && channels.includes('wx_app')) {
+    || (platform === 'WechatMiniProgram' && channels.includes('wx_lite'))
+    || (platform === 'App' && channels.includes('wx_app'))) {
     wechatMethod.disabled = false;
   }
+  wechatMethod.disabled = false; // TODO 芋艿:临时测试
+
   // 2. 处理【支付宝支付】
   const alipayMethod = payMethods[1];
   if ((platform === 'WechatOfficialAccount' && channels.includes('alipay_wap'))

+ 14 - 0
sheep/platform/provider/wechat/miniProgram.js

@@ -109,6 +109,19 @@ function setOpenid(openid) {
   uni.setStorageSync('openid', openid);
 }
 
+// 获得 openid
+async function getOpenid(force = false) {
+  let openid = uni.getStorageSync('openid');
+  if (!openid && force) {
+    const info = await getInfo();
+    if (info && info.openid) {
+      openid = info.openid;
+      setOpenid(openid);
+    }
+  }
+  return openid;
+}
+
 // 获得社交信息
 async function getInfo() {
   const { code, data } = await SocialApi.getSocialUser(socialType);
@@ -195,5 +208,6 @@ export default {
   bindUserPhoneNumber,
   mobileLogin,
   getInfo,
+  getOpenid,
   subscribeMessage,
 };

+ 14 - 0
sheep/platform/provider/wechat/officialAccount.js

@@ -75,6 +75,19 @@ function setOpenid(openid) {
   uni.setStorageSync('openid', openid);
 }
 
+// 获得 openid
+async function getOpenid(force = false) {
+  let openid = uni.getStorageSync('openid');
+  if (!openid && force) {
+    const info = await getInfo();
+    if (info && info.openid) {
+      openid = info.openid;
+      setOpenid(openid);
+    }
+  }
+  return openid;
+}
+
 // 获得社交信息
 async function getInfo() {
   const { code, data } = await SocialApi.getSocialUser(socialType);
@@ -90,5 +103,6 @@ export default {
   bind,
   unbind,
   getInfo,
+  getOpenid,
   jssdk: $wxsdk,
 };