import { AddReq, CreateCrudOptionsProps, CreateCrudOptionsRet, dict, UserPageQuery, compute, InfoReq, EditReq, DelReq } from '@fast-crud/fast-crud'; import * as api from './api'; import { auth } from '/@/utils/authFunction'; export const createCrudOptions = function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet { const pageRequest = async (query: UserPageQuery) => { return await api.GetList(query); }; const getDetail = async ({ row }: InfoReq) => { return await api.GetObj(row.id); }; const editRequest = async ({ form, row }: EditReq) => { form.id = row.id; return await api.UpdateObj(form); }; const delRequest = async ({ row }: DelReq) => { return await api.DelObj(row.id); }; const addRequest = async ({ form }: AddReq) => { return await api.AddObj(form); }; /** * 懒加载 * @param row * @returns {Promise} */ // const loadContentMethod = (tree: any, treeNode: any, resolve: Function) => { // pageRequest({ pcode: tree.code }).then((res: APIResponseData) => { // resolve(res.data); // }); // }; return { crudOptions: { request: { pageRequest, editRequest, delRequest, addRequest, getDetail, }, actionbar: { buttons: { add: { show: false,//auth('area:Create'), }, }, }, form:{ wrapper: { buttons: { ok:{ text:'提交', show:compute((row) => { return row.mode !=='view' }) } } } }, pagination: { show: true, }, columns: { _index: { title: '序号', form: { show: false }, column: { type: 'index', align: 'center', width: '70px', columnSetDisabled: true, //禁止在列设置中选择 }, }, search:{ title: '关键字搜索', search: { show: true,type: 'input', }, type: 'input', form: { component: { placeholder: '请输入' }, show:false}, column: {show:false} }, user_code:{ title:'学号', type:'input', column: { minWidth: 120, }, form: { component: { placeholder: '请填写学号' }, rules: [{ required: true, message: '请填写学号' }], }, viewForm:{ component: { placeholder: '' }, } }, username:{ title:'用户名', type:'input', column: { show: false, minWidth: 120, }, form: { component: { placeholder: '请填写用户名' }, rules: [{ required: true, message: '请填写用户名' }], }, viewForm:{ component: { placeholder: '' }, } }, name: { title: '姓名', search: { show: false, }, treeNode: true, type: 'input', column: { minWidth: 120, }, form: { rules: [ // 表单校验规则 { required: true, message: '姓名必填项' }, ], component: { placeholder: '请输入姓名', }, }, viewForm:{ component: { placeholder: '' }, } }, password:{ title: '密码', type: 'password', column: { minWidth: 120, show: false, }, form: { component: { placeholder: '请填写密码' }, /* rules: [ { required: true, message: '请填写密码' }, { validator: (rule: any, value: string, callback: Function) => { if (!value) { callback(); return; } // 检查密码长度不少于8位 if (value.length < 8) { callback(new Error('密码长度不能少于8位')); return; } // 检查是否包含字母 const hasLetter = /[a-zA-Z]/.test(value); // 检查是否包含数字 const hasNumber = /[0-9]/.test(value); // 检查是否包含特殊符号 const hasSpecial = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?`~]/.test(value); if (!hasLetter || !hasNumber || !hasSpecial) { callback(new Error('密码必须包含字母、数字和特殊符号')); return; } callback(); }, trigger: 'blur' } ], */ }, addForm:{ component:{placeholder: '请填写密码'} , rules: [{ required: true, message: '请填写密码' }, { validator: (rule: any, value: string, callback: Function) => { if (!value) { callback(); return; } // 检查密码长度不少于8位 if (value.length < 8) { callback(new Error('密码长度不能少于8位')); return; } // 检查是否包含字母 const hasLetter = /[a-zA-Z]/.test(value); // 检查是否包含数字 const hasNumber = /[0-9]/.test(value); // 检查是否包含特殊符号 const hasSpecial = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?`~]/.test(value); if (!hasLetter || !hasNumber || !hasSpecial) { callback(new Error('密码必须包含字母、数字和特殊符号')); return; } callback(); }, trigger: 'blur' } ],}, editForm:{ show:true, }, viewForm:{ show:false, } }, email:{ title: '邮箱', type: 'input', column: { show: false, minWidth: 120, }, form: { component: { placeholder: '请填写邮箱' }, rules: [{ required: false, message: '请填写邮箱' }], }, viewForm:{ component: { placeholder: '' }, } }, mobile:{ title: '手机号', type: 'input', column: { show: false, minWidth: 120, }, form: { component: { placeholder: '请填写手机号' }, rules: [{ required: true, message: '请填写手机号' }], }, viewForm:{ component: { placeholder: '' }, } }, gender:{ title: '性别', type: 'dict-select', column: { show: false, minWidth: 120, }, dict: dict({ data: [ { label: '男', value: 1 }, { label: '女', value: 2 }, ], }), form: { component: { placeholder: '请选择性别' }, rules: [{ required: true, message: '请选择性别' }], }, }, user_type:{ title: '用户类型', type: 'dict-select', column: { minWidth: 120, }, dict: dict({ data: [ { label: '学生', value: 0 }, /* { label: '教师', value: 1 }, { label: '校外团体', value: 2 }, { label: '学院领导', value: 3 }, */ ], }), form: { value:0, component: { placeholder: '请选择用户' }, rules: [{ required: false, message: '请选择用户' }], }, }, /* organization:{ title: '学院', type: 'input', column: { show: false, minWidth: 120, }, form: { show: false, component: { placeholder: '请填写学院' }, rules: [{ required: false, message: '请填写学院' }], }, viewForm:{ component: { placeholder: '' }, } }, */ organization_ref:{ title: '组织架构', type: "dict-cascader", column: { show: false, }, // 学生类型表单的级联选择依赖于 index.vue 中的 _college_id/_major_id/_grade_id // 这里通过 valueBuilder 在打开编辑/查看表单时,将 organization_detail.parent_chain 回显到这三个临时字段 // 同时通过 valueResolve 在提交时将最终选择写回 organization_ref(学生为年级ID,其它类型为学院ID) valueBuilder({ form, value }) { // 保留后端原始值 form.organization_ref = value; // 仅学生类型需要拆分层级进行回显 const od = (form as any).organization_detail; if ((form as any).user_type === 0 && od && Array.isArray(od.parent_chain)) { // 期望 parent_chain: [学校, 学院, 专业, 年级] 或至少包含 [学院, 专业, 年级] const chain = od.parent_chain; // 按长度精确映射,避免把学校ID当作学院ID if (chain.length >= 4) { // [学校, 学院, 专业, 年级] (form as any)._college_id = chain[1]?.id; (form as any)._major_id = chain[2]?.id; (form as any)._grade_id = od?.id; } else if (chain.length === 3) { // 常见后端:含学校,无年级 -> [学校, 学院, 专业] (form as any)._college_id = chain[1]?.id; (form as any)._major_id = chain[2]?.id; (form as any)._grade_id = od?.id; } else if (chain.length === 2) { // [学院, 专业] (form as any)._college_id = chain[0]?.id; (form as any)._major_id = chain[1]?.id; (form as any)._grade_id = od?.id; } else if (chain.length === 1) { // [学院] (form as any)._college_id = chain[0]?.id; (form as any)._major_id = undefined; (form as any)._grade_id = od?.id; } else { (form as any)._college_id = undefined; (form as any)._major_id = undefined; (form as any)._grade_id = od?.id; } // 学生模式下,不直接把 organization_ref 设为学院,保持由 index.vue 的联动逻辑在变更时再写入 } else if ((form as any).user_type === 1 || (form as any).user_type === 3) { // 教师/学院领导:直接以当前组织ID为学院选择 if (od && od.id) { (form as any).organization_ref = od.id; } } }, valueResolve({ form, value }) { // 非学生:提交学院ID if ((form as any).user_type !== 0) { (form as any).organization_ref = value ?? (form as any).organization_ref; return; } // 学生:当选择了年级,则以年级ID为最终 organization_ref if ((form as any)._grade_id) { (form as any).organization_ref = (form as any)._grade_id; return; } // 兜底:若仅选择到专业或学院,不改变已有值(让上层联动 onGradeChange 来写入) } }, organization:{ title: '学院', type: 'input', search:{ show:false }, column: { show: true, minWidth: 200, formatter: ({ row, value }: { row: any; value: any }) => { // 根据用户类型显示不同内容 if(row.user_type !== 0) { // 教师、学院领导:显示学院名称 if (row.organization_detail && row.organization_detail.name) { return row.organization_detail.name; } // 否则显示原始值 return value || ''; } else { // 学生:显示专业信息 if (row.organization_detail && row.organization_detail.parent_chain && row.organization_detail.parent_chain.length >= 2) { if(!row.organization_detail.parent_chain[1]) return const major = row.organization_detail.parent_chain[1]; // 第二项是专业 return major.name || major.code || ''; } // 否则显示原始值 return value || row.sub_organization; } }, /* formatter: ({ row, value }: { row: any; value: any }) => { // 如果有 organization_detail 对象,显示 full_path if (row.organization_detail && row.organization_detail.full_path) { return row.organization_detail.full_path; } // 否则显示原始值 return value || ''; }, */ }, form: { show: compute((row:any)=>{ // 学生、教师、学院领导:在表单中显示"学院" return row.user_type === 0 || row.user_type === 1 || row.user_type === 3 }), component: { placeholder: '请选择学院' }, rules: [{ required: false, message: '请选择学院' }], }, viewForm:{ component: { placeholder: '' }, } }, sub_organization:{ title: '专业', type: 'input', search:{ show:false }, column: { show: compute(({ row }) => { // 只有学生类型才显示专业列 return row && row.user_type === 0; }), minWidth: 120, formatter: ({ row, value }: { row: any; value: any }) => { // 如果有 organization_detail 对象,显示 parent_chain 的第二项(专业) if (row.organization_detail && row.organization_detail.parent_chain && row.organization_detail.parent_chain.length >= 2) { if(!row.organization_detail.parent_chain[2]) return const major = row.organization_detail.parent_chain[2]; // 第二项是专业 return major.name || major.code || ''; } // 否则显示原始值 return value || row.sub_organization; }, }, form: { show: compute(({ form }) => { // 只有当选择了学院时才显示专业选择 return form && form.user_type == 0; }), component: { placeholder: '请填写专业' }, rules: [{ required: false, message: '请填写专业' }], }, viewForm:{ component: { placeholder: '' }, } }, grade_or_level:{ title: '年级', type: 'input', search:{ show:false }, column: { show: compute(({ row }) => { // 只有学生类型才显示年级列 return row && row.user_type === 0; }), minWidth: 120, formatter: ({ row, value }: { row: any; value: any }) => { // 如果有 organization_detail 对象,显示 name if (row.organization_detail && row.organization_detail.name) { return row.organization_detail.name; } // 否则显示原始值 return value || row.class_or_group; }, }, form: { show: compute(({ form }) => { // 只有当选择了专业时才显示年级选择 return form && form.user_type == 0; }), component: { placeholder: '请填写年级' }, rules: [{ required: false, message: '请填写年级' }], }, viewForm:{ component: { placeholder: '' }, } }, class_or_group:{ title: '班级', type: 'input', search:{ show:false }, column: { show: compute(({ row }) => { // 只有学生类型才显示班级列 return row && row.user_type === 0; }), minWidth: 120, formatter: ({ row, value }: { row: any; value: any }) => { // 如果有 organization_detail 对象,显示 name if (row.organization_detail && row.organization_detail.name) { return row.organization_detail.name; } // 否则显示原始值 return value || row.class_or_group; }, }, form: { show:false, component: { placeholder: '请填写班级' }, rules: [{ required: false, message: '请填写班级' }], }, viewForm:{ component: { placeholder: '' }, } }, }, }, }; };