提交 | 用户 | 时间
|
b1d33f
|
1 |
<template> |
潘 |
2 |
<Dialog v-model="dialogVisible" :title="dialogTitle"> |
|
3 |
<el-form |
|
4 |
ref="formRef" |
|
5 |
v-loading="formLoading" |
|
6 |
:model="formData" |
|
7 |
:rules="formRules" |
|
8 |
label-width="100px" |
|
9 |
> |
|
10 |
<el-form-item label="上级菜单"> |
|
11 |
<el-tree-select |
|
12 |
v-model="formData.pid" |
|
13 |
:data="categoryTree" |
|
14 |
:default-expanded-keys="[0]" |
|
15 |
:props="categoryTreeProps" |
|
16 |
check-strictly |
|
17 |
node-key="id" |
|
18 |
/> |
|
19 |
</el-form-item> |
|
20 |
<el-form-item label="分类名称" prop="label"> |
|
21 |
<el-input v-model="formData.label" clearable placeholder="请输入分类名称" /> |
|
22 |
</el-form-item> |
|
23 |
<el-form-item label="显示排序" prop="sort"> |
|
24 |
<el-input-number v-model="formData.sort" :min="0" clearable controls-position="right" /> |
|
25 |
</el-form-item> |
|
26 |
</el-form> |
|
27 |
<template #footer> |
|
28 |
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
|
29 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
30 |
</template> |
|
31 |
</Dialog> |
|
32 |
</template> |
|
33 |
<script lang="ts" setup> |
|
34 |
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict' |
|
35 |
import * as CategoryApi from '@/api/data/plan/category' |
271781
|
36 |
import {CACHE_KEY, useCache, useSessionCache} from '@/hooks/web/useCache' |
b1d33f
|
37 |
import { CommonStatusEnum, SystemMenuTypeEnum } from '@/utils/constants' |
潘 |
38 |
import { defaultProps, handleTree } from '@/utils/tree' |
|
39 |
|
|
40 |
defineOptions({ name: 'PlanItemCategoryForm' }) |
|
41 |
|
271781
|
42 |
const { wsSessionCache } = useSessionCache() |
b1d33f
|
43 |
const { t } = useI18n() // 国际化 |
潘 |
44 |
const message = useMessage() // 消息弹窗 |
|
45 |
|
|
46 |
const categoryTreeProps = ref({ |
|
47 |
children: 'children', |
|
48 |
label: 'label', |
|
49 |
value: 'id', |
|
50 |
isLeaf: 'leaf', |
|
51 |
emitPath: false |
|
52 |
}) |
|
53 |
|
|
54 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
55 |
const dialogTitle = ref('') // 弹窗的标题 |
|
56 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
57 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
58 |
const formData = ref({ |
|
59 |
id: undefined, |
|
60 |
label: '', |
|
61 |
pid: '0', |
|
62 |
sort: Number(undefined) |
|
63 |
}) |
|
64 |
const formRules = reactive({ |
|
65 |
label: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }], |
|
66 |
sort: [{ required: true, message: '分类顺序不能为空', trigger: 'blur' }] |
|
67 |
}) |
|
68 |
const formRef = ref() // 表单 Ref |
|
69 |
|
|
70 |
/** 打开弹窗 */ |
|
71 |
const open = async (type: string, id?: number, parentId?: number) => { |
|
72 |
dialogVisible.value = true |
|
73 |
dialogTitle.value = t('action.' + type) |
|
74 |
formType.value = type |
|
75 |
resetForm() |
|
76 |
if (parentId) { |
|
77 |
formData.value.parentId = parentId |
|
78 |
} |
|
79 |
// 修改时,设置数据 |
|
80 |
if (id) { |
|
81 |
formLoading.value = true |
|
82 |
try { |
|
83 |
formData.value = await CategoryApi.getCategory(id) |
|
84 |
} finally { |
|
85 |
formLoading.value = false |
|
86 |
} |
|
87 |
} |
|
88 |
// 获得菜单列表 |
|
89 |
await getTree() |
|
90 |
} |
|
91 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
92 |
|
|
93 |
/** 提交表单 */ |
|
94 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
95 |
const submitForm = async () => { |
|
96 |
// 校验表单 |
|
97 |
if (!formRef) return |
|
98 |
const valid = await formRef.value.validate() |
|
99 |
if (!valid) return |
|
100 |
// 提交请求 |
|
101 |
formLoading.value = true |
|
102 |
try { |
|
103 |
const data = formData.value as unknown as CategoryApi.PlanItemCategoryVO |
|
104 |
if (formType.value === 'create') { |
|
105 |
await CategoryApi.createCategory(data) |
|
106 |
message.success(t('common.createSuccess')) |
|
107 |
} else { |
|
108 |
await CategoryApi.updateCategory(data) |
|
109 |
message.success(t('common.updateSuccess')) |
|
110 |
} |
|
111 |
dialogVisible.value = false |
|
112 |
// 发送操作成功的事件 |
|
113 |
emit('success') |
|
114 |
} finally { |
|
115 |
formLoading.value = false |
|
116 |
// 清空,从而触发刷新 |
271781
|
117 |
wsSessionCache.delete(CACHE_KEY.ROLE_ROUTERS) |
b1d33f
|
118 |
} |
潘 |
119 |
} |
|
120 |
|
|
121 |
/** 获取下拉框[上级菜单]的数据 */ |
|
122 |
const categoryTree = ref<Tree[]>([]) // 树形结构 |
|
123 |
const getTree = async () => { |
|
124 |
categoryTree.value = [] |
|
125 |
const res = await CategoryApi.getCategoryListAllSimple() |
|
126 |
let menu: Tree = { id: '0', label: '主类目', children: [] } |
|
127 |
menu.children = handleTree(res, 'id', 'pid') |
|
128 |
categoryTree.value.push(menu) |
|
129 |
} |
|
130 |
|
|
131 |
/** 重置表单 */ |
|
132 |
const resetForm = () => { |
|
133 |
formData.value = { |
|
134 |
id: undefined, |
|
135 |
label: '', |
|
136 |
pid: '0', |
|
137 |
sort: Number(undefined) |
|
138 |
} |
|
139 |
formRef.value?.resetFields() |
|
140 |
} |
|
141 |
</script> |