Files
wxapp_companion/pages/set/index.js
T
lik c8bcc79865 feat(admin): 新增陪诊员入驻申请与全局角色守卫
- 新增 pages/apply 入驻申请页:填写资料、上传证件、查看审核状态
- 新增 utils/role.js 统一角色解析与管理端页面守卫
- app.js 包装 Page 构造器集中做 onShow 守卫,无身份用户重定向至申请页
- 订单、客户、档案、统计等按角色区分能力:陪诊员不请求档案接口,改用订单快照
- 订单列表支持待取消订单一键同意取消;新增订单价格由服务目录决定只读展示
2026-11-08 12:01:48 +08:00

222 lines
5.3 KiB
JavaScript

const API = require('../../utils/api.js')
const role = require('../../utils/role.js')
Page({
data: {
isLoggedIn: false,
displayName: '',
avatar: '',
levelText: '初级陪诊员',
version: '1.0.0',
showLoginPopup: false,
loginForm: {
name: '',
mobile: ''
},
menuList: [
{ icon: 'user', title: '个人资料', url: '/pages/me/index' },
{ icon: 'notification', title: '消息通知', url: '' },
{ icon: 'help-circle', title: '帮助中心', url: '' },
]
},
onLoad() {
this.checkLoginStatus()
this.setData({ version: this.getAppVersion() })
const app = getApp()
app.eventBus.on('user-login', this.onUserLogin)
},
onUnload() {
const app = getApp()
app.eventBus.off('user-login', this.onUserLogin)
},
onShow() {
this.checkLoginStatus()
},
onUserLogin(user) {
this.applyUser(user)
},
checkLoginStatus() {
const app = getApp()
this.applyUser(app.globalData.user)
},
/**
* 将用户对象映射为页面展示字段(真实信息)
*/
applyUser(user) {
if (!user || !user.security || !user.security.token) {
this.setData({
isLoggedIn: false,
displayName: '',
avatar: '',
levelText: '初级陪诊员'
})
return
}
const p = user.profile || {}
this.setData({
isLoggedIn: true,
displayName: p.name || '管理员',
avatar: p.avatar || '',
levelText: user.level || p.level || '初级陪诊员'
})
},
getAppVersion() {
const accountInfo = wx.getAccountInfoSync()
return accountInfo.miniProgram.version || '1.0.0'
},
onShowLoginPopup() {
this.setData({ showLoginPopup: true })
},
onCloseLoginPopup() {
this.setData({ showLoginPopup: false, loginForm: { name: '', mobile: '' } })
},
onNameInput(e) {
this.setData({ 'loginForm.name': e.detail.value })
},
onMobileInput(e) {
this.setData({ 'loginForm.mobile': e.detail.value })
},
onLoginSubmit() {
const { name, mobile } = this.data.loginForm
if (!name.trim()) {
wx.showToast({ title: '请输入姓名', icon: 'none' })
return
}
if (!mobile.trim()) {
wx.showToast({ title: '请输入手机号', icon: 'none' })
return
}
const phoneReg = /^1[3-9]\d{9}$/
if (!phoneReg.test(mobile.trim())) {
wx.showToast({ title: '手机号格式不正确', icon: 'none' })
return
}
wx.showLoading({ title: '登录中...' })
wx.login({
success: (res) => {
if (res.code) {
API.user.wxSignin({ phoneNumber: mobile.trim(), name: name.trim(), code: res.code })
.then((data) => {
if (data.code == 0) {
const app = getApp()
app.globalData.user = data.data.user
this.applyUser(app.globalData.user)
this.setData({
showLoginPopup: false,
loginForm: { name: '', mobile: '' }
})
wx.showToast({ title: '登录成功', icon: 'success' })
} else {
wx.showToast({ title: '登录失败', icon: 'none' })
}
wx.hideLoading()
})
} else {
wx.showToast({ title: '登录失败', icon: 'none' })
}
}
}).catch(err => {
console.error('登录失败', err)
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
}).finally(() => {
wx.hideLoading()
})
},
onMenuTap(e) {
const { index } = e.currentTarget.dataset
const item = this.data.menuList[index]
if (!this.data.isLoggedIn) {
wx.showToast({ title: '请先登录', icon: 'none' })
return
}
if (item.url) {
wx.navigateTo({ url: item.url })
} else {
wx.showToast({ title: '功能开发中', icon: 'none' })
}
},
onLogout() {
wx.showModal({
title: '退出登录',
content: '确定要退出登录吗?',
confirmColor: '#C26A2E',
success: (res) => {
if (res.confirm) {
wx.showLoading({ title: '退出中...' })
API.user.signout().then(() => {
this.doLogout()
}).catch(() => {
this.doLogout()
}).finally(() => {
wx.hideLoading()
})
}
}
})
},
doLogout() {
const app = getApp()
app.globalData.user = null
// 角色随账号失效,避免下一个登录账号沿用旧角色
role.resetRole()
wx.removeStorageSync('admin_user_info')
wx.removeStorageSync('admin_ai_session_id')
wx.removeStorageSync('admin_ai_chat_history')
this.applyUser(null)
wx.showToast({ title: '已退出登录', icon: 'success' })
},
onPopupContentTap() {
// 阻止冒泡,防止点击弹窗内容时关闭弹窗
},
onClearCache() {
wx.showModal({
title: '清除缓存',
content: '确定要清除所有缓存数据吗?',
confirmColor: '#C26A2E',
success: (res) => {
if (res.confirm) {
wx.clearStorage({
success: () => {
const app = getApp()
app.globalData.user = null
role.resetRole()
this.applyUser(null)
wx.showToast({ title: '缓存已清除', icon: 'success' })
}
})
}
}
})
}
})