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[], }, onLoad() { const app = getApp() // 从 globalData 初始化扫码参数 const pharmacistId = app.globalData.pharmacistId || '' const projectId = app.globalData.projectId || '' this.setData({ pharmacistId, projectId, }) app.waitLogin().then(() => { this.getProjectList() this.getBanner() this.checkStatus() }) }, // 获取项目列表 getProjectList() { wx.ajax({ method: 'GET', url: '/app/patient/patient/project-list', }).then((res: any) => { const projectList = res.list || [] const currentProjectId = res.currentProjectId // 找到当前项目的索引 let projectIndex = 0 projectList.forEach((item: any, index: number) => { if (item.projectId === currentProjectId) { projectIndex = index } }) this.setData({ projectList, projectIndex, currentProjectName: projectList[projectIndex]?.projectName || '华观健康', }) }) }, // 切换项目 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/recent-project', }) .then((res: any) => { if (res && res.projectId) { // 检查扫码的项目是否与当前项目一致 const scanProjectId = this.data.projectId if (scanProjectId && String(scanProjectId) !== String(res.projectId)) { // 扫码的是不同项目,检查患者是否已参与过该项目 this.checkAndSwitchProject(scanProjectId, res) } else { // 已有项目且与扫码项目一致,显示已参加项目状态 this.setData({ isLogin: 1, isPatient: 1, projectId: res.projectId, projectName: res.projectName, hasProject: true, selectedIndicationName: res.indicationName, }) } } else { // 没有项目,获取项目列表供选择 this.setData({ hasProject: false, }) this.getProjectInfo() } }) .catch(() => { // 接口失败,获取项目列表 this.setData({ hasProject: false, }) this.getProjectInfo() }) }, // 检查并切换项目 checkAndSwitchProject(scanProjectId: string, currentProject: any) { // 获取患者的项目列表 wx.ajax({ method: 'GET', url: '/app/patient/patient/project-list', }).then((projectRes: any) => { const projectList = projectRes.list || [] // 检查扫码的项目是否在患者已参与的项目列表中 const hasProject = projectList.some((p: any) => String(p.projectId) === String(scanProjectId)) if (hasProject) { // 患者已参与过该项目,自动切换 wx.ajax({ method: 'POST', url: '/app/patient/patient/switch-project', data: { projectId: scanProjectId, }, }).then(() => { wx.showToast({ title: '已切换到该项目', icon: 'success', }) // 刷新页面数据 this.getProjectList() this.checkStatus() }).catch(() => { // 切换失败,显示当前项目 this.setData({ isLogin: 1, isPatient: 1, projectId: currentProject.projectId, projectName: currentProject.projectName, hasProject: true, selectedIndicationName: currentProject.indicationName, }) }) } else { // 患者未参与过该项目,进入选择流程 this.setData({ hasProject: false, }) this.getProjectInfo() } }).catch(() => { // 获取项目列表失败,显示当前项目 this.setData({ isLogin: 1, isPatient: 1, projectId: currentProject.projectId, projectName: currentProject.projectName, hasProject: true, selectedIndicationName: currentProject.indicationName, }) }) }, 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, 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() 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 } wx.ajax({ method: 'POST', url: '/app/patient/patient/wx-login', data: params, }).then((res: any) => { const jumpUrl = res.jumpUrl // 如果有药师ID,创建绑定关系 if (this.data.pharmacistId) { wx.ajax({ method: 'POST', url: '/app/patient/patient/bind-pharmacist', data: { pharmacistId: Number.parseInt(this.data.pharmacistId), projectId: this.data.projectId, indicationId: selectedOption.indicationId, }, }).then(() => { // 绑定成功后跳转 wx.navigateToMiniProgram({ appId: 'wx05551c5ee1fd1c12', path: jumpUrl, }) this.checkStatus() }) } else { // 没有药师ID,直接跳转 wx.navigateToMiniProgram({ appId: 'wx05551c5ee1fd1c12', path: jumpUrl, success: () => { 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', } }, })