index.vue 9.9 KB

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