diff --git a/miniprogram/pages/subscribe-plans/subscribe-plans.js b/miniprogram/pages/subscribe-plans/subscribe-plans.js index 03d63b5..e9045a3 100644 --- a/miniprogram/pages/subscribe-plans/subscribe-plans.js +++ b/miniprogram/pages/subscribe-plans/subscribe-plans.js @@ -32,33 +32,52 @@ Page({ 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 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.price, - desc: p.key === 'yearly' ? '省¥' + ((p.price / 12 * 12) > 0 ? Math.round(self._monthlyPrice(data.plans) * 12 - p.price) : '') : '约' + daily + '元/天', - tag: p.key === 'yearly' ? '推荐' : '' + 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() }) }, - _monthlyPrice: function (plans) { - for (var i = 0; i < plans.length; i++) { - if (plans[i].key === 'monthly') return plans[i].price + 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' }) } - return 99 }, loadSubscription: function () { @@ -68,6 +87,7 @@ Page({ subscription: sub, subRemaining: sub.remaining_days || 0 }) + self.checkTrialUsed() }).catch(function () {}) }, @@ -85,7 +105,18 @@ Page({ break } } - if (!plan) return + if (!plan || plan.disabled) return + + if (plan.key === 'trial') { + wx.showModal({ + title: '激活试用', + content: '免费试用 ' + plan.desc + '?', + success: function (res) { + if (res.confirm) self.doActivateTrial() + } + }) + return + } wx.showModal({ title: '确认支付', @@ -98,6 +129,21 @@ Page({ }) }, + 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 }) diff --git a/miniprogram/pages/subscribe-plans/subscribe-plans.wxml b/miniprogram/pages/subscribe-plans/subscribe-plans.wxml index 399c0e8..7e33f32 100644 --- a/miniprogram/pages/subscribe-plans/subscribe-plans.wxml +++ b/miniprogram/pages/subscribe-plans/subscribe-plans.wxml @@ -29,8 +29,8 @@ - - {{item.tag}} + + {{item.tag}} {{item.priceLabel}} {{item.name}} {{item.desc}} diff --git a/miniprogram/pages/subscribe-plans/subscribe-plans.wxss b/miniprogram/pages/subscribe-plans/subscribe-plans.wxss index f7ea7ec..8a6839b 100644 --- a/miniprogram/pages/subscribe-plans/subscribe-plans.wxss +++ b/miniprogram/pages/subscribe-plans/subscribe-plans.wxss @@ -118,3 +118,13 @@ .btn-primary-gold::after { border: none; } + +.plan-disabled { + opacity: 0.5; + pointer-events: none; +} + +.plan-tag-disabled { + background: #ccc; + color: #fff; +} diff --git a/server/src/routes/subscription.js b/server/src/routes/subscription.js index c166e13..2688885 100644 --- a/server/src/routes/subscription.js +++ b/server/src/routes/subscription.js @@ -27,13 +27,15 @@ router.get('/subscription/plans', wrap(async (req, res) => { router.get('/subscription', requireUser, wrap(async (req, res) => { const sub = await subscriptionDao.findActive(req.user.user_id) - if (!sub) return res.json(ok({ status: 'inactive', plan: 'none', remaining_days: 0 })) + const trialUsed = await subscriptionDao.findTrial(req.user.user_id) + if (!sub) return res.json(ok({ status: 'inactive', plan: 'none', remaining_days: 0, trial_used: !!trialUsed })) res.json(ok({ status: sub.remaining_days > 0 ? 'active' : 'expired', plan: sub.plan, start_time: sub.start_time, expire_time: sub.expire_time, - remaining_days: sub.remaining_days + remaining_days: sub.remaining_days, + trial_used: !!trialUsed })) }))