index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <template>
  2. <CollapseContainer
  3. class="sys-container"
  4. title="角色组"
  5. :canExpan="false"
  6. helpMessage="角色组可以有多个,角色有上下级层级关系,如果子角色有角色组和管理员的权限则可以派生属于自己组别的下级角色组或管理员"
  7. >
  8. <BasicTable
  9. ref="tableRef"
  10. @register="registerTable"
  11. @fetchSuccess="ExpandAllRows"
  12. @selectionChange="selectionChange"
  13. @rowClick="rowClick"
  14. @rowDbClick="handleEdit"
  15. defaultExpandAllRows
  16. showTableSetting
  17. rowKey="id"
  18. >
  19. <template #toolbar>
  20. <div class="tool-btn-wrap">
  21. <a-button type="primary" @click="toggleRowShow">{{ btn_text }}</a-button>
  22. <a-button type="primary" @click="addGroupFn"> 添加 </a-button>
  23. <a-button color="error" :disabled="disable_btn" @click="deleteBatches"> 删除 </a-button>
  24. </div>
  25. </template>
  26. <template #action="{ record, column }">
  27. <TableAction :actions="createActions(record, column)" stopButtonPropagation />
  28. </template>
  29. </BasicTable>
  30. <Popup @register="addRegister" :popupData="popupData" @saveData="saveData" />
  31. </CollapseContainer>
  32. </template>
  33. <script lang="ts">
  34. import { defineComponent, reactive, ref, toRefs, unref, nextTick, createVNode } from 'vue';
  35. import { CollapseContainer } from '/@/components/Container/index';
  36. import Popup from './popup.vue';
  37. import { useModal } from '/@/components/Modal';
  38. import { columns } from './data';
  39. import { useUserStore } from '/@/store/modules/user';
  40. import { useMessage } from '/@/hooks/web/useMessage';
  41. import { Modal } from 'ant-design-vue';
  42. import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  43. import {
  44. getGroupTree,
  45. addGroup,
  46. editGroup,
  47. deleteGroup,
  48. deleteBatchesGroup,
  49. getGroupById,
  50. } from '/@/api/sys/user';
  51. import {
  52. BasicTable,
  53. useTable,
  54. TableAction,
  55. ActionItem,
  56. EditRecordRow,
  57. TableActionType,
  58. } from '/@/components/Table';
  59. interface PopupData {
  60. title: string;
  61. treeData: object[];
  62. group_ids: number[];
  63. }
  64. interface Btn {
  65. btn_text: string;
  66. btn_status: boolean;
  67. disable_btn: boolean;
  68. }
  69. interface Group {
  70. id: any;
  71. name: string;
  72. }
  73. export default defineComponent({
  74. name: 'Group',
  75. components: { CollapseContainer, BasicTable, TableAction, Popup },
  76. setup() {
  77. const userStore = useUserStore();
  78. const groups = userStore.getUserInfo.groups as Group[];
  79. const group_ids = groups.map((item) => item.id);
  80. const { createMessage } = useMessage();
  81. const { success /*, error */ } = createMessage;
  82. const tableRef = ref<Nullable<TableActionType>>(null);
  83. const popupData = reactive<PopupData>({
  84. title: '添加',
  85. treeData: [{}],
  86. group_ids: [],
  87. });
  88. const btn = reactive<Btn>({
  89. btn_text: '展开全部',
  90. btn_status: true,
  91. disable_btn: true,
  92. });
  93. const [registerTable, { expandAll, collapseAll }] = useTable({
  94. rowSelection: {
  95. type: 'checkbox',
  96. getCheckboxProps: (record) => ({
  97. disabled: group_ids.includes(record.id), // Column configuration not to be checked
  98. }),
  99. },
  100. columns: columns,
  101. // clickToRowSelect: false, // 点击行不勾选
  102. api: getGroupTree,
  103. isTreeTable: true,
  104. pagination: false, // 树列表不显示分页
  105. beforeFetch: beforeFetch,
  106. afterFetch: afterFetch,
  107. actionColumn: {
  108. width: 160,
  109. title: '操作',
  110. dataIndex: 'action',
  111. slots: { customRender: 'action' },
  112. fixed: undefined,
  113. },
  114. tableSetting: {
  115. // redo: false,
  116. size: false,
  117. formSearch: false,
  118. },
  119. showIndexColumn: false,
  120. bordered: true,
  121. });
  122. const [addRegister, { openModal: openPopup }] = useModal();
  123. function beforeFetch(params) {
  124. for (let k in params) {
  125. if (k !== 'page' && k !== 'pageSize' && k !== 'field' && k !== 'order') {
  126. if (params[k] === '') {
  127. delete params[k];
  128. } else {
  129. if (!params.filter) {
  130. params.filter = {};
  131. }
  132. params.filter[k] = params[k];
  133. delete params[k];
  134. }
  135. }
  136. }
  137. params.filter = JSON.stringify(params.filter);
  138. params.offset = params.page;
  139. params.limit = params.pageSize;
  140. delete params.page;
  141. delete params.pageSize;
  142. }
  143. function afterFetch(result) {
  144. popupData.treeData = result;
  145. }
  146. function getTableAction() {
  147. // 获取组件
  148. const tableAction = unref(tableRef);
  149. if (!tableAction) {
  150. throw new Error('tableAction is null');
  151. }
  152. return tableAction;
  153. }
  154. function addGroupFn() {
  155. console.log('添加');
  156. popupData.title = '添加';
  157. popupData.group_ids = group_ids;
  158. openPopup(true, {});
  159. }
  160. async function ExpandAllRows() {
  161. await nextTick();
  162. toggleRowShow();
  163. }
  164. function toggleRowShow() {
  165. if (btn.btn_status) {
  166. expandAll();
  167. btn.btn_text = '折叠全部';
  168. btn.btn_status = false;
  169. } else {
  170. collapseAll();
  171. btn.btn_text = '展开全部';
  172. btn.btn_status = true;
  173. }
  174. }
  175. function handleEdit(record: EditRecordRow) {
  176. if (group_ids.includes(record.id)) {
  177. return;
  178. }
  179. // currentEditKeyRef.value = record.id; // record.key
  180. popupData.title = '编辑';
  181. getGroupById({ id: record.id }).then((res) => {
  182. const data = res.row;
  183. openPopup(true, data);
  184. });
  185. }
  186. async function handleDelete(record: Recordable) {
  187. console.log(record);
  188. await deleteGroup({ id: record.id }).then(() => {
  189. getTableAction().reload();
  190. ExpandAllRows();
  191. success('删除成功!');
  192. });
  193. }
  194. async function selectionChange() {
  195. const keys = await getTableAction().getSelectRowKeys();
  196. if (keys.length) {
  197. btn.disable_btn = false;
  198. } else {
  199. btn.disable_btn = true;
  200. }
  201. }
  202. async function rowClick(target) {
  203. const keys = await getTableAction().getSelectRowKeys();
  204. if (group_ids.includes(target.id)) {
  205. keys.splice(
  206. keys.findIndex((item) => item === target.id),
  207. 1
  208. );
  209. }
  210. if (keys.length) {
  211. btn.disable_btn = false;
  212. } else {
  213. btn.disable_btn = true;
  214. }
  215. }
  216. async function deleteBatches() {
  217. const keys = await getTableAction().getSelectRowKeys();
  218. const count = keys.length;
  219. const ids = keys.toString();
  220. if (!ids) {
  221. return;
  222. }
  223. Modal.confirm({
  224. title: '删除提示',
  225. icon: createVNode(ExclamationCircleOutlined),
  226. content: '确定删除选中的' + count + '项?',
  227. okText: '确定',
  228. okType: 'danger',
  229. cancelText: '取消',
  230. maskClosable: true,
  231. async onOk() {
  232. await deleteBatchesGroup({ ids }).then(() => {
  233. getTableAction().reload();
  234. ExpandAllRows();
  235. success('删除成功!');
  236. getTableAction().setSelectedRowKeys([]);
  237. });
  238. },
  239. onCancel() {
  240. console.log('Cancel');
  241. },
  242. });
  243. }
  244. async function saveData(params: any) {
  245. params.data.rules = params.data.rules.toString();
  246. const data = params.data;
  247. const closeModel = params.closeModal;
  248. console.log(`data`, data);
  249. if (!data.id) {
  250. await addGroup(data).then(() => {
  251. getTableAction().reload();
  252. closeModel();
  253. ExpandAllRows();
  254. success('创建成功!');
  255. });
  256. } else {
  257. await editGroup(data).then(() => {
  258. getTableAction().reload();
  259. closeModel();
  260. ExpandAllRows();
  261. success('修改成功!');
  262. });
  263. }
  264. }
  265. function createActions(record: EditRecordRow): ActionItem[] {
  266. if (record.pid === 0 || group_ids.includes(record.id)) {
  267. return [
  268. {
  269. label: '',
  270. icon: '',
  271. },
  272. ];
  273. } else {
  274. return [
  275. {
  276. label: '编辑',
  277. icon: 'ant-design:edit-outlined',
  278. color: 'warning',
  279. onClick: handleEdit.bind(null, record),
  280. },
  281. {
  282. label: '删除',
  283. color: 'error',
  284. icon: 'ic:outline-delete-outline',
  285. popConfirm: {
  286. title: '是否确认删除',
  287. confirm: handleDelete.bind(null, record),
  288. },
  289. },
  290. ];
  291. }
  292. }
  293. return {
  294. popupData,
  295. tableRef,
  296. registerTable,
  297. addGroupFn,
  298. handleEdit,
  299. deleteBatches,
  300. createActions,
  301. getTableAction,
  302. rowClick,
  303. selectionChange,
  304. toggleRowShow,
  305. addRegister,
  306. saveData,
  307. ExpandAllRows,
  308. ...toRefs(btn),
  309. };
  310. },
  311. });
  312. </script>
  313. <style scoped>
  314. .tool-btn-wrap {
  315. flex: 1;
  316. }
  317. .tool-btn-wrap {
  318. flex: 1;
  319. }
  320. .tool-btn-wrap button {
  321. margin-right: 5px;
  322. }
  323. </style>