form.vue 10 KB

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