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.
 
 
 

307 lines
7.0 KiB

const app = getApp<IAppOption>()
Page({
data: {
// 表单数据
name: '',
pharmacyId: '',
pharmacyName: '',
// 项目选择
projectId: '',
projectName: '',
projectList: [] as Array<{ projectId: number; projectName: string }>,
projectIndex: 0,
projectDisabled: false, // 是否禁用项目选择(扫码进入时)
// 药店选择弹窗
show: false,
// 省市区选择
provinceId: '',
provinceName: '',
cityId: '',
cityName: '',
districtId: '',
districtName: '',
// 药店列表
pharmacyList: [] as any[],
// 搜索关键词
keyword: '',
// 分页
page: 1,
pageSize: 20,
loading: false,
hasMore: true,
pagination: {
count: 0,
page: 0,
pages: 0,
},
},
onLoad() {
// 如果通过扫码进入,存在项目ID,则回显并禁用选择
const projectId = app.globalData.projectId
if (projectId) {
this.setData({
projectId,
projectDisabled: true,
})
}
// 获取项目列表
this.getProjectList()
},
// 获取项目列表
getProjectList() {
wx.ajax({
method: 'GET',
url: '/app/common/common/project-list',
}).then((res: any) => {
const list = res.data || res || []
// 转换数据格式:接口返回 id/name,组件使用 projectId/projectName
const projectList = list.map((item: any) => ({
projectId: item.id,
projectName: item.name,
}))
// 如果有预设的项目ID(扫码进入),找到对应索引并回显
let projectIndex = 0
let projectId = ''
let projectName = ''
if (this.data.projectId) {
const index = projectList.findIndex((item: any) => String(item.projectId) === String(this.data.projectId))
if (index !== -1) {
projectIndex = index
projectId = projectList[index].projectId
projectName = projectList[index].projectName
}
}
this.setData({
projectList,
projectIndex,
projectId,
projectName,
})
})
},
// 项目选择变化
onProjectChange(e: WechatMiniprogram.CustomEvent) {
const index = e.detail.value
const project = this.data.projectList[index]
this.setData({
projectIndex: index,
projectId: project?.projectId || '',
projectName: project?.projectName || '',
})
},
// 输入姓名
handleNameInput(e: WechatMiniprogram.CustomEvent) {
this.setData({
name: e.detail.value,
})
},
// 打开药店选择弹窗
handleDrug() {
this.setData({
show: true,
page: 1,
pharmacyList: [],
hasMore: true,
pagination: {
count: 0,
page: 0,
pages: 0,
},
})
this.getPharmacyList()
},
// 关闭弹窗
onClose() {
this.setData({
show: false,
})
},
// 获取药店列表
getPharmacyList() {
if (this.data.loading || !this.data.hasMore) return
this.setData({ loading: true })
wx.ajax({
method: 'GET',
url: '/app/common/common/pharmacy-list',
data: {
keyword: this.data.keyword,
provinceId: this.data.provinceId,
cityId: this.data.cityId,
districtId: this.data.districtId,
page: this.data.page,
pageSize: this.data.pageSize,
},
})
.then((res: any) => {
const list = res.list || []
const currentPage = this.data.page
const total = res.total || 0
const pages = Math.ceil(total / this.data.pageSize)
// 判断是否有筛选条件(关键词或省市区)
const hasFilter = this.data.keyword || this.data.provinceId || this.data.cityId || this.data.districtId
// 只有在没有筛选条件且是最后一页时,才添加"其他药店"
const isLastPage = currentPage >= pages
const newList = [...this.data.pharmacyList, ...list]
if (hasFilter && isLastPage) {
const otherPharmacy = {
id: '-1',
name: '其他药店',
address: '没有找到所在药店',
}
newList.push(otherPharmacy)
}
this.setData({
pharmacyList: newList,
total,
page: currentPage + 1,
hasMore: currentPage < pages,
loading: false,
pagination: {
count: total,
page: currentPage,
pages,
},
})
})
.catch(() => {
this.setData({ loading: false })
})
},
// 省市区选择变化
handleChange(e: WechatMiniprogram.CustomEvent) {
const [province, city, district] = e.detail
this.setData({
provinceId: province?.value || '',
provinceName: province?.label || '',
cityId: city?.value || '',
cityName: city?.label || '',
districtId: district?.value || '',
districtName: district?.label || '',
page: 1,
pharmacyList: [],
hasMore: true,
pagination: {
count: 0,
page: 0,
pages: 0,
},
})
this.getPharmacyList()
},
// 搜索药店
handleSearch(e: WechatMiniprogram.CustomEvent) {
this.setData({
keyword: e.detail.value,
page: 1,
pharmacyList: [],
hasMore: true,
pagination: {
count: 0,
page: 0,
pages: 0,
},
})
this.getPharmacyList()
},
// 加载更多
loadMore() {
this.getPharmacyList()
},
// 选择药店
selectPharmacy(e: WechatMiniprogram.CustomEvent) {
const { id, name } = e.currentTarget.dataset
this.setData({
pharmacyId: id,
pharmacyName: name,
show: false,
})
},
// 提交注册
handleSubmit() {
const { name, pharmacyId, projectId, projectDisabled } = this.data
if (!name.trim()) {
wx.showToast({
title: '请输入姓名',
icon: 'none',
})
return
}
if (!pharmacyId) {
wx.showToast({
title: '请选择所属药店',
icon: 'none',
})
return
}
if (!projectId) {
wx.showToast({
title: '请选择项目',
icon: 'none',
})
return
}
wx.showLoading({ title: '提交中...' })
wx.ajax({
method: 'POST',
url: '/app/pharmacist/pharmacist/register',
data: {
name: name.trim(),
pharmacyId,
projectId,
},
})
.then((res: any) => {
wx.hideLoading()
// 保存登录信息
app.globalData.initLoginInfo = {
...app.globalData.initLoginInfo,
isLogin: 1,
loginIdentity: 4,
loginIdentityId: res.pharmacistId,
}
wx.showToast({
title: '注册成功',
icon: 'success',
})
// 跳转到药店端首页
setTimeout(() => {
wx.reLaunch({
url: '/doctor/pages/home/index',
})
}, 1500)
})
.catch(() => {
wx.hideLoading()
wx.showToast({
title: '注册失败',
icon: 'none',
})
})
},
})
export {}