// 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: '' }, 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' }) }, /** * 加载当前登录用户信息 */ loadUser() { const app = getApp() app.ensureLogin() .then(user => { this.setUser(user) }) .catch(() => { wx.showToast({ title: '获取用户信息失败', icon: 'none' }) }) }, /** * 处理并展示用户信息 */ setUser(user) { if (!user) return 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) } }) }, /** * 格式化日期为 yyyy-MM-dd */ formatDate(dateStr) { if (!dateStr) return '' const date = new Date(dateStr) if (isNaN(date.getTime())) 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' } } })