|
@@ -1,22 +1,264 @@
|
|
|
-import { queryProfileTemplateList } from '@/services/api/business'
|
|
|
+import {
|
|
|
+ createTemplateFile,
|
|
|
+ createTemplateFolder,
|
|
|
+ delProfileTemplate,
|
|
|
+ moveTemplate,
|
|
|
+ moveTemplateWithOperation,
|
|
|
+ queryProfileTemplateList,
|
|
|
+ updateProfileTemplate
|
|
|
+} from '@/services/api/business'
|
|
|
import { useRequest } from '@umijs/max'
|
|
|
import { useState } from 'react'
|
|
|
+import ProForm, { ProFormCheckbox, ProFormRadio, ProFormText, ProFormTreeSelect } from '@ant-design/pro-form'
|
|
|
|
|
|
+import { TemplateMode } from '../'
|
|
|
+import type { ModalAction } from '@/components/Modal'
|
|
|
+import { message, Modal, TreeNodeProps } from 'antd'
|
|
|
+import consts from '@/utils/consts'
|
|
|
interface IState {
|
|
|
list?: API.ProfileTemplateItem[]
|
|
|
+ record?: API.ProfileTemplateItem
|
|
|
}
|
|
|
|
|
|
-export function useRowScript() {
|
|
|
- const [state, setState] = useState<IState>({})
|
|
|
+/** 格式化树表数据 */
|
|
|
+function formatTreeTable(nodes: API.ProfileTemplateItem[]) {
|
|
|
+ return nodes.map((node, idx) => {
|
|
|
+ node.moveable = true
|
|
|
+ if (nodes.length === 1 && idx + 1 === 1) {
|
|
|
+ // 叶子结点只有一个的时候不可移动(包括上下移)
|
|
|
+ node.moveable = false
|
|
|
+ }
|
|
|
+ if (node.children?.length) {
|
|
|
+ node.children = formatTreeTable(node.children)
|
|
|
+ }
|
|
|
+ return node
|
|
|
+ })
|
|
|
+}
|
|
|
|
|
|
- const { refresh } = useRequest(queryProfileTemplateList, {
|
|
|
- onSuccess: (result: API.ProfileTemplateItem[]) => {
|
|
|
- setState({ ...state, list: result?.children || [] })
|
|
|
+/**
|
|
|
+ * 格式化新增目录/移动操作所需的TreeNode
|
|
|
+ * @param currentID 不传则为新增目录
|
|
|
+ * */
|
|
|
+function formatTreeNode(nodes: API.ProfileTemplateItem[], currentID?: string): TreeNodeProps[] {
|
|
|
+ return nodes.map(item => {
|
|
|
+ const newItem: TreeNodeProps = {
|
|
|
+ title: item.name,
|
|
|
+ value: item.ID,
|
|
|
+ disable: false
|
|
|
+ }
|
|
|
+ // 移动操作判断逻辑
|
|
|
+ if (currentID) {
|
|
|
+ if (item.ID === currentID || !item.moveable) {
|
|
|
+ newItem.disable = true
|
|
|
+ }
|
|
|
+ if (item.children?.length) {
|
|
|
+ newItem.children = formatTreeNode(item.children)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (!item.folder) {
|
|
|
+ newItem.disabled = true
|
|
|
+ }
|
|
|
+ if (item.children?.length) {
|
|
|
+ newItem.children = formatTreeNode(item.children)
|
|
|
+ }
|
|
|
}
|
|
|
+ return newItem
|
|
|
})
|
|
|
+}
|
|
|
+
|
|
|
+export function useRowScript(modal: ModalAction) {
|
|
|
+ const [state, setState] = useState<IState>({})
|
|
|
+ const { refresh, loading } = useRequest(queryProfileTemplateList, {
|
|
|
+ onSuccess: (result?: API.ProfileTemplateItem[]) => {
|
|
|
+ setState({ ...state, list: formatTreeTable(result) || [] })
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ const handleRowClick = (record: API.ProfileTemplateItem) => setState({ ...state, record })
|
|
|
+
|
|
|
+ /** 新建目录 */
|
|
|
+ const addFolder = () => {
|
|
|
+ modal.open({
|
|
|
+ title: '新建目录',
|
|
|
+ okText: '确认',
|
|
|
+ type: 'form',
|
|
|
+ cancelText: '取消',
|
|
|
+ initialValues: { parentID: state.record?.parentID },
|
|
|
+ children: (
|
|
|
+ <ProForm submitter={false} layout="horizontal" labelCol={{ span: 4 }} isKeyPressSubmit>
|
|
|
+ <ProFormTreeSelect
|
|
|
+ name="parentID"
|
|
|
+ label="父节点"
|
|
|
+ fieldProps={{ options: formatTreeNode(state.list) }}
|
|
|
+ rules={[{ required: true, message: '请选择父节点' }]}
|
|
|
+ />
|
|
|
+ <ProFormText name="name" label="目录名称" rules={[{ required: true, message: '请输入目录名称' }]} />
|
|
|
+ </ProForm>
|
|
|
+ ),
|
|
|
+ onOk: async (values: { name: string; parentID: string }) => {
|
|
|
+ const { code = -1 } = await createTemplateFolder(values)
|
|
|
+ if (code === consts.RET_CODE.SUCCESS) {
|
|
|
+ message.success('新建目录成功')
|
|
|
+ modal.close()
|
|
|
+ refresh()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 编辑目录 */
|
|
|
+ const editFolder = () => {
|
|
|
+ modal.open({
|
|
|
+ title: '编辑目录',
|
|
|
+ okText: '确认',
|
|
|
+ type: 'form',
|
|
|
+ cancelText: '取消',
|
|
|
+ initialValues: { ...state.record },
|
|
|
+ children: (
|
|
|
+ <ProForm submitter={false} layout="horizontal" isKeyPressSubmit>
|
|
|
+ <ProFormText name="name" label="目录名称" rules={[{ required: true, message: '请输入目录名称' }]} />
|
|
|
+ <ProFormText hidden name="ID" />
|
|
|
+ </ProForm>
|
|
|
+ ),
|
|
|
+ onOk: async (values: { ID: string; name: string }) => {
|
|
|
+ const { code = -1 } = await updateProfileTemplate(values)
|
|
|
+ if (code === consts.RET_CODE.SUCCESS) {
|
|
|
+ message.success('编辑成功')
|
|
|
+ modal.close()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 新建文件 */
|
|
|
+ const addFile = () => {
|
|
|
+ modal.open({
|
|
|
+ title: '新增文件',
|
|
|
+ okText: '确认',
|
|
|
+ type: 'form',
|
|
|
+ cancelText: '取消',
|
|
|
+ initialValues: { parentID: state.record?.ID, enable: 0, required: 1 },
|
|
|
+ children: (
|
|
|
+ <ProForm submitter={false} layout="horizontal" labelCol={{ span: 4 }} isKeyPressSubmit>
|
|
|
+ <ProFormTreeSelect
|
|
|
+ name="parentID"
|
|
|
+ label="父节点"
|
|
|
+ fieldProps={{ options: formatTreeNode(state.list) }}
|
|
|
+ rules={[{ required: true, message: '请选择目录节点' }]}
|
|
|
+ />
|
|
|
+ <ProFormText name="name" label="名称" rules={[{ required: true, message: '请输入目录名称' }]} />
|
|
|
+ <ProFormRadio.Group
|
|
|
+ options={[
|
|
|
+ { label: '启用', value: 1 },
|
|
|
+ { label: '停用', value: 0 }
|
|
|
+ ]}
|
|
|
+ name="enable"
|
|
|
+ label="状态"
|
|
|
+ rules={[{ required: true, message: '请选择' }]}
|
|
|
+ />
|
|
|
+ <ProFormRadio.Group
|
|
|
+ options={[
|
|
|
+ { label: '是', value: 1 },
|
|
|
+ { label: '否', value: 0 }
|
|
|
+ ]}
|
|
|
+ name="required"
|
|
|
+ label="是否必填"
|
|
|
+ rules={[{ required: true, message: '请选择' }]}
|
|
|
+ />
|
|
|
+ <ProFormCheckbox.Group
|
|
|
+ options={[
|
|
|
+ { label: '纸质', value: TemplateMode.PAPER },
|
|
|
+ { label: '上传', value: TemplateMode.UPLOAD }
|
|
|
+ ]}
|
|
|
+ name="mode"
|
|
|
+ label="资料提供"
|
|
|
+ rules={[{ required: true, message: '请选择' }]}
|
|
|
+ />
|
|
|
+ </ProForm>
|
|
|
+ ),
|
|
|
+ onOk: async (values: any) => {
|
|
|
+ const { code = -1 } = await createTemplateFile({
|
|
|
+ ...values,
|
|
|
+ required: !!values.required,
|
|
|
+ enable: !!values.enable
|
|
|
+ })
|
|
|
+ if (code === consts.RET_CODE.SUCCESS) {
|
|
|
+ message.success('新增文件成功')
|
|
|
+ modal.close()
|
|
|
+ refresh()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 删除 */
|
|
|
+ const deleteFolderOrFile = () => {
|
|
|
+ Modal.confirm({
|
|
|
+ title: '删除',
|
|
|
+ content: '确认删除该行数据?',
|
|
|
+ okText: '确定',
|
|
|
+ cancelText: '取消',
|
|
|
+ onOk: async () => {
|
|
|
+ // 进行删除的接口请求
|
|
|
+ const { code = -1 } = await delProfileTemplate({ ID: state.record?.ID })
|
|
|
+ if (code === consts.RET_CODE.SUCCESS) {
|
|
|
+ message.success('删除成功')
|
|
|
+ refresh()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 上下移 */
|
|
|
+ const moveWithOperation = async (operation: 'up' | 'down') => {
|
|
|
+ const { code = -1 } = await moveTemplateWithOperation({ ID: state.record?.ID, operation })
|
|
|
+ if (code === consts.RET_CODE.SUCCESS) {
|
|
|
+ message.success('移动成功')
|
|
|
+ refresh()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 移动 */
|
|
|
+ const move = async () => {
|
|
|
+ modal.open({
|
|
|
+ title: '移动至',
|
|
|
+ type: 'form',
|
|
|
+ initialValues: { ID: state.record?.ID },
|
|
|
+ children: (
|
|
|
+ <ProForm submitter={false} layout="horizontal" isKeyPressSubmit>
|
|
|
+ <ProFormText name="ID" hidden />
|
|
|
+ <ProFormTreeSelect
|
|
|
+ name="moveID"
|
|
|
+ label="目录名称"
|
|
|
+ fieldProps={{ options: formatTreeNode(state.list, state.record?.ID) }}
|
|
|
+ rules={[{ required: true, message: '请选择目标节点' }]}
|
|
|
+ />
|
|
|
+ </ProForm>
|
|
|
+ ),
|
|
|
+ okText: '确定',
|
|
|
+ cancelText: '取消',
|
|
|
+ onOk: async (values: { ID: string; moveID: string }) => {
|
|
|
+ const { code = -1 } = await moveTemplate(values)
|
|
|
+ if (code === consts.RET_CODE.SUCCESS) {
|
|
|
+ message.success('移动成功')
|
|
|
+ modal.close()
|
|
|
+ refresh()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
return {
|
|
|
- list: state.list,
|
|
|
- refresh
|
|
|
+ loading,
|
|
|
+ list: state.list?.[0]?.children || [],
|
|
|
+ record: state.record,
|
|
|
+ refresh,
|
|
|
+ rowClick: handleRowClick,
|
|
|
+ addFolder,
|
|
|
+ editFolder,
|
|
|
+ addFile,
|
|
|
+ move,
|
|
|
+ deleteFolderOrFile,
|
|
|
+ moveWithOperation
|
|
|
}
|
|
|
}
|