Files
wxapp_companion/pages/apply/index.js
T
lik c8bcc79865 feat(admin): 新增陪诊员入驻申请与全局角色守卫
- 新增 pages/apply 入驻申请页:填写资料、上传证件、查看审核状态
- 新增 utils/role.js 统一角色解析与管理端页面守卫
- app.js 包装 Page 构造器集中做 onShow 守卫,无身份用户重定向至申请页
- 订单、客户、档案、统计等按角色区分能力:陪诊员不请求档案接口,改用订单快照
- 订单列表支持待取消订单一键同意取消;新增订单价格由服务目录决定只读展示
2026-11-08 12:01:48 +08:00

304 lines
9.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// pages/apply/index.js
// 陪诊师入驻申请页
//
// 无身份用户(role=none)被全局守卫拦住后落到这里;提交后进入待审核视图。
// 审核通过后本页 onShow 会强刷角色,直接切回首页,不需要用户手动重新登录。
const API = require('../../utils/api.js')
const role = require('../../utils/role.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}$/
const ID_NUMBER_REGEX = /^\d{17}[\dXx]$/
// 服务端单张限 5MB,但 wx.request 请求体上限 10MB:
// base64 膨胀 4/3,两张 5MB 图 ≈ 13MB 会在发送前就失败,端上按 3MB/张收紧
const MAX_IMAGE_BYTES = 3 * 1024 * 1024
Page({
data: {
loading: true,
view: 'form', // form 填写 / pending 待审核 / approved 已通过但角色未刷新
submitting: false,
rejectReason: '',
application: null,
serviceAreaText: '',
submitTimeText: '',
accountName: '微信用户',
today: '',
sexLabels: SEX_LABELS,
sexIndex: 0,
form: {
name: '',
mobile: '',
idNumber: '',
sex: '',
serviceAreas: '',
introduction: ''
},
healthExpireDate: '',
photos: {
qualification: '',
healthCertificate: ''
}
},
onLoad() {
const now = new Date()
const pad = (n) => String(n).padStart(2, '0')
this.setData({ today: `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}` })
},
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 || []).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,
healthExpireDate: application.healthExpireDate || '',
form: {
name: applicant.name || '',
mobile: applicant.mobile || '',
idNumber: applicant.idNumber || '',
sex: applicant.sex || '',
serviceAreas: (applicant.serviceAreas || []).join(','),
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 : '' })
},
onExpireChange(e) {
this.setData({ healthExpireDate: e.detail.value })
},
pickImage(e) {
const field = e.currentTarget.dataset.field
if (!field) return
wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['album', 'camera'],
sizeType: ['compressed'],
success: (res) => {
const file = res.tempFiles && res.tempFiles[0]
if (!file) return
if (file.size && file.size > 10 * 1024 * 1024) {
wx.showToast({ title: '请选择 10MB 以内的图片', icon: 'none' })
return
}
// 先压再读:原图直传很容易把请求体顶到小程序 10MB 上限
wx.compressImage({
src: file.tempFilePath,
quality: 60,
success: (compressed) => this.readAsDataUrl(compressed.tempFilePath, field),
fail: () => this.readAsDataUrl(file.tempFilePath, field)
})
},
fail: () => {}
})
},
readAsDataUrl(path, field) {
wx.getFileSystemManager().readFile({
filePath: path,
encoding: 'base64',
success: (res) => {
const bytes = Math.floor((res.data.length * 3) / 4)
if (bytes > MAX_IMAGE_BYTES) {
wx.showToast({ title: '图片过大,请重新拍摄或裁剪到 3MB 以内', icon: 'none' })
return
}
const mime = /\.png$/i.test(path) ? 'png' : 'jpeg'
this.setData({ [`photos.${field}`]: `data:image/${mime};base64,${res.data}` })
},
fail: () => {
wx.showToast({ title: '读取图片失败', icon: 'none' })
}
})
},
submit() {
if (this.data.submitting) return
const { form, photos, healthExpireDate, application } = this.data
const name = form.name.trim()
const mobile = form.mobile.trim()
const idNumber = form.idNumber.trim().toUpperCase()
const areas = form.serviceAreas
.split(/[,,、\s]+/)
.map((item) => item.trim())
.filter(Boolean)
if (!name) return this.toast('请填写姓名')
if (!MOBILE_REGEX.test(mobile)) return this.toast('请填写正确的手机号')
if (!ID_NUMBER_REGEX.test(idNumber)) return this.toast('请填写 18 位身份证号')
if (!areas.length) return this.toast('请填写服务城市')
if (!healthExpireDate) return this.toast('请选择健康证明有效期')
// 驳回后重新提交时允许沿用上次上传的证件照
const isResubmit = !!application && application.status === 'rejected'
if (!photos.qualification && !isResubmit) return this.toast('请上传资质证书照片')
if (!photos.healthCertificate && !isResubmit) return this.toast('请上传健康证明照片')
const payload = {
applicant: {
name,
mobile,
idNumber,
sex: form.sex,
serviceAreas: areas,
introduction: form.introduction.trim()
},
healthExpireDate
}
const materials = {}
if (photos.qualification) materials.qualification = photos.qualification
if (photos.healthCertificate) materials.healthCertificate = photos.healthCertificate
if (Object.keys(materials).length) payload.materials = materials
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 || []).join('、') : areas.join('、'),
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()
}
})