import { uploadFile } from '@/api/fulfiller/app' export default { data() { return { formData: { idType: '居民身份证', name: '', idNumber: '', expiryDate: '' }, idCardFront: '', // 身份证正面本地预览路径 idCardBack: '', // 身份证反面本地预览路径 idCardFrontOssId: '', // 身份证正面 OSS ID idCardBackOssId: '', // 身份证反面 OSS ID showPicker: false, pickerValue: [0, 0, 0], // YYYY-MM-DD years: [], months: [], days: [], tempYear: 0, tempMonth: 0, tempDay: 0, serviceType: [], // 接收上一页的服务类型 isChoosingImage: false // 标志位:是否正在选择图片中,防止 onShow 重置数据 } }, onLoad(options) { if (options.services) { try { this.serviceType = JSON.parse(decodeURIComponent(options.services)); } catch (e) { console.error('Parse services failed', e); } } this.initDateData(); // 进入页面即恢复历史填写数据(回显) this.restoreAuthData(); }, onShow() { // 如果是从选择图片页面返回,不重置数据 if (this.isChoosingImage) { this.isChoosingImage = false; } }, methods: { // --- 日期选择器逻辑 --- initDateData() { const date = new Date(); const year = date.getFullYear(); for (let i = year; i <= year + 50; i++) { this.years.push(i); } for (let i = 1; i <= 12; i++) { this.months.push(i); } for (let i = 1; i <= 31; i++) { this.days.push(i); } }, openPicker() { const date = new Date(); const dateStr = this.formData.expiryDate || `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`; const [y, m, d] = dateStr.split('-').map(Number); let yIndex = this.years.indexOf(y); let mIndex = this.months.indexOf(m); let dIndex = this.days.indexOf(d); this.pickerValue = [ yIndex > -1 ? yIndex : 0, mIndex > -1 ? mIndex : 0, dIndex > -1 ? dIndex : 0 ]; this.tempYear = this.years[this.pickerValue[0]]; this.tempMonth = this.months[this.pickerValue[1]]; this.tempDay = this.days[this.pickerValue[2]]; this.showPicker = true; }, closePicker() { this.showPicker = false; }, onPickerChange(e) { const val = e.detail.value; this.tempYear = this.years[val[0]]; this.tempMonth = this.months[val[1]]; this.tempDay = this.days[val[2]]; }, confirmPicker() { const mStr = this.tempMonth < 10 ? '0' + this.tempMonth : this.tempMonth; const dStr = this.tempDay < 10 ? '0' + this.tempDay : this.tempDay; this.formData.expiryDate = `${this.tempYear}-${mStr}-${dStr}`; this.closePicker(); }, // --- 数据持久化(防止返回上一级丢失) --- restoreAuthData() { try { const saved = uni.getStorageSync('recruit_auth_data'); if (saved) { const d = JSON.parse(saved); this.formData.name = d.name || ''; this.formData.idNumber = d.idNumber || ''; this.formData.expiryDate = d.expiryDate || ''; this.idCardFront = d.idCardFront || ''; this.idCardBack = d.idCardBack || ''; this.idCardFrontOssId = d.idCardFrontOssId || ''; this.idCardBackOssId = d.idCardBackOssId || ''; } } catch (e) { console.error('恢复认证数据失败', e); } }, saveAuthData() { try { uni.setStorageSync('recruit_auth_data', JSON.stringify({ name: this.formData.name, idNumber: this.formData.idNumber, expiryDate: this.formData.expiryDate, idCardFront: this.idCardFront, idCardBack: this.idCardBack, idCardFrontOssId: this.idCardFrontOssId, idCardBackOssId: this.idCardBackOssId })); } catch (e) { console.error('保存认证数据失败', e); } }, // --- 重置表单数据 --- resetFormData() { // 清空本地组件数据 this.formData.name = ''; this.formData.idNumber = ''; this.formData.expiryDate = ''; this.idCardFront = ''; this.idCardBack = ''; this.idCardFrontOssId = ''; this.idCardBackOssId = ''; // 清除本地缓存 try { uni.removeStorageSync('recruit_auth_data'); } catch (e) { console.error('清除缓存失败', e); } }, // --- 图片上传(选择后自动上传到OSS) --- chooseImage(side) { this.isChoosingImage = true; uni.chooseImage({ count: 1, sizeType: ['compressed'], sourceType: ['album', 'camera'], success: async (res) => { const tempPath = res.tempFilePaths[0]; if (side === 'front') { this.idCardFront = tempPath; } else { this.idCardBack = tempPath; } // 上传到OSS try { uni.showLoading({ title: '上传中...' }); const uploadRes = await uploadFile(tempPath); if (side === 'front') { this.idCardFrontOssId = uploadRes.data.ossId; } else { this.idCardBackOssId = uploadRes.data.ossId; } uni.hideLoading(); this.saveAuthData(); } catch (err) { uni.hideLoading(); console.error('上传身份证图片失败:', err); } } }); }, // --- 提交 --- goToQualifications() { // 简单校验 // 简单校验 /* if (!this.formData.name || !this.formData.idNumber) { uni.showToast({ title: '请完善信息', icon: 'none' }); return; } if (!this.idCardFront || !this.idCardBack) { uni.showToast({ title: '请上传证件照', icon: 'none' }); return; } */ // 保存认证数据到缓存 this.saveAuthData(); // 合并身份认证数据到已暂存的招募表单 try { const stored = uni.getStorageSync('recruit_form_data') if (stored) { const data = JSON.parse(stored) data.realName = this.formData.name data.idNumber = this.formData.idNumber data.expiryDate = this.formData.expiryDate data.idCardFrontOssId = this.idCardFrontOssId data.idCardBackOssId = this.idCardBackOssId uni.setStorageSync('recruit_form_data', JSON.stringify(data)) } } catch (e) { console.error('保存认证数据失败', e) } // 传递数据(服务类型对象数组 {id, name}) const services = JSON.stringify(this.serviceType); uni.navigateTo({ url: `/pages/recruit/qualifications?services=${encodeURIComponent(services)}` }); } } }