Files
YelayMaths/yelaymaths/pages/kecheng/kecheng.js
T
2026-10-01 11:43:07 +08:00

76 lines
2.1 KiB
JavaScript

// pages/kecheng/kecheng.js
const api = require('../../utils/api');
const store = require('../../utils/store');
const config = require('../../config/index');
const app = getApp();
Page({
data: {
staticBase: config.STATIC_BASE_URL,
hasCourse: false,
ownedCourses: [],
tipsHtml: '',
/** 账号被封禁:不显示已购权益,改为显示提示。文字全部来自后台配置 */
blocked: false,
blockedTitle: '',
blockedDesc: '',
},
onLoad() {
// 旧版本用 10ms 轮询最多 5 秒等登录数据,这里改为等初始化完成
app.ready().then(() => this.refresh());
},
onShow() {
if (store.isLoggedIn()) this.refresh();
// 未登录(含刚退出登录)不能留着一份别人的已购列表
else this.setData({ ownedCourses: [], hasCourse: false });
},
refresh() {
this.loadTips();
api
.getEntitlements()
.then((list) => {
store.setEntitlements(list);
this.setData({ ownedCourses: list || [], hasCourse: (list || []).length > 0, blocked: false });
})
.catch((err) => {
if (err && err.code === api.ACCOUNT_DISABLED) {
// 服务端不返回权益了,本地缓存也一并清掉,免得露出上一次的已购内容
store.setEntitlements([]);
this.setData({
ownedCourses: [],
hasCourse: false,
blocked: true,
blockedTitle: store.cfg('account.blocked_title'),
blockedDesc: store.cfg('account.blocked_desc'),
});
return;
}
console.warn('[kecheng] 已购内容加载失败', err);
});
},
loadTips() {
if (this.data.tipsHtml) return;
api
.getTips()
.then((tip) => this.setData({ tipsHtml: (tip && tip.content) || ' ' }))
.catch((err) => console.warn('[kecheng] 文案加载失败', err));
},
toMyCourse(e) {
wx.navigateTo({
url: '/pages/VideoDetails/VideoDetails?code=' + e.currentTarget.dataset.code,
});
},
onShareAppMessage() {
return { title: store.cfg('share.title'), path: '/pages/index/index' };
},
});