Browse Source

feat(患者绑定): 添加药师换绑逻辑防止重复绑定

新增hasBindPharmacist标志位防止重复换绑
重构扫码项目处理逻辑,分离为showCurrentProject和handleBoundProject方法
添加switchProjectAndBind方法处理项目切换和绑定流程
master
kola-web 4 days ago
parent
commit
ce25c7cbf2
  1. 170
      src/pages/index/index.ts

170
src/pages/index/index.ts

@ -34,6 +34,9 @@ Page({ @@ -34,6 +34,9 @@ Page({
nickname: '',
},
// 是否已经换绑,防止死循环
hasBindPharmacist: false,
onLoad() {
const app = getApp<IAppOption>()
@ -107,38 +110,46 @@ Page({ @@ -107,38 +110,46 @@ Page({
currentProjectId,
})
if (projectList.length > 0) {
// 没有绑定任何项目,进入选择流程
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
if (scanProjectId) {
const scanPharmacistId = this.data.pharmacistId
// 没有扫码,直接显示当前项目
if (!scanProjectId) {
this.showCurrentProject(currentProject)
return
}
// 查找扫码的项目
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,
})
// 扫码的项目未绑定,进入选择流程
if (!scanProject) {
this.setData({ hasProject: false })
this.getProjectInfo()
return
}
} else {
// 扫码的项目未绑定,进入选择流程(需要绑定新项目)
this.setData({
hasProject: false,
// 扫码的项目已绑定,处理换绑逻辑
this.handleBoundProject(scanProjectId, scanPharmacistId, scanProject, currentProjectId, currentProject)
})
.catch(() => {
// 接口失败,获取项目列表
this.setData({ hasProject: false })
this.getProjectInfo()
}
} else {
// 没有扫码,显示当前项目
})
},
// 显示当前项目
showCurrentProject(currentProject: any) {
this.setData({
isPatient: 1,
projectId: currentProject.projectId,
@ -147,21 +158,110 @@ Page({ @@ -147,21 +158,110 @@ Page({
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.setData({
hasProject: false,
})
this.getProjectInfo()
// 已经是当前项目,直接进行绑定
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(() => {
// 接口失败,获取项目列表
this.setData({
hasProject: false,
wx.showToast({
title: '换绑失败',
icon: 'none',
})
this.getProjectInfo()
// 刷新页面数据
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()
})
},

Loading…
Cancel
Save