import { sendSmsCode } from '@/api/resource/sms' import { getAreaStationList } from '@/api/system/areaStation' import { listAllService } from '@/api/service/list' import { getAgreement } from '@/api/system/agreement' export default { data() { return { formData: { mobile: '', code: '', name: '', gender: 1, // 1男 2女 birthday: '', password: '', serviceType: [], station: '', stationId: null, areaPath: '' // 用于回显“区域+站点”名称 }, showPwd: false, isAgreed: false, serviceTypes: [], // 验证码倒计时 countDown: 0, timer: null, // 日期选择器相关 showPicker: false, years: [], months: [], days: [], pickerValue: [0, 0, 0], tempYear: 0, tempMonth: 0, tempDay: 0, // 站点选择器(级联版) showStationPickerCascader: false, selectStep: 0, selectedPathway: [], currentList: [], fullStationData: [], // 全量数据 selectedStationId: null, // 协议弹窗 showPrivacy: false, agreementTitle: '', // 协议标题 agreementContent: '', // 协议内容 currentAgreementId: '' // 当前协议ID } }, onLoad() { this.initDateData(); this.loadServiceTypes(); this.loadAreaStationData(); // 预加载站点全量数据 // 尝试从缓存中恢复数据(回显) this.restoreFormData(); }, beforeDestroy() { if (this.timer) clearInterval(this.timer); }, methods: { async loadAreaStationData() { try { const res = await getAreaStationList(); this.fullStationData = res.data || []; // 暂时保存在内存 } catch (err) { console.error('加载站点列表失败:', err); } }, restoreFormData() { try { const saved = uni.getStorageSync('recruit_form_data'); if (saved) { const d = JSON.parse(saved); // 深度合并或手动赋值 Object.assign(this.formData, d); // 恢复私有的路径状态 if (d._selectedPathway) { this.selectedPathway = d._selectedPathway; this.selectStep = this.selectedPathway.length; } // 加载站点列表(如果选了区域的话) if (this.selectedPathway.length > 0) { const last = this.selectedPathway[this.selectedPathway.length - 1]; if (last) this.loadStations(last.id); } } } catch (e) { console.error('恢复表单数据失败', e); } }, initDateData() { const now = new Date(); const currentYear = now.getFullYear(); // 初始化年份 (1980 - 2030) for (let i = 1980; i <= currentYear + 5; i++) { this.years.push(i); } // 初始化月份 for (let i = 1; i <= 12; i++) { this.months.push(i); } // 初始化日期 (默认31天, 实际应动态计算, 这里简化处理或在change中联动) for (let i = 1; i <= 31; i++) { this.days.push(i); } }, // 打开选择器 openPicker() { // 解析当前选中日期或默认日期 const dateStr = this.formData.birthday || '2000-01-01'; const [y, m, d] = dateStr.split('-').map(Number); // 查找索引 const yIndex = this.years.indexOf(y); const mIndex = this.months.indexOf(m); const 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.birthday = `${this.tempYear}-${mStr}-${dStr}`; this.closePicker(); }, async loadServiceTypes() { try { const res = await listAllService(); this.serviceTypes = (res.data || []).map(item => ({ id: item.id, name: item.name })); } catch (err) { console.error('加载服务类型失败:', err); this.serviceTypes = []; } }, toggleService(item) { const idx = this.formData.serviceType.indexOf(item.id); if (idx > -1) { this.formData.serviceType.splice(idx, 1); } else { this.formData.serviceType.push(item.id); } }, // 验证码 /* async getVerifyCode() { if (this.countDown > 0) return; if (!this.formData.mobile || this.formData.mobile.length !== 11) { uni.showToast({ title: '请输入正确的手机号', icon: 'none' }); return; } try { const res = await sendSmsCode(this.formData.mobile); this.countDown = 60; this.timer = setInterval(() => { this.countDown--; if (this.countDown <= 0) clearInterval(this.timer); }, 1000); // TODO 【生产环境必须删除】开发模式自动填入验证码 const devCode = res.data; if (devCode) { this.formData.code = devCode; uni.showToast({ title: '验证码: ' + devCode, icon: 'none', duration: 3000 }); } else { uni.showToast({ title: '验证码已发送', icon: 'none' }); } } catch (err) { console.error('发送验证码失败:', err); } }, */ // 站点级联选择逻辑 (从全量本地数据中根据 parentId 过滤) async openStationPickerCascader() { this.showStationPickerCascader = true; if (this.selectedPathway.length === 0) { await this.resetStationPicker(); } }, async resetStationPicker() { this.selectStep = 0; this.selectedPathway = []; this.filterLocalChildren(0); }, closeStationPickerCascader() { this.showStationPickerCascader = false; }, filterLocalChildren(parentId) { // 从全量数据中筛选当前层级的子项 this.currentList = this.fullStationData.filter(item => item.parentId == parentId); }, async selectStationItem(item) { this.selectedPathway[this.selectStep] = item; // 在全量数据中查找这是否有子节点 (子级联) const sons = this.fullStationData.filter(i => i.parentId == item.id); if (sons.length > 0) { // 进入下一步 this.selectStep++; this.selectedPathway = this.selectedPathway.slice(0, this.selectStep); this.currentList = sons; } else { // 没有子节点了,说明是最终的站点(叶子节点) this.confirmStation(); } }, async jumpToStep(step) { this.selectStep = step; if (step === 0) { this.filterLocalChildren(0); } else { const parent = this.selectedPathway[step - 1]; if (parent) { this.filterLocalChildren(parent.id); } } }, confirmStation() { const path = this.selectedPathway.map(i => i.name); const stationName = path[path.length - 1]; const areaName = path.slice(0, -1).join(' '); // 排除站点后的父级名 this.formData.station = stationName; this.formData.stationId = this.selectedPathway[this.selectedPathway.length - 1].id; this.formData.areaPath = areaName; this.closeStationPickerCascader(); }, // --- 废弃的功能逻辑 (已被站点选择合并或移除) --- /* async loadStations(parentId) { ... } */ /* openStationPicker() { ... } */ /* selectStation(item) { ... } */ /* async openCityPicker() { ... } */ /* loadAreaChildren(parentId) { ... } */ /* async selectCityItem(item) { ... } */ /* confirmCity() { ... } */ async openPrivacy() { try { uni.showLoading({ title: '加载中...' }); const res = await getAgreement(3); // 3-履约者说明 if (res.code === 200 && res.data) { this.agreementTitle = res.data.title; this.agreementContent = res.data.content; this.showPrivacy = true; } else { uni.showToast({ title: res.msg || '获取协议失败', icon: 'none' }); } } catch (err) { console.error('获取协议详情失败:', err); } finally { uni.hideLoading(); } }, goToAuth() { if (!this.isAgreed) { uni.showToast({ title: '请勾选协议', icon: 'none' }); return; } if (!this.formData.mobile || this.formData.mobile.length !== 11) { uni.showToast({ title: '请输入正确的手机号', icon: 'none' }); return; } if (!this.formData.name) { uni.showToast({ title: '请输入姓名', icon: 'none' }); return; } if (this.formData.serviceType.length === 0) { uni.showToast({ title: '请选择服务类型', icon: 'none' }); return; } if (!this.formData.stationId) { uni.showToast({ title: '请选择所属站点', icon: 'none' }); return; } // 暂存表单数据到本地,供后续页面组装提交 // 同时保存 selectedPathway 确保站点级联状态能恢复 uni.setStorageSync('recruit_form_data', JSON.stringify({ ...this.formData, _selectedPathway: this.selectedPathway // 私有存储,仅用于回显 })); // 传递选中的服务类型对象(id+name)给后续页面 const selectedServices = this.serviceTypes.filter(s => this.formData.serviceType.includes(s.id)); const services = JSON.stringify(selectedServices); uni.navigateTo({ url: `/pages/recruit/auth?services=${encodeURIComponent(services)}` }); } } }