index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. rowKey="id"
  17. >
  18. <template #toolbar>
  19. <a-button type="primary" @click="toggleRowShow">{{ btn_text }}</a-button>
  20. <a-button type="primary" @click="addGroupFn"> 添加 </a-button>
  21. <a-button color="error" :disabled="disable_btn" @click="deleteBatches"> 删除 </a-button>
  22. <a-button @click="openModal"> 导出 </a-button>
  23. </template>
  24. <template #action="{ record, column }">
  25. <TableAction :actions="createActions(record, column)" stopButtonPropagation />
  26. </template>
  27. </BasicTable>
  28. <ExpExcelModel @register="register" @success="defaultHeader" />
  29. <Popup @register="addRegister" :popupData="popupData" @saveData="saveData" />
  30. </CollapseContainer>
  31. </template>
  32. <script lang="ts">
  33. import { defineComponent, reactive, ref, toRefs, unref, nextTick, createVNode } from 'vue';
  34. import { CollapseContainer } from '/@/components/Container/index';
  35. import Popup from './popup.vue';
  36. import { useModal } from '/@/components/Modal';
  37. import { columns } from './data';
  38. import { useUserStore } from '/@/store/modules/user';
  39. import { useMessage } from '/@/hooks/web/useMessage';
  40. import { jsonToSheetXlsx, ExpExcelModel, ExportModalResult } from '/@/components/Excel';
  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. components: { CollapseContainer, BasicTable, TableAction, Popup, ExpExcelModel },
  75. setup() {
  76. const userStore = useUserStore();
  77. const groups = userStore.getUserInfo.groups as Group[];
  78. const group_ids = groups.map((item) => item.id);
  79. const { createMessage } = useMessage();
  80. const { success /*, error */ } = createMessage;
  81. const tableRef = ref<Nullable<TableActionType>>(null);
  82. const popupData = reactive<PopupData>({
  83. title: '添加',
  84. treeData: [{}],
  85. group_ids: [],
  86. });
  87. const btn = reactive<Btn>({
  88. btn_text: '展开全部',
  89. btn_status: true,
  90. disable_btn: true,
  91. });
  92. const [registerTable, { expandAll, collapseAll }] = useTable({
  93. rowSelection: {
  94. type: 'checkbox',
  95. getCheckboxProps: (record) => ({
  96. disabled: group_ids.includes(record.id), // Column configuration not to be checked
  97. }),
  98. },
  99. columns: columns,
  100. // clickToRowSelect: false, // 点击行不勾选
  101. api: getGroupTree,
  102. isTreeTable: true,
  103. pagination: false, // 树列表不显示分页
  104. afterFetch: afterFetch,
  105. actionColumn: {
  106. width: 160,
  107. title: '操作',
  108. dataIndex: 'action',
  109. slots: { customRender: 'action' },
  110. fixed: undefined,
  111. },
  112. showIndexColumn: false,
  113. bordered: true,
  114. });
  115. const [register, { openModal }] = useModal();
  116. const [addRegister, { openModal: openPopup }] = useModal();
  117. function afterFetch(result) {
  118. popupData.treeData = result;
  119. console.log(`result`, result);
  120. }
  121. function getTableAction() {
  122. // 获取组件
  123. const tableAction = unref(tableRef);
  124. if (!tableAction) {
  125. throw new Error('tableAction is null');
  126. }
  127. return tableAction;
  128. }
  129. function addGroupFn() {
  130. console.log('添加');
  131. popupData.title = '添加';
  132. popupData.group_ids = group_ids;
  133. openPopup(true, {});
  134. }
  135. async function ExpandAllRows() {
  136. await nextTick();
  137. toggleRowShow();
  138. }
  139. function toggleRowShow() {
  140. if (btn.btn_status) {
  141. expandAll();
  142. btn.btn_text = '折叠全部';
  143. btn.btn_status = false;
  144. } else {
  145. collapseAll();
  146. btn.btn_text = '展开全部';
  147. btn.btn_status = true;
  148. }
  149. }
  150. function handleEdit(record: EditRecordRow) {
  151. if (group_ids.includes(record.id)) {
  152. return;
  153. }
  154. // currentEditKeyRef.value = record.id; // record.key
  155. popupData.title = '编辑';
  156. getGroupById({ id: record.id }).then((res) => {
  157. const data = res.row;
  158. openPopup(true, data);
  159. });
  160. }
  161. async function handleDelete(record: Recordable) {
  162. console.log(record);
  163. await deleteGroup({ id: record.id }).then(() => {
  164. getTableAction().reload();
  165. ExpandAllRows();
  166. success('删除成功!');
  167. });
  168. }
  169. async function selectionChange() {
  170. const keys = await getTableAction().getSelectRowKeys();
  171. if (keys.length) {
  172. btn.disable_btn = false;
  173. } else {
  174. btn.disable_btn = true;
  175. }
  176. }
  177. async function rowClick(target) {
  178. const keys = await getTableAction().getSelectRowKeys();
  179. if (group_ids.includes(target.id)) {
  180. keys.splice(
  181. keys.findIndex((item) => item === target.id),
  182. 1
  183. );
  184. }
  185. if (keys.length) {
  186. btn.disable_btn = false;
  187. } else {
  188. btn.disable_btn = true;
  189. }
  190. }
  191. async function deleteBatches() {
  192. const keys = await getTableAction().getSelectRowKeys();
  193. const count = keys.length;
  194. const ids = keys.toString();
  195. if (!ids) {
  196. return;
  197. }
  198. Modal.confirm({
  199. title: '删除提示',
  200. icon: createVNode(ExclamationCircleOutlined),
  201. content: '确定删除选中的' + count + '项?',
  202. okText: '确定',
  203. okType: 'danger',
  204. cancelText: '取消',
  205. maskClosable: true,
  206. async onOk() {
  207. await deleteBatchesGroup({ ids }).then(() => {
  208. getTableAction().reload();
  209. ExpandAllRows();
  210. success('删除成功!');
  211. getTableAction().setSelectedRowKeys([]);
  212. });
  213. },
  214. onCancel() {
  215. console.log('Cancel');
  216. },
  217. });
  218. }
  219. async function saveData(params: any) {
  220. console.log('------------save---------');
  221. params.data.rules = params.data.rules.toString();
  222. const data = params.data;
  223. const closeModel = params.closeModal;
  224. console.log(`data`, data);
  225. if (!data.id) {
  226. await addGroup(data).then(() => {
  227. getTableAction().reload();
  228. closeModel();
  229. ExpandAllRows();
  230. success('创建成功!');
  231. });
  232. } else {
  233. await editGroup(data).then(() => {
  234. getTableAction().reload();
  235. closeModel();
  236. ExpandAllRows();
  237. success('修改成功!');
  238. });
  239. }
  240. }
  241. // 导出
  242. function defaultHeader({ filename, bookType }: ExportModalResult) {
  243. // 默认Object.keys(data[0])作为header
  244. jsonToSheetXlsx({
  245. data: getTableAction().getDataSource(),
  246. filename,
  247. write2excelOpts: {
  248. bookType,
  249. },
  250. });
  251. }
  252. function createActions(record: EditRecordRow): ActionItem[] {
  253. if (record.pid === 0 || group_ids.includes(record.id)) {
  254. return [
  255. {
  256. label: '',
  257. icon: '',
  258. },
  259. ];
  260. } else {
  261. return [
  262. {
  263. label: '编辑',
  264. icon: 'ant-design:edit-outlined',
  265. color: 'warning',
  266. onClick: handleEdit.bind(null, record),
  267. },
  268. {
  269. label: '删除',
  270. color: 'error',
  271. icon: 'ic:outline-delete-outline',
  272. popConfirm: {
  273. title: '是否确认删除',
  274. confirm: handleDelete.bind(null, record),
  275. },
  276. },
  277. ];
  278. }
  279. }
  280. return {
  281. popupData,
  282. tableRef,
  283. registerTable,
  284. addGroupFn,
  285. handleEdit,
  286. deleteBatches,
  287. createActions,
  288. getTableAction,
  289. rowClick,
  290. selectionChange,
  291. toggleRowShow,
  292. addRegister,
  293. saveData,
  294. ExpandAllRows,
  295. defaultHeader,
  296. openModal,
  297. register,
  298. ...toRefs(btn),
  299. };
  300. },
  301. });
  302. </script>