diff --git a/JY-Salon-WeChat/app.json b/JY-Salon-WeChat/app.json index fa8da8c4..08b44afb 100644 --- a/JY-Salon-WeChat/app.json +++ b/JY-Salon-WeChat/app.json @@ -15,6 +15,7 @@ "pages/color-setting/index", "pages/employee-orders/index", "pages/profile/index", + "pages/profile-settings/index", "pages/shop-settings/index", "pages/export/index", "pages/password/index", diff --git a/JY-Salon-WeChat/pages/profile-settings/index.js b/JY-Salon-WeChat/pages/profile-settings/index.js new file mode 100644 index 00000000..29c44cac --- /dev/null +++ b/JY-Salon-WeChat/pages/profile-settings/index.js @@ -0,0 +1,177 @@ +const { BRAND_CONFIG } = require('../../utils/config') +const userApi = require('../../api/user') +const auth = require('../../utils/auth') +const { chooseImageFilePath, uploadAvatar } = require('../../utils/upload') + +const DEFAULT_BACKEND_AVATAR_URL = 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png' + +function resolveRoleText(type) { + const role = Number(type) + if (role === 2) return '店长' + if (role === 3) return '美甲师' + if (role === 4) return '管理员' + return '管理端' +} + +function normalizeAvatarUrl(avatar) { + const value = String(avatar || '').trim() + if (!value) return '' + return value.split('?')[0] +} + +function resolveDisplayAvatar(avatar) { + const value = String(avatar || '').trim() + if (!value) return '' + if (normalizeAvatarUrl(value) === DEFAULT_BACKEND_AVATAR_URL) return '' + return value +} + +function buildProfileForm(profile) { + return { + nickname: profile.nickname || '', + avatar: resolveDisplayAvatar(profile.avatar || profile.displayAvatar || ''), + contactPhone: profile.contactPhone || '', + wechatId: profile.wechatId || '', + signature: profile.signature || '' + } +} + +Page({ + data: { + shopName: BRAND_CONFIG.defaultShopName, + roleText: '管理端', + userInfo: {}, + loading: false, + profileSaving: false, + avatarUploading: false, + profileForm: buildProfileForm({}) + }, + + onLoad() { + this.refreshFromSession() + this.loadProfile() + }, + + refreshFromSession() { + const app = getApp() + const userInfo = app.globalData.userInfo || {} + const nextUserInfo = { + ...userInfo, + displayAvatar: resolveDisplayAvatar(userInfo.avatar) + } + this.setData({ + shopName: app.globalData.shopName || BRAND_CONFIG.appName, + roleText: resolveRoleText(nextUserInfo.type), + userInfo: nextUserInfo, + profileForm: buildProfileForm(nextUserInfo) + }) + }, + + 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 currentUserInfo = session.userInfo || app.globalData.userInfo || {} + const nextUserInfo = { + ...currentUserInfo, + id: profile.id || currentUserInfo.id, + account: profile.account || currentUserInfo.account, + nickname: profile.nickname || '', + avatar: profile.avatar || '', + phone: profile.phone || currentUserInfo.phone || '', + contactPhone: profile.contactPhone || '', + wechatId: profile.wechatId || '', + signature: profile.signature || '', + type: profile.type || currentUserInfo.type, + shopId: profile.shopId || currentUserInfo.shopId, + displayAvatar: resolveDisplayAvatar(profile.avatar || '') + } + app.setSession({ + ...session, + userInfo: nextUserInfo + }) + this.setData({ + userInfo: nextUserInfo, + roleText: resolveRoleText(nextUserInfo.type), + profileForm: buildProfileForm(nextUserInfo) + }) + }, + + onProfileInput(event) { + const field = event.currentTarget.dataset.field + this.setData({ + [`profileForm.${field}`]: event.detail.value + }) + }, + + async chooseProfileAvatar() { + if (this.data.loading || this.data.avatarUploading || this.data.profileSaving) { + return + } + try { + const filePath = await chooseImageFilePath() + if (!filePath) { + return + } + this.setData({ avatarUploading: true }) + const avatar = await uploadAvatar(filePath) + const profile = await userApi.info() + this.syncProfileToSession({ + ...profile, + avatar: profile.avatar || avatar + }) + this.setData({ 'profileForm.avatar': resolveDisplayAvatar(profile.avatar || avatar) }) + } catch (error) { + wx.showToast({ title: error.message || '头像上传失败', icon: 'none' }) + } finally { + this.setData({ avatarUploading: false }) + } + }, + + removeProfileAvatar() { + if (this.data.avatarUploading || this.data.profileSaving) { + return + } + this.setData({ 'profileForm.avatar': '' }) + }, + + async saveProfile() { + if (this.data.loading || this.data.profileSaving || this.data.avatarUploading) { + return + } + const form = this.data.profileForm + const nickname = String(form.nickname || '').trim() + if (!nickname) { + wx.showToast({ title: '请填写昵称', icon: 'none' }) + return + } + this.setData({ profileSaving: true }) + try { + await userApi.updateInfo({ + nickname, + avatar: form.avatar || '', + contactPhone: String(form.contactPhone || '').trim(), + wechatId: String(form.wechatId || '').trim(), + signature: String(form.signature || '').trim() + }) + const profile = await userApi.info() + this.syncProfileToSession(profile) + wx.showToast({ title: '已保存', icon: 'success' }) + } catch (error) { + wx.showToast({ title: error.message || '保存失败', icon: 'none' }) + } finally { + this.setData({ profileSaving: false }) + } + } +}) diff --git a/JY-Salon-WeChat/pages/profile-settings/index.json b/JY-Salon-WeChat/pages/profile-settings/index.json new file mode 100644 index 00000000..a2e0676e --- /dev/null +++ b/JY-Salon-WeChat/pages/profile-settings/index.json @@ -0,0 +1,3 @@ +{ + "navigationBarTitleText": "个人资料" +} diff --git a/JY-Salon-WeChat/pages/profile-settings/index.wxml b/JY-Salon-WeChat/pages/profile-settings/index.wxml new file mode 100644 index 00000000..5dc6208f --- /dev/null +++ b/JY-Salon-WeChat/pages/profile-settings/index.wxml @@ -0,0 +1,73 @@ + + 个人资料加载中 + + + 个人资料 + 把你的姓名、联系方式和头像整理成店内可识别的名片。 + + + + + + + + + 头像 + + {{profileForm.nickname || userInfo.nickname || '个人头像'}} + {{roleText}} · {{shopName}} + + + + + + + + + + + 登录账号 + {{userInfo.account || '未设置'}} + + + 身份 + {{roleText}} + + + 登录手机号 + {{userInfo.phone || '未绑定'}} + + + 当前店铺 + {{shopName}} + + + + + + 资料档案 + 用于店内识别与客户沟通 + + + 昵称 + + + + 联系电话 + + + + 微信号 + + + + 签名 +