import type { CreateCrudOptionsRet, CreateCrudOptionsProps } from '@fast-crud/fast-crud'; import type { FsUploaderFormRequestOptions } from '@fast-crud/fast-extends'; import dayjs from 'dayjs'; import { dict } from '@fast-crud/fast-crud'; import axios from 'axios'; import { usePermission } from '@/composables'; import { getServiceEnvConfig } from '~/.env-config'; import type { AddArchivesParams } from './api'; import { getArchives, getFile, addArchives, deleteArchives, downloadArchives } from './api'; const { url, proxyPattern } = getServiceEnvConfig(import.meta.env); const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y'; const { hasPermission } = usePermission(); export default function createCrudOptions(crudOptionsProps: CreateCrudOptionsProps): CreateCrudOptionsRet { return { crudOptions: { pagination: { show: false }, request: { pageRequest: async ({ page, query }) => { query.studentNumber = crudOptionsProps.context?.studentNumber; const { data } = await getArchives(query); return { records: data, total: 0, currentPage: page.offset, pageSize: page.limit }; }, addRequest: ({ form }) => { if (!crudOptionsProps.context) { return Promise.resolve(true); } if (!crudOptionsProps.context.studentNumber) { crudOptionsProps.context.message.info('没有筛选人员的情况下 , 不允许上传档案!'); throw new Error('没有筛选人员的情况下 , 不允许上传档案'); } return addArchives({ studentNumber: form.studentNumber, filePath: form.filePath, remarks: form.remarks, filetype: form.filetype }); }, editRequest: () => { return Promise.resolve(true); }, delRequest: (ctx: { row: AddArchivesParams }) => { const { row } = ctx; if (!crudOptionsProps.context || !row.id) { return Promise.resolve(true); } if (row.filetype === 'attendance' || row.filetype === 'scores' || row.filetype === 'prefile') { crudOptionsProps.context.message.info('基础信息自动生成, 不能删除或者编辑!'); return Promise.resolve(true); } return deleteArchives(row.id); } }, rowHandle: { show: true, buttons: { remove: { show: true }, edit: { show: false }, view: { show: false }, review: { text: null, size: 'small', icon: 'lucide:view', tooltip: { slots: { default() { return '查看文件'; } } }, click: async ({ row }) => { const { data } = await getFile(row.archiveNumber); crudOptionsProps.context?.viewActiveFunc(row.arctype, data); } } } }, toolbar: { show: false }, actionbar: { show: hasPermission('admin' as Auth.RoleType), buttons: { add: { text: '添加学员档案' }, download: { text: '打包下载档案', title: '下载当前学员所有档案', circle: false, tooltip: { slots: { default() { return '下载当前学员所有档案'; } } }, click: async () => { if (!crudOptionsProps.context) { return; } if (!crudOptionsProps.context.studentNumber) { crudOptionsProps.context.message.info('没有筛选人员的情况下 , 不允许下载档案!'); return; } const { data } = await downloadArchives(crudOptionsProps.context.studentNumber); if (data) { const fileUrl = `http://localhost:3200/proxy-pattern/archive/getFileByToken?archiveToken=${data}`; window.location.href = fileUrl; } } } } }, form: { wrapper: { draggable: false, onOpen: () => { if (!crudOptionsProps.context) { return; } if (!crudOptionsProps.context.studentNumber) { crudOptionsProps.context.message.info('没有筛选人员的情况下 , 不允许上传档案!'); throw new Error('没有筛选人员的情况下 , 不允许上传档案'); } }, onOpened: async context => { if (crudOptionsProps.context) { crudOptionsProps.context.archivesForm = context.form; } if (!context.form.studentNumber) { context.form.studentNumber = crudOptionsProps.context?.studentNumber; } } } }, search: { show: false }, columns: { studentNumber: { title: '学员编号', search: { show: true }, type: 'text', column: { align: 'center' }, form: { show: true } }, arctype: { title: '档案类型', type: 'text', column: { align: 'center' }, form: { show: false } }, filetype: { title: '类别', type: 'dict-select', dict: dict({ data: [ { value: 'avatar', label: '头像' }, { value: 'prefile', disabled: true, label: '个人资料' }, { value: 'scores', disabled: true, label: '考核' }, { value: 'attendance', disabled: true, label: '出勤' }, { value: 'other', label: '其他' } ] }) }, filePath: { title: '档案文件', type: 'file-uploader', form: { component: { limit: 1, uploader: { uploadRequest: async (props: FsUploaderFormRequestOptions) => { if (!crudOptionsProps.context) { return false; } if (!crudOptionsProps.context.archivesForm.filetype) { crudOptionsProps.context.message.info('请先选择类别, 在上传档案!'); throw new Error('请先选择类别, 在上传档案!'); } const action = isHttpProxy ? `${proxyPattern}/student/upload?fileType` : `${url}/student/upload?fileType`; const { file, onProgress } = props; const data = new FormData(); data.append('file', file); data.append('fileType', crudOptionsProps.context.archivesForm.filetype); data.append('studentNumber', crudOptionsProps.context.archivesForm.studentNumber); const res = await axios.post(action, data, { headers: { 'Content-Type': 'multipart/form-data' }, timeout: 60000, onUploadProgress(progress) { onProgress({ percent: Math.round((progress.loaded / progress.total!) * 100) }); } }); // 上传完成后的结果,一般返回个url 或者key,具体看你的后台返回啥 return res.data.data; } } } }, column: { show: false } }, remarks: { title: '备注', type: 'text', column: { align: 'center' } }, validityTime: { title: '档案有效期', type: 'datetime', column: { align: 'center' }, form: { show: false }, search: { show: false }, valueBuilder(context) { const { value, row, key } = context; if (value) { row[key] = dayjs(value).valueOf(); } }, valueResolve(context) { const { value, form, key } = context; if (value) { form[key] = dayjs(value).format('YYYY-MM-DD HH:mm:ss'); } } }, createTime: { key: 'createTime', title: '创建时间', type: 'datetime', column: { align: 'center' }, form: { show: false }, search: { show: false }, valueBuilder(context) { const { value, row, key } = context; if (value) { row[key] = dayjs(value).valueOf(); } }, valueResolve(context) { const { value, form, key } = context; if (value) { form[key] = dayjs(value).format('YYYY-MM-DD HH:mm:ss'); } } }, modifyTime: { title: '修改时间', key: 'modifyTime', type: 'datetime', align: 'center', column: { align: 'center' }, form: { show: false }, valueBuilder(context) { const { value, row, key } = context; if (value) { row[key] = dayjs(value).valueOf(); } }, valueResolve(context) { const { value, form, key } = context; if (value) { form[key] = dayjs(value).format('YYYY-MM-DD HH:mm:ss'); } } } } } }; }