From ce25c7cbf24800632442fa89560433cc332619b2 Mon Sep 17 00:00:00 2001 From: kola-web Date: Sat, 7 Mar 2026 18:43:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=82=A3=E8=80=85=E7=BB=91=E5=AE=9A):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8D=AF=E5=B8=88=E6=8D=A2=E7=BB=91=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E9=98=B2=E6=AD=A2=E9=87=8D=E5=A4=8D=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增hasBindPharmacist标志位防止重复换绑 重构扫码项目处理逻辑,分离为showCurrentProject和handleBoundProject方法 添加switchProjectAndBind方法处理项目切换和绑定流程 --- src/pages/index/index.ts | 198 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 149 insertions(+), 49 deletions(-) diff --git a/src/pages/index/index.ts b/src/pages/index/index.ts index 0da703c..2e86ba2 100644 --- a/src/pages/index/index.ts +++ b/src/pages/index/index.ts @@ -34,6 +34,9 @@ Page({ nickname: '', }, + // 是否已经换绑,防止死循环 + hasBindPharmacist: false, + onLoad() { const app = getApp() @@ -107,64 +110,161 @@ Page({ 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({ - 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({ - isPatient: 1, - projectId: currentProject.projectId, - projectName: currentProject.projectName, - currentProjectName: currentProject.projectName, - hasProject: true, - selectedIndicationName: currentProject.indicationName, - }) - } - } else { - // 没有绑定任何项目,获取项目列表供选择 - this.setData({ - hasProject: false, - }) + // 没有绑定任何项目,进入选择流程 + if (projectList.length === 0) { + this.setData({ hasProject: false }) this.getProjectInfo() + return + } + + // 找到当前项目 + const currentProject = projectList.find((p: any) => p.projectId === currentProjectId) || projectList[0] + const scanProjectId = this.data.projectId + const scanPharmacistId = this.data.pharmacistId + + // 没有扫码,直接显示当前项目 + if (!scanProjectId) { + this.showCurrentProject(currentProject) + return } + + // 查找扫码的项目 + const scanProject = projectList.find((p: any) => String(p.projectId) === String(scanProjectId)) + + // 扫码的项目未绑定,进入选择流程 + if (!scanProject) { + this.setData({ hasProject: false }) + this.getProjectInfo() + return + } + + // 扫码的项目已绑定,处理换绑逻辑 + this.handleBoundProject(scanProjectId, scanPharmacistId, scanProject, currentProjectId, currentProject) }) .catch(() => { // 接口失败,获取项目列表 - this.setData({ - hasProject: false, - }) + this.setData({ hasProject: false }) this.getProjectInfo() }) }, + // 显示当前项目 + showCurrentProject(currentProject: any) { + this.setData({ + isPatient: 1, + projectId: currentProject.projectId, + projectName: currentProject.projectName, + currentProjectName: currentProject.projectName, + hasProject: true, + selectedIndicationName: currentProject.indicationName, + }) + }, + + // 处理已绑定的扫码项目 + handleBoundProject( + scanProjectId: string, + scanPharmacistId: string, + scanProject: any, + currentProjectId: number, + currentProject: any + ) { + const isDifferentProject = String(scanProjectId) !== String(currentProjectId) + const needBind = scanPharmacistId && !this.hasBindPharmacist + + // 需要换绑 + if (needBind) { + if (isDifferentProject) { + // 当前项目与扫码项目不一致,先切换项目再绑定 + this.switchProjectAndBind(scanProjectId, scanPharmacistId, scanProject.indicationId) + } else { + // 已经是当前项目,直接进行绑定 + this.bindPharmacist(scanProjectId, scanPharmacistId, scanProject.indicationId) + } + return + } + + // 只是切换项目 + if (isDifferentProject) { + this.switchProject(scanProjectId) + return + } + + // 已经是当前项目,直接显示 + this.showCurrentProject(currentProject) + }, + + // 换绑(建立新的绑定关系) + bindPharmacist(projectId: string, pharmacistId: string, indicationId: number) { + // 设置标记,防止重复换绑 + this.hasBindPharmacist = true + wx.ajax({ + method: 'POST', + url: '/app/patient/patient/bind-pharmacist', + data: { + projectId: Number(projectId), + pharmacistId: Number(pharmacistId), + indicationId, + }, + }) + .then(() => { + wx.showToast({ + title: '换绑成功', + icon: 'success', + }) + // 刷新页面数据 + this.checkStatus() + }) + .catch(() => { + wx.showToast({ + title: '换绑失败', + icon: 'none', + }) + // 刷新页面数据 + this.checkStatus() + }) + }, + + // 切换项目并绑定 + switchProjectAndBind(projectId: string, pharmacistId: string, indicationId: number) { + // 设置标记,防止重复换绑 + this.hasBindPharmacist = true + wx.ajax({ + method: 'POST', + url: '/app/patient/patient/switch-project', + data: { + projectId, + }, + }) + .then(() => { + // 切换成功后进行绑定 + return wx.ajax({ + method: 'POST', + url: '/app/patient/patient/bind-pharmacist', + data: { + projectId: Number(projectId), + pharmacistId: Number(pharmacistId), + indicationId, + }, + }) + }) + .then(() => { + wx.showToast({ + title: '换绑成功', + icon: 'success', + }) + // 刷新页面数据 + this.checkStatus() + }) + .catch(() => { + wx.showToast({ + title: '操作失败', + icon: 'none', + }) + // 刷新页面数据 + this.checkStatus() + }) + }, + // 切换项目 switchProject(projectId: string) { wx.ajax({