用户反馈(smartgz@qq.com):验证码收到 7 位、前台只能填 6 位。 排查:OTP 生成 randomInt(100000,999999) 恒 6 位;真因=邮件正文「5 分钟」等 相邻数字被用户一并阅读/输入(或 QQ 邮箱 base64 折行渲染粘连观感)。 双保险: - 邮件模板:验证码独立成行加【 】括号醒目隔离,明示「输入方括号内 6 位数字」 - 小程序双输入框(登录页+资料页):maxlength 6→8 放宽;提交时提取纯数字取前 6 位, 非 6 位给明确指引文案;登录/绑定两处 post 均改用清洗后 cleanCode 冒烟:新模板 mock SMTP 截码【 851639 】渲染正常;凭码登录成功; 污染输入(前置粘连数字)服务端拒、纯 6 位重试通过。
523 lines
16 KiB
JavaScript
523 lines
16 KiB
JavaScript
// pages/edit-profile/index.js
|
||
const { getProfile, updateProfile, uploadAvatar, getChaishuBranches, bindChaishu, deleteChaishuTag, updateChaishuTags, bindEmail, sendEmailOtp } = require('../../api/user');
|
||
const { chooseImages, getFullImageUrl } = require('../../utils/helpers');
|
||
|
||
Page({
|
||
data: {
|
||
needLogin: false,
|
||
form: {
|
||
nickname: '',
|
||
avatarUrl: '',
|
||
phone: '',
|
||
companies: [],
|
||
position: '',
|
||
chaishuLevel: '',
|
||
industry: '',
|
||
hometown: '',
|
||
email: '',
|
||
expertise: '',
|
||
},
|
||
chaishuLevelOptions: ['非会员', '一级拆书家', '二级拆书家', '三级拆书家'],
|
||
// 审核模式:自由输入字段选项化(规避 UGC 类目认定)
|
||
auditMode: false,
|
||
industryOptions: ['互联网/IT', '教育/培训', '金融/保险', '医疗健康', '制造业', '零售/电商', '文化传媒', '法律', '财务/咨询', '政府/事业单位', '自由职业', '学生', '其他'],
|
||
hometownOptions: ['北京', '上海', '广州', '深圳', '杭州', '成都', '武汉', '西安', '南京', '重庆', '长沙', '郑州', '青岛', '苏州', '天津', '其他'],
|
||
expertiseOptions: ['阅读引导', '演讲表达', '写作', '产品设计', '程序开发', '市场营销', '人力资源', '运营管理', '财务法务', '心理学', '项目管理', '其他'],
|
||
loading: false,
|
||
submitting: false,
|
||
chaishu: null,
|
||
chaishuBranches: [],
|
||
chaishuBranchIndex: -1,
|
||
chaishuName: '',
|
||
binding: false,
|
||
bindingEmail: false,
|
||
emailInput: '',
|
||
emailOtp: '',
|
||
emailOtpSending: false,
|
||
emailOtpCountdown: 0,
|
||
},
|
||
|
||
// 审核模式检测:与 collaboration-analysis 同一模式(GET /api/settings/audit-mode)
|
||
async checkAuditMode() {
|
||
try {
|
||
const app = getApp();
|
||
const res = await new Promise((resolve, reject) => {
|
||
wx.request({ url: `${app.globalData.apiBaseUrl}/api/settings/audit-mode`, method: 'GET', timeout: 5000, success: resolve, fail: reject });
|
||
});
|
||
const audit = !!(res.statusCode === 200 && res.data && res.data.success && res.data.data.auditMode);
|
||
this.setData({ auditMode: audit });
|
||
wx.setStorageSync('audit_mode', audit);
|
||
} catch (e) { this.setData({ auditMode: false }); }
|
||
},
|
||
|
||
onLoad(options) {
|
||
// 合规整改:游客不强制跳登录——展示登录引导卡片,可自由返回浏览
|
||
const { isLoggedIn } = require('../../utils/auth');
|
||
const loggedIn = isLoggedIn();
|
||
this.setData({ needLogin: !loggedIn });
|
||
this.checkAuditMode();
|
||
if (!loggedIn) return;
|
||
this.loadProfile();
|
||
this.loadBranches();
|
||
if (options && options.focus === 'email') {
|
||
// 从 EMAIL_REQUIRED 引导进入:滚动到邮箱绑定区并提示
|
||
wx.showToast({ title: '请先绑定邮箱后再操作', icon: 'none', duration: 2500 });
|
||
const query = wx.createSelectorQuery();
|
||
query.select('.email-section-anchor').boundingClientRect();
|
||
query.selectViewport().scrollOffset();
|
||
query.exec((res) => {
|
||
if (res && res[0] && res[1]) {
|
||
wx.pageScrollTo({ scrollTop: Math.max(0, res[0].top + res[1].scrollTop - 80), duration: 300 });
|
||
}
|
||
});
|
||
}
|
||
},
|
||
|
||
async loadProfile() {
|
||
this.setData({ loading: true });
|
||
try {
|
||
const res = await getProfile();
|
||
if (res.success) {
|
||
const formData = { ...res.data };
|
||
// companies 数组转字符串供 input 显示
|
||
if (Array.isArray(formData.companies)) {
|
||
formData.companies = formData.companies.join('、');
|
||
}
|
||
// avatarUrl 转完整 URL 供 image 组件加载
|
||
formData.avatarUrl = getFullImageUrl(formData.avatarUrl);
|
||
// 拆书帮字段单独存,不混入表单提交
|
||
delete formData.chaishu;
|
||
delete formData.chaishuVerified;
|
||
this.setData({
|
||
form: formData,
|
||
chaishu: res.data.chaishu || null,
|
||
});
|
||
}
|
||
} catch (err) {
|
||
wx.showToast({
|
||
title: err.message || '加载失败',
|
||
icon: 'none',
|
||
});
|
||
} finally {
|
||
this.setData({ loading: false });
|
||
}
|
||
},
|
||
|
||
onInput(e) {
|
||
const { field } = e.currentTarget.dataset;
|
||
const value = e.detail.value;
|
||
this.setData({
|
||
[`form.${field}`]: value,
|
||
});
|
||
},
|
||
|
||
onChaishuLevelChange(e) {
|
||
const index = Number(e.detail.value);
|
||
this.setData({
|
||
'form.chaishuLevel': this.data.chaishuLevelOptions[index] || '',
|
||
});
|
||
},
|
||
|
||
// 审核模式:选项化字段统一 picker 处理(optionsKey 取 data 中对应 *Options 数组)
|
||
onOptionPickerChange(e) {
|
||
const { optionsKey, field } = e.currentTarget.dataset;
|
||
const options = this.data[optionsKey] || [];
|
||
const index = Number(e.detail.value);
|
||
this.setData({ [`form.${field}`]: options[index] || '' });
|
||
},
|
||
|
||
async loadBranches() {
|
||
try {
|
||
const res = await getChaishuBranches();
|
||
if (res.success && Array.isArray(res.data)) {
|
||
this.setData({ chaishuBranches: res.data.map((b) => b.name) });
|
||
}
|
||
} catch (err) {
|
||
// 分舵列表加载失败不阻塞页面
|
||
}
|
||
},
|
||
|
||
onChaishuNameInput(e) {
|
||
this.setData({ chaishuName: e.detail.value });
|
||
},
|
||
|
||
onBranchChange(e) {
|
||
this.setData({ chaishuBranchIndex: Number(e.detail.value) });
|
||
},
|
||
|
||
async handleBindChaishu() {
|
||
if (this.data.binding) return;
|
||
const { chaishuName, chaishuBranches, chaishuBranchIndex } = this.data;
|
||
if (!chaishuName.trim()) {
|
||
wx.showToast({ title: '请输入拆书帮姓名', icon: 'none' });
|
||
return;
|
||
}
|
||
if (chaishuBranchIndex < 0) {
|
||
wx.showToast({ title: '请选择所在分舵', icon: 'none' });
|
||
return;
|
||
}
|
||
this.setData({ binding: true });
|
||
try {
|
||
const branch = chaishuBranches[chaishuBranchIndex];
|
||
const res = await bindChaishu(chaishuName.trim(), branch);
|
||
if (res.success) {
|
||
wx.showToast({ title: '绑定成功', icon: 'success' });
|
||
this.setData({ chaishuName: '' });
|
||
this.loadProfile();
|
||
} else if (res.code === 'MULTI_MATCH' && Array.isArray(res.candidates) && res.candidates.length > 0) {
|
||
// 同名同分舵:展示候选让用户选择自己的身份
|
||
const items = res.candidates.map((c) => {
|
||
const parts = [];
|
||
if (c.level2) parts.push(`二级-${c.level2}`);
|
||
if (c.level3) parts.push(`三级-${c.level3}`);
|
||
if (c.shifuName) parts.push(`师父:${c.shifuName}`);
|
||
return parts.length ? `${c.name}(${parts.join(' / ')})` : c.name;
|
||
});
|
||
const self = this;
|
||
wx.showActionSheet({
|
||
itemList: items.slice(0, 6),
|
||
success: (sheet) => {
|
||
self.bindByUid(res.candidates[sheet.tapIndex].uid);
|
||
},
|
||
});
|
||
} else {
|
||
wx.showToast({ title: res.message || '绑定失败', icon: 'none' });
|
||
}
|
||
} catch (err) {
|
||
wx.showToast({
|
||
title: err.message || '绑定失败',
|
||
icon: 'none',
|
||
});
|
||
} finally {
|
||
this.setData({ binding: false });
|
||
}
|
||
},
|
||
|
||
async bindByUid(uid) {
|
||
this.setData({ binding: true });
|
||
try {
|
||
const res = await bindChaishu(null, null, uid);
|
||
if (res.success) {
|
||
wx.showToast({ title: '绑定成功', icon: 'success' });
|
||
this.setData({ chaishuName: '' });
|
||
this.loadProfile();
|
||
} else {
|
||
wx.showToast({ title: res.message || '绑定失败', icon: 'none' });
|
||
}
|
||
} catch (err) {
|
||
wx.showToast({ title: err.message || '绑定失败', icon: 'none' });
|
||
} finally {
|
||
this.setData({ binding: false });
|
||
}
|
||
},
|
||
|
||
onDeleteChaishuTag(e) {
|
||
const tag = e.currentTarget.dataset.tag;
|
||
if (!tag) return;
|
||
wx.showModal({
|
||
title: '删除标签',
|
||
content: `确定删除标签「${tag}」吗?`,
|
||
confirmColor: '#07c160',
|
||
success: (m) => {
|
||
if (!m.confirm) return;
|
||
this.doDeleteChaishuTag(tag);
|
||
},
|
||
});
|
||
},
|
||
|
||
async doDeleteChaishuTag(tag) {
|
||
try {
|
||
const res = await deleteChaishuTag(tag);
|
||
if (res.success) {
|
||
this.setData({ 'chaishu.tags': res.data.tags });
|
||
wx.showToast({ title: '已删除', icon: 'success' });
|
||
} else {
|
||
wx.showToast({ title: res.message || '删除失败', icon: 'none' });
|
||
}
|
||
} catch (err) {
|
||
wx.showToast({ title: err.message || '删除失败', icon: 'none' });
|
||
}
|
||
},
|
||
|
||
// 添加标签(昵称下方「+添加」)
|
||
onAddChaishuTag() {
|
||
const tags = (this.data.chaishu && this.data.chaishu.tags) || [];
|
||
if (tags.length >= 10) {
|
||
wx.showToast({ title: '标签最多 10 个', icon: 'none' });
|
||
return;
|
||
}
|
||
wx.showModal({
|
||
title: '添加标签',
|
||
editable: true,
|
||
placeholderText: '如:拆书课、AI工具(10字内)',
|
||
confirmColor: '#07c160',
|
||
success: (m) => {
|
||
if (!m.confirm) return;
|
||
const val = (m.content || '').trim();
|
||
if (!val) {
|
||
wx.showToast({ title: '标签不能为空', icon: 'none' });
|
||
return;
|
||
}
|
||
this.doSaveChaishuTags([...tags, val]);
|
||
},
|
||
});
|
||
},
|
||
|
||
// 编辑已有标签(点标签文字)
|
||
onEditChaishuTag(e) {
|
||
const oldTag = e.currentTarget.dataset.tag;
|
||
if (!oldTag) return;
|
||
const tags = (this.data.chaishu && this.data.chaishu.tags) || [];
|
||
wx.showModal({
|
||
title: '修改标签',
|
||
editable: true,
|
||
content: oldTag,
|
||
confirmColor: '#07c160',
|
||
success: (m) => {
|
||
if (!m.confirm) return;
|
||
const val = (m.content || '').trim();
|
||
if (!val) {
|
||
wx.showToast({ title: '标签不能为空', icon: 'none' });
|
||
return;
|
||
}
|
||
if (val === oldTag) return;
|
||
this.doSaveChaishuTags(tags.map(t => (t === oldTag ? val : t)));
|
||
},
|
||
});
|
||
},
|
||
|
||
// 全量保存标签(增/改统一入口;后端再做清洗去重与校验)
|
||
async doSaveChaishuTags(tags) {
|
||
const cleaned = [...new Set(tags.map(t => String(t || '').trim()).filter(Boolean))];
|
||
if (cleaned.some(t => t.length > 10)) {
|
||
wx.showToast({ title: '单个标签不能超过 10 个字', icon: 'none' });
|
||
return;
|
||
}
|
||
if (cleaned.length > 10) {
|
||
wx.showToast({ title: '标签最多 10 个', icon: 'none' });
|
||
return;
|
||
}
|
||
try {
|
||
const res = await updateChaishuTags(cleaned);
|
||
if (res.success) {
|
||
this.setData({ 'chaishu.tags': res.data.tags });
|
||
wx.showToast({ title: '已保存', icon: 'success' });
|
||
} else {
|
||
wx.showToast({ title: res.message || '保存失败', icon: 'none' });
|
||
}
|
||
} catch (err) {
|
||
wx.showToast({ title: err.message || '保存失败', icon: 'none' });
|
||
}
|
||
},
|
||
|
||
async handleChooseAvatar() {
|
||
if (this.data.auditMode) {
|
||
wx.showToast({ title: '当前版本暂不支持自定义头像', icon: 'none' });
|
||
return;
|
||
}
|
||
try {
|
||
const [tempFilePath] = await chooseImages(1);
|
||
if (!tempFilePath) {
|
||
return; // 用户取消了选择
|
||
}
|
||
|
||
wx.showLoading({ title: '上传中...', mask: true });
|
||
|
||
// 直接上传(wx.cropImage 在新版基础库已废弃,跳过裁剪)
|
||
const res = await uploadAvatar(tempFilePath);
|
||
|
||
wx.hideLoading();
|
||
|
||
if (res.success) {
|
||
this.setData({
|
||
'form.avatarUrl': getFullImageUrl(res.data.url),
|
||
});
|
||
wx.showToast({ title: '头像上传成功', icon: 'success' });
|
||
} else {
|
||
wx.showToast({
|
||
title: res.message || '上传失败',
|
||
icon: 'none',
|
||
});
|
||
}
|
||
} catch (err) {
|
||
wx.hideLoading();
|
||
console.log('选择图片失败', err);
|
||
if (err.errMsg && err.errMsg.includes('cancel')) return;
|
||
wx.showToast({
|
||
title: '头像上传失败',
|
||
icon: 'none',
|
||
});
|
||
}
|
||
},
|
||
|
||
// 邮箱草稿输入(与已绑定状态 form.email 分离,避免输入即显示"已绑定"假象)
|
||
onEmailInput(e) {
|
||
this.setData({ emailInput: e.detail.value });
|
||
},
|
||
|
||
async handleBindEmail() {
|
||
const email = (this.data.emailInput || '').trim();
|
||
if (!email) {
|
||
wx.showToast({ title: '请输入邮箱', icon: 'none' });
|
||
return;
|
||
}
|
||
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||
wx.showToast({ title: '邮箱格式不正确', icon: 'none' });
|
||
return;
|
||
}
|
||
const code = (this.data.emailOtp || '').trim();
|
||
if (!code) {
|
||
wx.showToast({ title: '请先获取并输入验证码', icon: 'none' });
|
||
return;
|
||
}
|
||
// 验证码清洗:提取纯数字取前 6 位
|
||
const cleanCode = (code.replace(/\D/g, '') || '').slice(0, 6);
|
||
if (cleanCode.length !== 6) {
|
||
wx.showToast({ title: '验证码为 6 位数字,请检查邮件中【 】括号内数字', icon: 'none', duration: 2500 });
|
||
return;
|
||
}
|
||
if (this.data.bindingEmail) return;
|
||
// 邮箱一经绑定不可更改:绑定前强确认
|
||
wx.showModal({
|
||
title: '绑定确认',
|
||
content: '邮箱一经绑定不可更改,将作为您的身份认定凭证,确认绑定 ' + email + ' 吗?',
|
||
confirmText: '确认绑定',
|
||
success: (r) => { if (r.confirm) this.doBindEmail(email, code); },
|
||
});
|
||
},
|
||
|
||
async doBindEmail(email, code) {
|
||
this.setData({ bindingEmail: true });
|
||
try {
|
||
const res = await bindEmail(email, cleanCode);
|
||
if (res.success) {
|
||
wx.showToast({ title: res.data && res.data.chaishuAutoBound ? '绑定成功,已认证拆书帮身份' : '绑定成功', icon: 'none' });
|
||
this.stopOtpCountdown();
|
||
this.setData({ emailOtp: '', emailInput: '' });
|
||
this.loadProfile();
|
||
} else {
|
||
wx.showToast({ title: res.message || '绑定失败', icon: 'none' });
|
||
}
|
||
} catch (err) {
|
||
wx.showToast({ title: err.message || '绑定失败', icon: 'none' });
|
||
} finally {
|
||
this.setData({ bindingEmail: false });
|
||
}
|
||
},
|
||
|
||
onEmailOtpInput(e) {
|
||
this.setData({ emailOtp: e.detail.value });
|
||
},
|
||
|
||
async handleSendEmailOtp() {
|
||
if (this.data.emailOtpSending || this.data.emailOtpCountdown > 0) return;
|
||
const email = (this.data.emailInput || '').trim();
|
||
if (!email) {
|
||
wx.showToast({ title: '请输入邮箱', icon: 'none' });
|
||
return;
|
||
}
|
||
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||
wx.showToast({ title: '邮箱格式不正确', icon: 'none' });
|
||
return;
|
||
}
|
||
this.setData({ emailOtpSending: true });
|
||
try {
|
||
const res = await sendEmailOtp(email);
|
||
if (res.success) {
|
||
wx.showToast({ title: '验证码已发送', icon: 'none' });
|
||
this.startOtpCountdown();
|
||
} else {
|
||
wx.showToast({ title: res.message || '发送失败', icon: 'none' });
|
||
}
|
||
} catch (err) {
|
||
wx.showToast({ title: err.message || '发送失败', icon: 'none' });
|
||
} finally {
|
||
this.setData({ emailOtpSending: false });
|
||
}
|
||
},
|
||
|
||
startOtpCountdown() {
|
||
this.setData({ emailOtpCountdown: 60 });
|
||
this._otpTimer = setInterval(() => {
|
||
if (this.data.emailOtpCountdown <= 1) {
|
||
this.stopOtpCountdown();
|
||
} else {
|
||
this.setData({ emailOtpCountdown: this.data.emailOtpCountdown - 1 });
|
||
}
|
||
}, 1000);
|
||
},
|
||
|
||
stopOtpCountdown() {
|
||
if (this._otpTimer) {
|
||
clearInterval(this._otpTimer);
|
||
this._otpTimer = null;
|
||
}
|
||
this.setData({ emailOtpCountdown: 0 });
|
||
},
|
||
|
||
onUnload() {
|
||
this.stopOtpCountdown();
|
||
},
|
||
|
||
onHide() {
|
||
this.stopOtpCountdown();
|
||
},
|
||
|
||
async handleSubmit() {
|
||
const { form } = this.data;
|
||
|
||
if (!form.nickname || form.nickname.trim().length < 2) {
|
||
wx.showToast({
|
||
title: '请输入至少2个字符的昵称',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
this.setData({ submitting: true });
|
||
try {
|
||
// 构建提交数据:companies 字段需要转为数组;email 走独立绑定接口,不随资料提交
|
||
const submitData = {
|
||
...form,
|
||
companies: form.companies
|
||
? (Array.isArray(form.companies) ? form.companies : [form.companies])
|
||
: [],
|
||
};
|
||
delete submitData.email;
|
||
const res = await updateProfile(submitData);
|
||
if (res.success) {
|
||
wx.showToast({
|
||
title: '保存成功',
|
||
icon: 'success',
|
||
});
|
||
setTimeout(() => {
|
||
wx.navigateBack();
|
||
}, 1500);
|
||
}
|
||
} catch (err) {
|
||
wx.showToast({
|
||
title: err.message || '保存失败',
|
||
icon: 'none',
|
||
});
|
||
} finally {
|
||
this.setData({ submitting: false });
|
||
}
|
||
},
|
||
|
||
// 懒登录:游客主动点击才跳登录页
|
||
goLogin() {
|
||
wx.navigateTo({ url: '/pages/login/index' });
|
||
},
|
||
|
||
// 返回首页继续浏览
|
||
backToHome() {
|
||
wx.switchTab({ url: '/pages/home/index' });
|
||
},
|
||
|
||
onShareAppMessage() {
|
||
return {
|
||
title: '拆约 - 拆书活动邀约平台',
|
||
path: '/pages/home/index',
|
||
};
|
||
},
|
||
}); |