You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

428 lines
10 KiB

Page({
data: {
background: 'transparent',
imageUrl: '',
Timestamp: Date.now(),
popupShow: false,
popupType: '',
popupParams: {},
options: [] as Array<{ indicationId: number; indicationName: string; active: boolean }>,
agreementChecked: true,
projectId: '',
projectName: '',
currentProjectName: '',
projectIndex: 0,
projectList: [] as Array<{ projectId: number; projectName: string; projectDescription: string }>,
// 扫码获取的药师ID
pharmacistId: '',
isLogin: 0,
isPatient: 0,
// 是否已有项目
hasProject: false,
// 选中的适应症名称
selectedIndicationName: '',
bannerList: [] as any[],
// 用户信息
avatarUrl: '',
nickname: '',
},
onLoad() {
const app = getApp<IAppOption>()
// 从 globalData 初始化扫码参数
const pharmacistId = app.globalData.pharmacistId || ''
const projectId = app.globalData.projectId || ''
this.setData({
pharmacistId,
projectId,
})
app.waitLogin().then(() => {
this.getBanner()
this.checkStatus()
this.getUserInfo()
})
},
// 切换项目
onProjectChange(e: WechatMiniprogram.CustomEvent) {
const index = e.detail.value
const project = this.data.projectList[index]
if (!project || project.projectId === this.data.projectList[this.data.projectIndex]?.projectId) {
return
}
wx.ajax({
method: 'POST',
url: '/app/patient/patient/switch-project',
data: {
projectId: project.projectId,
},
}).then(() => {
this.setData({
projectIndex: index,
currentProjectName: project.projectName,
projectId: String(project.projectId),
})
// 刷新页面数据
this.getBanner()
this.checkStatus()
})
},
getBanner() {
wx.ajax({
method: 'GET',
url: '/app/patient/patient/banner-list',
data: {
projectId: this.data.projectId,
},
}).then((res: any) => {
this.setData({
bannerList: res,
})
})
},
checkStatus() {
wx.ajax({
method: 'GET',
url: '/app/patient/patient/patient-project-list',
})
.then((res: any) => {
const projectList = res.list || []
const currentProjectId = res.currentProjectId
// 存储项目列表和当前项目ID
this.setData({
projectList,
currentProjectId,
})
if (projectList.length > 0) {
// 找到当前项目
const currentProject = projectList.find((p: any) => p.projectId === currentProjectId) || projectList[0]
// 检查扫码的项目是否在已绑定项目列表中
const scanProjectId = this.data.projectId
if (scanProjectId) {
const scanProject = projectList.find((p: any) => String(p.projectId) === String(scanProjectId))
if (scanProject) {
// 扫码的项目已绑定,切换到该项目
if (String(scanProjectId) !== String(currentProjectId)) {
this.switchProject(scanProjectId)
} else {
// 已经是当前项目,直接显示
this.setData({
isLogin: 1,
isPatient: 1,
projectId: currentProject.projectId,
projectName: currentProject.projectName,
currentProjectName: currentProject.projectName,
hasProject: true,
selectedIndicationName: currentProject.indicationName,
})
}
} else {
// 扫码的项目未绑定,进入选择流程(需要绑定新项目)
this.setData({
hasProject: false,
})
this.getProjectInfo()
}
} else {
// 没有扫码,显示当前项目
this.setData({
isLogin: 1,
isPatient: 1,
projectId: currentProject.projectId,
projectName: currentProject.projectName,
currentProjectName: currentProject.projectName,
hasProject: true,
selectedIndicationName: currentProject.indicationName,
})
}
} else {
// 没有绑定任何项目,获取项目列表供选择
this.setData({
hasProject: false,
})
this.getProjectInfo()
}
})
.catch(() => {
// 接口失败,获取项目列表
this.setData({
hasProject: false,
})
this.getProjectInfo()
})
},
// 切换项目
switchProject(projectId: string) {
wx.ajax({
method: 'POST',
url: '/app/patient/patient/switch-project',
data: {
projectId,
},
})
.then(() => {
wx.showToast({
title: '已切换到该项目',
icon: 'success',
})
// 刷新页面数据
this.checkStatus()
})
.catch(() => {
// 切换失败,重新检查状态
this.checkStatus()
})
},
// 获取用户信息
getUserInfo() {
wx.ajax({
method: 'GET',
url: '/app/patient/patient/patient-info',
}).then((res: any) => {
this.setData({
avatarUrl: res.avatar || '',
nickname: res.name || '',
})
})
},
// 选择头像
onChooseAvatar(e: WechatMiniprogram.CustomEvent) {
const { avatarUrl } = e.detail
const app = getApp<IAppOption>()
// 上传头像
wx.uploadFile({
url: `${app.globalData.url }/app/common/common/upload`,
filePath: avatarUrl,
name: 'file',
header: {
Authorization: wx.getStorageSync('token'),
},
success: (res) => {
const data = JSON.parse(res.data)
if (data.code === 0) {
const url = data.data.url
this.setData({
avatarUrl: url,
})
// 更新用户信息(头像)
this.updateUserInfo('avatar')
} else {
wx.showToast({
title: data.msg || '上传失败',
icon: 'none',
})
}
},
fail: () => {
wx.showToast({
title: '上传失败',
icon: 'none',
})
},
})
},
// 昵称选择/输入时保存
onNicknameReview(e: WechatMiniprogram.CustomEvent) {
const nickname = e.detail.value
this.setData({
nickname,
})
// 昵称修改后保存,允许空值(清空昵称)
this.updateUserInfo('name')
},
// 更新用户信息
updateUserInfo(type?: 'avatar' | 'name') {
const data: any = {}
// 如果是头像更新,只传头像
if (type === 'avatar' && this.data.avatarUrl) {
data.avatar = this.data.avatarUrl
}
// 如果是昵称更新,只传昵称
if (type === 'name') {
data.name = this.data.nickname
}
if (!data.avatar && !data.name) {
return
}
wx.ajax({
method: 'POST',
url: '/app/patient/patient/update-info',
data,
}).then(() => {
wx.showToast({
title: '保存成功',
icon: 'success',
})
}).catch(() => {
wx.showToast({
title: '保存失败',
icon: 'none',
})
})
},
getProjectInfo() {
const params: any = {}
// 如果有 projectId,传入获取指定项目信息
if (this.data.projectId) {
params.projectId = this.data.projectId
}
wx.ajax({
method: 'GET',
url: '/app/patient/patient/project-info',
data: params,
}).then((res: any) => {
this.setData({
projectId: res.projectId,
projectName: res.projectName,
currentProjectName: res.projectName,
options: res.indications.map((item: any) => ({
indicationId: item.indicationId,
indicationName: item.indicationName,
active: false,
})),
})
})
},
handlePopupOk() {
this.setData({
popupShow: false,
})
},
handlePopupCancel() {
this.setData({
popupShow: false,
})
},
selectOption(e) {
const indicationId = e.currentTarget.dataset.id as number
const newOptions = this.data.options.map((option) => ({
...option,
active: option.indicationId === indicationId,
}))
this.setData({
options: newOptions,
})
},
handleWxSubmit(e: WechatMiniprogram.CustomEvent) {
if (!this.data.agreementChecked) {
wx.showToast({
icon: 'none',
title: '请同意用户协议',
})
return
}
const selectedOption = this.data.options.find((item) => item.active)
if (!selectedOption) {
wx.showToast({
icon: 'none',
title: '请选择适应症',
})
return
}
const { iv, encryptedData } = e.detail
if (iv && encryptedData) {
const app = getApp<IAppOption>()
const sessionKey = app.globalData.initLoginInfo?.sessionKey || ''
// 患者微信登录/注册
const params: any = {
sessionKey,
encryptedData,
iv,
}
// 如果有扫码参数,传入
if (this.data.pharmacistId) {
params.pharmacistId = Number.parseInt(this.data.pharmacistId)
}
if (this.data.projectId) {
params.projectId = this.data.projectId
}
// 添加适应症ID
params.indicationId = selectedOption.indicationId
wx.ajax({
method: 'POST',
url: '/app/patient/patient/wx-login',
data: params,
}).then((res: any) => {
const jumpUrl = res.jumpUrl
// 直接跳转药箱小程序
wx.navigateToMiniProgram({
appId: 'wx05551c5ee1fd1c12',
path: jumpUrl,
success: () => {
this.checkStatus()
},
fail: () => {
this.checkStatus()
},
})
})
}
},
toggleAgreement() {
this.setData({
agreementChecked: !this.data.agreementChecked,
})
},
handleAgreement() {
wx.navigateTo({
url: '/privacy/pages/policy1/index',
})
},
handleWork() {
wx.navigateTo({
url: '/pages/work/index',
})
},
handleJump() {
// 跳转到腾讯药箱小程序
wx.navigateToMiniProgram({
appId: 'wx05551c5ee1fd1c12',
path: '',
})
},
onShareAppMessage() {
return {
title: '华观健康',
path: '/pages/index/index',
}
},
})