212 lines
6.2 KiB
JavaScript
212 lines
6.2 KiB
JavaScript
// pages/train/apply.js
|
|
// 2026 医疗陪诊顾问(高级)考位报名。图片 base64 只放实例变量,
|
|
// 不进 setData,避免大字符串造成渲染与未保存快照的性能问题。
|
|
import API from '../../utils/api'
|
|
import { setUnsavedGuard, disableUnsavedGuard } from '../../utils/form'
|
|
|
|
const MAX_IMAGE_BYTES = 5 * 1024 * 1024
|
|
|
|
Page({
|
|
data: {
|
|
educationOptions: ['初中及以下', '高中/中专', '大专', '本科', '硕士及以上'],
|
|
educationIndex: -1,
|
|
form: {
|
|
name: '',
|
|
idNumber: '',
|
|
mobile: '',
|
|
education: '',
|
|
examMonth: ''
|
|
},
|
|
photoPath: '',
|
|
idCardPath: '',
|
|
errors: {},
|
|
saving: false,
|
|
submitted: false,
|
|
successDesc: '',
|
|
application: null
|
|
},
|
|
|
|
onLoad() {
|
|
this.materialFiles = { photo: null, idCard: null }
|
|
this.captureInitialForm()
|
|
|
|
// 授权登录后预填姓名和手机号;用户已开始填写则不覆盖
|
|
getApp().waitForAuth().then(() => {
|
|
if (this.isDirty()) return
|
|
const profile = getApp().globalData.user?.profile || {}
|
|
this.setData({
|
|
'form.name': String(profile.name || '').trim(),
|
|
'form.mobile': String(profile.mobile || '').trim()
|
|
})
|
|
this.captureInitialForm()
|
|
}).catch(() => {})
|
|
},
|
|
|
|
onUnload() {
|
|
disableUnsavedGuard()
|
|
},
|
|
|
|
getSnapshot() {
|
|
const { form, photoPath, idCardPath } = this.data
|
|
return JSON.stringify({ form, photoPath, idCardPath })
|
|
},
|
|
|
|
captureInitialForm() {
|
|
this.initialSnapshot = this.getSnapshot()
|
|
this.syncUnsavedGuard()
|
|
},
|
|
|
|
isDirty() {
|
|
return this.getSnapshot() !== this.initialSnapshot
|
|
},
|
|
|
|
syncUnsavedGuard() {
|
|
setUnsavedGuard(this.isDirty())
|
|
},
|
|
|
|
onInput(e) {
|
|
const { field } = e.currentTarget.dataset
|
|
this.setData({
|
|
[`form.${field}`]: e.detail.value,
|
|
[`errors.${field}`]: ''
|
|
})
|
|
this.syncUnsavedGuard()
|
|
},
|
|
|
|
onEducationChange(e) {
|
|
const index = Number(e.detail.value)
|
|
this.setData({
|
|
educationIndex: index,
|
|
'form.education': this.data.educationOptions[index],
|
|
'errors.education': ''
|
|
})
|
|
this.syncUnsavedGuard()
|
|
},
|
|
|
|
onExamMonthChange(e) {
|
|
this.setData({ 'form.examMonth': e.detail.value, 'errors.examMonth': '' })
|
|
this.syncUnsavedGuard()
|
|
},
|
|
|
|
onChooseImage(e) {
|
|
const { kind } = e.currentTarget.dataset
|
|
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 > MAX_IMAGE_BYTES) {
|
|
wx.showToast({ title: '图片不能超过 5MB', icon: 'none' })
|
|
return
|
|
}
|
|
this.materialFiles[kind] = file
|
|
this.setData({
|
|
[`${kind}Path`]: file.tempFilePath,
|
|
[`errors.${kind}`]: ''
|
|
})
|
|
this.syncUnsavedGuard()
|
|
},
|
|
fail: () => {}
|
|
})
|
|
},
|
|
|
|
readImage(kind) {
|
|
const file = this.materialFiles[kind]
|
|
if (!file) return Promise.reject(new Error('未选择图片'))
|
|
const mime = file.tempFilePath.toLowerCase().endsWith('.png') ? 'image/png' : 'image/jpeg'
|
|
return new Promise((resolve, reject) => {
|
|
wx.getFileSystemManager().readFile({
|
|
filePath: file.tempFilePath,
|
|
encoding: 'base64',
|
|
success: (res) => resolve(`data:${mime};base64,${res.data}`),
|
|
fail: () => reject(new Error('图片读取失败,请重新选择'))
|
|
})
|
|
})
|
|
},
|
|
|
|
validate() {
|
|
const { form, photoPath, idCardPath } = this.data
|
|
const errors = {}
|
|
if (!form.name.trim()) errors.name = '请填写姓名'
|
|
if (!/^\d{17}[\dXx]$/.test(form.idNumber.trim())) errors.idNumber = '请填写 18 位身份证号,末位可为 X'
|
|
if (!/^1[3-9]\d{9}$/.test(form.mobile.trim())) errors.mobile = '请填写正确的 11 位手机号'
|
|
if (!form.education) errors.education = '请选择学历'
|
|
if (!form.examMonth) errors.examMonth = '请选择考试月份'
|
|
if (!photoPath) errors.photo = '请上传 2 寸蓝底照片电子版'
|
|
if (!idCardPath) errors.idCard = '请上传身份证电子版'
|
|
return errors
|
|
},
|
|
|
|
async onSubmit() {
|
|
if (this.data.saving) return
|
|
const errors = this.validate()
|
|
this.setData({ errors })
|
|
if (Object.keys(errors).length) {
|
|
wx.showToast({ title: Object.values(errors)[0], icon: 'none' })
|
|
return
|
|
}
|
|
|
|
this.setData({ saving: true })
|
|
wx.showLoading({ title: '提交中...', mask: true })
|
|
try {
|
|
await getApp().waitForAuth()
|
|
const [photo, idCard] = await Promise.all([
|
|
this.readImage('photo'),
|
|
this.readImage('idCard')
|
|
])
|
|
const { form } = this.data
|
|
const res = await API.training.createApplication({
|
|
applicant: {
|
|
name: form.name.trim(),
|
|
idNumber: form.idNumber.trim().toUpperCase(),
|
|
mobile: form.mobile.trim(),
|
|
education: form.education
|
|
},
|
|
examMonth: form.examMonth,
|
|
materials: { photo, idCard }
|
|
})
|
|
if (res.code !== 0) {
|
|
wx.showToast({ title: res.msg || '提交失败,请重试', icon: 'none' })
|
|
return
|
|
}
|
|
|
|
const serverApplication = res.data?.application
|
|
const application = serverApplication || {
|
|
applicant: {
|
|
name: form.name.trim(),
|
|
mobile: form.mobile.trim(),
|
|
education: form.education
|
|
},
|
|
examMonth: form.examMonth,
|
|
course: { name: '医疗陪诊顾问(高级)', issuer: '国家开放大学培训中心' }
|
|
}
|
|
setUnsavedGuard(false)
|
|
this.setData({
|
|
submitted: true,
|
|
application,
|
|
successDesc: res.msg === '你已提交过报名,客服会尽快联系你'
|
|
? res.msg
|
|
: '报名资料已提交,客服会尽快与你核对考位和考试安排,请保持手机畅通。'
|
|
})
|
|
wx.hideLoading()
|
|
wx.showToast({ title: '报名已提交', icon: 'success' })
|
|
} catch (err) {
|
|
wx.showToast({ title: err?.message || '网络异常,请稍后重试', icon: 'none' })
|
|
} finally {
|
|
wx.hideLoading()
|
|
this.setData({ saving: false })
|
|
}
|
|
},
|
|
|
|
goToContact() {
|
|
wx.navigateTo({ url: '/pages/mine/contact' })
|
|
},
|
|
|
|
goHome() {
|
|
wx.switchTab({ url: '/pages/home/index' })
|
|
}
|
|
})
|