310 lines
7.9 KiB
JavaScript
310 lines
7.9 KiB
JavaScript
// pages/me/index.js
|
||
const API = require('../../utils/api.js')
|
||
const role = require('../../utils/role.js')
|
||
const { CITY_OPTIONS, CITY_LABELS, cityIndex, cityLabel } = require('../../utils/city.js')
|
||
|
||
// 性别映射
|
||
const SEX_MAP = {
|
||
male: '男',
|
||
female: '女'
|
||
}
|
||
|
||
Page({
|
||
data: {
|
||
user: {},
|
||
identityText: '',
|
||
isAttendant: false,
|
||
serviceAreaLabels: CITY_LABELS,
|
||
serviceAreaIndex: 0,
|
||
serviceAreaText: '',
|
||
// 个人信息编辑态:只开放后端支持自助修改的字段(姓名/性别/生日/地区)
|
||
editing: false,
|
||
saving: false,
|
||
today: '',
|
||
form: {
|
||
name: '',
|
||
sex: '',
|
||
birth: '',
|
||
province: '',
|
||
city: '',
|
||
district: '',
|
||
regionText: ''
|
||
}
|
||
},
|
||
|
||
onLoad() {
|
||
this.rawUser = null
|
||
this.setData({ today: this.formatDate(new Date()) })
|
||
},
|
||
|
||
onShow() {
|
||
this.loadUser()
|
||
this.loadIdentity()
|
||
},
|
||
|
||
/**
|
||
* 身份文案:管理员/实习陪诊师/正式陪诊师·等级
|
||
*/
|
||
loadIdentity() {
|
||
role
|
||
.ensureRoleSafe()
|
||
.then((detail) => {
|
||
const attendant = detail.attendant || null
|
||
const isAttendant = detail.role === 'attendant'
|
||
this.setData({
|
||
isAttendant,
|
||
identityText: detail.role === 'admin' ? '管理员' : (attendant ? attendant.text : '')
|
||
})
|
||
if (isAttendant) this.loadServiceArea()
|
||
})
|
||
.catch(() => {})
|
||
},
|
||
|
||
/**
|
||
* 服务城市:以员工档案 attendantInfo.serviceAreas 为准
|
||
*/
|
||
loadServiceArea() {
|
||
API.employee
|
||
.getMe()
|
||
.then((res) => {
|
||
if (res.code !== 0 || !res.data || !res.data.employee) return
|
||
const areas = res.data.employee.attendantInfo
|
||
? res.data.employee.attendantInfo.serviceAreas || []
|
||
: []
|
||
this.applyServiceArea(areas[0])
|
||
})
|
||
.catch(() => {})
|
||
},
|
||
|
||
applyServiceArea(value) {
|
||
this.setData({
|
||
serviceAreaIndex: cityIndex(value),
|
||
serviceAreaText: value ? cityLabel(value) : '未设置'
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 修改服务城市:只影响订单池可见范围
|
||
*/
|
||
onServiceAreaChange(e) {
|
||
const index = Number(e.detail.value) || 0
|
||
const option = CITY_OPTIONS[index]
|
||
if (!option) return
|
||
|
||
wx.showLoading({ title: '保存中...' })
|
||
API.employee
|
||
.setServiceArea(option.value)
|
||
.then((res) => {
|
||
wx.hideLoading()
|
||
if (res.code === 0) {
|
||
this.applyServiceArea(option.value)
|
||
wx.showToast({ title: '服务城市已更新', icon: 'success' })
|
||
return
|
||
}
|
||
wx.showToast({ title: res.msg || '保存失败', icon: 'none' })
|
||
})
|
||
.catch((err) => {
|
||
wx.hideLoading()
|
||
wx.showToast({ title: (err && err.message) || '网络错误,请重试', icon: 'none' })
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 证件材料后补入口
|
||
*/
|
||
onMaterialsTap() {
|
||
wx.navigateTo({ url: '/pages/materials/index' })
|
||
},
|
||
|
||
/**
|
||
* 用当前用户数据构建编辑表单
|
||
*/
|
||
buildForm(user) {
|
||
const profile = (user && user.profile) || {}
|
||
const location = (user && user.location) || {}
|
||
const province = location.province || ''
|
||
const city = location.city || ''
|
||
const district = location.district || ''
|
||
return {
|
||
name: profile.name || '',
|
||
sex: profile.sex || 'male',
|
||
birth: this.formatDate(profile.birth),
|
||
province,
|
||
city,
|
||
district,
|
||
regionText: [province, city, district].filter(Boolean).join(' ')
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 进入/退出编辑态:退出时不保存,直接丢弃未提交的修改
|
||
*/
|
||
onToggleEdit() {
|
||
if (this.data.saving) return
|
||
if (this.data.editing) {
|
||
this.setData({ editing: false, form: this.buildForm(this.rawUser) })
|
||
return
|
||
}
|
||
this.setData({ editing: true, form: this.buildForm(this.rawUser) })
|
||
},
|
||
|
||
onFormInput(e) {
|
||
this.setData({ [`form.${e.currentTarget.dataset.field}`]: e.detail.value })
|
||
},
|
||
|
||
onSelectSex(e) {
|
||
this.setData({ 'form.sex': e.currentTarget.dataset.sex })
|
||
},
|
||
|
||
onBirthChange(e) {
|
||
this.setData({ 'form.birth': e.detail.value })
|
||
},
|
||
|
||
onRegionChange(e) {
|
||
const region = e.detail.value || []
|
||
const province = region[0] || ''
|
||
const city = region[1] || ''
|
||
const district = region[2] || ''
|
||
this.setData({
|
||
'form.province': province,
|
||
'form.city': city,
|
||
'form.district': district,
|
||
'form.regionText': [province, city, district].filter(Boolean).join(' ')
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 保存个人信息:/user/update 只接受姓名、性别、生日、手机号与地区,这里提交可自助修改的字段
|
||
*/
|
||
onSaveProfile() {
|
||
if (this.data.saving) return
|
||
|
||
const form = this.data.form
|
||
const name = (form.name || '').trim()
|
||
if (!name) {
|
||
wx.showToast({ title: '请输入姓名', icon: 'none' })
|
||
return
|
||
}
|
||
if (!form.sex) {
|
||
wx.showToast({ title: '请选择性别', icon: 'none' })
|
||
return
|
||
}
|
||
|
||
const app = getApp()
|
||
if (!app.globalData.user) {
|
||
wx.showToast({ title: '请先登录', icon: 'none' })
|
||
return
|
||
}
|
||
|
||
this.setData({ saving: true })
|
||
wx.showLoading({ title: '保存中...' })
|
||
|
||
API.user
|
||
.update({
|
||
profile: {
|
||
name,
|
||
sex: form.sex,
|
||
birth: form.birth || ''
|
||
},
|
||
location: {
|
||
province: form.province,
|
||
city: form.city,
|
||
district: form.district
|
||
}
|
||
})
|
||
.then((res) => {
|
||
wx.hideLoading()
|
||
this.setData({ saving: false })
|
||
if (!res || res.code !== 0) {
|
||
wx.showToast({ title: (res && res.msg) || '保存失败', icon: 'none' })
|
||
return
|
||
}
|
||
// 回写全局用户:响应是脱敏用户(不含本机 token),需保留本地 security
|
||
const prev = app.globalData.user || {}
|
||
const updated = {
|
||
...prev,
|
||
...((res.data && res.data.user) || {}),
|
||
security: prev.security || {}
|
||
}
|
||
app.globalData.user = updated
|
||
this.rawUser = updated
|
||
this.setData({ editing: false })
|
||
this.setUser(updated)
|
||
wx.showToast({ title: '保存成功', icon: 'success' })
|
||
})
|
||
.catch((err) => {
|
||
wx.hideLoading()
|
||
this.setData({ saving: false })
|
||
wx.showToast({ title: (err && err.message) || '网络错误,请重试', icon: 'none' })
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 加载当前登录用户信息
|
||
*/
|
||
loadUser() {
|
||
const app = getApp()
|
||
app.ensureLogin()
|
||
.then(user => {
|
||
this.setUser(user)
|
||
})
|
||
.catch(() => {
|
||
wx.showToast({ title: '获取用户信息失败', icon: 'none' })
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 处理并展示用户信息
|
||
*/
|
||
setUser(user) {
|
||
if (!user) return
|
||
this.rawUser = user
|
||
const profile = user.profile || {}
|
||
const location = user.location || {}
|
||
const meta = user.meta || {}
|
||
|
||
const name = profile.name || '微信用户'
|
||
const locationText = [location.province, location.city, location.district]
|
||
.filter(Boolean).join(' ')
|
||
|
||
this.setData({
|
||
user: {
|
||
avatar: profile.avatar || '',
|
||
avatarText: name[0],
|
||
name,
|
||
mobile: profile.mobile || '',
|
||
sexLabel: SEX_MAP[profile.sex] || '未知',
|
||
birthText: this.formatDate(profile.birth),
|
||
email: profile.email || '',
|
||
idnumber: profile.idnumber || '',
|
||
locationText,
|
||
createText: this.formatDate(meta.createtime)
|
||
}
|
||
})
|
||
|
||
// 编辑中不覆盖用户正在填写的表单
|
||
if (!this.data.editing) {
|
||
this.setData({ form: this.buildForm(user) })
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 格式化日期为 yyyy-MM-dd
|
||
*/
|
||
formatDate(dateStr) {
|
||
if (!dateStr) return ''
|
||
const date = new Date(dateStr)
|
||
// birth 默认值是 0(1970-01-01),视为未填写
|
||
if (isNaN(date.getTime()) || date.getTime() <= 0) return ''
|
||
const pad = (n) => String(n).padStart(2, '0')
|
||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
|
||
},
|
||
|
||
onShareAppMessage() {
|
||
return {
|
||
title: '个人中心',
|
||
path: '/pages/me/index'
|
||
}
|
||
}
|
||
})
|