- 添加游客模式,展示公共内容无需强制登录 - 实现游客模块展示打卡、计划、关于我们等功能介绍 - 添加产品介绍页面显示学习成长工具详情 - 优化登录流程,添加用户协议和隐私政策同意选项 - 更新角色标签将老师改为管理员,学员改为成员 - 添加注销账号功能替代退出登录 - 实现webview页面域名白名单安全验证 - 添加隐私政策和用户协议页面 - 优化页面跳转逻辑和错误提示信息 - 统一项目名称从明缘进阶场改为明缘云道场
158 lines
4.8 KiB
JavaScript
158 lines
4.8 KiB
JavaScript
const api = require('../../utils/api');
|
||
const auth = require('../../utils/auth');
|
||
|
||
function errMessage(err) {
|
||
if (err && typeof err === 'object') {
|
||
if (err.detail) return String(err.detail);
|
||
if (err.error) return String(err.error);
|
||
if (err.message) return String(err.message);
|
||
}
|
||
return '加载失败,请稍后再试';
|
||
}
|
||
|
||
Page({
|
||
data: {
|
||
greeting: '',
|
||
displayName: '',
|
||
encouragement: '',
|
||
modules: [],
|
||
introData: null,
|
||
loading: true,
|
||
error: '',
|
||
skeletonItems: [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
||
},
|
||
|
||
onShow() { this.loadHome(); },
|
||
|
||
loadHome(silent) {
|
||
if (!silent) {
|
||
this.setData({ loading: true, error: '' });
|
||
}
|
||
const token = auth.getToken();
|
||
if (!token) {
|
||
// 游客模式:展示公开内容,不强制登录
|
||
this.setData({
|
||
loading: false,
|
||
error: '',
|
||
greeting: '欢迎来到明缘云道场',
|
||
displayName: '游客',
|
||
encouragement: '登录后即可打卡、提交计划',
|
||
modules: this.getGuestModules(),
|
||
introData: this.getIntroData()
|
||
});
|
||
return Promise.resolve();
|
||
}
|
||
return auth.ensureLogin()
|
||
.then((token) => {
|
||
if (!token) {
|
||
this.setData({
|
||
loading: false,
|
||
error: '',
|
||
greeting: '欢迎来到明缘云道场',
|
||
displayName: '游客',
|
||
encouragement: '登录后即可打卡、提交计划',
|
||
modules: this.getGuestModules(),
|
||
introData: this.getIntroData()
|
||
});
|
||
return;
|
||
}
|
||
return api.request({ url: '/api/v1/pages/home' });
|
||
})
|
||
.then((res) => {
|
||
if (!res) return;
|
||
this.setData({
|
||
loading: false,
|
||
error: '',
|
||
greeting: res.greeting || '',
|
||
displayName: res.display_name || '',
|
||
encouragement: res.encouragement || '',
|
||
modules: (res.modules || []).filter((m) => m.visible),
|
||
introData: null
|
||
});
|
||
})
|
||
.catch((err) => {
|
||
this.setData({ loading: false, error: errMessage(err) });
|
||
});
|
||
},
|
||
|
||
getGuestModules() {
|
||
return [
|
||
{ id: 'checkin', icon: '📋', label: '打卡介绍', desc: '每日打卡记录', visible: true, url: '', guest: true },
|
||
{ id: 'plan', icon: '📝', label: '计划介绍', desc: '周/月/年计划', visible: true, url: '', guest: true },
|
||
{ id: 'about', icon: 'ℹ️', label: '关于我们', desc: '了解明缘云道场', visible: true, url: '', guest: true }
|
||
];
|
||
},
|
||
|
||
getIntroData() {
|
||
return {
|
||
title: '产品介绍',
|
||
sections: [
|
||
{
|
||
icon: '📚',
|
||
heading: '学习成长工具',
|
||
text: '明缘云道场是一款专注于个人成长的效率工具,通过每周打卡与计划管理,帮助你养成良好的学习习惯,实现持续进步。好好学习,天天向上。'
|
||
},
|
||
{
|
||
icon: '✅',
|
||
heading: '周打卡',
|
||
text: '每周打卡,支持多种状态记录(正常、迟到、请假、早退、不参加讨论等)。查看小组打卡情况,与同学们互相监督、共同进步。'
|
||
},
|
||
{
|
||
icon: '📝',
|
||
heading: '计划管理',
|
||
text: '提供周计划、月计划、年计划三级管理,支持文字与图片上传。清晰追踪每个周期的目标完成情况,让成长有迹可循。'
|
||
},
|
||
{
|
||
icon: '👥',
|
||
heading: '小组共学',
|
||
text: '加入学习小组,查看组员的打卡与计划完成情况。在集体的氛围中保持学习动力,与志同道合的伙伴一起成长。'
|
||
}
|
||
]
|
||
};
|
||
},
|
||
|
||
onPullDownRefresh() {
|
||
this.loadHome(true).then(() => {
|
||
wx.stopPullDownRefresh();
|
||
});
|
||
},
|
||
|
||
onRetry() {
|
||
this.loadHome();
|
||
},
|
||
|
||
goLogin() {
|
||
wx.switchTab({ url: '/pages/profile/profile' });
|
||
},
|
||
|
||
onModuleTap(e) {
|
||
const url = e.currentTarget.dataset.url;
|
||
const token = auth.getToken();
|
||
// 游客模式:引导登录
|
||
if (!token) {
|
||
wx.showModal({
|
||
title: '登录提示',
|
||
content: '登录后使用此功能,是否前往登录?',
|
||
confirmText: '去登录',
|
||
confirmColor: '#C67B30',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
wx.switchTab({ url: '/pages/profile/profile' });
|
||
}
|
||
}
|
||
});
|
||
return;
|
||
}
|
||
if (!url) return;
|
||
|
||
// 通过 webview redirect 接口:后端验证 token 后设置 HttpOnly cookie,再 302 到目标页
|
||
// H5 页面内的同域请求会自动携带 cookie,H5 端零改动
|
||
const redirectUrl = api.BASE_URL + '/api/v1/webview/redirect'
|
||
+ '?token=' + encodeURIComponent(token)
|
||
+ '&target=' + encodeURIComponent(url);
|
||
wx.navigateTo({ url: '/pages/webview/webview?url=' + encodeURIComponent(redirectUrl) });
|
||
}
|
||
});
|
||
|
||
|