import { AppGlobalData } from '../../types/index' const PROVINCES = [ '北京', '天津', '上海', '重庆', '河北', '山西', '辽宁', '吉林', '黑龙江', '江苏', '浙江', '安徽', '福建', '江西', '山东', '河南', '湖北', '湖南', '广东', '海南', '四川', '贵州', '云南', '陕西', '甘肃', '青海', '内蒙古', '广西', '西藏', '宁夏', '新疆', '香港', '澳门', '台湾', ] const app = getApp<{ globalData: AppGlobalData }>() Page({ data: { statusBarHeight: 0, navbarHeight: 44, navbarPaddingRight: 0, form: { nickName: '', bio: '', location: '' }, provinces: PROVINCES, locationIndex: -1, }, onLoad() { const { statusBarHeight, navbarHeight, navbarPaddingRight } = app.globalData.navBarInfo this.setData({ statusBarHeight, navbarHeight, navbarPaddingRight }) const user = app.globalData?.userInfo if (user) { const locationIndex = PROVINCES.indexOf(user.location || '') this.setData({ form: { nickName: user.nickName || '', bio: user.bio || '', location: user.location || '' }, locationIndex, }) } }, onLocationChange(e: WechatMiniprogram.CustomEvent) { const idx = Number(e.detail.value) this.setData({ locationIndex: idx, 'form.location': PROVINCES[idx] }) }, onField(e: WechatMiniprogram.CustomEvent) { this.setData({ [`form.${e.currentTarget.dataset.key}`]: e.detail.value }) }, async onSave() { const { form } = this.data if (!form.nickName.trim()) { wx.showToast({ title: '昵称不能为空', icon: 'none' }) return } wx.showLoading({ title: '保存中...' }) try { await wx.cloud.callFunction({ name: 'updateProfile', data: form }) if (app.globalData.userInfo) { Object.assign(app.globalData.userInfo, form) } wx.hideLoading() wx.showToast({ title: '保存成功', icon: 'success' }) setTimeout(() => wx.navigateBack(), 800) } catch { wx.hideLoading() wx.showToast({ title: '保存失败', icon: 'none' }) } }, onBack() { wx.navigateBack() }, })