import { BizContainer, BizValueType, MaterialCategoryList, ModeType, ProFormBizTreeSelect, ProFormBizTreeSelectHandles, ProFormUploadOss, Remark, } from '@/components'; import { business } from '@/services'; import { useIntl } from '@@/exports'; import { ProColumns, ProFormDependency, ProFormRadio, } from '@ant-design/pro-components'; import { useModel } from '@umijs/max'; import { TreeSelect } from 'antd'; import moment from 'moment'; import React, { useRef, useState } from 'react'; import { v4 as uuidv4 } from 'uuid'; export interface IMaterialListProps { ghost?: boolean; search?: boolean; mode?: ModeType; trigger?: () => React.ReactNode; onValueChange?: () => void; } export default function MaterialList(props: IMaterialListProps) { const { mode = 'page', ghost = false, search = true, onValueChange, trigger, } = props; const intl = useIntl(); const intlPrefix = 'material'; const actionBizRef = useRef(null); const { getOssToken } = useModel('useOssToken', (model) => ({ getOssToken: model.getOssToken, })); const [type, setType] = useState(); const formContext = [ {({ type: formType }, form) => { if (formType !== type) { setType(formType); } return ( <> { form.setFieldValue('categoryId', undefined); form.setFieldValue('path', undefined); }, }} options={[ { label: intl.formatMessage({ id: intlPrefix + '.form.type.enum.image', }), value: 'FILE_IMAGE', }, { label: intl.formatMessage({ id: intlPrefix + '.form.type.enum.video', }), value: 'FILE_VIDEO', }, ]} rules={[ { required: true, message: intl.formatMessage({ id: intlPrefix + '.form.type.required', }), }, ]} /> ); }} , { const { data } = await business.materialCategory.treeMaterialCategory({ categoryTreeQry: { ...params, }, }); return data || []; }} rules={[ { required: true, message: intl.formatMessage({ id: intlPrefix + '.form.categoryId.required', }), }, ]} fieldProps={{ suffixIcon: null, showSearch: true, allowClear: true, autoClearSearchValue: true, filterTreeNode: true, multiple: false, treeNodeFilterProp: 'name', showCheckedStrategy: TreeSelect.SHOW_ALL, fieldNames: { label: 'name', children: 'children', value: 'categoryId', }, }} addonAfter={ actionBizRef.current?.reload()} /> } />, {({ type }) => { switch (type) { case 'FILE_IMAGE': case 'FILE_VIDEO': return ( ); } }} , ]; const columns: ProColumns[] = [ { title: intl.formatMessage({ id: intlPrefix + '.column.name' }), dataIndex: 'name', order: 3, }, { title: intl.formatMessage({ id: intlPrefix + '.column.url' }), dataIndex: 'url', search: false, render: (_, record) => { return record.type === 'FILE_IMAGE' || record.type === 'FILE_VIDEO' ? ( // 可预览 {record.name} ) : ( ); }, }, { title: intl.formatMessage({ id: intlPrefix + '.column.type' }), dataIndex: 'type', valueType: 'select', valueEnum: { FILE_IMAGE: { text: intl.formatMessage({ id: intlPrefix + '.column.type.enum.image', }), }, FILE_VIDEO: { text: intl.formatMessage({ id: intlPrefix + '.column.type.enum.video', }), }, }, fieldProps: { showSearch: true, allowClear: true, autoClearSearchValue: true, }, order: 2, }, { title: intl.formatMessage({ id: intlPrefix + '.column.category' }), dataIndex: 'categoryId', valueType: 'treeSelect', request: async (params) => { const { data } = await business.materialCategory.treeMaterialCategory({ categoryTreeQry: { ...params, }, }); return data || []; }, dependencies: ['type'], fieldProps: { fieldNames: { label: 'name', children: 'children', value: 'categoryId', }, showSearch: true, allowClear: true, autoClearSearchValue: true, filterTreeNode: true, multiple: false, treeNodeFilterProp: 'name', }, order: 2, }, ]; return ( rowKey={'materialId'} permission={'operation-community'} func={business.material} method={'material'} methodUpper={'Material'} intlPrefix={intlPrefix} onValueChange={onValueChange} modeType={mode} container={{ ghost }} page={{ fieldProps: { ghost, //@ts-ignore search, }, columns, }} create={{ formType: 'drawer', formContext, trigger, request: async ( formData: BusinessAPI.MaterialCreateCmd & { file: any; }, ) => { const OSS = require('ali-oss'); const ossToken = await getOssToken(); const client = new OSS({ // yourRegion填写Bucket所在地域。以华东1(杭州)为例,yourRegion填写为oss-cn-hangzhou。 region: 'oss-' + ossToken?.region, // 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。 accessKeyId: ossToken?.accessKeyId, accessKeySecret: ossToken?.accessKeySecret, // 从STS服务获取的安全令牌(SecurityToken)。 stsToken: ossToken?.securityToken, // 填写Bucket名称。 bucket: ossToken?.bucket, }); const path = moment(new Date()).format('YYMM/DD'); const materialCreateCmdList = new Array(); for (const file of formData.file) { const filename = uuidv4(); // @ts-ignore const fileExtension = ((file.originFileObj?.name || '').match( /\.([0-9a-z]+)(?:[\\?#]|$)/i, ) || ['.png'])[0]; // 目录存放 `/uploads/operations/${path}/${filename}${fileExtension}` const result = await client.put( `/uploads/operations/${path}/${filename}${fileExtension}`, file.originFileObj, ); materialCreateCmdList.push({ name: file.name, categoryId: formData.categoryId, path: result.name, type: formData.type, }); } const { success } = await business.material.batchCreateMaterial({ materialCreateCmdList, }); return success; }, }} destroy={{}} update={{ formType: 'drawer', formContext, }} /> ); }