Files
wxapp_escort/pages/mine/mine.js
T

252 lines
6.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const API = require('../../utils/api.js')
Page({
data: {
userInfo: {
// 默认头像:暖墨色品牌标记(home-ink #9B4508)。
// home.png 本体是 tabBar 未选中态的灰色 #8A8A8A,故另存一份重上色位图;
// 与 ai-ink.png 同一做法(位图着色走不了 CSS 变量,改色须手改,见 DESIGN.md §7.3)
avatar: '/images/home-ink.png',
name: '用户',
phone: '',
sex: '',
birth: '',
email: '',
idnumber: '',
ssn: '',
isLoggedIn: false
},
profileVisible: false,
menuGroups: [{
groupName: '个人服务',
items: [{
name: '个人信息',
icon: 'verify',
bindtap: 'onTapProfile'
}, {
name: '健康档案',
icon: 'attach',
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()
// 同步自定义 tabBar 选中态(我的为第 2 项)
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
selected: 2
})
}
},
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() {
wx.navigateTo({
url: '/pages/health/profilelist'
})
},
onHealthClose() {
this.setData({ healthVisible: false })
},
onTapEscortRecord() {
wx.navigateTo({
url: '/pages/escort/recordlist'
})
},
onTapAboutUs() {
wx.navigateTo({
url: '/pages/mine/about'
})
},
onTapSetting() {
this.setData({ settingVisible: true })
},
// 页脚 B 端合作链接:暂无独立页面,统一转接人工客服
onFooterTap() {
wx.makePhoneCall({
phoneNumber: '18618162956'
})
},
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, phoneCode, 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)
})
}
})