123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- import {
- androidbuyVip,
- aliPay,
- buyVip,
- } from '~/api/user'
- import {
- userEvent
- } from '~/api/global'
- let app = getApp()
- Component({
- /**
- * 组件的属性列表
- */
- properties: {
- },
- data: {
- state: false,
- product: {},
- type: "wxpay",
- items: [{
- value: 'wxpay',
- name: '微信',
- checked: true
- },
- {
- value: 'alipay',
- name: '支付宝',
- checked: false
- },
- ]
- },
- methods: {
- open(product) {
- if (!product) {
- return
- }
- this.setData({
- product
- })
- // #if MP
- this.toBuy()
- // #elif ANDROID
- if (this.getTabBar()) {
- this.getTabBar().setData({
- mask: true
- })
- }
- this.setData({
- state: true,
- })
- // #endif
- },
- radioChange(e) {
- const items = this.data.items
- for (let i = 0, len = items.length; i < len; ++i) {
- items[i].checked = items[i].value === e.detail.value
- }
- this.setData({
- items,
- type: e.detail.value
- })
- },
- closeTranscript() {
- if (this.getTabBar()) {
- this.getTabBar().setData({
- mask: true
- })
- }
- this.setData({
- state: false
- })
- },
- async toBuy() {
- // #if MP
- wx.showLoading({
- title: '提交中',
- mask: true
- })
- let res = await buyVip({
- productId: this.data.product.id
- }).finally(() => {
- wx.hideLoading()
- })
- userEvent({
- action: 'ANDROID_PAY_ACTIVITY',
- })
- let {
- timeStamp,
- nonceStr,
- signType,
- paySign
- } = res
- // package保留字
- wx.requestPayment({
- timeStamp,
- nonceStr,
- package: res.package,
- signType,
- paySign,
- success: async (res) => {
- userEvent({
- action: 'ANDROID_PAY_SUCCESS',
- })
- setTimeout(() => {
- this.triggerEvent('reload')
- this.closeTranscript()
- }, 1500)
- },
- fail(res) {
- wx.showToast({
- title: "支付失败",
- icon: "none",
- duration: 3000
- })
- }
- })
- // #elif ANDROID
- 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',
- })
- setTimeout(() => {
- this.triggerEvent('reload')
- this.closeTranscript()
- }, 1500)
- })
- }
- // #endif
- },
- }
- })
|