// pages/mine/comp_profile/comp_profile.js Component({ properties: { visible: { type: Boolean, value: false } }, data: { editData: { name: '', sex: 'male', birth: '1970-1-1', province: '', city: '', district: '', phone: '' }, today: '' }, lifetimes: { attached() { const today = new Date() this.setData({ today: `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}` }) }, ready() { this.initEditData() } }, observers: { visible(newVal) { if (newVal) { this.initEditData() } } }, methods: { initEditData() { const app = getApp() const user = app.globalData.user || {} const profile = user.profile || {} const location = user.location || {} let birth = '' if (profile.birth) { const date = new Date(profile.birth) birth = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}` } this.setData({ editData: { avatar: profile.avatar || '/images/home-active2.png', name: profile.name || '', phone: profile.mobile || '', sex: profile.sex || '', birth: birth, province: location.province || '', city: location.city || '', district: location.district || '' } }) }, onClose() { this.triggerEvent('close') }, onSheetTap() {}, onChooseAvatar() { wx.chooseMedia({ count: 1, mediaType: ['image'], sourceType: ['album', 'camera'], success: (res) => { const tempFilePath = res.tempFiles[0].tempFilePath this.setData({ 'editData.avatar': tempFilePath }) } }) }, onSelectSex(e) { const sex = e.currentTarget.dataset.sex this.setData({ 'editData.sex': sex }) }, onBirthChange(e) { this.setData({ 'editData.birth': e.detail.value }) }, onRegionChange(e) { const region = e.detail.value this.setData({ 'editData.province': region[0] || '', 'editData.city': region[1] || '', 'editData.district': region[2] || '' }) }, onFieldInput(e) { const field = e.currentTarget.dataset.field this.setData({ [`editData.${field}`]: e.detail.value }) }, onSave() { const editData = this.data.editData if (editData.idnumber && editData.idnumber.length !== 18) { wx.showToast({ title: '身份证号应为18位', icon: 'none' }) return } this.triggerEvent('save', { editData: editData }) } } })