// pages/apply/index.js // 陪诊师入驻申请页 // // 无身份用户(role=none)被全局守卫拦住后落到这里;提交后进入待审核视图。 // 审核通过后本页 onShow 会强刷角色,直接切回首页,不需要用户手动重新登录。 const API = require('../../utils/api.js') const role = require('../../utils/role.js') const { CITY_OPTIONS, CITY_LABELS, cityIndex, cityLabel } = require('../../utils/city.js') const SEX_OPTIONS = [ { value: 'female', label: '女' }, { value: 'male', label: '男' } ] const SEX_LABELS = SEX_OPTIONS.map((item) => item.label) const MOBILE_REGEX = /^1[3-9]\d{9}$/ Page({ data: { loading: true, view: 'form', // form 填写 / pending 待审核 / approved 已通过但角色未刷新 submitting: false, rejectReason: '', application: null, serviceAreaText: '', submitTimeText: '', accountName: '微信用户', sexLabels: SEX_LABELS, sexIndex: 0, cityLabels: CITY_LABELS, cityIndex: 0, form: { name: '', mobile: '', sex: '', introduction: '' } }, onShow() { this.refresh() }, /** * 先确认身份,再决定展示哪个视图 * force=true:审批可能刚刚通过,必须绕过角色缓存 */ refresh() { this.setData({ loading: true }) role .ensureRoleSafe(true) .then((detail) => { if (detail.ok && detail.role !== 'none') { wx.switchTab({ url: '/pages/home/index' }) return } this.loadApplication() }) .catch(() => this.loadApplication()) }, loadApplication() { const app = getApp() const user = app && app.globalData ? app.globalData.user : null const accountName = user && user.profile && user.profile.name ? user.profile.name : '微信用户' API.attendantApplication .getMine() .then((res) => { if (res.code !== 0) { wx.showToast({ title: res.msg || '加载失败', icon: 'none' }) this.setData({ loading: false, accountName }) return } const application = res.data ? res.data.application : null if (!application) { this.setData({ loading: false, view: 'form', accountName }) return } if (application.status === 'pending') { this.setData({ loading: false, view: 'pending', application, accountName, serviceAreaText: (application.applicant.serviceAreas || []).map(cityLabel).join('、'), submitTimeText: this.formatTime(application.createtime) }) return } if (application.status === 'approved') { // 走到这里说明员工档案还没查到(接口抖动或数据延迟),给一个手动重试入口 this.setData({ loading: false, view: 'approved', application, accountName }) return } // 已驳回:回填上次填写的内容,证件照不必重复上传 const applicant = application.applicant || {} const sexIndex = SEX_OPTIONS.findIndex((item) => item.value === applicant.sex) this.setData({ loading: false, view: 'form', application, accountName, rejectReason: application.rejectReason || '', sexIndex: sexIndex > -1 ? sexIndex : 0, cityIndex: cityIndex((applicant.serviceAreas || [])[0]), form: { name: applicant.name || '', mobile: applicant.mobile || '', sex: applicant.sex || '', introduction: applicant.introduction || '' } }) }) .catch(() => { this.setData({ loading: false, accountName }) wx.showToast({ title: '网络错误,请重试', icon: 'none' }) }) }, formatTime(value) { if (!value) return '' const date = new Date(value) if (isNaN(date.getTime())) return '' const pad = (n) => String(n).padStart(2, '0') return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}` }, onInput(e) { const key = e.currentTarget.dataset.key if (!key) return this.setData({ [`form.${key}`]: e.detail.value }) }, onSexChange(e) { const index = Number(e.detail.value) const option = SEX_OPTIONS[index] this.setData({ sexIndex: index, 'form.sex': option ? option.value : '' }) }, onCityChange(e) { this.setData({ cityIndex: Number(e.detail.value) || 0 }) }, submit() { if (this.data.submitting) return const { form, cityIndex: cityIdx } = this.data const name = form.name.trim() const mobile = form.mobile.trim() const serviceArea = CITY_OPTIONS[cityIdx] || CITY_OPTIONS[0] if (!name) return this.toast('请填写姓名') if (!MOBILE_REGEX.test(mobile)) return this.toast('请填写正确的手机号') // 先只收基本信息:身份证与证件材料不在此填写,材料可审核通过后补传 const payload = { applicant: { name, mobile, sex: form.sex, serviceAreas: [serviceArea.value], introduction: form.introduction.trim() } } this.setData({ submitting: true }) API.attendantApplication .create(payload) .then((res) => { if (res.code === 0) { const next = res.data ? res.data.application : null wx.showToast({ title: '申请已提交', icon: 'success' }) this.setData({ submitting: false, view: 'pending', application: next, rejectReason: '', serviceAreaText: next ? (next.applicant.serviceAreas || []).map(cityLabel).join('、') : cityLabel(serviceArea.value), submitTimeText: next ? this.formatTime(next.createtime) : '' }) return } // 409 分两种:已是陪诊师 → 刷新角色直接进首页;身份证号被绑/账号冲突 → 弹窗说明,留在表单 if (res.code === 409) { const isAttendant = res.data && res.data.isAttendant if (isAttendant) { wx.showToast({ title: res.msg || '你已是陪诊师', icon: 'none' }) setTimeout(() => this.refresh(), 800) return } wx.showModal({ title: '无法提交', content: res.msg || '提交失败,请联系客服', showCancel: false }) this.setData({ submitting: false }) return } wx.showToast({ title: res.msg || '提交失败', icon: 'none' }) this.setData({ submitting: false }) }) .catch(() => { wx.showToast({ title: '网络错误,请重试', icon: 'none' }) this.setData({ submitting: false }) }) }, toast(title) { wx.showToast({ title, icon: 'none' }) }, onRetry() { this.refresh() } })