index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <XTable @register="registerTable">
  5. <!-- 操作:新增 -->
  6. <template #toolbar_buttons>
  7. <XButton
  8. type="primary"
  9. preIcon="ep:zoom-in"
  10. :title="t('action.add')"
  11. v-hasPermi="['system:role:create']"
  12. @click="handleCreate()"
  13. />
  14. </template>
  15. <template #actionbtns_default="{ row }">
  16. <!-- 操作:编辑 -->
  17. <XTextButton
  18. preIcon="ep:edit"
  19. :title="t('action.edit')"
  20. v-hasPermi="['system:role:update']"
  21. @click="handleUpdate(row.id)"
  22. />
  23. <!-- 操作:详情 -->
  24. <XTextButton
  25. preIcon="ep:view"
  26. :title="t('action.detail')"
  27. v-hasPermi="['system:role:query']"
  28. @click="handleDetail(row.id)"
  29. />
  30. <!-- 操作:菜单权限 -->
  31. <XTextButton
  32. preIcon="ep:basketball"
  33. title="菜单权限"
  34. v-hasPermi="['system:permission:assign-role-menu']"
  35. @click="handleScope('menu', row)"
  36. />
  37. <!-- 操作:数据权限 -->
  38. <XTextButton
  39. preIcon="ep:coin"
  40. title="数据权限"
  41. v-hasPermi="['system:permission:assign-role-data-scope']"
  42. @click="handleScope('data', row)"
  43. />
  44. <!-- 操作:删除 -->
  45. <XTextButton
  46. preIcon="ep:delete"
  47. :title="t('action.del')"
  48. v-hasPermi="['system:role:delete']"
  49. @click="deleteData(row.id)"
  50. />
  51. </template>
  52. </XTable>
  53. </ContentWrap>
  54. <XModal v-model="dialogVisible" :title="dialogTitle">
  55. <!-- 对话框(添加 / 修改) -->
  56. <Form
  57. v-if="['create', 'update'].includes(actionType)"
  58. :schema="allSchemas.formSchema"
  59. :rules="rules"
  60. ref="formRef"
  61. />
  62. <!-- 对话框(详情) -->
  63. <Descriptions
  64. v-if="actionType === 'detail'"
  65. :schema="allSchemas.detailSchema"
  66. :data="detailData"
  67. />
  68. <!-- 操作按钮 -->
  69. <template #footer>
  70. <XButton
  71. v-if="['create', 'update'].includes(actionType)"
  72. type="primary"
  73. :title="t('action.save')"
  74. :loading="actionLoading"
  75. @click="submitForm()"
  76. />
  77. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  78. </template>
  79. </XModal>
  80. <XModal v-model="dialogScopeVisible" :title="dialogScopeTitle">
  81. <el-form :model="dataScopeForm" label-width="140px" :inline="true">
  82. <el-form-item label="角色名称">
  83. <el-tag>{{ dataScopeForm.name }}</el-tag>
  84. </el-form-item>
  85. <el-form-item label="角色标识">
  86. <el-tag>{{ dataScopeForm.code }}</el-tag>
  87. </el-form-item>
  88. <!-- 分配角色的数据权限对话框 -->
  89. <el-form-item label="权限范围" v-if="actionScopeType === 'data'">
  90. <el-select v-model="dataScopeForm.dataScope">
  91. <el-option
  92. v-for="item in dataScopeDictDatas"
  93. :key="item.value"
  94. :label="item.label"
  95. :value="item.value"
  96. />
  97. </el-select>
  98. </el-form-item>
  99. <!-- 分配角色的菜单权限对话框 -->
  100. <el-form-item
  101. label="权限范围"
  102. v-if="
  103. actionScopeType === 'menu' || dataScopeForm.dataScope === SystemDataScopeEnum.DEPT_CUSTOM
  104. "
  105. >
  106. <el-card shadow="never">
  107. <template #header>
  108. 父子联动(选中父节点,自动选择子节点):
  109. <el-switch v-model="checkStrictly" inline-prompt active-text="是" inactive-text="否" />
  110. 全选/全不选:
  111. <el-switch
  112. v-model="treeNodeAll"
  113. inline-prompt
  114. active-text="是"
  115. inactive-text="否"
  116. @change="handleCheckedTreeNodeAll()"
  117. />
  118. </template>
  119. <el-tree
  120. ref="treeRef"
  121. node-key="id"
  122. show-checkbox
  123. :check-strictly="!checkStrictly"
  124. :props="defaultProps"
  125. :data="treeOptions"
  126. empty-text="加载中,请稍后"
  127. />
  128. </el-card>
  129. </el-form-item>
  130. </el-form>
  131. <!-- 操作按钮 -->
  132. <template #footer>
  133. <XButton
  134. type="primary"
  135. :title="t('action.save')"
  136. :loading="actionLoading"
  137. @click="submitScope()"
  138. />
  139. <XButton
  140. :loading="actionLoading"
  141. :title="t('dialog.close')"
  142. @click="dialogScopeVisible = false"
  143. />
  144. </template>
  145. </XModal>
  146. </template>
  147. <script setup lang="ts" name="Role">
  148. import type { ElTree } from 'element-plus'
  149. import type { FormExpose } from '@/components/Form'
  150. import { handleTree, defaultProps } from '@/utils/tree'
  151. import { SystemDataScopeEnum } from '@/utils/constants'
  152. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  153. import { rules, allSchemas } from './role.data'
  154. import * as RoleApi from '@/api/system/role'
  155. import { listSimpleMenusApi } from '@/api/system/menu'
  156. import { listSimpleDeptApi } from '@/api/system/dept'
  157. import * as PermissionApi from '@/api/system/permission'
  158. const { t } = useI18n() // 国际化
  159. const message = useMessage() // 消息弹窗
  160. // 列表相关的变量
  161. const [registerTable, { reload, deleteData }] = useXTable({
  162. allSchemas: allSchemas,
  163. getListApi: RoleApi.getRolePageApi,
  164. deleteApi: RoleApi.deleteRoleApi
  165. })
  166. // ========== CRUD 相关 ==========
  167. const actionLoading = ref(false) // 遮罩层
  168. const actionType = ref('') // 操作按钮的类型
  169. const dialogVisible = ref(false) // 是否显示弹出层
  170. const dialogTitle = ref('edit') // 弹出层标题
  171. const formRef = ref<FormExpose>() // 表单 Ref
  172. const detailData = ref() // 详情 Ref
  173. // 设置标题
  174. const setDialogTile = (type: string) => {
  175. dialogTitle.value = t('action.' + type)
  176. actionType.value = type
  177. dialogVisible.value = true
  178. }
  179. // 新增操作
  180. const handleCreate = () => {
  181. setDialogTile('create')
  182. }
  183. // 修改操作
  184. const handleUpdate = async (rowId: number) => {
  185. setDialogTile('update')
  186. // 设置数据
  187. const res = await RoleApi.getRoleApi(rowId)
  188. unref(formRef)?.setValues(res)
  189. }
  190. // 详情操作
  191. const handleDetail = async (rowId: number) => {
  192. setDialogTile('detail')
  193. // 设置数据
  194. const res = await RoleApi.getRoleApi(rowId)
  195. detailData.value = res
  196. }
  197. // 提交按钮
  198. const submitForm = async () => {
  199. const elForm = unref(formRef)?.getElFormRef()
  200. if (!elForm) return
  201. elForm.validate(async (valid) => {
  202. if (valid) {
  203. actionLoading.value = true
  204. // 提交请求
  205. try {
  206. const data = unref(formRef)?.formModel as RoleApi.RoleVO
  207. if (actionType.value === 'create') {
  208. await RoleApi.createRoleApi(data)
  209. message.success(t('common.createSuccess'))
  210. } else {
  211. await RoleApi.updateRoleApi(data)
  212. message.success(t('common.updateSuccess'))
  213. }
  214. dialogVisible.value = false
  215. } finally {
  216. actionLoading.value = false
  217. // 刷新列表
  218. await reload()
  219. }
  220. }
  221. })
  222. }
  223. // ========== 数据权限 ==========
  224. const dataScopeForm = reactive({
  225. id: 0,
  226. name: '',
  227. code: '',
  228. dataScope: 0,
  229. checkList: []
  230. })
  231. const treeOptions = ref<any[]>([]) // 菜单树形结构
  232. const treeRef = ref<InstanceType<typeof ElTree>>()
  233. const dialogScopeVisible = ref(false)
  234. const dialogScopeTitle = ref('数据权限')
  235. const actionScopeType = ref('')
  236. const dataScopeDictDatas = ref()
  237. // 选项
  238. const checkStrictly = ref(true)
  239. const treeNodeAll = ref(false)
  240. // 全选/全不选
  241. const handleCheckedTreeNodeAll = () => {
  242. treeRef.value!.setCheckedNodes(treeNodeAll.value ? treeOptions.value : [])
  243. }
  244. // 权限操作
  245. const handleScope = async (type: string, row: RoleApi.RoleVO) => {
  246. dataScopeForm.id = row.id
  247. dataScopeForm.name = row.name
  248. dataScopeForm.code = row.code
  249. actionScopeType.value = type
  250. dialogScopeVisible.value = true
  251. if (type === 'menu') {
  252. const menuRes = await listSimpleMenusApi()
  253. treeOptions.value = handleTree(menuRes)
  254. const role = await PermissionApi.listRoleMenusApi(row.id)
  255. if (role) {
  256. role?.forEach((item: any) => {
  257. unref(treeRef)?.setChecked(item, true,false);
  258. })
  259. }
  260. } else if (type === 'data') {
  261. const deptRes = await listSimpleDeptApi()
  262. treeOptions.value = handleTree(deptRes)
  263. const role = await RoleApi.getRoleApi(row.id)
  264. dataScopeForm.dataScope = role.dataScope
  265. if (role.dataScopeDeptIds) {
  266. role.dataScopeDeptIds?.forEach((item: any) => {
  267. unref(treeRef)?.setChecked(item, true,false);
  268. })
  269. }
  270. }
  271. }
  272. // 保存权限
  273. const submitScope = async () => {
  274. if ('data' === actionScopeType.value) {
  275. const data = ref<PermissionApi.PermissionAssignRoleDataScopeReqVO>({
  276. roleId: dataScopeForm.id,
  277. dataScope: dataScopeForm.dataScope,
  278. dataScopeDeptIds:
  279. dataScopeForm.dataScope !== SystemDataScopeEnum.DEPT_CUSTOM
  280. ? []
  281. : (treeRef.value!.getCheckedKeys(false) as unknown as Array<number>)
  282. })
  283. await PermissionApi.assignRoleDataScopeApi(data.value)
  284. } else if ('menu' === actionScopeType.value) {
  285. const data = ref<PermissionApi.PermissionAssignRoleMenuReqVO>({
  286. roleId: dataScopeForm.id,
  287. menuIds: [
  288. ...(treeRef.value!.getCheckedKeys(false) as unknown as Array<number>),
  289. ...(treeRef.value!.getHalfCheckedKeys() as unknown as Array<number>)
  290. ]
  291. })
  292. await PermissionApi.assignRoleMenuApi(data.value)
  293. }
  294. message.success(t('common.updateSuccess'))
  295. dialogScopeVisible.value = false
  296. }
  297. const init = () => {
  298. dataScopeDictDatas.value = getIntDictOptions(DICT_TYPE.SYSTEM_DATA_SCOPE)
  299. }
  300. // ========== 初始化 ==========
  301. onMounted(() => {
  302. init()
  303. })
  304. </script>