- 添加游客模式,展示公共内容无需强制登录 - 实现游客模块展示打卡、计划、关于我们等功能介绍 - 添加产品介绍页面显示学习成长工具详情 - 优化登录流程,添加用户协议和隐私政策同意选项 - 更新角色标签将老师改为管理员,学员改为成员 - 添加注销账号功能替代退出登录 - 实现webview页面域名白名单安全验证 - 添加隐私政策和用户协议页面 - 优化页面跳转逻辑和错误提示信息 - 统一项目名称从明缘进阶场改为明缘云道场
258 lines
6.7 KiB
JavaScript
258 lines
6.7 KiB
JavaScript
const api = require('../../utils/api');
|
|
const auth = require('../../utils/auth');
|
|
|
|
const ROLE_LABELS = {
|
|
student: '成员',
|
|
member: '成员',
|
|
teacher: '管理员',
|
|
leader: '组长',
|
|
'group_leader': '组长',
|
|
admin: '管理员'
|
|
};
|
|
const LOGIN_TYPE_LABELS = {
|
|
wechat: '微信授权',
|
|
phone: '手机号',
|
|
admin: '管理员'
|
|
};
|
|
|
|
Page({
|
|
data: {
|
|
view: 'loading',
|
|
profile: null,
|
|
phoneLoading: false,
|
|
phoneError: '',
|
|
loadError: '',
|
|
avatarSrc: '',
|
|
avatarFallback: '明',
|
|
roleLabel: '',
|
|
loginTypeLabel: '',
|
|
classGroups: [],
|
|
avatarUploading: false,
|
|
privacyAgreed: false
|
|
},
|
|
|
|
onShow() {
|
|
this.setData({
|
|
phoneError: '',
|
|
privacyAgreed: !!wx.getStorageSync('privacy_agreed')
|
|
});
|
|
this.loadPage();
|
|
},
|
|
|
|
onPullDownRefresh() {
|
|
this.refresh().then(() => wx.stopPullDownRefresh());
|
|
},
|
|
|
|
refresh() {
|
|
if (this.data.view !== 'profile') {
|
|
return this.loadPage();
|
|
}
|
|
return new Promise((resolve) => {
|
|
api.request({ url: '/api/v1/profile' })
|
|
.then((res) => {
|
|
this.applyProfile(res);
|
|
resolve();
|
|
})
|
|
.catch(() => {
|
|
wx.showToast({ title: '刷新失败', icon: 'none' });
|
|
resolve();
|
|
});
|
|
});
|
|
},
|
|
|
|
loadPage() {
|
|
this.setData({ loadError: '' });
|
|
return auth.ensureLogin()
|
|
.then((token) => {
|
|
if (!token) {
|
|
this.showPhoneLogin();
|
|
return null;
|
|
}
|
|
this.setData({ view: 'loading' });
|
|
return this.loadProfile();
|
|
})
|
|
.catch(() => {
|
|
this.showPhoneLogin();
|
|
});
|
|
},
|
|
|
|
loadProfile() {
|
|
return api.request({ url: '/api/v1/profile' })
|
|
.then((res) => {
|
|
this.applyProfile(res);
|
|
})
|
|
.catch((err) => {
|
|
if (!auth.getToken()) {
|
|
this.showPhoneLogin();
|
|
} else {
|
|
this.setData({
|
|
view: 'loadError',
|
|
loadError: (err && err.error) || '加载失败'
|
|
});
|
|
}
|
|
});
|
|
},
|
|
|
|
applyProfile(res) {
|
|
const displayName = res.display_name || res.name || '';
|
|
const classGroups = (res.class_groups || []).map((g) => ({
|
|
class_id: g.class_id,
|
|
class_code: g.class_code,
|
|
period: g.period,
|
|
group: g.group,
|
|
role: this.roleLabel(g.role)
|
|
}));
|
|
this.setData({
|
|
view: 'profile',
|
|
profile: res,
|
|
phoneError: '',
|
|
loadError: '',
|
|
avatarSrc: this.fullUrl(res.wechat_avatar),
|
|
avatarFallback: this.firstChar(displayName),
|
|
roleLabel: this.roleLabel(res.role),
|
|
loginTypeLabel: this.loginTypeLabel(res.login_type),
|
|
classGroups
|
|
});
|
|
},
|
|
|
|
showPhoneLogin() {
|
|
this.setData({
|
|
view: 'phoneLogin',
|
|
phoneLoading: false,
|
|
privacyAgreed: !!wx.getStorageSync('privacy_agreed')
|
|
});
|
|
},
|
|
|
|
roleLabel(role) {
|
|
return ROLE_LABELS[role] || role || '成员';
|
|
},
|
|
|
|
loginTypeLabel(type) {
|
|
return LOGIN_TYPE_LABELS[type] || type || '手机号';
|
|
},
|
|
|
|
fullUrl(rel) {
|
|
if (!rel) return '';
|
|
return /^https?:\/\//i.test(rel) ? rel : api.STATIC_BASE_URL + rel;
|
|
},
|
|
|
|
firstChar(str) {
|
|
const t = (str && typeof str === 'string') ? str.trim() : '';
|
|
return t ? t.charAt(0) : '明';
|
|
},
|
|
|
|
onChooseAvatar(e) {
|
|
const path = e.detail && e.detail.avatarUrl;
|
|
if (!path) {
|
|
wx.showToast({ title: '未选择头像', icon: 'none' });
|
|
return;
|
|
}
|
|
this.setData({ avatarUploading: true });
|
|
wx.uploadFile({
|
|
url: api.BASE_URL + '/api/v1/profile/avatar',
|
|
filePath: path,
|
|
name: 'file',
|
|
header: { Authorization: 'Bearer ' + auth.getToken() },
|
|
success: (res) => {
|
|
let data = {};
|
|
try {
|
|
data = JSON.parse(res.data);
|
|
} catch (e) {
|
|
data = {};
|
|
}
|
|
if (data.success && data.wechat_avatar) {
|
|
const newAvatar = data.wechat_avatar;
|
|
const newUrl = this.fullUrl(newAvatar);
|
|
this.setData({
|
|
'profile.wechat_avatar': newAvatar,
|
|
avatarSrc: newUrl
|
|
});
|
|
wx.showToast({ title: '头像已更新', icon: 'success' });
|
|
} else {
|
|
wx.showToast({ title: data.error || '上传失败', icon: 'none' });
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
wx.showToast({ title: '上传失败', icon: 'none' });
|
|
},
|
|
complete: () => {
|
|
this.setData({ avatarUploading: false });
|
|
}
|
|
});
|
|
},
|
|
|
|
onGetPhoneNumber(e) {
|
|
if (this.data.phoneLoading) {
|
|
return;
|
|
}
|
|
if (!this.data.privacyAgreed) {
|
|
wx.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none', duration: 2000 });
|
|
return;
|
|
}
|
|
const phoneCode = e.detail && e.detail.code;
|
|
if (!phoneCode) {
|
|
const message = '未授权手机号,请点击上方按钮重试';
|
|
this.setData({ phoneError: message });
|
|
wx.showToast({ title: message, icon: 'none' });
|
|
return;
|
|
}
|
|
this.setData({ phoneLoading: true, phoneError: '' });
|
|
auth.loginWithPhone(phoneCode)
|
|
.then(() => this.loadProfile())
|
|
.catch((err) => {
|
|
const message = (err && err.error) || '手机号登录失败';
|
|
this.setData({ phoneLoading: false, phoneError: message });
|
|
wx.showToast({ title: message, icon: 'none', duration: 2500 });
|
|
});
|
|
},
|
|
|
|
onRetry() {
|
|
this.loadPage();
|
|
},
|
|
|
|
togglePrivacy() {
|
|
const agreed = !this.data.privacyAgreed;
|
|
this.setData({ privacyAgreed: agreed });
|
|
if (agreed) {
|
|
wx.setStorageSync('privacy_agreed', true);
|
|
} else {
|
|
wx.removeStorageSync('privacy_agreed');
|
|
}
|
|
},
|
|
|
|
openPrivacy(e) {
|
|
const type = e.currentTarget.dataset.type || 'privacy';
|
|
wx.navigateTo({ url: '/pages/privacy/privacy?type=' + type });
|
|
},
|
|
|
|
deleteAccount() {
|
|
wx.showModal({
|
|
title: '注销账号',
|
|
content: '确定要注销账号吗?注销后将清除本机登录信息,需要重新登录。',
|
|
confirmText: '注销',
|
|
confirmColor: '#c0392b',
|
|
success: (res) => {
|
|
if (!res.confirm) return;
|
|
wx.showModal({
|
|
title: '再次确认',
|
|
content: '注销后本机登录状态将被清除,你可以使用手机号重新登录。确定继续吗?',
|
|
confirmText: '确认注销',
|
|
confirmColor: '#c0392b',
|
|
success: (res2) => {
|
|
if (!res2.confirm) return;
|
|
// 仅清除前端登录信息
|
|
try {
|
|
api.request({ url: '/api/v1/auth/logout', method: 'POST' }).catch(() => {});
|
|
} catch (e) {}
|
|
auth.clearToken();
|
|
wx.removeStorageSync('privacy_agreed');
|
|
wx.showToast({ title: '已注销', icon: 'success' });
|
|
setTimeout(() => {
|
|
wx.reLaunch({ url: '/pages/home/home' });
|
|
}, 1000);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}); |