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: '', // 扫码获取的药师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 || '0' this.setData({ pharmacistId, projectId, }) app.waitLogin({ types: [1, 2] }).then(() => { 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) { // 已有项目,显示已参加项目状态 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() }) }, getProjectInfo() { wx.ajax({ method: 'GET', url: '/app/patient/patient/project-info', }).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) { wx.login({ success: (loginRes) => { wx.ajax({ method: 'POST', url: '/app/common/common/wx-login', data: { code: loginRes.code, }, }) .then((loginRes: any) => { // 解密手机号 return wx.ajax({ method: 'POST', url: '/app/common/common/wx-phone', data: { sessionKey: loginRes.sessionKey, encryptedData, iv, }, }) }) .then(() => { // 选择适应症 return wx.ajax({ method: 'POST', url: '/app/patient/patient/select-indication', data: { projectId: this.data.projectId, indicationId: selectedOption.indicationId, }, }) }) .then((res: any) => { const jumpUrl = res.jumpUrl // 如果有药师ID,创建绑定关系 if (this.data.pharmacistId) { wx.ajax({ method: 'POST', url: '/app/patient/patient/bind-pharmacist', data: { pharmacistId: this.data.pharmacistId, projectId: this.data.projectId, indicationId: selectedOption.indicationId, }, }) .then(() => { // 绑定成功后跳转 wx.navigateToMiniProgram({ appId: 'wx05551c5ee1fd1c12', path: jumpUrl, }) }) .catch(() => { // 绑定失败也跳转 wx.navigateToMiniProgram({ appId: 'wx05551c5ee1fd1c12', path: jumpUrl, }) }) } else { // 没有药师ID,直接跳转 wx.navigateToMiniProgram({ appId: 'wx05551c5ee1fd1c12', path: jumpUrl, }) } }) }, }) } }, toggleAgreement() { this.setData({ agreementChecked: !this.data.agreementChecked, }) }, handleWork() { wx.navigateTo({ url: '/pages/work/index', }) }, handleJump() { // 跳转到腾讯药箱小程序 wx.navigateToMiniProgram({ appId: 'wx05551c5ee1fd1c12', path: '', }) }, onShareAppMessage() { return { title: '华观健康', path: '/pages/index/index', } }, })