import { ref, computed } from 'vue'; import { onLoad } from '@dcloudio/uni-app'; import { addStudentExperience, updateStudentExperience } from '../../api/studentExperience'; export default { setup() { const form = ref({ // Note: form fields map to MainStudentExperienceBo fields company: '', industry: '', startTime: '', endTime: '', positionName: '', department: '', content: '', // maps to desc isIntern: false, isHidden: false }); // "Wait for complete" logic: mandatory fields const isComplete = computed(() => { return !!(form.value.company && form.value.industry && form.value.startTime && form.value.positionName && form.value.content); }); const editIndex = ref(-1); const isEdit = computed(() => editIndex.value !== -1); onLoad((options) => { if (options && options.index !== undefined) { editIndex.value = parseInt(options.index); const data = uni.getStorageSync('edit_data_work'); if (data) { // 映射后端字段到表单字段 form.value = { id: data.id, company: data.company || '', industry: data.industry || '', startTime: data.startTime || '', endTime: data.endTime || '', positionName: data.jobTitle || data.positionName || '', department: data.department || '', content: data.workContent || data.content || '', isIntern: data.isInternship === 1 || data.isIntern || false, isHidden: !!data.isHidden }; } } uni.setNavigationBarTitle({ title: isEdit.value ? '修改工作经历' : '添加工作经历' }); uni.$on('select_industry', (val) => { form.value.industry = val; }); uni.$on('select_position', (val) => { form.value.positionName = val; }); }); const goBack = () => { uni.navigateBack(); }; const openIndustrySelector = () => { const currentSelected = form.value.industry ? encodeURIComponent(form.value.industry) : ''; uni.navigateTo({ url: `/pages/experience/industry-select?selected=${currentSelected}` }); }; const openPositionSelector = () => { const currentSelected = form.value.positionName ? encodeURIComponent(form.value.positionName) : ''; uni.navigateTo({ url: `/pages/experience/position-select?selected=${currentSelected}` }); }; const yearOptions = []; const monthOptions = []; const currentYear = new Date().getFullYear(); for (let i = 1990; i <= currentYear + 10; i++) { yearOptions.push(i + '年'); } for (let i = 1; i <= 12; i++) { monthOptions.push((i < 10 ? '0' + i : i) + '月'); } const showDatePicker = ref(false); const datePickerType = ref('start'); // 'start' or 'end' const datePickerValue = ref([currentYear - 1990, 0]); // 默认当前年 1月 const tempStartTime = ref(''); const tempEndTime = ref(''); const switchDateTab = (type) => { datePickerType.value = type; const target = type === 'start' ? tempStartTime.value : tempEndTime.value; if (target && target !== '至今') { const parts = target.split('.'); let yIdx = yearOptions.indexOf(parts[0] + '年'); let mIdx = monthOptions.indexOf(parts[1] + '月'); if (yIdx === -1) yIdx = currentYear - 1990; if (mIdx === -1) mIdx = 0; datePickerValue.value = [yIdx, mIdx]; } else { datePickerValue.value = [currentYear - 1990, 0]; if (type === 'start' && !tempStartTime.value) { tempStartTime.value = `${currentYear}.01`; } } }; const openDatePicker = (type) => { tempStartTime.value = form.value.startTime; tempEndTime.value = form.value.endTime; showDatePicker.value = true; switchDateTab(type); }; const closeDatePicker = () => { showDatePicker.value = false; }; const onDatePickerChange = (e) => { datePickerValue.value = e.detail.value; const yIdx = datePickerValue.value[0] !== undefined ? datePickerValue.value[0] : (currentYear - 1990); const mIdx = datePickerValue.value[1] !== undefined ? datePickerValue.value[1] : 0; const yVal = yearOptions[yIdx].replace('年', ''); const mVal = monthOptions[mIdx].replace('月', ''); const formatted = `${yVal}.${mVal}`; if (datePickerType.value === 'start') { tempStartTime.value = formatted; } else { tempEndTime.value = formatted; } }; const confirmDatePicker = () => { form.value.startTime = tempStartTime.value; form.value.endTime = tempEndTime.value; showDatePicker.value = false; }; const saveForm = async () => { if (!isComplete.value) return; uni.showLoading({ title: '保存中...' }); try { const userInfo = uni.getStorageSync('userInfo'); const studentId = userInfo ? userInfo.studentId : null; if (!studentId) { uni.hideLoading(); uni.showToast({ title: '登录失效,请重新登录', icon: 'none' }); return; } const reqData = { studentId: studentId, company: form.value.company, industry: form.value.industry, jobTitle: form.value.positionName, department: form.value.department, startTime: form.value.startTime, endTime: form.value.endTime, workContent: form.value.content, isInternship: form.value.isIntern ? 1 : 0, isHidden: form.value.isHidden ? 1 : 0 }; if (isEdit.value) { reqData.id = form.value.id; const res = await updateStudentExperience(reqData); uni.hideLoading(); if (res.code === 200) { uni.showToast({ title: '修改成功', icon: 'success' }); uni.removeStorageSync('edit_data_work'); setTimeout(() => { uni.$emit('refresh_experience'); uni.navigateBack(); }, 1000); } else { uni.showToast({ title: res.msg || '修改失败', icon: 'none' }); } } else { const res = await addStudentExperience(reqData); uni.hideLoading(); if (res.code === 200) { uni.showToast({ title: '添加成功', icon: 'success' }); setTimeout(() => { uni.$emit('refresh_experience'); uni.navigateBack(); }, 1000); } else { uni.showToast({ title: res.msg || '添加失败', icon: 'none' }); } } } catch (error) { uni.hideLoading(); uni.showToast({ title: '网络异常,保存失败', icon: 'none' }); console.error(error); } }; return { form, isComplete, isEdit, goBack, openIndustrySelector, openPositionSelector, showDatePicker, datePickerType, datePickerValue, yearOptions, monthOptions, tempStartTime, tempEndTime, openDatePicker, closeDatePicker, switchDateTab, onDatePickerChange, confirmDatePicker, saveForm }; } }