const userApi = require('../../api/user') const auth = require('../../utils/auth') const { chooseImageFilePath, uploadProfileImage } = require('../../utils/upload') const { buildProfileCard, normalizeAvatar } = require('../../utils/profile-card') const { BRAND_CONFIG } = require('../../utils/config') function resolveRoleText(type) { const role = Number(type) if (role === 2) return '店长' if (role === 3) return '美甲师' if (role === 4) return '管理员' return '管理端' } function buildPageState(profile, fallbackShopName) { const source = profile || {} const card = buildProfileCard(source, fallbackShopName) return { profile: source, avatarText: card.avatarText, roleText: resolveRoleText(source.type), shopName: card.shopName, form: { nickname: source.nickname || '', avatar: normalizeAvatar(source.avatar), contactPhone: source.contactPhone || '', wechatId: source.wechatId || '', signature: source.signature || '' } } } Page({ data: { loading: true, saving: false, avatarUploading: false, profile: {}, avatarText: '用', roleText: '管理端', shopName: BRAND_CONFIG.defaultShopName, form: { nickname: '', avatar: '', contactPhone: '', wechatId: '', signature: '' } }, onLoad() { const app = getApp() const state = buildPageState(app.globalData.userInfo || {}, app.globalData.shopName || BRAND_CONFIG.defaultShopName) this.setData(state) }, onShow() { this.loadProfile() }, async loadProfile() { this.setData({ loading: true }) try { const profile = await userApi.info() this.syncProfileToSession(profile) } catch (error) { wx.showToast({ title: error.message || '个人信息加载失败', icon: 'none' }) } finally { this.setData({ loading: false }) } }, syncProfileToSession(profile) { const app = getApp() const session = auth.getSession() const current = session.userInfo || app.globalData.userInfo || {} const next = { ...current, ...profile, nickname: profile.nickname || '', avatar: profile.avatar || '', contactPhone: profile.contactPhone || '', wechatId: profile.wechatId || '', signature: profile.signature || '', shopName: profile.shopName || current.shopName || session.shopName || app.globalData.shopName || '' } app.setSession({ ...session, shopName: next.shopName || session.shopName, userInfo: next }) this.setData(buildPageState(next, app.globalData.shopName || BRAND_CONFIG.defaultShopName)) }, onInput(event) { this.setData({ [`form.${event.currentTarget.dataset.field}`]: event.detail.value }) }, async chooseAvatar() { if (this.data.avatarUploading || this.data.saving) return try { const filePath = await chooseImageFilePath() if (!filePath) return this.setData({ avatarUploading: true }) const avatar = await uploadProfileImage(filePath) this.setData({ 'form.avatar': avatar }) } catch (error) { wx.showToast({ title: error.message || '头像上传失败', icon: 'none' }) } finally { this.setData({ avatarUploading: false }) } }, removeAvatar() { if (!this.data.avatarUploading && !this.data.saving) this.setData({ 'form.avatar': '' }) }, async save() { if (this.data.saving || this.data.avatarUploading) return const form = this.data.form const nickname = String(form.nickname || '').trim() const contactPhone = String(form.contactPhone || '').trim() if (!nickname) { wx.showToast({ title: '请填写昵称', icon: 'none' }) return } if (contactPhone && !/^1[3-9]\d{9}$/.test(contactPhone)) { wx.showToast({ title: '请输入正确联系电话', icon: 'none' }) return } this.setData({ saving: true }) try { await userApi.updateInfo({ nickname, avatar: form.avatar || '', contactPhone, wechatId: String(form.wechatId || '').trim(), signature: String(form.signature || '').trim() }) const profile = await userApi.info() this.syncProfileToSession(profile) wx.showToast({ title: '已保存', icon: 'success' }) setTimeout(() => wx.navigateBack(), 350) } catch (error) { wx.showToast({ title: error.message || '保存失败', icon: 'none' }) } finally { this.setData({ saving: false }) } } })