- Add GET /api/v1/subscription/plans public endpoint (no auth needed) that reads prices from system_settings table - Subscribe-plans page now loads prices from server on show - Falls back to hardcoded defaults if API fails - Add api.getPlans() to miniprogram API module
116 行
3.1 KiB
JavaScript
116 行
3.1 KiB
JavaScript
var api = require('../../utils/api')
|
|
var config = require('../../config/env')
|
|
|
|
Page({
|
|
data: {
|
|
statusBarHeight: 44,
|
|
plans: [],
|
|
selected: 'yearly',
|
|
purchasing: false,
|
|
subscription: null,
|
|
subRemaining: 0
|
|
},
|
|
|
|
onBack: function () {
|
|
wx.navigateBack({
|
|
fail: function () {
|
|
wx.reLaunch({ url: '/pages/index/index' })
|
|
}
|
|
})
|
|
},
|
|
|
|
onLoad: function () {
|
|
var app = getApp()
|
|
this.setData({ statusBarHeight: app.globalData.statusBarHeight })
|
|
},
|
|
|
|
onShow: function () {
|
|
this.loadPlans()
|
|
this.loadSubscription()
|
|
},
|
|
|
|
loadPlans: function () {
|
|
var self = this
|
|
api.getPlans().then(function (data) {
|
|
var plans = (data.plans || []).filter(function (p) { return p.key !== 'trial' }).map(function (p) {
|
|
var daily = p.days > 0 ? (p.price / p.days).toFixed(1) : 0
|
|
return {
|
|
key: p.key,
|
|
name: p.name,
|
|
price: p.price,
|
|
priceLabel: '¥' + p.price,
|
|
desc: p.key === 'yearly' ? '省¥' + ((p.price / 12 * 12) > 0 ? Math.round(self._monthlyPrice(data.plans) * 12 - p.price) : '') : '约' + daily + '元/天',
|
|
tag: p.key === 'yearly' ? '推荐' : ''
|
|
}
|
|
})
|
|
if (plans.length > 0) self.setData({ plans: plans })
|
|
}).catch(function () {
|
|
self.setData({
|
|
plans: [
|
|
{ key: 'monthly', name: '月卡', price: 99, priceLabel: '¥99', desc: '约3.3元/天' },
|
|
{ key: 'yearly', name: '年卡', price: 899, priceLabel: '¥899', desc: '省¥289', tag: '推荐' }
|
|
]
|
|
})
|
|
})
|
|
},
|
|
|
|
_monthlyPrice: function (plans) {
|
|
for (var i = 0; i < plans.length; i++) {
|
|
if (plans[i].key === 'monthly') return plans[i].price
|
|
}
|
|
return 99
|
|
},
|
|
|
|
loadSubscription: function () {
|
|
var self = this
|
|
api.getSubscription().then(function (sub) {
|
|
self.setData({
|
|
subscription: sub,
|
|
subRemaining: sub.remaining_days || 0
|
|
})
|
|
}).catch(function () {})
|
|
},
|
|
|
|
onSelect: function (e) {
|
|
this.setData({ selected: e.currentTarget.dataset.plan })
|
|
},
|
|
|
|
onPurchase: function () {
|
|
var self = this
|
|
var selected = this.data.selected
|
|
var plan = null
|
|
for (var i = 0; i < this.data.plans.length; i++) {
|
|
if (this.data.plans[i].key === selected) {
|
|
plan = this.data.plans[i]
|
|
break
|
|
}
|
|
}
|
|
if (!plan) return
|
|
|
|
wx.showModal({
|
|
title: '确认支付',
|
|
content: '模拟支付 ¥' + plan.price + '?(测试环境)',
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
self.doPurchase(plan)
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
doPurchase: function (plan) {
|
|
var self = this
|
|
self.setData({ purchasing: true })
|
|
api.purchase(plan.key).then(function (order) {
|
|
self.setData({ purchasing: false })
|
|
wx.showToast({ title: '支付成功', icon: 'success' })
|
|
setTimeout(function () {
|
|
wx.redirectTo({ url: '/pages/subscribe-success/subscribe-success?plan=' + plan.key })
|
|
}, 1000)
|
|
}).catch(function (err) {
|
|
self.setData({ purchasing: false })
|
|
wx.showToast({ title: err.message || '支付失败', icon: 'none' })
|
|
})
|
|
}
|
|
})
|