425 lines
14 KiB
JavaScript
425 lines
14 KiB
JavaScript
// 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 已通过但角色未刷新
|
|
editing: false, // 待审核期间修改基本信息
|
|
loggedIn: false, // 是否已完成微信登录
|
|
loggingIn: false, // 微信登录请求中
|
|
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() {
|
|
// 修改资料时前后台切换不重置表单
|
|
if (this.data.editing) return
|
|
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 : '微信用户'
|
|
const loggedIn = !!(user && user.security && user.security.token)
|
|
|
|
API.attendantApplication
|
|
.getMine()
|
|
.then((res) => {
|
|
if (res.code !== 0) {
|
|
// 未登录(新用户走到这里时还没有账号)不弹错误,直接展示登录入口
|
|
if (res.code !== 401) {
|
|
wx.showToast({ title: res.msg || '加载失败', icon: 'none' })
|
|
} else {
|
|
console.info('[apply] 未登录,展示登录入口')
|
|
}
|
|
this.setData({ loading: false, accountName, loggedIn })
|
|
return
|
|
}
|
|
|
|
const application = res.data ? res.data.application : null
|
|
if (!application) {
|
|
// 新账号的手机号/姓名来自微信登录,表单为空时自动带出
|
|
const profile = (user && user.profile) || {}
|
|
const patch = { loading: false, view: 'form', accountName, loggedIn }
|
|
if (!this.data.form.mobile.trim() && profile.mobile) patch['form.mobile'] = profile.mobile
|
|
if (!this.data.form.name.trim() && profile.name && profile.name !== profile.mobile) {
|
|
patch['form.name'] = profile.name
|
|
}
|
|
this.setData(patch)
|
|
return
|
|
}
|
|
|
|
if (application.status === 'pending') {
|
|
this.setData({
|
|
loading: false,
|
|
view: 'pending',
|
|
editing: false,
|
|
loggedIn: true,
|
|
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', loggedIn: true, application, accountName })
|
|
return
|
|
}
|
|
|
|
// 已驳回:回填上次填写的内容,证件照不必重复上传
|
|
const applicant = application.applicant || {}
|
|
const sexIndex = SEX_OPTIONS.findIndex((item) => item.value === applicant.sex)
|
|
this.setData({
|
|
loading: false,
|
|
view: 'form',
|
|
loggedIn: true,
|
|
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, loggedIn })
|
|
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 })
|
|
},
|
|
|
|
/**
|
|
* 提交前确保已登录:
|
|
* 老用户在启动页已完成静默登录;首次使用的新用户还没有账号,
|
|
* 用表单里的手机号+姓名走 wxSignin 注册(后端仅允许未验证手机号用于新建账号,
|
|
* 与健康档案手机号无关联)。
|
|
*/
|
|
ensureLoginForApply() {
|
|
const app = getApp()
|
|
const current = app && app.globalData ? app.globalData.user : null
|
|
if (current && current.security && current.security.token) {
|
|
return Promise.resolve(current)
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
wx.login({
|
|
success: (res) => {
|
|
if (!res.code) {
|
|
reject(new Error('wx.login 未返回 code'))
|
|
return
|
|
}
|
|
API.user.wxSignin({
|
|
code: res.code,
|
|
phoneNumber: this.data.form.mobile.trim(),
|
|
name: this.data.form.name.trim()
|
|
})
|
|
.then((data) => {
|
|
if (data.code === 0) {
|
|
console.info('[apply] 注册/登录成功',
|
|
data.data.user && data.data.user.profile && data.data.user.profile.mobile)
|
|
app.globalData.user = data.data.user
|
|
app.eventBus.emit('user-login', data.data.user)
|
|
resolve(data.data.user)
|
|
return
|
|
}
|
|
console.warn('[apply] 注册/登录被拒:', data.msg)
|
|
reject(new Error(data.msg || '登录失败'))
|
|
})
|
|
.catch(reject)
|
|
},
|
|
fail: (err) => reject(new Error((err && err.errMsg) || 'wx.login 失败'))
|
|
})
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 第一步:手机号登录
|
|
*
|
|
* 陪诊师端没有开通「手机号快速验证」组件,手机号由用户自己输入;
|
|
* 后端在「手机号 + 姓名」与已有账号完全一致时,会把当前微信并入该账号
|
|
*/
|
|
onLogin() {
|
|
const name = this.data.form.name.trim()
|
|
const mobile = this.data.form.mobile.trim()
|
|
if (!name) return this.toast('请填写姓名')
|
|
if (!MOBILE_REGEX.test(mobile)) return this.toast('请填写正确的手机号')
|
|
if (this.data.loggingIn) return
|
|
this.setData({ loggingIn: true })
|
|
|
|
wx.login({
|
|
success: (res) => {
|
|
if (!res.code) {
|
|
this.setData({ loggingIn: false })
|
|
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
|
|
return
|
|
}
|
|
API.user
|
|
.wxSignin({ code: res.code, phoneNumber: mobile, name })
|
|
.then((data) => {
|
|
if (data.code !== 0) {
|
|
this.setData({ loggingIn: false })
|
|
wx.showModal({
|
|
title: '登录失败',
|
|
content: data.msg || '请重试',
|
|
showCancel: false
|
|
})
|
|
return
|
|
}
|
|
const app = getApp()
|
|
app.globalData.user = data.data.user
|
|
app.eventBus.emit('user-login', data.data.user)
|
|
// 清掉登录失败时缓存的角色结果,登录成功后重新解析
|
|
role.resetRole()
|
|
|
|
// 并入已有账号时,姓名与手机号以账号里的为准
|
|
const profile = (data.data.user && data.data.user.profile) || {}
|
|
const form = { ...this.data.form }
|
|
if (profile.mobile) form.mobile = profile.mobile
|
|
if (profile.name && profile.name !== profile.mobile) form.name = profile.name
|
|
this.setData({ loggingIn: false, loggedIn: true, form })
|
|
wx.showToast({ title: '登录成功', icon: 'success' })
|
|
this.loadApplication()
|
|
})
|
|
.catch((err) => {
|
|
console.warn('[apply] 登录失败:', err)
|
|
this.setData({ loggingIn: false })
|
|
wx.showToast({ title: (err && err.message) || '网络错误,请重试', icon: 'none' })
|
|
})
|
|
},
|
|
fail: () => {
|
|
this.setData({ loggingIn: false })
|
|
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 待审核 → 修改模式:回填已提交的基本信息
|
|
*/
|
|
startEdit() {
|
|
const application = this.data.application
|
|
if (!application) return
|
|
const applicant = application.applicant || {}
|
|
const sexIndex = SEX_OPTIONS.findIndex((item) => item.value === applicant.sex)
|
|
this.setData({
|
|
view: 'form',
|
|
editing: true,
|
|
rejectReason: '',
|
|
sexIndex: sexIndex > -1 ? sexIndex : 0,
|
|
cityIndex: cityIndex((applicant.serviceAreas || [])[0]),
|
|
form: {
|
|
name: applicant.name || '',
|
|
mobile: applicant.mobile || '',
|
|
sex: applicant.sex || '',
|
|
introduction: applicant.introduction || ''
|
|
}
|
|
})
|
|
},
|
|
|
|
cancelEdit() {
|
|
// 放弃本次修改,回到待审核视图
|
|
this.setData({ view: 'pending', editing: false })
|
|
},
|
|
|
|
submitUpdate(payload) {
|
|
this.setData({ submitting: true })
|
|
API.attendantApplication
|
|
.updateMine(payload)
|
|
.then((res) => {
|
|
if (res.code === 0) {
|
|
const application = res.data ? res.data.application : null
|
|
wx.showToast({ title: '已保存', icon: 'success' })
|
|
this.setData({
|
|
submitting: false,
|
|
view: 'pending',
|
|
editing: false,
|
|
application,
|
|
serviceAreaText: application
|
|
? (application.applicant.serviceAreas || []).map(cityLabel).join('、')
|
|
: '',
|
|
submitTimeText: application ? this.formatTime(application.createtime) : ''
|
|
})
|
|
return
|
|
}
|
|
this.setData({ submitting: false })
|
|
wx.showToast({ title: res.msg || '保存失败', icon: 'none' })
|
|
})
|
|
.catch((err) => {
|
|
const msg = err && err.message ? err.message : ''
|
|
this.setData({ submitting: false })
|
|
wx.showToast({ title: msg || '网络错误,请重试', icon: 'none' })
|
|
})
|
|
},
|
|
|
|
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()
|
|
}
|
|
}
|
|
|
|
// 待审核修改:只更新已有的申请单,不重新走提交逻辑
|
|
if (this.data.editing) {
|
|
this.submitUpdate(payload)
|
|
return
|
|
}
|
|
|
|
this.setData({ submitting: true })
|
|
this.ensureLoginForApply()
|
|
.then(() => 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((err) => {
|
|
const msg = err && err.message ? err.message : ''
|
|
console.warn('[apply] 提交失败:', msg)
|
|
this.setData({ submitting: false })
|
|
// 登录环节的业务拒绝(手机号已注册且姓名不一致/微信已绑定其他账号)需要弹窗说明
|
|
if (/已注册|已绑定|手机号/.test(msg)) {
|
|
wx.showModal({
|
|
title: '无法提交',
|
|
content: msg,
|
|
showCancel: false
|
|
})
|
|
return
|
|
}
|
|
wx.showToast({ title: msg || '网络错误,请重试', icon: 'none' })
|
|
})
|
|
},
|
|
|
|
toast(title) {
|
|
wx.showToast({ title, icon: 'none' })
|
|
},
|
|
|
|
onRetry() {
|
|
this.refresh()
|
|
}
|
|
})
|