提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
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.parentId" |
|
13 |
:data="menuTree" |
|
14 |
:default-expanded-keys="[0]" |
|
15 |
:props="defaultProps" |
|
16 |
check-strictly |
|
17 |
node-key="id" |
|
18 |
/> |
|
19 |
</el-form-item> |
|
20 |
<el-form-item label="菜单名称" prop="name"> |
|
21 |
<el-input v-model="formData.name" clearable placeholder="请输入菜单名称" /> |
|
22 |
</el-form-item> |
|
23 |
<el-form-item label="菜单类型" prop="type"> |
|
24 |
<el-radio-group v-model="formData.type"> |
|
25 |
<el-radio-button |
|
26 |
v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_MENU_TYPE)" |
|
27 |
:key="dict.label" |
|
28 |
:label="dict.value" |
|
29 |
> |
|
30 |
{{ dict.label }} |
|
31 |
</el-radio-button> |
|
32 |
</el-radio-group> |
|
33 |
</el-form-item> |
|
34 |
<el-form-item v-if="formData.type !== 3" label="菜单图标"> |
|
35 |
<IconSelect v-model="formData.icon" clearable /> |
|
36 |
</el-form-item> |
|
37 |
<el-form-item v-if="formData.type !== 3" label="路由地址" prop="path"> |
|
38 |
<template #label> |
|
39 |
<Tooltip |
|
40 |
message="访问的路由地址,如:`user`。如需外网地址时,则以 `http(s)://` 开头" |
|
41 |
title="路由地址" |
|
42 |
/> |
|
43 |
</template> |
|
44 |
<el-input v-model="formData.path" clearable placeholder="请输入路由地址" /> |
|
45 |
</el-form-item> |
|
46 |
<el-form-item v-if="formData.type === 2" label="组件地址" prop="component"> |
|
47 |
<el-input v-model="formData.component" clearable placeholder="例如说:system/user/index" /> |
|
48 |
</el-form-item> |
|
49 |
<el-form-item v-if="formData.type === 2" label="组件名字" prop="componentName"> |
|
50 |
<el-input v-model="formData.componentName" clearable placeholder="例如说:SystemUser" /> |
|
51 |
</el-form-item> |
|
52 |
<el-form-item v-if="formData.type !== 1" label="权限标识" prop="permission"> |
|
53 |
<template #label> |
|
54 |
<Tooltip |
|
55 |
message="Controller 方法上的权限字符,如:@PreAuthorize(`@ss.hasPermission('system:user:list')`)" |
|
56 |
title="权限标识" |
|
57 |
/> |
|
58 |
</template> |
|
59 |
<el-input v-model="formData.permission" clearable placeholder="请输入权限标识" /> |
|
60 |
</el-form-item> |
|
61 |
<el-form-item label="显示排序" prop="sort"> |
|
62 |
<el-input-number v-model="formData.sort" :min="0" clearable controls-position="right" /> |
|
63 |
</el-form-item> |
|
64 |
<el-form-item label="菜单状态" prop="status"> |
|
65 |
<el-radio-group v-model="formData.status"> |
|
66 |
<el-radio |
|
67 |
v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" |
|
68 |
:key="dict.label" |
|
69 |
:label="dict.value" |
|
70 |
> |
|
71 |
{{ dict.label }} |
|
72 |
</el-radio> |
|
73 |
</el-radio-group> |
|
74 |
</el-form-item> |
|
75 |
<el-form-item v-if="formData.type !== 3" label="显示状态" prop="visible"> |
|
76 |
<template #label> |
|
77 |
<Tooltip message="选择隐藏时,路由将不会出现在侧边栏,但仍然可以访问" title="显示状态" /> |
|
78 |
</template> |
|
79 |
<el-radio-group v-model="formData.visible"> |
|
80 |
<el-radio key="true" :label="true" border>显示</el-radio> |
|
81 |
<el-radio key="false" :label="false" border>隐藏</el-radio> |
|
82 |
</el-radio-group> |
|
83 |
</el-form-item> |
|
84 |
<el-form-item v-if="formData.type !== 3" label="总是显示" prop="alwaysShow"> |
|
85 |
<template #label> |
|
86 |
<Tooltip |
|
87 |
message="选择不是时,当该菜单只有一个子菜单时,不展示自己,直接展示子菜单" |
|
88 |
title="总是显示" |
|
89 |
/> |
|
90 |
</template> |
|
91 |
<el-radio-group v-model="formData.alwaysShow"> |
|
92 |
<el-radio key="true" :label="true" border>总是</el-radio> |
|
93 |
<el-radio key="false" :label="false" border>不是</el-radio> |
|
94 |
</el-radio-group> |
|
95 |
</el-form-item> |
|
96 |
<el-form-item v-if="formData.type === 2" label="缓存状态" prop="keepAlive"> |
|
97 |
<template #label> |
|
98 |
<Tooltip |
|
99 |
message="选择缓存时,则会被 `keep-alive` 缓存,必须填写「组件名称」字段" |
|
100 |
title="缓存状态" |
|
101 |
/> |
|
102 |
</template> |
|
103 |
<el-radio-group v-model="formData.keepAlive"> |
|
104 |
<el-radio key="true" :label="true" border>缓存</el-radio> |
|
105 |
<el-radio key="false" :label="false" border>不缓存</el-radio> |
|
106 |
</el-radio-group> |
|
107 |
</el-form-item> |
|
108 |
</el-form> |
|
109 |
<template #footer> |
|
110 |
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
|
111 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
112 |
</template> |
|
113 |
</Dialog> |
|
114 |
</template> |
|
115 |
<script lang="ts" setup> |
|
116 |
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict' |
|
117 |
import * as MenuApi from '@/api/system/menu' |
271781
|
118 |
import {CACHE_KEY, useCache, useSessionCache} from '@/hooks/web/useCache' |
820397
|
119 |
import { CommonStatusEnum, SystemMenuTypeEnum } from '@/utils/constants' |
H |
120 |
import { defaultProps, handleTree } from '@/utils/tree' |
|
121 |
|
|
122 |
defineOptions({ name: 'SystemMenuForm' }) |
|
123 |
|
271781
|
124 |
const { wsSessionCache } = useSessionCache() |
820397
|
125 |
const { t } = useI18n() // 国际化 |
H |
126 |
const message = useMessage() // 消息弹窗 |
|
127 |
|
|
128 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
129 |
const dialogTitle = ref('') // 弹窗的标题 |
|
130 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
131 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
132 |
const formData = ref({ |
|
133 |
id: undefined, |
|
134 |
name: '', |
|
135 |
permission: '', |
|
136 |
type: SystemMenuTypeEnum.DIR, |
|
137 |
sort: Number(undefined), |
|
138 |
parentId: 0, |
|
139 |
path: '', |
|
140 |
icon: '', |
|
141 |
component: '', |
|
142 |
componentName: '', |
|
143 |
status: CommonStatusEnum.ENABLE, |
|
144 |
visible: true, |
|
145 |
keepAlive: true, |
|
146 |
alwaysShow: true |
|
147 |
}) |
|
148 |
const formRules = reactive({ |
|
149 |
name: [{ required: true, message: '菜单名称不能为空', trigger: 'blur' }], |
|
150 |
sort: [{ required: true, message: '菜单顺序不能为空', trigger: 'blur' }], |
|
151 |
path: [{ required: true, message: '路由地址不能为空', trigger: 'blur' }], |
|
152 |
status: [{ required: true, message: '状态不能为空', trigger: 'blur' }] |
|
153 |
}) |
|
154 |
const formRef = ref() // 表单 Ref |
|
155 |
|
|
156 |
/** 打开弹窗 */ |
|
157 |
const open = async (type: string, id?: number, parentId?: number) => { |
|
158 |
dialogVisible.value = true |
|
159 |
dialogTitle.value = t('action.' + type) |
|
160 |
formType.value = type |
|
161 |
resetForm() |
|
162 |
if (parentId) { |
|
163 |
formData.value.parentId = parentId |
|
164 |
} |
|
165 |
// 修改时,设置数据 |
|
166 |
if (id) { |
|
167 |
formLoading.value = true |
|
168 |
try { |
|
169 |
formData.value = await MenuApi.getMenu(id) |
|
170 |
} finally { |
|
171 |
formLoading.value = false |
|
172 |
} |
|
173 |
} |
|
174 |
// 获得菜单列表 |
|
175 |
await getTree() |
|
176 |
} |
|
177 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
178 |
|
|
179 |
/** 提交表单 */ |
|
180 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
181 |
const submitForm = async () => { |
|
182 |
// 校验表单 |
|
183 |
if (!formRef) return |
|
184 |
const valid = await formRef.value.validate() |
|
185 |
if (!valid) return |
|
186 |
// 提交请求 |
|
187 |
formLoading.value = true |
|
188 |
try { |
|
189 |
if ( |
|
190 |
formData.value.type === SystemMenuTypeEnum.DIR || |
|
191 |
formData.value.type === SystemMenuTypeEnum.MENU |
|
192 |
) { |
|
193 |
if (!isExternal(formData.value.path)) { |
|
194 |
if (formData.value.parentId === 0 && formData.value.path.charAt(0) !== '/') { |
|
195 |
message.error('路径必须以 / 开头') |
|
196 |
return |
|
197 |
} else if (formData.value.parentId !== 0 && formData.value.path.charAt(0) === '/') { |
|
198 |
message.error('路径不能以 / 开头') |
|
199 |
return |
|
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
const data = formData.value as unknown as MenuApi.MenuVO |
|
204 |
if (formType.value === 'create') { |
|
205 |
await MenuApi.createMenu(data) |
|
206 |
message.success(t('common.createSuccess')) |
|
207 |
} else { |
|
208 |
await MenuApi.updateMenu(data) |
|
209 |
message.success(t('common.updateSuccess')) |
|
210 |
} |
|
211 |
dialogVisible.value = false |
|
212 |
// 发送操作成功的事件 |
|
213 |
emit('success') |
|
214 |
} finally { |
|
215 |
formLoading.value = false |
|
216 |
// 清空,从而触发刷新 |
271781
|
217 |
wsSessionCache.delete(CACHE_KEY.ROLE_ROUTERS) |
820397
|
218 |
} |
H |
219 |
} |
|
220 |
|
|
221 |
/** 获取下拉框[上级菜单]的数据 */ |
|
222 |
const menuTree = ref<Tree[]>([]) // 树形结构 |
|
223 |
const getTree = async () => { |
|
224 |
menuTree.value = [] |
|
225 |
const res = await MenuApi.getSimpleMenusList() |
|
226 |
let menu: Tree = { id: 0, name: '主类目', children: [] } |
|
227 |
menu.children = handleTree(res) |
|
228 |
menuTree.value.push(menu) |
|
229 |
} |
|
230 |
|
|
231 |
/** 重置表单 */ |
|
232 |
const resetForm = () => { |
|
233 |
formData.value = { |
|
234 |
id: undefined, |
|
235 |
name: '', |
|
236 |
permission: '', |
|
237 |
type: SystemMenuTypeEnum.DIR, |
|
238 |
sort: Number(undefined), |
|
239 |
parentId: 0, |
|
240 |
path: '', |
|
241 |
icon: '', |
|
242 |
component: '', |
|
243 |
componentName: '', |
|
244 |
status: CommonStatusEnum.ENABLE, |
|
245 |
visible: true, |
|
246 |
keepAlive: true, |
|
247 |
alwaysShow: true |
|
248 |
} |
|
249 |
formRef.value?.resetFields() |
|
250 |
} |
|
251 |
|
|
252 |
/** 判断 path 是不是外部的 HTTP 等链接 */ |
|
253 |
const isExternal = (path: string) => { |
|
254 |
return /^(https?:|mailto:|tel:)/.test(path) |
|
255 |
} |
|
256 |
</script> |