crud.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. import { AddReq, CreateCrudOptionsProps, CreateCrudOptionsRet, dict, UserPageQuery, compute, InfoReq, EditReq, DelReq } from '@fast-crud/fast-crud';
  2. import * as api from './api';
  3. import { auth } from '/@/utils/authFunction';
  4. export const createCrudOptions = function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
  5. const pageRequest = async (query: UserPageQuery) => {
  6. return await api.GetList(query);
  7. };
  8. const getDetail = async ({ row }: InfoReq) => {
  9. return await api.GetObj(row.id);
  10. };
  11. const editRequest = async ({ form, row }: EditReq) => {
  12. form.id = row.id;
  13. return await api.UpdateObj(form);
  14. };
  15. const delRequest = async ({ row }: DelReq) => {
  16. return await api.DelObj(row.id);
  17. };
  18. const addRequest = async ({ form }: AddReq) => {
  19. return await api.AddObj(form);
  20. };
  21. /**
  22. * 懒加载
  23. * @param row
  24. * @returns {Promise<unknown>}
  25. */
  26. // const loadContentMethod = (tree: any, treeNode: any, resolve: Function) => {
  27. // pageRequest({ pcode: tree.code }).then((res: APIResponseData) => {
  28. // resolve(res.data);
  29. // });
  30. // };
  31. return {
  32. crudOptions: {
  33. request: {
  34. pageRequest,
  35. editRequest,
  36. delRequest,
  37. addRequest,
  38. getDetail,
  39. },
  40. actionbar: {
  41. buttons: {
  42. add: {
  43. show: false,//auth('area:Create'),
  44. },
  45. },
  46. },
  47. form:{
  48. wrapper: {
  49. buttons: {
  50. ok:{
  51. text:'提交',
  52. show:compute((row) => {
  53. return row.mode !=='view'
  54. })
  55. }
  56. }
  57. }
  58. },
  59. pagination: {
  60. show: true,
  61. },
  62. columns: {
  63. _index: {
  64. title: '序号',
  65. form: { show: false },
  66. column: {
  67. type: 'index',
  68. align: 'center',
  69. width: '70px',
  70. columnSetDisabled: true, //禁止在列设置中选择
  71. },
  72. },
  73. search:{
  74. title: '关键字搜索',
  75. search: { show: true,type: 'input', },
  76. type: 'input',
  77. form: {
  78. component: { placeholder: '请输入' },
  79. show:false},
  80. column: {show:false}
  81. },
  82. user_code:{
  83. title:'学号',
  84. type:'input',
  85. column: {
  86. minWidth: 120,
  87. },
  88. form: {
  89. component: { placeholder: '请填写学号' },
  90. rules: [{ required: true, message: '请填写学号' }],
  91. },
  92. viewForm:{
  93. component: { placeholder: '' },
  94. }
  95. },
  96. username:{
  97. title:'用户名',
  98. type:'input',
  99. column: {
  100. show: false,
  101. minWidth: 120,
  102. },
  103. form: {
  104. component: { placeholder: '请填写用户名' },
  105. rules: [{ required: true, message: '请填写用户名' }],
  106. },
  107. viewForm:{
  108. component: { placeholder: '' },
  109. }
  110. },
  111. name: {
  112. title: '姓名',
  113. search: {
  114. show: false,
  115. },
  116. treeNode: true,
  117. type: 'input',
  118. column: {
  119. minWidth: 120,
  120. },
  121. form: {
  122. rules: [
  123. // 表单校验规则
  124. { required: true, message: '姓名必填项' },
  125. ],
  126. component: {
  127. placeholder: '请输入姓名',
  128. },
  129. },
  130. viewForm:{
  131. component: { placeholder: '' },
  132. }
  133. },
  134. password:{
  135. title: '密码',
  136. type: 'password',
  137. column: {
  138. minWidth: 120,
  139. show: false,
  140. },
  141. form: {
  142. component: { placeholder: '请填写密码' },
  143. /* rules: [
  144. { required: true, message: '请填写密码' },
  145. {
  146. validator: (rule: any, value: string, callback: Function) => {
  147. if (!value) {
  148. callback();
  149. return;
  150. }
  151. // 检查密码长度不少于8位
  152. if (value.length < 8) {
  153. callback(new Error('密码长度不能少于8位'));
  154. return;
  155. }
  156. // 检查是否包含字母
  157. const hasLetter = /[a-zA-Z]/.test(value);
  158. // 检查是否包含数字
  159. const hasNumber = /[0-9]/.test(value);
  160. // 检查是否包含特殊符号
  161. const hasSpecial = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?`~]/.test(value);
  162. if (!hasLetter || !hasNumber || !hasSpecial) {
  163. callback(new Error('密码必须包含字母、数字和特殊符号'));
  164. return;
  165. }
  166. callback();
  167. },
  168. trigger: 'blur'
  169. }
  170. ], */
  171. },
  172. addForm:{
  173. component:{placeholder: '请填写密码'} ,
  174. rules: [{ required: true, message: '请填写密码' },
  175. {
  176. validator: (rule: any, value: string, callback: Function) => {
  177. if (!value) {
  178. callback();
  179. return;
  180. }
  181. // 检查密码长度不少于8位
  182. if (value.length < 8) {
  183. callback(new Error('密码长度不能少于8位'));
  184. return;
  185. }
  186. // 检查是否包含字母
  187. const hasLetter = /[a-zA-Z]/.test(value);
  188. // 检查是否包含数字
  189. const hasNumber = /[0-9]/.test(value);
  190. // 检查是否包含特殊符号
  191. const hasSpecial = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?`~]/.test(value);
  192. if (!hasLetter || !hasNumber || !hasSpecial) {
  193. callback(new Error('密码必须包含字母、数字和特殊符号'));
  194. return;
  195. }
  196. callback();
  197. },
  198. trigger: 'blur'
  199. }
  200. ],},
  201. editForm:{
  202. show:true,
  203. },
  204. viewForm:{
  205. show:false,
  206. }
  207. },
  208. email:{
  209. title: '邮箱',
  210. type: 'input',
  211. column: {
  212. show: false,
  213. minWidth: 120,
  214. },
  215. form: {
  216. component: { placeholder: '请填写邮箱' },
  217. rules: [{ required: false, message: '请填写邮箱' }],
  218. },
  219. viewForm:{
  220. component: { placeholder: '' },
  221. }
  222. },
  223. mobile:{
  224. title: '手机号',
  225. type: 'input',
  226. column: {
  227. show: false,
  228. minWidth: 120,
  229. },
  230. form: {
  231. component: { placeholder: '请填写手机号' },
  232. rules: [{ required: true, message: '请填写手机号' }],
  233. },
  234. viewForm:{
  235. component: { placeholder: '' },
  236. }
  237. },
  238. gender:{
  239. title: '性别',
  240. type: 'dict-select',
  241. column: {
  242. show: false,
  243. minWidth: 120,
  244. },
  245. dict: dict({
  246. data: [
  247. { label: '男', value: 1 },
  248. { label: '女', value: 2 },
  249. ],
  250. }),
  251. form: {
  252. component: { placeholder: '请选择性别' },
  253. rules: [{ required: true, message: '请选择性别' }],
  254. },
  255. },
  256. user_type:{
  257. title: '用户类型',
  258. type: 'dict-select',
  259. column: {
  260. minWidth: 120,
  261. },
  262. dict: dict({
  263. data: [
  264. { label: '学生', value: 0 },
  265. /* { label: '教师', value: 1 },
  266. { label: '校外团体', value: 2 },
  267. { label: '学院领导', value: 3 }, */
  268. ],
  269. }),
  270. form: {
  271. value:0,
  272. component: { placeholder: '请选择用户' },
  273. rules: [{ required: false, message: '请选择用户' }],
  274. },
  275. },
  276. /* organization:{
  277. title: '学院',
  278. type: 'input',
  279. column: {
  280. show: false,
  281. minWidth: 120,
  282. },
  283. form: {
  284. show: false,
  285. component: { placeholder: '请填写学院' },
  286. rules: [{ required: false, message: '请填写学院' }],
  287. },
  288. viewForm:{
  289. component: { placeholder: '' },
  290. }
  291. }, */
  292. organization_ref:{
  293. title: '组织架构',
  294. type: "dict-cascader",
  295. column: {
  296. show: false,
  297. },
  298. // 学生类型表单的级联选择依赖于 index.vue 中的 _college_id/_major_id/_grade_id
  299. // 这里通过 valueBuilder 在打开编辑/查看表单时,将 organization_detail.parent_chain 回显到这三个临时字段
  300. // 同时通过 valueResolve 在提交时将最终选择写回 organization_ref(学生为年级ID,其它类型为学院ID)
  301. valueBuilder({ form, value }) {
  302. // 保留后端原始值
  303. form.organization_ref = value;
  304. // 仅学生类型需要拆分层级进行回显
  305. const od = (form as any).organization_detail;
  306. if ((form as any).user_type === 0 && od && Array.isArray(od.parent_chain)) {
  307. // 期望 parent_chain: [学校, 学院, 专业, 年级] 或至少包含 [学院, 专业, 年级]
  308. const chain = od.parent_chain;
  309. // 按长度精确映射,避免把学校ID当作学院ID
  310. if (chain.length >= 4) {
  311. // [学校, 学院, 专业, 年级]
  312. (form as any)._college_id = chain[1]?.id;
  313. (form as any)._major_id = chain[2]?.id;
  314. (form as any)._grade_id = od?.id;
  315. } else if (chain.length === 3) {
  316. // 常见后端:含学校,无年级 -> [学校, 学院, 专业]
  317. (form as any)._college_id = chain[1]?.id;
  318. (form as any)._major_id = chain[2]?.id;
  319. (form as any)._grade_id = od?.id;
  320. } else if (chain.length === 2) {
  321. // [学院, 专业]
  322. (form as any)._college_id = chain[0]?.id;
  323. (form as any)._major_id = chain[1]?.id;
  324. (form as any)._grade_id = od?.id;
  325. } else if (chain.length === 1) {
  326. // [学院]
  327. (form as any)._college_id = chain[0]?.id;
  328. (form as any)._major_id = undefined;
  329. (form as any)._grade_id = od?.id;
  330. } else {
  331. (form as any)._college_id = undefined;
  332. (form as any)._major_id = undefined;
  333. (form as any)._grade_id = od?.id;
  334. }
  335. // 学生模式下,不直接把 organization_ref 设为学院,保持由 index.vue 的联动逻辑在变更时再写入
  336. } else if ((form as any).user_type === 1 || (form as any).user_type === 3) {
  337. // 教师/学院领导:直接以当前组织ID为学院选择
  338. if (od && od.id) {
  339. (form as any).organization_ref = od.id;
  340. }
  341. }
  342. },
  343. valueResolve({ form, value }) {
  344. // 非学生:提交学院ID
  345. if ((form as any).user_type !== 0) {
  346. (form as any).organization_ref = value ?? (form as any).organization_ref;
  347. return;
  348. }
  349. // 学生:当选择了年级,则以年级ID为最终 organization_ref
  350. if ((form as any)._grade_id) {
  351. (form as any).organization_ref = (form as any)._grade_id;
  352. return;
  353. }
  354. // 兜底:若仅选择到专业或学院,不改变已有值(让上层联动 onGradeChange 来写入)
  355. }
  356. },
  357. organization:{
  358. title: '学院',
  359. type: 'input',
  360. search:{
  361. show:false
  362. },
  363. column: {
  364. show: true,
  365. minWidth: 200,
  366. formatter: ({ row, value }: { row: any; value: any }) => {
  367. // 根据用户类型显示不同内容
  368. if(row.user_type !== 0) {
  369. // 教师、学院领导:显示学院名称
  370. if (row.organization_detail && row.organization_detail.name) {
  371. return row.organization_detail.name;
  372. }
  373. // 否则显示原始值
  374. return value || '';
  375. } else {
  376. // 学生:显示专业信息
  377. if (row.organization_detail && row.organization_detail.parent_chain && row.organization_detail.parent_chain.length >= 2) {
  378. if(!row.organization_detail.parent_chain[1]) return
  379. const major = row.organization_detail.parent_chain[1]; // 第二项是专业
  380. return major.name || major.code || '';
  381. }
  382. // 否则显示原始值
  383. return value || row.sub_organization;
  384. }
  385. },
  386. /* formatter: ({ row, value }: { row: any; value: any }) => {
  387. // 如果有 organization_detail 对象,显示 full_path
  388. if (row.organization_detail && row.organization_detail.full_path) {
  389. return row.organization_detail.full_path;
  390. }
  391. // 否则显示原始值
  392. return value || '';
  393. }, */
  394. },
  395. form: {
  396. show: compute((row:any)=>{
  397. // 学生、教师、学院领导:在表单中显示"学院"
  398. return row.user_type === 0 || row.user_type === 1 || row.user_type === 3
  399. }),
  400. component: { placeholder: '请选择学院' },
  401. rules: [{ required: false, message: '请选择学院' }],
  402. },
  403. viewForm:{
  404. component: { placeholder: '' },
  405. }
  406. },
  407. sub_organization:{
  408. title: '专业',
  409. type: 'input',
  410. search:{
  411. show:false
  412. },
  413. column: {
  414. show: compute(({ row }) => {
  415. // 只有学生类型才显示专业列
  416. return row && row.user_type === 0;
  417. }),
  418. minWidth: 120,
  419. formatter: ({ row, value }: { row: any; value: any }) => {
  420. // 如果有 organization_detail 对象,显示 parent_chain 的第二项(专业)
  421. if (row.organization_detail && row.organization_detail.parent_chain && row.organization_detail.parent_chain.length >= 2) {
  422. if(!row.organization_detail.parent_chain[2]) return
  423. const major = row.organization_detail.parent_chain[2]; // 第二项是专业
  424. return major.name || major.code || '';
  425. }
  426. // 否则显示原始值
  427. return value || row.sub_organization;
  428. },
  429. },
  430. form: {
  431. show: compute(({ form }) => {
  432. // 只有当选择了学院时才显示专业选择
  433. return form && form.user_type == 0;
  434. }),
  435. component: { placeholder: '请填写专业' },
  436. rules: [{ required: false, message: '请填写专业' }],
  437. },
  438. viewForm:{
  439. component: { placeholder: '' },
  440. }
  441. },
  442. grade_or_level:{
  443. title: '年级',
  444. type: 'input',
  445. search:{
  446. show:false
  447. },
  448. column: {
  449. show: compute(({ row }) => {
  450. // 只有学生类型才显示年级列
  451. return row && row.user_type === 0;
  452. }),
  453. minWidth: 120,
  454. formatter: ({ row, value }: { row: any; value: any }) => {
  455. // 如果有 organization_detail 对象,显示 name
  456. if (row.organization_detail && row.organization_detail.name) {
  457. return row.organization_detail.name;
  458. }
  459. // 否则显示原始值
  460. return value || row.class_or_group;
  461. },
  462. },
  463. form: {
  464. show: compute(({ form }) => {
  465. // 只有当选择了专业时才显示年级选择
  466. return form && form.user_type == 0;
  467. }),
  468. component: { placeholder: '请填写年级' },
  469. rules: [{ required: false, message: '请填写年级' }],
  470. },
  471. viewForm:{
  472. component: { placeholder: '' },
  473. }
  474. },
  475. class_or_group:{
  476. title: '班级',
  477. type: 'input',
  478. search:{
  479. show:false
  480. },
  481. column: {
  482. show: compute(({ row }) => {
  483. // 只有学生类型才显示班级列
  484. return row && row.user_type === 0;
  485. }),
  486. minWidth: 120,
  487. formatter: ({ row, value }: { row: any; value: any }) => {
  488. // 如果有 organization_detail 对象,显示 name
  489. if (row.organization_detail && row.organization_detail.name) {
  490. return row.organization_detail.name;
  491. }
  492. // 否则显示原始值
  493. return value || row.class_or_group;
  494. },
  495. },
  496. form: {
  497. show:false,
  498. component: { placeholder: '请填写班级' },
  499. rules: [{ required: false, message: '请填写班级' }],
  500. },
  501. viewForm:{
  502. component: { placeholder: '' },
  503. }
  504. },
  505. },
  506. },
  507. };
  508. };