import { updatesessionkey, userInfo, vips, vipVirtualPayment } from "../../utils/request" interface VipPlan { id: number name: string price: number desc: string recommend?: boolean selected: boolean } Component({ properties: { visible: { type: Boolean, value: false, }, }, data: { benefits: [ { title: '更低生成成本', desc: '享受更多优惠', iconClass: 'cost-icon' }, { title: '专属会员标识', desc: '尊享身份标识', iconClass: 'badge-icon' }, { title: '优先体验新功能', desc: '抢先体验特权', iconClass: 'flash-icon' }, ], plans: [ { id: 1, name: '月卡会员', price: 25, desc: '¥25/月', selected: false }, { id: 2, name: '半年卡会员', price: 98, desc: '¥16.3/月', selected: false }, { id: 3, name: '年卡会员', price: 168, desc: '¥14/月', recommend: true, selected: true }, { id: 4, name: '终身会员', price: 298, desc: '一次付费\n永久使用', selected: false }, ] as VipPlan[], selectedPackage: null as VipPlan | null }, lifetimes: { attached() { vips().then((res: any) => { if (!res.code) { let array = []; for (let q = 0; q < res.data.length; q++) { let item = res.data[q]; item.price = (+item.price).toFixed(0); array.push(item); } this.setData({ plans: array }); } }) }, detached() { } }, attached() { vips().then((res: any) => { if (!res.code) { let array = []; for (let q = 0; q < res.data.length; q++) { let item = res.data[q]; item.price = (+item.price).toFixed(0); array.push(item); } this.setData({ plans: array }); } }) }, detached() { }, pageLifetimes: { }, methods: { noop() { }, handleOverlayTap() { this.handleClose() }, handleClose() { this.triggerEvent('close') }, handlePlanTap(e: WechatMiniprogram.TouchEvent) { const id = e.currentTarget.dataset.id this.data.plans.forEach((value, i) => { if (id == value.id) { console.log(value); this.setData({ selectedPackage: value, }) } }) }, handleConfirm() { this.handleBuy() }, handleBuy() { const that = this; if (that.data.selectedPackage == null) { return; } wx.showLoading({ title: '处理中...', mask: true }); wx.checkSession({ success() { that.payment(); }, fail() { wx.login({ success(login) { updatesessionkey(login.code).then((res: any) => { if (!res.code) { that.payment(); } else { wx.showModal({ title: '温馨提示', content: res.message, showCancel: false }) } }) } }) wx.hideLoading(); } }) }, payment() { let that = this; vipVirtualPayment((that.data.selectedPackage || { id: 0 }).id).then((res: any) => { wx.requestVirtualPayment({ signData: JSON.stringify(res.param.signData), paySig: res.param.paySig, signature: res.param.signature, mode: res.param.mode, success(result: any) { wx.hideLoading(); console.log(result) userInfo().then((res: any) => { if (!res.code) { wx.setStorageSync('userInfo', res.data); that.setData({ userInfo: res.data }); } }) }, fail(error: any) { wx.hideLoading(); console.log(error); wx.showModal({ title: '温馨提示', content: '支付失败', showCancel: false }) } }); }).catch(() => { wx.hideLoading(); wx.showModal({ title: '温馨提示', content: '系统繁忙, 请稍后再试', showCancel: false }) }) } }, })