Browse Source

Merge branch 'iteration_0307' into online

lex 1 year ago
parent
commit
1d8ac258d8
3 changed files with 161 additions and 26 deletions
  1. 36 3
      src/state.ts
  2. 54 7
      src/views/adapay/pay-define/index.tsx
  3. 71 16
      src/views/adapay/pay-result/index.tsx

+ 36 - 3
src/state.ts

@@ -67,7 +67,7 @@ export const openDefaultWebView = (url?: string, callBack?: any) => {
 
 /**
  * @description 微信授权-会根据环境去判断
- * @param wxAppId 
+ * @param wxAppId
  * @param urlString 回调链接【默认当前页面地址】
  * @returns void
  */
@@ -75,8 +75,9 @@ export const goWechatAuth = (wxAppId: string, urlString?: string) => {
   // 开发环境
   if (import.meta.env.DEV) {
     const replaceUrl =
-      `https://online.lexiaoya.cn/getWxCode?appid=${wxAppId || 'wx8654c671631cfade'}&state=STATE&redirect_uri=` +
-      encodeURIComponent(urlString || window.location.href)
+      `https://online.lexiaoya.cn/getWxCode?appid=${
+        wxAppId || 'wx8654c671631cfade'
+      }&state=STATE&redirect_uri=` + encodeURIComponent(urlString || window.location.href)
     window.location.replace(replaceUrl)
   }
 
@@ -96,3 +97,35 @@ const goAuth = (wxAppId: string, urlString?: string) => {
   const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect`
   window.location.replace(url)
 }
+
+/**
+ * @description 支付宝授权-会根据环境去判断
+ * @param wxAppId
+ * @param urlString 回调链接【默认当前页面地址】
+ * @returns void
+ */
+export const goAliAuth = (alipayAppId: string, urlString?: string) => {
+  // 支付宝授权
+  const urlNow = encodeURIComponent(urlString || window.location.href)
+  const appid = alipayAppId || '2021004100630808'
+  // 开发环境
+  if (import.meta.env.DEV) {
+    const url = `https://kt.colexiu.com/getAliCode?app_id=${appid}&state=STATE&redirect_uri=${urlNow}`
+    window.location.replace(url)
+  }
+
+  // 生产环境
+  if (import.meta.env.PROD) {
+    alipayAuth(alipayAppId, urlString)
+  }
+}
+
+const alipayAuth = (alipayAppId: string, urlString?: string) => {
+  // 用户授权
+  const urlNow = encodeURIComponent(urlString || window.location.href)
+  const scope = 'auth_base' //snsapi_userinfo   //静默授权 用户无感知
+  const appid = alipayAppId || '2021004100630808'
+  // 判断是否是线上
+  const url = `https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=${appid}&redirect_uri=${urlNow}&response_type=auth_code&scope=${scope}&state=STATE`
+  window.location.replace(url)
+}

+ 54 - 7
src/views/adapay/pay-define/index.tsx

@@ -17,11 +17,14 @@ export default defineComponent({
       code: null as any,
       pay_channel: route.query.pay_channel as any,
       wxAppId: route.query.wxAppId as any,
+      paymentType: route.query.paymentType as any,
+      alipayAppId: route.query.alipayAppId as any,
       body: route.query.body as any,
       price: route.query.price as any,
       orderNo: route.query.orderNo as any,
       userId: route.query.userId as any,
-      payInfo: {} as any
+      payInfo: {} as any,
+      isYeePay: false // 是否为易宝支付
     })
 
     const getPayment = async () => {
@@ -47,6 +50,7 @@ export default defineComponent({
           }
         })
 
+        state.isYeePay = data.paymentVender?.indexOf('yeepay') !== -1
         console.log(data, 'payment')
         scanCodePay(data.reqParams)
       } catch (e) {
@@ -58,13 +62,23 @@ export default defineComponent({
     const scanCodePay = (data: any) => {
       // 判断支付方式 如果是 test 模式 支付用测试url 否则用生产url
       if (state.pay_channel == 'alipay_qr') {
-        const url =
-          data.prod_mode === 'false'
-            ? data.expend.qrcode_url + '?payment_id=' + data.id + '&pay_channel=' + data.pay_channel
-            : data.expend.qrcode_url
-        window.location.href = url
+        if (state.isYeePay) {
+          tradePay(data.prePayTn)
+        } else {
+          const url =
+            data.prod_mode === 'false'
+              ? data.expend.qrcode_url +
+                '?payment_id=' +
+                data.id +
+                '&pay_channel=' +
+                data.pay_channel
+              : data.expend.qrcode_url
+          window.location.href = url
+        }
       } else if (state.pay_channel == 'wx_pub') {
-        const tempPayInfo = JSON.parse(data.expend.pay_info)
+        const tempPayInfo = state.isYeePay
+          ? JSON.parse(data.prePayTn)
+          : JSON.parse(data.expend.pay_info)
         state.payInfo = tempPayInfo
         if (typeof (window as any).WeixinJSBridge == 'undefined') {
           if (document.addEventListener) {
@@ -83,6 +97,39 @@ export default defineComponent({
       }
     }
 
+    // 由于js的载入是异步的,所以可以通过该方法,当AlipayJSBridgeReady事件发生后,再执行callback方法
+    const ready = (callback: any) => {
+      if ((window as any).AlipayJSBridge) {
+        callback && callback()
+      } else {
+        document.addEventListener('AlipayJSBridgeReady', callback, false)
+      }
+    }
+    const tradePay = (tradeNO: any) => {
+      ready(function () {
+        // 通过传入交易号唤起快捷调用方式(注意tradeNO大小写严格)
+        ;(window as any).AlipayJSBridge.call(
+          'tradePay',
+          {
+            tradeNO: tradeNO
+          },
+          function (data: any) {
+            if ('9000' == data.resultCode) {
+              window.location.replace(
+                location.origin + '/orchestra-student/#/payment-result?orderNo=' + state.orderNo
+              )
+            } else {
+              window.location.replace(
+                location.origin + '/orchestra-student/#/payment-result?orderNo=' + state.orderNo
+              )
+            }
+            //使用的支付宝内置api实现关闭H5
+            ;(window as any).AlipayJSBridge.call('closeWebview')
+          }
+        )
+      })
+    }
+
     const onBridgeReady = () => {
       const payInfo = state.payInfo
       // let orderNo = state.orderNo

+ 71 - 16
src/views/adapay/pay-result/index.tsx

@@ -1,6 +1,6 @@
 import request from '@/helpers/request'
 import { browser, getUrlCode, moneyFormat } from '@/helpers/utils'
-import { goWechatAuth } from '@/state'
+import { goAliAuth, goWechatAuth } from '@/state'
 import { Cell, CellGroup, Icon, Loading, showConfirmDialog, showToast } from 'vant'
 import { defineComponent, onMounted, reactive } from 'vue'
 import { useRouter, useRoute } from 'vue-router'
@@ -15,20 +15,22 @@ export default defineComponent({
       errorText: '',
       browserStatus: false,
       code: null as any,
+      paymentType: route.query.paymentType as any,
       pay_channel: route.query.pay_channel as any,
       wxAppId: route.query.wxAppId as any,
+      alipayAppId: route.query.alipayAppId as any,
       body: route.query.body as any,
       price: route.query.price as any,
       orderNo: route.query.orderNo as any,
       userId: route.query.userId as any,
-      payInfo: {} as any
+      payInfo: {} as any,
+      isYeePay: false // 是否为易宝支付
     })
 
-    console.log(state.errorText)
-
     const init = () => {
       const query = route.query
       console.log(query)
+      const isYeePay = state.paymentType.indexOf('yeepay') !== -1
       // 判断是否有支付对象
       if (!query.orderNo || !query.pay_channel) {
         showConfirmDialog({
@@ -43,8 +45,19 @@ export default defineComponent({
           state.browserStatus = true
           getWxPay()
         } else if (browser().alipay) {
-          state.browserStatus = true
-          getPayment()
+          if (isYeePay) {
+            const code = getUrlCode('auth_code')
+            if (code) {
+              state.code = code // 赋值code码
+              state.browserStatus = true
+              getPayment()
+            } else {
+              goAliAuth(state.alipayAppId)
+            }
+          } else {
+            state.browserStatus = true
+            getPayment()
+          }
         } else {
           state.errorText = '请在微信或支付宝客户端打开'
           document.title = 'ERROR'
@@ -74,6 +87,7 @@ export default defineComponent({
           }
         })
 
+        state.isYeePay = data.paymentVender?.indexOf('yeepay') !== -1
         console.log(data, 'payment', state.pay_channel)
         scanCodePay(data.reqParams)
       } catch (e) {
@@ -86,22 +100,63 @@ export default defineComponent({
       }
     }
 
+    // 由于js的载入是异步的,所以可以通过该方法,当AlipayJSBridgeReady事件发生后,再执行callback方法
+    const ready = (callback: any) => {
+      if ((window as any).AlipayJSBridge) {
+        callback && callback()
+      } else {
+        document.addEventListener('AlipayJSBridgeReady', callback, false)
+      }
+    }
+    const tradePay = (tradeNO: any) => {
+      ready(function () {
+        // 通过传入交易号唤起快捷调用方式(注意tradeNO大小写严格)
+        ;(window as any).AlipayJSBridge.call(
+          'tradePay',
+          {
+            tradeNO: tradeNO
+          },
+          function (data: any) {
+            if ('9000' == data.resultCode) {
+              window.location.replace(
+                location.origin + '/orchestra-student/#/payment-result?orderNo=' + state.orderNo
+              )
+            } else {
+              window.location.replace(
+                location.origin + '/orchestra-student/#/payment-result?orderNo=' + state.orderNo
+              )
+            }
+            //使用的支付宝内置api实现关闭H5
+            ;(window as any).AlipayJSBridge.call('closeWebview')
+          }
+        )
+      })
+    }
+
     const scanCodePay = (data: any) => {
       // 判断支付方式 如果是 test 模式 支付用测试url 否则用生产url
       if (state.pay_channel == 'alipay_qr') {
-        const url =
-          data.prod_mode === 'false'
-            ? data?.expend?.qrcode_url +
-              '?payment_id=' +
-              data.id +
-              '&pay_channel=' +
-              data.pay_channel
-            : data?.expend?.qrcode_url
-        window.location.href = url
+        if (state.isYeePay) {
+          tradePay(data.tradeNO)
+        } else {
+          const url =
+            data.prod_mode === 'false'
+              ? data?.expend?.qrcode_url +
+                '?payment_id=' +
+                data.id +
+                '&pay_channel=' +
+                data.pay_channel
+              : data?.expend?.qrcode_url
+          window.location.href = url
+        }
       } else if (state.pay_channel == 'alipay_wap') {
         window.location.href = data?.expend?.pay_info
       } else if (state.pay_channel == 'wx_pub') {
-        const tempPayInfo = data?.expend ? JSON.parse(data?.expend?.pay_info) : data
+        const tempPayInfo = state.isYeePay
+          ? JSON.parse(data.prePayTn)
+          : data?.expend
+          ? JSON.parse(data?.expend?.pay_info)
+          : data
         state.payInfo = tempPayInfo
         if (typeof (window as any).WeixinJSBridge == 'undefined') {
           if (document.addEventListener) {