crud.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import type { CreateCrudOptionsRet, CreateCrudOptionsProps } from '@fast-crud/fast-crud';
  2. import type { FsUploaderFormRequestOptions } from '@fast-crud/fast-extends';
  3. import dayjs from 'dayjs';
  4. import { dict } from '@fast-crud/fast-crud';
  5. import axios from 'axios';
  6. import { usePermission } from '@/composables';
  7. import { getServiceEnvConfig } from '~/.env-config';
  8. import type { AddArchivesParams } from './api';
  9. import { getArchives, getFile, addArchives, deleteArchives, downloadArchives } from './api';
  10. const { url, proxyPattern } = getServiceEnvConfig(import.meta.env);
  11. const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y';
  12. const { hasPermission } = usePermission();
  13. export default function createCrudOptions(crudOptionsProps: CreateCrudOptionsProps): CreateCrudOptionsRet {
  14. return {
  15. crudOptions: {
  16. pagination: {
  17. show: false
  18. },
  19. request: {
  20. pageRequest: async ({ page, query }) => {
  21. query.studentNumber = crudOptionsProps.context?.studentNumber;
  22. const { data } = await getArchives(query);
  23. return { records: data, total: 0, currentPage: page.offset, pageSize: page.limit };
  24. },
  25. addRequest: ({ form }) => {
  26. if (!crudOptionsProps.context) {
  27. return Promise.resolve(true);
  28. }
  29. if (!crudOptionsProps.context.studentNumber) {
  30. crudOptionsProps.context.message.info('没有筛选人员的情况下 , 不允许上传档案!');
  31. throw new Error('没有筛选人员的情况下 , 不允许上传档案');
  32. }
  33. return addArchives({
  34. studentNumber: form.studentNumber,
  35. filePath: form.filePath,
  36. remarks: form.remarks,
  37. filetype: form.filetype
  38. });
  39. },
  40. editRequest: () => {
  41. return Promise.resolve(true);
  42. },
  43. delRequest: (ctx: { row: AddArchivesParams }) => {
  44. const { row } = ctx;
  45. if (!crudOptionsProps.context || !row.id) {
  46. return Promise.resolve(true);
  47. }
  48. if (row.filetype === 'attendance' || row.filetype === 'scores' || row.filetype === 'prefile') {
  49. crudOptionsProps.context.message.info('基础信息自动生成, 不能删除或者编辑!');
  50. return Promise.resolve(true);
  51. }
  52. return deleteArchives(row.id);
  53. }
  54. },
  55. rowHandle: {
  56. show: true,
  57. buttons: {
  58. remove: {
  59. show: true
  60. },
  61. edit: {
  62. show: false
  63. },
  64. view: {
  65. show: false
  66. },
  67. review: {
  68. text: null,
  69. size: 'small',
  70. icon: 'lucide:view',
  71. tooltip: {
  72. slots: {
  73. default() {
  74. return '查看文件';
  75. }
  76. }
  77. },
  78. click: async ({ row }) => {
  79. const { data } = await getFile(row.archiveNumber);
  80. crudOptionsProps.context?.viewActiveFunc(row.arctype, data);
  81. }
  82. }
  83. }
  84. },
  85. toolbar: {
  86. show: false
  87. },
  88. actionbar: {
  89. show: hasPermission('admin' as Auth.RoleType),
  90. buttons: {
  91. add: {
  92. text: '添加学员档案'
  93. },
  94. download: {
  95. text: '打包下载档案',
  96. title: '下载当前学员所有档案',
  97. circle: false,
  98. tooltip: {
  99. slots: {
  100. default() {
  101. return '下载当前学员所有档案';
  102. }
  103. }
  104. },
  105. click: async () => {
  106. if (!crudOptionsProps.context) {
  107. return;
  108. }
  109. if (!crudOptionsProps.context.studentNumber) {
  110. crudOptionsProps.context.message.info('没有筛选人员的情况下 , 不允许下载档案!');
  111. return;
  112. }
  113. const { data } = await downloadArchives(crudOptionsProps.context.studentNumber);
  114. if (data) {
  115. const fileUrl = `http://localhost:3200/proxy-pattern/archive/getFileByToken?archiveToken=${data}`;
  116. window.location.href = fileUrl;
  117. }
  118. }
  119. }
  120. }
  121. },
  122. form: {
  123. wrapper: {
  124. draggable: false,
  125. onOpen: () => {
  126. if (!crudOptionsProps.context) {
  127. return;
  128. }
  129. if (!crudOptionsProps.context.studentNumber) {
  130. crudOptionsProps.context.message.info('没有筛选人员的情况下 , 不允许上传档案!');
  131. throw new Error('没有筛选人员的情况下 , 不允许上传档案');
  132. }
  133. },
  134. onOpened: async context => {
  135. if (crudOptionsProps.context) {
  136. crudOptionsProps.context.archivesForm = context.form;
  137. }
  138. if (!context.form.studentNumber) {
  139. context.form.studentNumber = crudOptionsProps.context?.studentNumber;
  140. }
  141. }
  142. }
  143. },
  144. search: {
  145. show: false
  146. },
  147. columns: {
  148. studentNumber: {
  149. title: '学员编号',
  150. search: {
  151. show: true
  152. },
  153. type: 'text',
  154. column: {
  155. align: 'center'
  156. },
  157. form: {
  158. show: true
  159. }
  160. },
  161. arctype: {
  162. title: '档案类型',
  163. type: 'text',
  164. column: {
  165. align: 'center'
  166. },
  167. form: {
  168. show: false
  169. }
  170. },
  171. filetype: {
  172. title: '类别',
  173. type: 'dict-select',
  174. dict: dict({
  175. data: [
  176. { value: 'avatar', label: '头像' },
  177. { value: 'prefile', disabled: true, label: '个人资料' },
  178. { value: 'scores', disabled: true, label: '考核' },
  179. { value: 'attendance', disabled: true, label: '出勤' },
  180. { value: 'other', label: '其他' }
  181. ]
  182. })
  183. },
  184. filePath: {
  185. title: '档案文件',
  186. type: 'file-uploader',
  187. form: {
  188. component: {
  189. limit: 1,
  190. uploader: {
  191. uploadRequest: async (props: FsUploaderFormRequestOptions) => {
  192. if (!crudOptionsProps.context) {
  193. return false;
  194. }
  195. if (!crudOptionsProps.context.archivesForm.filetype) {
  196. crudOptionsProps.context.message.info('请先选择类别, 在上传档案!');
  197. throw new Error('请先选择类别, 在上传档案!');
  198. }
  199. const action = isHttpProxy
  200. ? `${proxyPattern}/student/upload?fileType`
  201. : `${url}/student/upload?fileType`;
  202. const { file, onProgress } = props;
  203. const data = new FormData();
  204. data.append('file', file);
  205. data.append('fileType', crudOptionsProps.context.archivesForm.filetype);
  206. data.append('studentNumber', crudOptionsProps.context.archivesForm.studentNumber);
  207. const res = await axios.post(action, data, {
  208. headers: {
  209. 'Content-Type': 'multipart/form-data'
  210. },
  211. timeout: 60000,
  212. onUploadProgress(progress) {
  213. onProgress({ percent: Math.round((progress.loaded / progress.total!) * 100) });
  214. }
  215. });
  216. // 上传完成后的结果,一般返回个url 或者key,具体看你的后台返回啥
  217. return res.data.data;
  218. }
  219. }
  220. }
  221. },
  222. column: {
  223. show: false
  224. }
  225. },
  226. remarks: {
  227. title: '备注',
  228. type: 'text',
  229. column: {
  230. align: 'center'
  231. }
  232. },
  233. validityTime: {
  234. title: '档案有效期',
  235. type: 'datetime',
  236. column: {
  237. align: 'center'
  238. },
  239. form: {
  240. show: false
  241. },
  242. search: { show: false },
  243. valueBuilder(context) {
  244. const { value, row, key } = context;
  245. if (value) {
  246. row[key] = dayjs(value).valueOf();
  247. }
  248. },
  249. valueResolve(context) {
  250. const { value, form, key } = context;
  251. if (value) {
  252. form[key] = dayjs(value).format('YYYY-MM-DD HH:mm:ss');
  253. }
  254. }
  255. },
  256. createTime: {
  257. key: 'createTime',
  258. title: '创建时间',
  259. type: 'datetime',
  260. column: {
  261. align: 'center'
  262. },
  263. form: {
  264. show: false
  265. },
  266. search: { show: false },
  267. valueBuilder(context) {
  268. const { value, row, key } = context;
  269. if (value) {
  270. row[key] = dayjs(value).valueOf();
  271. }
  272. },
  273. valueResolve(context) {
  274. const { value, form, key } = context;
  275. if (value) {
  276. form[key] = dayjs(value).format('YYYY-MM-DD HH:mm:ss');
  277. }
  278. }
  279. },
  280. modifyTime: {
  281. title: '修改时间',
  282. key: 'modifyTime',
  283. type: 'datetime',
  284. align: 'center',
  285. column: {
  286. align: 'center'
  287. },
  288. form: {
  289. show: false
  290. },
  291. valueBuilder(context) {
  292. const { value, row, key } = context;
  293. if (value) {
  294. row[key] = dayjs(value).valueOf();
  295. }
  296. },
  297. valueResolve(context) {
  298. const { value, form, key } = context;
  299. if (value) {
  300. form[key] = dayjs(value).format('YYYY-MM-DD HH:mm:ss');
  301. }
  302. }
  303. }
  304. }
  305. }
  306. };
  307. }