index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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-row>
  101. <el-col :span="24">
  102. <el-form-item
  103. label="权限范围"
  104. v-if="
  105. actionScopeType === 'menu' ||
  106. dataScopeForm.dataScope === SystemDataScopeEnum.DEPT_CUSTOM
  107. "
  108. style="display: flex"
  109. >
  110. <el-card class="card" shadow="never">
  111. <template #header>
  112. 父子联动(选中父节点,自动选择子节点):
  113. <el-switch
  114. v-model="checkStrictly"
  115. inline-prompt
  116. active-text="是"
  117. inactive-text="否"
  118. />
  119. 全选/全不选:
  120. <el-switch
  121. v-model="treeNodeAll"
  122. inline-prompt
  123. active-text="是"
  124. inactive-text="否"
  125. @change="handleCheckedTreeNodeAll()"
  126. />
  127. </template>
  128. <el-tree
  129. ref="treeRef"
  130. node-key="id"
  131. show-checkbox
  132. :check-strictly="!checkStrictly"
  133. :props="defaultProps"
  134. :data="treeOptions"
  135. empty-text="加载中,请稍后"
  136. />
  137. </el-card>
  138. </el-form-item> </el-col
  139. ></el-row>
  140. </el-form>
  141. <!-- 操作按钮 -->
  142. <template #footer>
  143. <XButton
  144. type="primary"
  145. :title="t('action.save')"
  146. :loading="actionLoading"
  147. @click="submitScope()"
  148. />
  149. <XButton
  150. :loading="actionLoading"
  151. :title="t('dialog.close')"
  152. @click="dialogScopeVisible = false"
  153. />
  154. </template>
  155. </XModal>
  156. </template>
  157. <script setup lang="ts" name="Role">
  158. import type { ElTree } from 'element-plus'
  159. import type { FormExpose } from '@/components/Form'
  160. import { handleTree, defaultProps } from '@/utils/tree'
  161. import { SystemDataScopeEnum } from '@/utils/constants'
  162. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  163. import { rules, allSchemas } from './role.data'
  164. import * as RoleApi from '@/api/system/role'
  165. import { listSimpleMenusApi } from '@/api/system/menu'
  166. import { getSimpleDeptList } from '@/api/system/dept'
  167. import * as PermissionApi from '@/api/system/permission'
  168. const { t } = useI18n() // 国际化
  169. const message = useMessage() // 消息弹窗
  170. // 列表相关的变量
  171. const [registerTable, { reload, deleteData }] = useXTable({
  172. allSchemas: allSchemas,
  173. getListApi: RoleApi.getRolePageApi,
  174. deleteApi: RoleApi.deleteRoleApi
  175. })
  176. // ========== CRUD 相关 ==========
  177. const actionLoading = ref(false) // 遮罩层
  178. const actionType = ref('') // 操作按钮的类型
  179. const dialogVisible = ref(false) // 是否显示弹出层
  180. const dialogTitle = ref('edit') // 弹出层标题
  181. const formRef = ref<FormExpose>() // 表单 Ref
  182. const detailData = ref() // 详情 Ref
  183. // 设置标题
  184. const setDialogTile = (type: string) => {
  185. dialogTitle.value = t('action.' + type)
  186. actionType.value = type
  187. dialogVisible.value = true
  188. }
  189. // 新增操作
  190. const handleCreate = () => {
  191. setDialogTile('create')
  192. }
  193. // 修改操作
  194. const handleUpdate = async (rowId: number) => {
  195. setDialogTile('update')
  196. // 设置数据
  197. const res = await RoleApi.getRoleApi(rowId)
  198. unref(formRef)?.setValues(res)
  199. }
  200. // 详情操作
  201. const handleDetail = async (rowId: number) => {
  202. setDialogTile('detail')
  203. // 设置数据
  204. const res = await RoleApi.getRoleApi(rowId)
  205. detailData.value = res
  206. }
  207. // 提交按钮
  208. const submitForm = async () => {
  209. const elForm = unref(formRef)?.getElFormRef()
  210. if (!elForm) return
  211. elForm.validate(async (valid) => {
  212. if (valid) {
  213. actionLoading.value = true
  214. // 提交请求
  215. try {
  216. const data = unref(formRef)?.formModel as RoleApi.RoleVO
  217. if (actionType.value === 'create') {
  218. await RoleApi.createRoleApi(data)
  219. message.success(t('common.createSuccess'))
  220. } else {
  221. await RoleApi.updateRoleApi(data)
  222. message.success(t('common.updateSuccess'))
  223. }
  224. dialogVisible.value = false
  225. } finally {
  226. actionLoading.value = false
  227. // 刷新列表
  228. await reload()
  229. }
  230. }
  231. })
  232. }
  233. // ========== 数据权限 ==========
  234. const dataScopeForm = reactive({
  235. id: 0,
  236. name: '',
  237. code: '',
  238. dataScope: 0,
  239. checkList: []
  240. })
  241. const treeOptions = ref<any[]>([]) // 菜单树形结构
  242. const treeRef = ref<InstanceType<typeof ElTree>>()
  243. const dialogScopeVisible = ref(false)
  244. const dialogScopeTitle = ref('数据权限')
  245. const actionScopeType = ref('')
  246. const dataScopeDictDatas = ref()
  247. // 选项
  248. const checkStrictly = ref(true)
  249. const treeNodeAll = ref(false)
  250. // 全选/全不选
  251. const handleCheckedTreeNodeAll = () => {
  252. treeRef.value!.setCheckedNodes(treeNodeAll.value ? treeOptions.value : [])
  253. }
  254. // 权限操作
  255. const handleScope = async (type: string, row: RoleApi.RoleVO) => {
  256. dataScopeForm.id = row.id
  257. dataScopeForm.name = row.name
  258. dataScopeForm.code = row.code
  259. actionScopeType.value = type
  260. dialogScopeVisible.value = true
  261. if (type === 'menu') {
  262. const menuRes = await listSimpleMenusApi()
  263. treeOptions.value = handleTree(menuRes)
  264. const role = await PermissionApi.listRoleMenusApi(row.id)
  265. if (role) {
  266. role?.forEach((item: any) => {
  267. unref(treeRef)?.setChecked(item, true, false)
  268. })
  269. }
  270. } else if (type === 'data') {
  271. const deptRes = await getSimpleDeptList()
  272. treeOptions.value = handleTree(deptRes)
  273. const role = await RoleApi.getRoleApi(row.id)
  274. dataScopeForm.dataScope = role.dataScope
  275. if (role.dataScopeDeptIds) {
  276. role.dataScopeDeptIds?.forEach((item: any) => {
  277. unref(treeRef)?.setChecked(item, true, false)
  278. })
  279. }
  280. }
  281. }
  282. // 保存权限
  283. const submitScope = async () => {
  284. if ('data' === actionScopeType.value) {
  285. const data = ref<PermissionApi.PermissionAssignRoleDataScopeReqVO>({
  286. roleId: dataScopeForm.id,
  287. dataScope: dataScopeForm.dataScope,
  288. dataScopeDeptIds:
  289. dataScopeForm.dataScope !== SystemDataScopeEnum.DEPT_CUSTOM
  290. ? []
  291. : (treeRef.value!.getCheckedKeys(false) as unknown as Array<number>)
  292. })
  293. await PermissionApi.assignRoleDataScopeApi(data.value)
  294. } else if ('menu' === actionScopeType.value) {
  295. const data = ref<PermissionApi.PermissionAssignRoleMenuReqVO>({
  296. roleId: dataScopeForm.id,
  297. menuIds: [
  298. ...(treeRef.value!.getCheckedKeys(false) as unknown as Array<number>),
  299. ...(treeRef.value!.getHalfCheckedKeys() as unknown as Array<number>)
  300. ]
  301. })
  302. await PermissionApi.assignRoleMenuApi(data.value)
  303. }
  304. message.success(t('common.updateSuccess'))
  305. dialogScopeVisible.value = false
  306. }
  307. const init = () => {
  308. dataScopeDictDatas.value = getIntDictOptions(DICT_TYPE.SYSTEM_DATA_SCOPE)
  309. }
  310. // ========== 初始化 ==========
  311. onMounted(() => {
  312. init()
  313. })
  314. </script>
  315. <style scoped>
  316. .card {
  317. width: 100%;
  318. max-height: 400px;
  319. overflow-y: scroll;
  320. }
  321. </style>