144 lines
3.4 KiB
JavaScript
Raw Normal View History

2026-03-30 18:08:27 +08:00
const auth = require('../../utils/auth');
2026-04-10 16:10:33 +08:00
function normalizeAuthStatus(v) {
if (v == null || v === '') return 0;
const n = Number(v);
return Number.isNaN(n) ? 0 : n;
}
/** 0 未认证 | 1 审核中 | 2 审核通过 | 3 审核失败 | 4 已取消 */
2026-03-30 18:08:27 +08:00
function authStatusText(status) {
const map = {
0: '未认证',
2026-04-10 16:10:33 +08:00
1: '审核中',
2: '审核通过',
3: '审核失败',
4: '已取消'
2026-03-30 18:08:27 +08:00
};
2026-04-10 16:10:33 +08:00
return map[normalizeAuthStatus(status)] || '未认证';
2026-03-31 23:27:35 +08:00
}
function maskPhone(phone) {
if (!phone || phone.length < 7) return phone || '';
return phone.substring(0, 3) + '****' + phone.substring(7);
2026-03-30 18:08:27 +08:00
}
Page({
data: {
loggedIn: false,
2026-03-31 23:27:35 +08:00
profile: {},
maskedPhone: '',
2026-04-10 16:10:33 +08:00
authStatus: 0,
2026-03-31 23:27:35 +08:00
authStatusText: '未认证'
2026-03-30 18:08:27 +08:00
},
onShow() {
const accessToken = wx.getStorageSync('accessToken') || '';
if (!accessToken) {
2026-04-10 16:10:33 +08:00
this.setData({ loggedIn: false, profile: {}, maskedPhone: '', authStatus: 0, authStatusText: '未认证' });
2026-03-30 18:08:27 +08:00
return;
}
this.setData({ loggedIn: true });
this.loadProfile();
},
onPullDownRefresh() {
this.loadProfile().finally(() => wx.stopPullDownRefresh());
},
loadProfile() {
return auth
.fetchMyProfile()
.then((profile) => {
const p = profile || {};
2026-04-10 16:10:33 +08:00
const st = normalizeAuthStatus(p.authStatus != null ? p.authStatus : p.AuthStatus);
2026-03-31 23:27:35 +08:00
const phone = p.mobile || p.phone || '';
2026-03-30 18:08:27 +08:00
this.setData({
profile: p,
2026-03-31 23:27:35 +08:00
maskedPhone: maskPhone(phone),
2026-04-03 17:57:08 +08:00
authStatusText: authStatusText(st),
authStatus: st
2026-03-30 18:08:27 +08:00
});
})
.catch(() => {});
},
goToLogin() {
wx.navigateTo({ url: '/subpackages/mine/login/login' });
},
goToPoints() {
wx.navigateTo({ url: '/subpackages/mine/points/points' });
},
goToCert() {
wx.navigateTo({ url: '/subpackages/mine/cert/cert' });
},
goToRecords() {
wx.navigateTo({ url: '/subpackages/mine/records/records' });
},
2026-03-31 23:27:35 +08:00
goToAuth() {
wx.navigateTo({ url: '/subpackages/mine/auth/auth' });
2026-03-30 18:08:27 +08:00
},
2026-03-31 23:27:35 +08:00
goToOrders() {
2026-04-03 00:47:20 +08:00
wx.navigateTo({ url: '/subpackages/mine/order/order' });
2026-03-30 18:08:27 +08:00
},
2026-03-31 23:27:35 +08:00
goToTeam() {
2026-04-03 00:47:20 +08:00
wx.navigateTo({ url: '/subpackages/mine/team/team' });
2026-03-31 23:27:35 +08:00
},
2026-03-30 18:08:27 +08:00
2026-04-30 17:16:30 +08:00
onLogout() {
wx.showModal({
title: '提示',
content: '确定要退出登录吗?',
confirmText: '退出',
confirmColor: '#e53935',
success: (res) => {
if (!res.confirm) return;
wx.clearStorageSync();
this.setData({ loggedIn: false, profile: {}, maskedPhone: '', authStatus: 0, authStatusText: '未认证' });
}
});
},
2026-05-09 16:19:37 +08:00
onChooseAvatar(e) {
const tempFilePath = e.detail.avatarUrl;
if (!tempFilePath) return;
// 先展示本地预览
// this.setData({ 'profile.avatar': tempFilePath });
wx.showLoading({ title: '上传中...', mask: true });
auth
.uploadFile(tempFilePath)
.then((url) => {
return auth.updateProfile({ "avatarUrl": url }).then(() => url);
})
.then((url) => {
this.setData({ 'profile.avatar': url });
wx.showToast({ title: '头像更新成功', icon: 'success' });
})
.catch((err) => {
wx.showToast({ title: (err && err.message) || '头像更新失败', icon: 'none' });
// 回滚预览,重新加载 profile
this.loadProfile();
})
.finally(() => {
wx.hideLoading();
});
},
onFetchOpenId() {
wx.login({
success: (res) => {
console.log('res', res);
}
});
2026-03-30 18:08:27 +08:00
}
});