大修改
This commit is contained in:
1 parent
a1cbc9404a
commit
e81481623c
47 files changed
+1453
-285
No files matched your search
@@ -3,19 +3,18 @@ const api = require('../../utils/api');
|
||||
const store = require('../../utils/store');
|
||||
const format = require('../../utils/format');
|
||||
|
||||
const POLL_INTERVAL = 1500;
|
||||
const POLL_MAX = 8;
|
||||
const app = getApp();
|
||||
|
||||
Page({
|
||||
data: {
|
||||
code: '',
|
||||
owned: false,
|
||||
loading: true,
|
||||
buying: false,
|
||||
buyText: '',
|
||||
contactText: '',
|
||||
VideoTitle: '',
|
||||
VideoDetails: '',
|
||||
coverUrl: '',
|
||||
priceYuan: '',
|
||||
playdata: [],
|
||||
VideoPlayerSrc: '',
|
||||
HiddenVideoControl: true,
|
||||
@@ -29,11 +28,20 @@ Page({
|
||||
return;
|
||||
}
|
||||
this.setData({ code: code });
|
||||
this.load(code);
|
||||
},
|
||||
|
||||
/** 首次进入和从购买页返回都会走这里:买完回来要立刻变成已拥有。 */
|
||||
onShow() {
|
||||
if (this.data.code && this.data.owned) this.load(this.data.code);
|
||||
app.ready().then(() => this.applyConfig());
|
||||
if (this.data.code) this.load(this.data.code);
|
||||
},
|
||||
|
||||
/** 按钮文案取自后台配置,后台改完小程序下拉刷新即可生效。 */
|
||||
applyConfig() {
|
||||
this.setData({
|
||||
buyText: store.cfg('course.button.buy'),
|
||||
contactText: store.cfg('course.button.contact'),
|
||||
});
|
||||
},
|
||||
|
||||
load(code) {
|
||||
@@ -46,7 +54,6 @@ Page({
|
||||
VideoTitle: course.title || '',
|
||||
VideoDetails: course.intro || '',
|
||||
coverUrl: course.coverUrl || '',
|
||||
priceYuan: format.fenToYuan(course.priceFen),
|
||||
});
|
||||
if (course.owned) this.loadLessons(code);
|
||||
})
|
||||
@@ -96,57 +103,10 @@ Page({
|
||||
});
|
||||
},
|
||||
|
||||
/** 购买:下单 → 拉起支付 → 轮询订单状态确认发货。 */
|
||||
buyNow() {
|
||||
if (this.data.buying) return;
|
||||
if (typeof wx.requestVirtualPayment !== 'function') {
|
||||
wx.showToast({ title: '当前微信版本不支持支付,请升级微信', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
this.setData({ buying: true });
|
||||
wx.showLoading({ title: '发起支付', mask: true });
|
||||
|
||||
api
|
||||
.createOrder(this.data.code)
|
||||
.then((order) => {
|
||||
wx.hideLoading();
|
||||
return api.requestVirtualPayment(order.payData).then(() => order.outTradeNo);
|
||||
})
|
||||
.then((outTradeNo) => this.waitForDelivery(outTradeNo))
|
||||
.catch((err) => {
|
||||
wx.hideLoading();
|
||||
this.setData({ buying: false });
|
||||
// 用户主动取消不算错误
|
||||
if (err && err.errMsg && err.errMsg.indexOf('cancel') >= 0) return;
|
||||
wx.showToast({ title: (err && err.message) || '支付失败', icon: 'none' });
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 支付成功后权益由服务端发货推送发放,存在秒级延迟,这里轮询确认。
|
||||
* 即便轮询全部超时也不会丢单:服务端有定时查单兜底。
|
||||
*/
|
||||
waitForDelivery(outTradeNo) {
|
||||
let attempts = 0;
|
||||
const poll = () => {
|
||||
attempts += 1;
|
||||
return api.getOrder(outTradeNo).then((order) => {
|
||||
if (order.status === 2) {
|
||||
this.setData({ buying: false, owned: true });
|
||||
wx.showToast({ title: '购买成功', icon: 'success', mask: true });
|
||||
this.load(this.data.code);
|
||||
return;
|
||||
}
|
||||
if (attempts >= POLL_MAX || order.status === 3 || order.status === 4) {
|
||||
this.setData({ buying: false });
|
||||
wx.showToast({ title: '支付已提交,开通可能稍有延迟', icon: 'none', duration: 3000 });
|
||||
return;
|
||||
}
|
||||
return new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL)).then(poll);
|
||||
});
|
||||
};
|
||||
return poll();
|
||||
/** 购买入口:详情页不做交易,统一跳商业化页面。 */
|
||||
goBuy() {
|
||||
if (!this.data.code) return;
|
||||
wx.navigateTo({ url: '/pages/buy/buy?code=' + this.data.code });
|
||||
},
|
||||
|
||||
onShareAppMessage() {
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
</scroll-view>
|
||||
</block>
|
||||
|
||||
<!-- 未拥有:展示介绍与购买入口 -->
|
||||
<!-- 购买入口 -->
|
||||
<block wx:else>
|
||||
<scroll-view class="scrollarea" scroll-y type="list">
|
||||
<view class="jieshao">
|
||||
@@ -41,11 +41,11 @@
|
||||
|
||||
<view class="fixed-btn-wrapper">
|
||||
<view class="btn-group">
|
||||
<button class="DianJiXiangQing1" bind:tap="buyNow" disabled="{{buying}}">
|
||||
<text class="DianJiXiangQing2">{{buying ? '处理中…' : '立即购买 ¥' + priceYuan}}</text>
|
||||
<button class="DianJiXiangQing1" bind:tap="goBuy">
|
||||
<text class="DianJiXiangQing2">{{buyText}}</text>
|
||||
</button>
|
||||
<button class="DianJiXiangQing1" open-type="contact">
|
||||
<text class="DianJiXiangQing2">咨询客服</text>
|
||||
<text class="DianJiXiangQing2">{{contactText}}</text>
|
||||
</button>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
Reference in new issue
Block a user