index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <script lang="ts" setup>
  2. import type { OnActionClickParams, VxeTableGridOptions } from '#/adapter';
  3. import { reactive } from 'vue';
  4. import { BcPage } from '@vben/baicai-ui';
  5. import { confirm, useVbenModal } from '@vben/common-ui';
  6. import { useClipboard } from '@vueuse/core';
  7. import { Button, message } from 'ant-design-vue';
  8. import { useTableGridOptions, useVbenVxeGrid } from '#/adapter';
  9. import { DepartmentApi, RoleApi, UserApi } from '#/api';
  10. import { BcTree } from '#/components/bc-tree';
  11. import { SelectCard } from '#/components/select-card';
  12. import {
  13. SystemUserDelete,
  14. SystemUserGrant,
  15. SystemUserResetPassword,
  16. SystemUserStatus,
  17. } from '#/service/apis/System/UserController';
  18. import FormEdit from './components/edit.vue';
  19. import { useColumns, useSearchSchema } from './data.config';
  20. const { copy } = useClipboard({ legacy: true });
  21. const searchInfo = reactive<Record<string, any>>({});
  22. const [FormEditModal, formEditApi] = useVbenModal({
  23. connectedComponent: FormEdit,
  24. });
  25. const [SelectCardModal, selectCardApi] = useVbenModal({
  26. connectedComponent: SelectCard,
  27. });
  28. const handelResetPassword = async (id: number) => {
  29. const data = await SystemUserResetPassword([id]);
  30. confirm({
  31. title: '重置成功',
  32. content: `新密码为:${data}`,
  33. confirmText: '复制密码',
  34. beforeClose: ({ isConfirm }: { isConfirm: boolean }) => {
  35. if (isConfirm) {
  36. copy(data);
  37. message.success('密码复制成功!');
  38. }
  39. return true;
  40. },
  41. });
  42. };
  43. const handleDelete = async (id: string) => {
  44. await SystemUserDelete({ id });
  45. message.success('数据删除成功');
  46. reload();
  47. };
  48. const handleUpdateStatus = async (record: any) => {
  49. await SystemUserStatus({
  50. id: record.id,
  51. status: record.status === 1 ? 2 : 1,
  52. });
  53. reload();
  54. };
  55. const handleEdit = (record: any, isUpdate: boolean) => {
  56. formEditApi
  57. .setData({
  58. isUpdate,
  59. baseData: { id: record.id },
  60. })
  61. .open();
  62. };
  63. const handelGrantRole = async (id: number) => {
  64. const selectedIds = await UserApi.getRoleIds(id);
  65. selectCardApi
  66. .setData({
  67. baseData: {
  68. key: id,
  69. selectedIds,
  70. fieldNames: [
  71. { label: '编号', value: 'code', maxLength: 12 },
  72. { label: '名称', value: 'name', maxLength: 8 },
  73. ],
  74. config: {
  75. type: 'role',
  76. },
  77. api: {
  78. url: RoleApi.getPage,
  79. },
  80. searchName: 'name',
  81. },
  82. })
  83. .open();
  84. };
  85. const handelSuccess = () => {
  86. reload();
  87. };
  88. const handelRoleSuccess = async (data: any) => {
  89. await SystemUserGrant({ id: data.key, relationIds: data.ids });
  90. message.success('授权成功');
  91. };
  92. const handleActionClick = async ({
  93. code,
  94. row,
  95. }: OnActionClickParams<UserApi.RecordItem>) => {
  96. switch (code) {
  97. case 'delete': {
  98. await handleDelete(row.id);
  99. break;
  100. }
  101. case 'edit': {
  102. handleEdit(row, true);
  103. break;
  104. }
  105. case 'grantRole': {
  106. handelGrantRole(row.id);
  107. break;
  108. }
  109. case 'resetPwd': {
  110. handelResetPassword(row.id);
  111. break;
  112. }
  113. case 'setStatus': {
  114. await handleUpdateStatus(row);
  115. break;
  116. }
  117. }
  118. };
  119. const [Grid, { reload }] = useVbenVxeGrid(
  120. useTableGridOptions({
  121. formOptions: {
  122. schema: useSearchSchema(),
  123. },
  124. gridOptions: {
  125. columns: useColumns(handleActionClick),
  126. proxyConfig: {
  127. ajax: {
  128. query: async ({ page }, formValues) => {
  129. return await UserApi.getPage({
  130. pageIndex: page.currentPage,
  131. pageSize: page.pageSize,
  132. ...formValues,
  133. ...searchInfo,
  134. });
  135. },
  136. },
  137. },
  138. } as VxeTableGridOptions,
  139. }),
  140. );
  141. const handleDepartmentChange = (keys: any[]) => {
  142. searchInfo.departmentId = keys[0];
  143. reload();
  144. };
  145. </script>
  146. <template>
  147. <BcPage auto-content-height :default-layout="[20, 80, 0]">
  148. <template #left>
  149. <BcTree
  150. title="组织架构"
  151. :api="{ url: DepartmentApi.getTree, type: 'api' }"
  152. :field-names="{ label: 'name', value: 'id' }"
  153. @change="handleDepartmentChange"
  154. />
  155. </template>
  156. <FormEditModal @success="handelSuccess" />
  157. <SelectCardModal title="角色" @success="handelRoleSuccess" />
  158. <Grid>
  159. <template #table-title>
  160. <span class="border-l-primary border-l-8 border-solid pl-2">
  161. 用户列表
  162. </span>
  163. </template>
  164. <template #toolbar-tools>
  165. <Button
  166. class="mr-2"
  167. type="primary"
  168. v-access:code="'user:add'"
  169. @click="() => handleEdit({}, false)"
  170. >
  171. 新增用户
  172. </Button>
  173. </template>
  174. </Grid>
  175. </BcPage>
  176. </template>