index.vue 10.0 KB

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