123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- 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');
- }
- }
- }
- }
- }
- };
- }
|