| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- import { ref, computed } from 'vue';
- import { onLoad } from '@dcloudio/uni-app';
- import { addStudentEducation, updateStudentEducation } from '../../api/studentEducation';
- import { getDicts } from '../../api/dict';
- export default {
- setup() {
- // 从字典接口获取选项,初始化为空数组
- const degreeOptions = ref([]);
- const typeOptions = ref([]);
- const form = ref({
- school: '',
- degree: '',
- educationType: '',
- major: '',
- startTime: '',
- endTime: '',
- desc: ''
- });
- // "Wait for complete" logic: mandatory fields
- const isComplete = computed(() => {
- return !!(form.value.school &&
- form.value.degree &&
- form.value.major &&
- form.value.startTime &&
- form.value.endTime);
- });
- const editIndex = ref(-1);
- const isEdit = computed(() => editIndex.value !== -1);
- // 加载字典数据
- const loadDicts = async () => {
- try {
- const [eduRes, typeRes] = await Promise.all([
- getDicts('main_education'),
- getDicts('main_education_all')
- ]);
- degreeOptions.value = (eduRes.data || []).map(item => item.dictLabel);
- typeOptions.value = (typeRes.data || []).map(item => item.dictLabel);
- // 设置默认值
- if (!form.value.educationType && typeOptions.value.length > 0) {
- form.value.educationType = typeOptions.value[0];
- }
- } catch (e) {
- console.error('加载字典失败', e);
- }
- };
- onLoad((options) => {
- loadDicts();
- if (options && options.index !== undefined) {
- editIndex.value = parseInt(options.index);
- const data = uni.getStorageSync('edit_data_education');
- if (data) {
- // 映射后端字段到表单字段
- // education 字段可能是 "本科 统招" 格式(旧数据),也可能是纯 "本科"(新数据)
- let degreeVal = data.education || data.degree || '';
- let typeVal = '';
-
- if (degreeVal.includes(' ')) {
- // 旧格式 "本科 统招",拆分
- const parts = degreeVal.split(' ');
- degreeVal = parts[0];
- typeVal = parts[1] || '';
- }
- // 优先使用独立字段 educationType
- if (data.educationType) {
- typeVal = data.educationType;
- }
-
- form.value = {
- id: data.id,
- school: data.school || '',
- degree: degreeVal,
- educationType: typeVal,
- major: data.major || '',
- startTime: data.startTime || '',
- endTime: data.endTime || '',
- desc: data.campusExperience || data.desc || ''
- };
- }
- }
- uni.setNavigationBarTitle({ title: isEdit.value ? '修改教育经历' : '添加教育经历' });
- });
- const goBack = () => {
- uni.navigateBack();
- };
- const showPicker = ref(false);
- const pickerValue = ref([0, 0]);
- const openPicker = () => {
- if (form.value.degree) {
- let dIdx = degreeOptions.value.indexOf(form.value.degree);
- let tIdx = typeOptions.value.indexOf(form.value.educationType);
- if (dIdx === -1) dIdx = 0;
- if (tIdx === -1) tIdx = 0;
- pickerValue.value = [dIdx, tIdx];
- } else {
- pickerValue.value = [0, 0];
- }
- showPicker.value = true;
- };
- const closePicker = () => {
- showPicker.value = false;
- };
- const onPickerChange = (e) => {
- pickerValue.value = e.detail.value;
- };
- const confirmPicker = () => {
- const dIdx = pickerValue.value[0] !== undefined ? pickerValue.value[0] : 0;
- const tIdx = pickerValue.value[1] !== undefined ? pickerValue.value[1] : 0;
- if (degreeOptions.value.length > 0) {
- form.value.degree = degreeOptions.value[dIdx];
- }
- if (typeOptions.value.length > 0) {
- form.value.educationType = typeOptions.value[tIdx];
- }
- showPicker.value = false;
- };
- 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 openDatePicker = (type) => {
- datePickerType.value = type;
- const target = type === 'start' ? form.value.startTime : form.value.endTime;
- if (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];
- }
- showDatePicker.value = true;
- };
- const closeDatePicker = () => {
- showDatePicker.value = false;
- };
- const onDatePickerChange = (e) => {
- datePickerValue.value = e.detail.value;
- };
- const confirmDatePicker = () => {
- 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') {
- form.value.startTime = formatted;
- } else {
- form.value.endTime = formatted;
- }
- 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;
- }
- // 拼装后端需要的数据格式 (MainStudentEducationBo)
- const reqData = {
- studentId: studentId,
- school: form.value.school,
- education: form.value.degree,
- educationType: form.value.educationType,
- major: form.value.major,
- startTime: form.value.startTime,
- endTime: form.value.endTime,
- campusExperience: form.value.desc
- };
- if (isEdit.value) {
- reqData.id = form.value.id; // 如果是修改模式,需要附加主键 ID
- const res = await updateStudentEducation(reqData);
- uni.hideLoading();
- if (res.code === 200) {
- uni.showToast({ title: '修改成功', icon: 'success' });
- uni.removeStorageSync('edit_data_education');
- setTimeout(() => {
- uni.$emit('refresh_experience'); // 传给外层让它再次加载数据
- uni.navigateBack();
- }, 1000);
- } else {
- uni.showToast({ title: res.msg || '修改失败', icon: 'none' });
- }
- } else {
- const res = await addStudentEducation(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,
- degreeOptions,
- typeOptions,
- isComplete,
- isEdit,
- goBack,
- showPicker,
- pickerValue,
- openPicker,
- closePicker,
- onPickerChange,
- confirmPicker,
- showDatePicker,
- datePickerType,
- datePickerValue,
- yearOptions,
- monthOptions,
- openDatePicker,
- closeDatePicker,
- onDatePickerChange,
- confirmDatePicker,
- saveForm
- };
- }
- }
|