const app = getApp() Page({ data: { // 扫码参数 promoterId: '', projectId: '', // 表单数据 name: '', pharmacyId: '', pharmacyName: '', // 药店选择弹窗 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(option: { promoterId?: string; projectId?: string; scene?: string }) { // 解析扫码参数 let promoterId = option.promoterId || '' let projectId = option.projectId || '' // 如果是扫码进入,解析 scene 参数 if (option.scene) { const scene = decodeURIComponent(option.scene) const params = this.parseScene(scene) promoterId = params.promoterId || '' projectId = params.projectId || '' } if (!promoterId || !projectId) { wx.showToast({ title: '缺少必要参数', icon: 'none', }) return } this.setData({ promoterId, projectId, }) }, // 解析 scene 参数 parseScene(scene: string): { promoterId?: string; projectId?: string } { const params: { promoterId?: string; projectId?: string } = {} const pairs = scene.split('&') pairs.forEach((pair) => { const [key, value] = pair.split('=') if (key === 'promoterId') { params.promoterId = value } else if (key === 'projectId') { params.projectId = value } }) return params }, // 输入姓名 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) this.setData({ pharmacyList: [...this.data.pharmacyList, ...list], 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 { promoterId, projectId, name, pharmacyId } = this.data if (!name.trim()) { wx.showToast({ title: '请输入姓名', icon: 'none', }) return } if (!pharmacyId) { wx.showToast({ title: '请选择所属药店', icon: 'none', }) return } wx.showLoading({ title: '提交中...' }) wx.ajax({ method: 'POST', url: '/app/pharmacist/pharmacist/bind-promoter', data: { promoterId, projectId, name: name.trim(), pharmacyId, }, }).then((res: any) => { wx.hideLoading() // 保存登录信息 app.globalData.initLoginInfo = { loginType: 4, isLogin: 1, isReg: 1, ...res, } wx.showToast({ title: '绑定成功', icon: 'success', }) // 跳转到药店端首页 setTimeout(() => { wx.reLaunch({ url: '/doctor/pages/home/index', }) }, 1500) }).catch(() => { wx.hideLoading() wx.showToast({ title: '绑定失败', icon: 'none', }) }) }, }) export {}