// pages/me/index.js // 性别映射 const SEX_MAP = { male: '男', female: '女' } Page({ data: { user: {} }, onShow() { this.loadUser() }, /** * 加载当前登录用户信息 */ 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' } } })