- 读路由 7 处回退改 null:游客 joined/created/templates 自然空集、stats 零计数 - 写路由 8 处加 authenticateUser 401 守卫(profile/account/avatar/share/join/checkin/collab) - admin 守卫路由 3 处改用 req.authUser.id,消除 header 伪造面 - multer 文件名回退改 guest;isJoined 改显式布尔(undefined 会被 JSON 序列化丢弃) - 并入前期已上线未提交改动:拆书 level/level1 迁移与 memberLevelLabel、登录/资料编辑页、后台管理页
301 lines
8.5 KiB
JavaScript
301 lines
8.5 KiB
JavaScript
// pages/edit-profile/index.js
|
||
const { getProfile, updateProfile, uploadAvatar, getChaishuBranches, bindChaishu, deleteChaishuTag, bindEmail } = require('../../api/user');
|
||
const { chooseImages, getFullImageUrl } = require('../../utils/helpers');
|
||
|
||
Page({
|
||
data: {
|
||
form: {
|
||
nickname: '',
|
||
avatarUrl: '',
|
||
phone: '',
|
||
companies: [],
|
||
position: '',
|
||
chaishuLevel: '',
|
||
industry: '',
|
||
hometown: '',
|
||
email: '',
|
||
expertise: '',
|
||
},
|
||
chaishuLevelOptions: ['非拆书家', '一级拆书家', '二级拆书家', '三级拆书家'],
|
||
loading: false,
|
||
submitting: false,
|
||
chaishu: null,
|
||
chaishuBranches: [],
|
||
chaishuBranchIndex: -1,
|
||
chaishuName: '',
|
||
binding: false,
|
||
bindingEmail: false,
|
||
},
|
||
|
||
onLoad() {
|
||
this.loadProfile();
|
||
this.loadBranches();
|
||
},
|
||
|
||
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] || '',
|
||
});
|
||
},
|
||
|
||
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) {
|
||
wx.showToast({ title: '已删除', icon: 'success' });
|
||
this.loadProfile();
|
||
} else {
|
||
wx.showToast({ title: res.message || '删除失败', icon: 'none' });
|
||
}
|
||
} catch (err) {
|
||
wx.showToast({ title: err.message || '删除失败', icon: 'none' });
|
||
}
|
||
},
|
||
|
||
async handleChooseAvatar() {
|
||
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',
|
||
});
|
||
}
|
||
},
|
||
|
||
async handleBindEmail() {
|
||
const email = (this.data.form.email || '').trim();
|
||
if (!email) {
|
||
wx.showToast({ title: '请输入邮箱', icon: 'none' });
|
||
return;
|
||
}
|
||
if (this.data.bindingEmail) return;
|
||
this.setData({ bindingEmail: true });
|
||
try {
|
||
const res = await bindEmail(email);
|
||
if (res.success) {
|
||
const hadEmail = !!this.data.form.email;
|
||
wx.showToast({ title: res.data && res.data.chaishuAutoBound ? '绑定成功,已认证拆书帮身份' : (hadEmail ? '换绑成功' : '绑定成功'), icon: 'none' });
|
||
this.loadProfile();
|
||
} else {
|
||
wx.showToast({ title: res.message || '绑定失败', icon: 'none' });
|
||
}
|
||
} catch (err) {
|
||
wx.showToast({ title: err.message || '绑定失败', icon: 'none' });
|
||
} finally {
|
||
this.setData({ bindingEmail: false });
|
||
}
|
||
},
|
||
|
||
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 });
|
||
}
|
||
},
|
||
|
||
onShareAppMessage() {
|
||
return {
|
||
title: '拆约 - 拆书活动邀约平台',
|
||
path: '/pages/home/index',
|
||
};
|
||
},
|
||
});
|