优化:个人信息改为独立页面

This commit is contained in:
2026-10-10 17:32:26 +08:00
parent 72bfcffc25
commit 804c69c296
12 changed files with 226 additions and 326 deletions
+140
View File
@@ -0,0 +1,140 @@
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 })
}
}
})
@@ -0,0 +1,3 @@
{
"navigationBarTitleText": "个人信息"
}
@@ -0,0 +1,33 @@
<view class="page no-tab profile-edit-page">
<view wx:if="{{loading}}" class="loading-line">正在加载个人信息...</view>
<view class="card avatar-card">
<view class="avatar-preview">
<image wx:if="{{form.avatar}}" src="{{form.avatar}}" mode="aspectFill"></image>
<text wx:else>{{avatarText}}</text>
</view>
<view class="avatar-copy">
<text class="avatar-title">个人头像</text>
<text class="avatar-desc">上传清晰头像,方便门店成员识别</text>
</view>
<button class="avatar-action" loading="{{avatarUploading}}" disabled="{{saving}}" bindtap="chooseAvatar">{{form.avatar ? '更换' : '上传'}}</button>
</view>
<button wx:if="{{form.avatar}}" class="remove-avatar" disabled="{{saving || avatarUploading}}" bindtap="removeAvatar">移除当前头像</button>
<view class="section-title">基本资料</view>
<view class="card form-card">
<view class="field"><text class="field-label required">昵称</text><input value="{{form.nickname}}" data-field="nickname" bindinput="onInput" maxlength="30" placeholder="请输入昵称" /></view>
<view class="field"><text class="field-label">联系电话</text><input value="{{form.contactPhone}}" data-field="contactPhone" bindinput="onInput" type="number" maxlength="11" placeholder="请输入联系电话" /></view>
<view class="field"><text class="field-label">微信号</text><input value="{{form.wechatId}}" data-field="wechatId" bindinput="onInput" maxlength="50" placeholder="请输入微信号" /></view>
<view class="field signature-field"><text class="field-label">个性签名</text><textarea value="{{form.signature}}" data-field="signature" bindinput="onInput" maxlength="200" placeholder="请输入个人签名" /></view>
</view>
<view class="section-title account-title">账号信息</view>
<view class="card readonly-card">
<view class="readonly-row"><text>登录账号</text><text>{{profile.account || '未设置'}}</text></view>
<view class="readonly-row"><text>所属门店</text><text>{{shopName}}</text></view>
<view class="readonly-row"><text>身份</text><text>{{roleText}}</text></view>
</view>
<view class="fixed-bottom"><button class="primary-btn" loading="{{saving}}" disabled="{{loading || avatarUploading}}" bindtap="save">保存个人信息</button></view>
</view>
@@ -0,0 +1,23 @@
.profile-edit-page { padding-top:24rpx; }
.loading-line { margin-bottom:16rpx; color:#8e9c94; font-size:22rpx; text-align:center; }
.avatar-card { display:flex; align-items:center; padding:26rpx; }
.avatar-preview { display:flex; width:104rpx; height:104rpx; flex:none; align-items:center; justify-content:center; overflow:hidden; border-radius:26rpx; background:#6f8f83; color:#fff; font-size:40rpx; font-weight:900; }
.avatar-preview image { width:100%; height:100%; }
.avatar-copy { display:flex; min-width:0; flex:1; flex-direction:column; margin-left:20rpx; }
.avatar-title { color:#30483c; font-size:28rpx; font-weight:800; }
.avatar-desc { margin-top:8rpx; color:#8a9a91; font-size:21rpx; line-height:1.5; }
.avatar-action { min-width:112rpx; height:60rpx; margin-left:16rpx; padding:0 20rpx; border-radius:999rpx; background:#e5eee9; color:#4e7165; font-size:23rpx; font-weight:800; line-height:60rpx; }
.remove-avatar { width:auto; margin:14rpx 6rpx 0 auto; color:#a36b5d; font-size:22rpx; }
.section-title { margin:30rpx 4rpx 14rpx; }
.form-card { padding:0 24rpx; }
.field-label { width:170rpx; }
.field input,.field textarea { text-align:right; }
.signature-field { align-items:flex-start; }
.signature-field .field-label { padding-top:34rpx; }
.signature-field textarea { text-align:left; }
.account-title { margin-top:26rpx; }
.readonly-card { padding:0 24rpx; }
.readonly-row { display:flex; min-height:92rpx; align-items:center; justify-content:space-between; gap:30rpx; border-bottom:1rpx solid #eee8df; font-size:25rpx; }
.readonly-row:last-child { border-bottom:0; }
.readonly-row text:first-child { color:#625c55; font-weight:700; }
.readonly-row text:last-child { min-width:0; color:#8a837b; text-align:right; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }