123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import {
- buyVip,
- buyNum,
- } from '~/api/user'
- import {
- getProducts,
- getVoucher
- } from '~/api/global'
- Component({
- /**
- * 组件的属性列表
- */
- properties: {
- },
- /**
- * 组件的初始数据
- */
- data: {
- state: false,
- // 红包类型
- type: '',
- // 优惠金额
- preferential: '',
- productVip: 0,
- productNum: 0,
- // 红包id
- id: '',
- voucherType: ''
- },
- /**
- * 组件的方法列表
- */
- methods: {
- open(data) {
- this.getProducts()
- let {
- type,
- id,
- voucherType,
- preferential
- } = data
- this.setData({
- state: true,
- type,
- id,
- voucherType,
- preferential
- })
- },
- close() {
- this.setData({
- state: false
- })
- this.triggerEvent('reload')
- },
- async getProducts() {
- let products = await getProducts()
- let productVip = products.find(item => {
- return item.type == 1
- })
- let productNum = products.find(item => {
- return item.type == 2
- })
- this.setData({
- productVip,
- productNum
- })
- },
- async getVoucher() {
- if (!this.data.voucherType) {
- let res = await getVoucher({
- id: this.data.id
- })
- this.toBuy(res.type == 1 ? '1001' : '1010')
- } else {
- this.toBuy(this.data.voucherType == 1 ? '1001' : '1010')
- }
- },
- //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
- async toBuy(productId) {
- wx.showLoading({
- title: '提交中',
- mask: true
- })
- let res = ''
- if (productId == '1001') {
- res = await buyVip({
- productId
- }).finally(() => {
- wx.hideLoading()
- })
- } else if (productId == '1010') {
- res = await buyNum({
- productId
- }).finally(() => {
- wx.hideLoading()
- })
- } else {
- wx.hideLoading()
- wx.showToast({
- title: "支付失败,请重试",
- icon: "none"
- })
- }
- let {
- timeStamp,
- nonceStr,
- signType,
- paySign
- } = res
- // package保留字
- wx.requestPayment({
- timeStamp,
- nonceStr,
- package: res.package,
- signType,
- paySign,
- success: (res) => {
- wx.showToast({
- title: "支付成功",
- duration: 2500
- })
- this.close()
- },
- fail(res) {
- wx.showToast({
- title: "支付失败",
- icon: "none"
- })
- }
- })
- },
- }
- })
|