文件
jw-beauty/miniprogram/pages/subscribe-plans/subscribe-plans.js
T
Guoguo 1017fb5dae fix: subscription extend instead of overwrite, improve placeholder pages
- Subscription purchase now extends expire_time when user has active
  subscription, instead of cancelling and replacing
- Admin subscription creation uses same extend logic
- Subscribe page shows "续费" button and extend message for renewals
- Help and contact pages: add pink header, centered icon + text
2026-04-29 08:35:58 -07:00

169 行
5.0 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 serverPlans = data.plans || []
var monthlyPrice = 99
for (var i = 0; i < serverPlans.length; i++) {
if (serverPlans[i].key === 'monthly') monthlyPrice = serverPlans[i].price
}
var plans = serverPlans.map(function (p) {
var daily = p.days > 0 ? (p.price / p.days).toFixed(1) : 0
var desc = ''
if (p.key === 'trial') desc = p.days + '天免费体验'
else if (p.key === 'yearly') desc = '省¥' + Math.round(monthlyPrice * 12 - p.price)
else desc = '约' + daily + '元/天'
return {
key: p.key,
name: p.name,
price: p.price,
priceLabel: p.key === 'trial' ? '免费' : '¥' + p.price,
desc: desc,
tag: p.key === 'yearly' ? '推荐' : '',
disabled: false
}
})
if (plans.length > 0) self.setData({ plans: plans })
self.checkTrialUsed()
}).catch(function () {
self.setData({
plans: [
{ key: 'trial', name: '试用', price: 0, priceLabel: '免费', desc: '7天免费体验', disabled: false },
{ key: 'monthly', name: '月卡', price: 99, priceLabel: '¥99', desc: '约3.3元/天' },
{ key: 'yearly', name: '年卡', price: 899, priceLabel: '¥899', desc: '省¥289', tag: '推荐' }
]
})
self.checkTrialUsed()
})
},
checkTrialUsed: function () {
var self = this
var sub = self.data.subscription
if (sub && sub.trial_used) {
var plans = self.data.plans.map(function (p) {
if (p.key === 'trial') return Object.assign({}, p, { disabled: true, tag: '已使用' })
return p
})
self.setData({ plans: plans })
if (self.data.selected === 'trial') self.setData({ selected: 'yearly' })
}
},
loadSubscription: function () {
var self = this
api.getSubscription().then(function (sub) {
self.setData({
subscription: sub,
subRemaining: sub.remaining_days || 0
})
self.checkTrialUsed()
}).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 || plan.disabled) return
if (plan.key === 'trial') {
wx.showModal({
title: '激活试用',
content: '免费试用 ' + plan.desc + '?',
success: function (res) {
if (res.confirm) self.doActivateTrial()
}
})
return
}
var isRenew = self.data.subscription && self.data.subscription.status === 'active' && self.data.subRemaining > 0
var planDays = plan.key === 'yearly' ? 365 : 30
var title = isRenew ? '确认续费' : '确认支付'
var content = isRenew
? '在现有订阅基础上延长' + planDays + '天,模拟支付 ¥' + plan.price + '?(测试环境)'
: '模拟支付 ¥' + plan.price + '?(测试环境)'
wx.showModal({
title: title,
content: content,
success: function (res) {
if (res.confirm) {
self.doPurchase(plan)
}
}
})
},
doActivateTrial: function () {
var self = this
self.setData({ purchasing: true })
api.activateTrial().then(function () {
self.setData({ purchasing: false })
wx.showToast({ title: '试用已激活', icon: 'success' })
setTimeout(function () {
wx.redirectTo({ url: '/pages/subscribe-success/subscribe-success?plan=trial' })
}, 1000)
}).catch(function (err) {
self.setData({ purchasing: false })
wx.showToast({ title: err.message || '激活失败', icon: 'none' })
})
},
doPurchase: function (plan) {
var self = this
self.setData({ purchasing: true })
api.mockPurchase(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' })
})
}
})