// pages/materials/index.js // 证件材料后补:入驻申请提交时材料可为空,审核前/通过后在此补传。 // 只提交本次选择或修改的字段,未改动的保留服务端原值。 const API = require('../../utils/api.js') // 与服务端 IMAGE_MAX_BYTES 一致:单张 3MB,避免请求体超过小程序 10MB 上限 const MAX_IMAGE_BYTES = 3 * 1024 * 1024 Page({ data: { loading: true, submitting: false, application: null, statusText: '', hasQualification: false, hasHealthCertificate: false, healthExpireDate: '', photos: { qualification: '', healthCertificate: '' }, today: '' }, 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.load() }, load() { this.setData({ loading: true }) API.attendantApplication .getMine() .then((res) => { if (res.code !== 0) { this.setData({ loading: false }) wx.showToast({ title: res.msg || '加载失败', icon: 'none' }) return } const application = res.data ? res.data.application : null const statusText = application ? ({ pending: '待审核', approved: '已通过', rejected: '已驳回' }[application.status] || '') : '' this.originalExpireDate = (application && application.healthExpireDate) || '' this.setData({ loading: false, application, statusText, hasQualification: !!(application && application.hasQualification), hasHealthCertificate: !!(application && application.hasHealthCertificate), healthExpireDate: this.originalExpireDate }) }) .catch(() => { this.setData({ loading: false }) wx.showToast({ title: '网络错误,请重试', icon: 'none' }) }) }, 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 if (!this.data.application) return this.toast('没有可补充的入驻申请') const { photos, healthExpireDate, application } = this.data const payload = {} if (photos.qualification) payload.qualification = photos.qualification if (photos.healthCertificate) payload.healthCertificate = photos.healthCertificate if (healthExpireDate && healthExpireDate !== this.originalExpireDate) { payload.healthExpireDate = healthExpireDate } if (!Object.keys(payload).length) { if (application.status === 'rejected') return this.toast('申请已驳回,请重新提交入驻申请') return this.toast('请选择要补充的证件材料') } this.setData({ submitting: true }) API.attendantApplication .updateMaterials(payload) .then((res) => { if (res.code === 0) { wx.showToast({ title: '材料已补充', icon: 'success' }) this.originalExpireDate = healthExpireDate || this.originalExpireDate this.setData({ submitting: false, photos: { qualification: '', healthCertificate: '' }, hasQualification: photos.qualification ? true : this.data.hasQualification, hasHealthCertificate: photos.healthCertificate ? true : this.data.hasHealthCertificate }) return } this.setData({ submitting: false }) wx.showToast({ title: res.msg || '保存失败', icon: 'none' }) }) .catch(() => { this.setData({ submitting: false }) wx.showToast({ title: '网络错误,请重试', icon: 'none' }) }) }, toast(title) { wx.showToast({ title, icon: 'none' }) } })