소스 검색

对接支付宝支付

bayi 1 년 전
부모
커밋
468abf0b8d
4개의 변경된 파일66개의 추가작업 그리고 41개의 파일을 삭제
  1. 2 0
      api/user.js
  2. 6 5
      app.js
  3. 56 35
      components/donutBuy/index.js
  4. 2 1
      utils/request.js

+ 2 - 0
api/user.js

@@ -19,6 +19,8 @@ module.exports = {
     buyVip: data => request('/order', 'post', data, true),
     // 安卓购买vip
     androidbuyVip: data => request('/order/appCreate', 'post', data, true),
+    // 支付宝支付
+    aliPay: data => request('/mobile/aliPay/pay', 'post', data, true),
     //获取是否vip及过期时间
     getVipInfo: data => request('/auth', 'get', data),
     //获取自己朗读的作品

+ 6 - 5
app.js

@@ -18,6 +18,7 @@ App({
         this.updateApplet()
         this.checkIsIos()
         // #elif ANDROID
+        this.initPlugin()
         // #endif
         this.getNavbarInfo()
         await loginLog()
@@ -213,13 +214,12 @@ App({
         console.log("插件ID:" + miniAppPluginId)
         wx.miniapp.loadNativePlugin({
             pluginId: miniAppPluginId,
-            success: (plugin) => { 
+            success: (plugin) => {
+                this.globalData.plugin = plugin
                 console.log('load plugin success', plugin)
-                plugin.initPlugin(({}), (res) => {
+                this.globalData.plugin.initPlugin(({}), (res) => {
                     if (res === "success") {
                         console.log("初始化plugin成功")
-                        var device_uuid = plugin.getDeviceUuid({})
-                        console.log("获取的uuid:" + device_uuid)
                     } else {
                         console.log("初始化plugin失败")
                     }
@@ -239,6 +239,7 @@ App({
         menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
         menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
         windowWidth: 0,
-        windowHeight: 0
+        windowHeight: 0,
+        plugin: {}
     }
 })

+ 56 - 35
components/donutBuy/index.js

@@ -1,10 +1,12 @@
 import {
     androidbuyVip,
+    aliPay,
     buyVip,
 } from '~/api/user'
 import {
     userEvent
 } from '~/api/global'
+let app = getApp()
 Component({
     /**
      * 组件的属性列表
@@ -15,6 +17,7 @@ Component({
     data: {
         state: false,
         product: {},
+        type: "wxpay",
         items: [{
                 value: 'wxpay',
                 name: '微信',
@@ -52,7 +55,8 @@ Component({
                 items[i].checked = items[i].value === e.detail.value
             }
             this.setData({
-                items
+                items,
+                type: e.detail.value
             })
         },
         closeTranscript() {
@@ -64,11 +68,11 @@ Component({
             })
         },
         async toBuy() {
+            // #if MP
             wx.showLoading({
                 title: '提交中',
                 mask: true
             })
-            // #if MP
             let res = await buyVip({
                 productId: this.data.product.id
             }).finally(() => {
@@ -108,40 +112,57 @@ Component({
                 }
             })
             // #elif ANDROID
-            let res = await androidbuyVip({
-                productId: this.data.product.id
-            }).finally(() => {
-                wx.hideLoading()
-            })
-            wx.miniapp.requestPayment({
-                timeStamp: res.timestamp,
-                mchId: res.partnerid,
-                prepayId: res.prepayid,
-                package: res.package,
-                nonceStr: res.noncestr,
-                sign: res.sign,
-                success: async (res) => {
-                    /*  setTimeout(() => {
-                         this.setUserInfo()
-                         this.setData({
-                             mask: true
-                         })
-                     }, 1500)
-                     userEvent({
-                         action: 'ANDROID_PAY_SUCCESS',
-                     }) */
-                },
-                fail(res) {
-                    wx.showToast({
-                        title: "支付失败",
-                        icon: "none",
-                        duration: 3000
+            if (this.data.type == 'wxPay') {
+                let res = await androidbuyVip({
+                    productId: this.data.product.id
+                }).finally(() => {
+                    wx.hideLoading()
+                })
+                wx.miniapp.requestPayment({
+                    timeStamp: res.timestamp,
+                    mchId: res.partnerid,
+                    prepayId: res.prepayid,
+                    package: res.package,
+                    nonceStr: res.noncestr,
+                    sign: res.sign,
+                    success: async (res) => {
+                        userEvent({
+                            action: 'ANDROID_PAY_SUCCESS',
+                        })
+                        setTimeout(() => {
+                            this.triggerEvent('reload')
+                            this.closeTranscript()
+                        }, 1500)
+                    },
+                    fail(res) {
+                        wx.showToast({
+                            title: "支付失败",
+                            icon: "none",
+                            duration: 3000
+                        })
+                    },
+                    complete: (res) => {
+                        console.error('wx.miniapp.requestPayment complete:', res)
+                    }
+                })
+            } else {
+                let res = await aliPay({
+                    productId: this.data.product.id
+                }).finally(() => {
+                    wx.hideLoading()
+                })
+                app.globalData.plugin.aliPay({
+                    orderInfo: res
+                }, (res) => {
+                    userEvent({
+                        action: 'ANDROID_PAY_SUCCESS',
                     })
-                },
-                complete: (res) => {
-                    console.error('wx.miniapp.requestPayment complete:', res)
-                }
-            })
+                    setTimeout(() => {
+                        this.triggerEvent('reload')
+                        this.closeTranscript()
+                    }, 1500)
+                })
+            }
             // #endif
         },
     }

+ 2 - 1
utils/request.js

@@ -17,7 +17,8 @@ if (envVersion == 'develop') {
 function request(url, method, data, oldBaseUrl = false, intercept = true) {
     let header = {
         'uid': wx.getStorageSync('uid') || '',
-        'channelCode': wx.getStorageSync('user').channel == '4001' ? '4001' : '3001'
+        // 'channelCode': wx.getStorageSync('user').channel == '4001' ? '4001' : '3001'
+        'channelCode': 3006
     }
     return new Promise((reslove, reject) => {
         wx.request({