Files
ws_health/xui/wxapp/pages/mine/mine.js
2026-05-25 12:34:16 +08:00

228 lines
5.4 KiB
JavaScript

const API = require('../../utils/api.js')
Page({
data: {
userInfo: {
avatar: '/images/home-active2.png',
name: '用户',
phone: '',
sex: '',
birth: '',
email: '',
idnumber: '',
ssn: '',
isLoggedIn: false
},
profileVisible: false,
menuGroups: [{
groupName: '个人服务',
items: [{
name: '个人信息',
icon: 'verify',
bindtap: 'onTapProfile'
}, {
name: '健康档案',
icon: 'user-vip',
bindtap: 'onTapHealth'
}, {
name: '邮寄地址',
icon: 'location',
bindtap: 'onTapAddress'
}]
}, {
groupName: '陪诊',
items: [{
name: '陪诊记录',
icon: 'assignment',
bindtap: 'onTapEscortRecord'
}]
}, {
groupName: '其他',
items: [{
name: '关于我们',
icon: 'info-circle',
bindtap: 'onTapAboutUs'
}]
}]
},
onLoad() {
this.loadUserInfo()
},
onShow() {
this.loadUserInfo()
},
loadUserInfo() {
const app = getApp()
if (app.globalData.user && app.globalData.user.profile) {
const profile = app.globalData.user.profile
this.setData({
'userInfo.isLoggedIn': true,
'userInfo.name': profile.name || '用户',
'userInfo.phone': profile.mobile || '',
'userInfo.sex': profile.sex || '',
'userInfo.birth': profile.birth ? this.formatDate(profile.birth) : '',
'userInfo.email': profile.email || '',
'userInfo.idnumber': profile.idnumber || '',
'userInfo.ssn': profile.ssn || ''
})
}
},
formatDate(date) {
if (!date) return ''
const d = new Date(date)
const year = d.getFullYear()
const month = String(d.getMonth() + 1).padStart(2, '0')
const day = String(d.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
},
onTapProfile() {
this.setData({ profileVisible: true })
},
onTapAddress() {
this.setData({ addressVisible: true })
},
onAddressClose() {
this.setData({ addressVisible: false })
},
onAddressSave(e) {
const app = getApp()
const user = app.globalData.user
if (!user || !user._id) {
wx.showToast({ title: '请先登录', icon: 'none' })
return
}
wx.showLoading({ title: '保存中...' })
const updateData = {
_id: user._id,
addresses: e.detail.addresses
}
API.user.update(updateData)
.then((data) => {
if (data.code === 0) {
app.globalData.user = data.data.user
wx.showToast({ title: '保存成功', icon: 'success' })
} else {
wx.showToast({ title: data.msg || '保存失败', icon: 'none' })
}
})
.catch(() => {
wx.showToast({ title: '网络请求失败', icon: 'none' })
})
.finally(() => {
wx.hideLoading()
})
},
onTapHealth() {
this.setData({ healthVisible: true })
},
onHealthClose() {
this.setData({ healthVisible: false })
},
onTapEscortRecord() {
wx.navigateTo({
url: '/pages/escort_record_list/escort_record_list'
})
},
onTapSetting() {
this.setData({ settingVisible: true })
},
onSettingClose() {
this.setData({ settingVisible: false })
},
onProfileClose() {
this.setData({ profileVisible: false })
},
onProfileSave(e) {
const newUserInfo = e.detail.editData
const app = getApp()
const user = app.globalData.user
if (!user || !user._id) {
wx.showToast({ title: '请先登录', icon: 'none' })
return
}
wx.showLoading({ title: '保存中...' })
const updateData = {
_id: user._id,
profile: {
name: newUserInfo.name,
sex: newUserInfo.sex,
birth: newUserInfo.birth ? new Date(newUserInfo.birth).toISOString() : null,
mobile: newUserInfo.phone
},
location: {
province: newUserInfo.province,
city: newUserInfo.city,
district: newUserInfo.district,
},
}
API.user.update(updateData)
.then((data) => {
if (data.code === 0) {
app.globalData.user = data.data.user
this.loadUserInfo()
wx.showToast({ title: '保存成功', icon: 'success' })
this.setData({ profileVisible: false })
} else {
wx.showToast({ title: data.msg || '保存失败', icon: 'none' })
}
})
.catch(() => {
wx.showToast({ title: '网络请求失败', icon: 'none' })
})
.finally(() => {
wx.hideLoading()
})
},
handleLogin(e) {
const phoneCode = e.detail.code ? e.detail.code : ''
API.user.wxGetPhoneNumber({ code: phoneCode })
.then((data) => {
const phoneNumber = data.data.phoneNumber
wx.setStorageSync('user_phonenumber', phoneNumber)
wx.login({
success: (res) => {
if (res.code) {
API.user.wxSignin({ phoneNumber, code: res.code })
.then((data) => {
if (data.code == 0) {
const app = getApp()
app.globalData.user = data.data.user
this.loadUserInfo()
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
})
.catch((err) => {
wx.showToast({ title: '网络请求失败', icon: 'none' })
console.log('请求失败', err)
})
}
})