RoleManage.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <script setup lang="ts">
  2. import { nextTick, onMounted, ref, useTemplateRef, watch } from 'vue';
  3. import { message } from 'ant-design-vue';
  4. import ConfirmModal from '@/components/ConfirmModal.vue';
  5. import OrganizationalStructure from '@/components/OrganizationalStructure.vue';
  6. import SvgIcon from '@/components/SvgIcon.vue';
  7. import { useRequest } from '@/hooks/request';
  8. import { t } from '@/i18n';
  9. import {
  10. addCharacter,
  11. addDevicePermissions,
  12. addGrantRolePermissions,
  13. deleteCharacter,
  14. getAllGroupList,
  15. getFindRolesByOrgIds,
  16. getPermissionCheckTree,
  17. getSubOrgsByToken,
  18. updateCharacter,
  19. } from '@/api';
  20. import type { DataNode, TreeProps } from 'ant-design-vue/es/tree';
  21. import type { Key } from 'ant-design-vue/es/vc-tree/interface';
  22. import type { CheckInfo } from 'ant-design-vue/es/vc-tree/props';
  23. import type { OperationPermissions, TreeStructure } from '@/types';
  24. interface HaracterItem {
  25. name: string;
  26. id?: number;
  27. show: boolean;
  28. }
  29. const characterList = ref<HaracterItem[]>([]);
  30. const { handleRequest } = useRequest();
  31. const characterListId = ref();
  32. const inputRef = ref<HTMLInputElement[]>([]); // 明确的类型声明
  33. const permissions = ref<string>('dataPermissions');
  34. const equipmentChecked = ref<boolean>(false);
  35. const editorChecked = ref<boolean>(true);
  36. // const valueTime = ref<string>('1');
  37. const operationpermissions = ref<OperationPermissions[]>([]);
  38. const devicePermissionsExpandedKeys = ref<number[]>([]);
  39. const checkedFatherKeys = ref<number[]>([]);
  40. const devicePermissionsCheckedKeys = ref({
  41. checked: [] as number[], // 完全选中的节点
  42. halfChecked: [] as number[], // 半选中的节点(在严格模式下通常为空)
  43. });
  44. const allKeys = ref<number[]>([]);
  45. const indeterminate = ref<boolean>(false);
  46. const orgId = ref<number>();
  47. const characterId = ref<number>();
  48. const enterShow = ref<boolean>(false);
  49. const modalComponentRef = useTemplateRef('modalComponent');
  50. const expandedKeys = ref<number[]>([]);
  51. const checkedKeys = ref<number[]>([]);
  52. const treeStructure = ref<DataNode[]>([]);
  53. const fieldNamesCheck: TreeProps['fieldNames'] = {
  54. children: 'subPermissions',
  55. title: 'menuName',
  56. key: 'id',
  57. };
  58. const pagePermissionsTree = ref<DataNode[]>([]);
  59. const fieldNames: TreeProps['fieldNames'] = {
  60. children: 'deviceGroupChilds',
  61. title: 'groupName',
  62. key: 'id',
  63. };
  64. const addCharacterName = async () => {
  65. if (characterList.value.some((item) => !isValidString(item.name))) {
  66. return;
  67. }
  68. characterList.value.push({
  69. name: '',
  70. id: undefined,
  71. show: true,
  72. });
  73. await nextTick();
  74. inputRef.value[0].focus();
  75. };
  76. const clickCharacter = (id: number) => {
  77. if (characterList.value.some((item) => !isValidString(item.name))) {
  78. return;
  79. }
  80. characterListId.value = id;
  81. getDeviceGroupList();
  82. getFunctionPermList();
  83. };
  84. const addEditor = async (index: number) => {
  85. if (characterList.value.some((item) => !isValidString(item.name))) {
  86. return;
  87. }
  88. characterList.value[index].show = true;
  89. await nextTick();
  90. inputRef.value[0].focus();
  91. };
  92. const addDelete = (id: number | undefined) => {
  93. if (!id) {
  94. return message.warning(t('deviceList.pleaseSelectItemDelete'));
  95. }
  96. if (characterList.value.some((item) => !isValidString(item.name))) {
  97. return;
  98. }
  99. characterId.value = id;
  100. modalComponentRef.value?.showView();
  101. };
  102. const confirm = () => {
  103. handleRequest(async () => {
  104. if (characterId.value) {
  105. await deleteCharacter(characterId.value);
  106. if (orgId.value) {
  107. getFindRolesByOrg(orgId.value);
  108. }
  109. }
  110. modalComponentRef.value?.hideView();
  111. });
  112. };
  113. const editorCharacterBlur = (index: number, name: string) => {
  114. if (!enterShow.value) {
  115. editorCharacter(index, name);
  116. }
  117. };
  118. /**
  119. * 校验字符串是否非空且不包含任何空白字符
  120. * @param str 待检测的字符串
  121. * @returns 是否有效(非空且无空白)
  122. */
  123. const isValidString = (str: string): boolean => {
  124. return /^[\S]+$/.test(str);
  125. // ^ 开头 | \S 非空白字符 | + 至少一个 | $ 结尾
  126. };
  127. const editorCharacter = (index: number, name: string) => {
  128. if (name) {
  129. if (isValidString(name)) {
  130. enterShow.value = true;
  131. characterList.value[index].show = false;
  132. const id = characterList.value[index].id;
  133. handleRequest(async () => {
  134. if (id) {
  135. await updateCharacter({ roleName: name, orgId: orgId.value, id });
  136. } else {
  137. await addCharacter({ roleName: name, orgId: orgId.value });
  138. }
  139. enterShow.value = false;
  140. if (orgId.value) {
  141. getFindRolesByOrg(orgId.value);
  142. }
  143. });
  144. } else {
  145. return message.warning('名称中不能包含空格!');
  146. }
  147. } else {
  148. return message.warning('名称不能为空!');
  149. }
  150. };
  151. const editorPermission = () => {
  152. editorChecked.value = false;
  153. };
  154. const cancelPermission = () => {
  155. if (permissions.value === 'dataPermissions') {
  156. getDeviceGroupList();
  157. } else {
  158. getFunctionPermList();
  159. }
  160. editorChecked.value = true;
  161. };
  162. const addTree = (checked: Key[] | { checked: Key[]; halfChecked: Key[] }, info: CheckInfo) => {
  163. if (info.node.parent) {
  164. checkedFatherKeys.value.push(info.node.parent.node.id);
  165. }
  166. };
  167. const savePermission = () => {
  168. if (permissions.value === 'dataPermissions') {
  169. handleRequest(async () => {
  170. await addDevicePermissions({
  171. roleId: characterListId.value,
  172. groupIds: devicePermissionsCheckedKeys.value.checked,
  173. });
  174. getDeviceGroupList();
  175. });
  176. } else {
  177. let data: number[] = [];
  178. operationpermissions.value.forEach((item) => {
  179. if (item.list.length) {
  180. data = [...new Set([...data, ...item.list])];
  181. }
  182. });
  183. handleRequest(async () => {
  184. if (!checkedKeys.value.length) {
  185. checkedFatherKeys.value = [];
  186. }
  187. await addGrantRolePermissions({
  188. roleId: characterListId.value,
  189. permissionIds: [...new Set([...checkedKeys.value, ...checkedFatherKeys.value, ...data])],
  190. });
  191. getFunctionPermList();
  192. });
  193. }
  194. editorChecked.value = true;
  195. };
  196. const clickOrganizationChange = (id: number) => {
  197. orgId.value = id;
  198. getFindRolesByOrg(id);
  199. };
  200. const getFindRolesByOrg = (id: number) => {
  201. handleRequest(async () => {
  202. const data = await getFindRolesByOrgIds([id]);
  203. characterList.value = [];
  204. if (data.length) {
  205. data.forEach((item) => {
  206. const { roleName, id } = item;
  207. characterList.value.push({
  208. name: roleName,
  209. id,
  210. show: false,
  211. });
  212. });
  213. if (!characterListId.value) {
  214. characterListId.value = data[0].id;
  215. getDeviceGroupList();
  216. getFunctionPermList();
  217. } else {
  218. getDeviceGroupList();
  219. getFunctionPermList();
  220. }
  221. } else {
  222. pagePermissionsTree.value = [];
  223. treeStructure.value = [];
  224. operationpermissions.value = [];
  225. }
  226. });
  227. };
  228. const getDeviceGroupList = () => {
  229. handleRequest(async () => {
  230. const list = await getAllGroupList({
  231. roleId: undefined,
  232. });
  233. const data = await getAllGroupList({
  234. roleId: characterListId.value,
  235. });
  236. devicePermissionsExpandedKeys.value = [];
  237. devicePermissionsCheckedKeys.value.checked = [];
  238. allKeys.value = [];
  239. list.forEach((item) => {
  240. item.key = item.id;
  241. allKeys.value.push(item.id);
  242. devicePermissionsExpandedKeys.value.push(item.id);
  243. if (item.deviceGroupChilds.length) {
  244. item.deviceGroupChilds.forEach((i) => {
  245. i.key = i.id;
  246. allKeys.value.push(i.id);
  247. });
  248. }
  249. });
  250. data.forEach((item) => {
  251. devicePermissionsCheckedKeys.value.checked.push(item.id);
  252. item.deviceGroupChilds.forEach((i) => {
  253. devicePermissionsCheckedKeys.value.checked.push(i.id);
  254. });
  255. });
  256. pagePermissionsTree.value = list as DataNode[];
  257. });
  258. };
  259. const transformTreeData = (data: TreeStructure[]): DataNode[] => {
  260. return data.map((item) => ({
  261. ...item,
  262. key: item.id, // 关键:将 id 映射到 key
  263. title: item.menuName,
  264. children: item.subPermissions ? transformTreeData(item.subPermissions) : undefined,
  265. }));
  266. };
  267. const transformData = (data: TreeStructure[]): OperationPermissions[] => {
  268. return data.map((item) => {
  269. // 转换当前层级的属性
  270. const transformed = {
  271. value: item.id,
  272. label: item.menuName,
  273. list: item.subPermissions?.filter((sub) => sub.checked === 1).map((sub) => sub.id) || [],
  274. ...item,
  275. // 保留其他属性(如果需要)
  276. ...(item.subPermissions && {
  277. subPermissions: transformData(item.subPermissions),
  278. }),
  279. };
  280. return transformed;
  281. });
  282. };
  283. const getFunctionPermList = () => {
  284. handleRequest(async () => {
  285. // const data = await getSubPermList(0);
  286. const dataCheck = await getPermissionCheckTree(characterListId.value);
  287. checkedKeys.value = [];
  288. expandedKeys.value = [];
  289. if (dataCheck.length) {
  290. treeStructure.value = transformTreeData(dataCheck[0].subPermissions[0].subPermissions);
  291. operationpermissions.value = transformData(dataCheck[0].subPermissions[1].subPermissions);
  292. dataCheck[0].subPermissions[0].subPermissions.forEach((item) => {
  293. if (item.subPermissions) {
  294. item.subPermissions.forEach((i) => {
  295. if (i.checked === 1) {
  296. checkedKeys.value.push(i.id);
  297. }
  298. });
  299. expandedKeys.value.push(item.id);
  300. } else {
  301. if (item.checked === 1) {
  302. checkedKeys.value.push(item.id);
  303. }
  304. }
  305. });
  306. }
  307. });
  308. };
  309. const selectAll = () => {
  310. indeterminate.value = false;
  311. if (equipmentChecked.value) {
  312. devicePermissionsCheckedKeys.value.checked = allKeys.value;
  313. } else {
  314. devicePermissionsCheckedKeys.value.checked = [];
  315. }
  316. };
  317. const addRadioGroup = () => {
  318. if (permissions.value === 'functionPermissions') {
  319. getDeviceGroupList();
  320. } else {
  321. getFunctionPermList();
  322. }
  323. editorChecked.value = true;
  324. };
  325. watch(
  326. () => devicePermissionsCheckedKeys.value.checked,
  327. (count) => {
  328. if (count) {
  329. indeterminate.value = !!count.length && count.length < allKeys.value.length;
  330. equipmentChecked.value = count.length === allKeys.value.length;
  331. }
  332. },
  333. );
  334. onMounted(() => {
  335. handleRequest(async () => {
  336. await getSubOrgsByToken();
  337. getFunctionPermList();
  338. });
  339. });
  340. </script>
  341. <template>
  342. <div>
  343. <div class="text-top">角色管理</div>
  344. <AFlex>
  345. <OrganizationalStructure @change="clickOrganizationChange" />
  346. <div class="content">
  347. <AFlex justify="space-between" align="center" class="content-top">
  348. <div class="content-text">角色</div>
  349. <div class="icon-style pointer" @click="addCharacterName">
  350. <AFlex align="center"
  351. ><SvgIcon name="plus" />
  352. <div class="text-left">添加</div>
  353. </AFlex>
  354. </div>
  355. </AFlex>
  356. <div>
  357. <div v-for="(item, index) in characterList" :key="index">
  358. <div class="character-input" v-if="item.show">
  359. <AInput
  360. class="input-heught"
  361. v-model:value="item.name"
  362. :bordered="false"
  363. ref="inputRef"
  364. @pressEnter="editorCharacter(index, item.name)"
  365. @blur="editorCharacterBlur(index, item.name)"
  366. />
  367. </div>
  368. <AFlex
  369. v-else
  370. justify="space-between"
  371. align="center"
  372. :class="item.id === characterListId ? 'character-list character-list-color' : 'character-list'"
  373. >
  374. <div class="pointer text-height" @click="clickCharacter(item.id!)">{{ item.name }}</div>
  375. <div v-if="item.id === characterListId">
  376. <SvgIcon class="pointer" name="edit-o" @click="addEditor(index)" />
  377. <SvgIcon class="pointer icon-left" @click="addDelete(item.id)" name="delete" />
  378. </div>
  379. </AFlex>
  380. </div>
  381. </div>
  382. </div>
  383. <div class="permission-management">
  384. <AFlex justify="space-between" align="center" class="content-top">
  385. <div class="content-text">权限</div>
  386. <div class="pointer" @click="editorPermission" v-if="editorChecked">
  387. <AFlex align="center"
  388. ><SvgIcon name="edit-o" />
  389. <div class="text-left">编辑</div>
  390. </AFlex>
  391. </div>
  392. <AFlex v-else>
  393. <div class="pointer" @click="cancelPermission">
  394. <AFlex align="center"
  395. ><SvgIcon name="close" />
  396. <div class="text-left">取消</div>
  397. </AFlex>
  398. </div>
  399. <div class="pointer pointer-left" @click="savePermission">
  400. <AFlex align="center"
  401. ><SvgIcon name="close" />
  402. <div class="text-left">保存</div>
  403. </AFlex>
  404. </div>
  405. </AFlex>
  406. </AFlex>
  407. <ARadioGroup v-model:value="permissions" button-style="solid" size="large" @change="addRadioGroup">
  408. <ARadioButton value="dataPermissions">数据权限</ARadioButton>
  409. <ARadioButton value="functionPermissions">功能权限</ARadioButton>
  410. </ARadioGroup>
  411. <div v-if="permissions === 'dataPermissions'">
  412. <AFlex align="center" class="device-permissions">
  413. <ACheckbox
  414. class="select-all"
  415. v-model:checked="equipmentChecked"
  416. :indeterminate="indeterminate"
  417. :disabled="editorChecked"
  418. @change="selectAll"
  419. >设备组权限</ACheckbox
  420. >
  421. </AFlex>
  422. <div class="permission-div">
  423. <ATree
  424. v-model:expanded-keys="devicePermissionsExpandedKeys"
  425. v-model:checked-keys="devicePermissionsCheckedKeys"
  426. :tree-data="pagePermissionsTree"
  427. checkable
  428. default-expand-all
  429. :field-names="fieldNames"
  430. :disabled="editorChecked"
  431. :check-strictly="true"
  432. />
  433. </div>
  434. <!-- <AFlex align="center" class="device-permissions div-top">
  435. <ACheckbox class="select-all" :disabled="editorChecked" v-model:checked="equipmentChecked"
  436. >启用时间查询颗粒度设置</ACheckbox
  437. >
  438. </AFlex>
  439. <ARadioGroup v-model:value="valueTime" name="radioGroup" class="radio-group" :disabled="editorChecked">
  440. <ARadio value="1">分钟</ARadio>
  441. <ARadio value="2">小时</ARadio>
  442. <ARadio value="3">天</ARadio>
  443. <ARadio value="4">月</ARadio>
  444. </ARadioGroup> -->
  445. </div>
  446. <div v-if="permissions === 'functionPermissions'">
  447. <AFlex align="center" class="device-permissions">
  448. <div>查看权限</div>
  449. </AFlex>
  450. <div class="check-div">
  451. <ATree
  452. v-model:expanded-keys="expandedKeys"
  453. v-model:checked-keys="checkedKeys"
  454. :tree-data="treeStructure"
  455. checkable
  456. :field-names="fieldNamesCheck"
  457. :disabled="editorChecked"
  458. class="tree-permissions"
  459. default-expand-all
  460. @check="addTree"
  461. />
  462. </div>
  463. <AFlex align="center" class="device-permissions div-top">
  464. <div>操作权限</div>
  465. </AFlex>
  466. <div class="operation">
  467. <AFlex align="center" v-for="(item, index) in operationpermissions" :key="index" class="operation-div">
  468. <ACheckboxGroup v-model:value="item.list" :options="item.subPermissions" :disabled="editorChecked" />
  469. </AFlex>
  470. </div>
  471. </div>
  472. </div>
  473. </AFlex>
  474. <ConfirmModal
  475. ref="modalComponent"
  476. :title="t('common.deleteConfirmation')"
  477. :description-text="t('common.confirmDeletion')"
  478. :icon="{ name: 'delete' }"
  479. :icon-bg-color="'#F56C6C'"
  480. @confirm="confirm"
  481. />
  482. </div>
  483. </template>
  484. <style lang="scss" scoped>
  485. .operation {
  486. height: 200px;
  487. overflow: auto;
  488. }
  489. .operation-div {
  490. width: 100%;
  491. height: 48px;
  492. padding-left: 52px;
  493. border-bottom: 1px solid #e4e7ed;
  494. }
  495. .check-div {
  496. height: calc(100vh - 494px);
  497. overflow: auto;
  498. }
  499. .permission-div {
  500. height: calc(100vh - 244px);
  501. overflow: auto;
  502. }
  503. .pointer-left {
  504. margin-left: 24px;
  505. }
  506. :deep(.permission-management) {
  507. .ant-tree-list-holder-inner > div {
  508. display: flex;
  509. align-items: center;
  510. width: 100%;
  511. height: 48px;
  512. padding-left: 26px;
  513. }
  514. .tree-permissions .ant-tree-list-holder-inner > div {
  515. border-bottom: 1px solid #e4e7ed;
  516. }
  517. .ant-tree-list-holder-inner > div > .ant-tree-checkbox {
  518. margin-block-start: 0;
  519. }
  520. .ant-tree-list-holder-inner > div > .ant-tree-switcher > span {
  521. margin-top: 16px;
  522. }
  523. }
  524. .radio-group {
  525. margin-top: 13px;
  526. margin-left: 48px;
  527. }
  528. :deep(.radio-group) {
  529. .ant-radio-wrapper {
  530. margin-inline-end: 20px;
  531. }
  532. }
  533. .device-permissions {
  534. width: 100%;
  535. height: 48px;
  536. padding-left: 24px;
  537. margin-top: 16px;
  538. background: #f5f7fa;
  539. border-radius: 4px;
  540. }
  541. .permission-management .div-top {
  542. margin-top: 0;
  543. }
  544. .permission-management {
  545. width: 100%;
  546. height: calc(100vh - 80px);
  547. padding: 16px;
  548. background: #fff;
  549. border-radius: 16px;
  550. }
  551. .input-heught {
  552. height: 38px;
  553. }
  554. .character-input {
  555. width: 214px;
  556. height: 40px;
  557. background: rgb(255 255 255 / 15%);
  558. border: 1px solid var(--antd-color-primary);
  559. border-radius: 4px;
  560. }
  561. .text-left {
  562. margin-left: 10px;
  563. }
  564. .icon-style {
  565. color: var(--antd-color-primary);
  566. }
  567. .text-height {
  568. height: 40px;
  569. line-height: 40px;
  570. }
  571. .icon-left {
  572. margin-left: 13px;
  573. }
  574. .pointer {
  575. cursor: pointer;
  576. }
  577. .character-list {
  578. width: 214px;
  579. height: 40px;
  580. padding: 0 12px;
  581. font-size: 14px;
  582. font-style: normal;
  583. font-weight: 400;
  584. line-height: 22px;
  585. color: #000;
  586. text-align: left;
  587. border-radius: 4px;
  588. }
  589. .character-list-color {
  590. color: var(--antd-color-primary);
  591. background: var(--antd-color-primary-opacity-15);
  592. }
  593. .content-top {
  594. margin-bottom: 16px;
  595. }
  596. .button-style {
  597. color: var(--antd-color-primary);
  598. }
  599. .content-text {
  600. font-size: 16px;
  601. font-style: normal;
  602. font-weight: 600;
  603. line-height: 24px;
  604. color: #333;
  605. text-align: left;
  606. }
  607. .content {
  608. width: 246px;
  609. height: calc(100vh - 80px);
  610. padding: 16px;
  611. margin-right: 16px;
  612. background: #fff;
  613. border-radius: 16px;
  614. }
  615. .text-top {
  616. margin-bottom: 16px;
  617. font-size: 20px;
  618. font-style: normal;
  619. font-weight: 500;
  620. line-height: 32px;
  621. color: rgb(0 0 0 / 85%);
  622. text-align: left;
  623. }
  624. </style>