Jelajahi Sumber

feat: 租户

DESKTOP-USV654P\pc 9 bulan lalu
induk
melakukan
f6c79a5f15

+ 1 - 1
apps/web-baicai/src/views/system/role/data.config.ts

@@ -83,7 +83,7 @@ export function useColumns(
       headerAlign: 'center',
       showOverflow: false,
       title: '操作',
-      width: 100,
+      width: 170,
     },
   ];
 }

+ 8 - 6
apps/web-baicai/src/views/system/tenant/components/edit.vue

@@ -5,10 +5,10 @@ import { useVbenModal } from '@vben/common-ui';
 
 import { message } from 'ant-design-vue';
 
-import { useVbenForm } from '#/adapter/form';
+import { useFormOptions, useVbenForm } from '#/adapter';
 import { TenantApi } from '#/api';
 
-import { formOptions } from '../data.config';
+import { useSchema } from '../data.config';
 
 defineOptions({
   name: 'TenantEdit',
@@ -17,10 +17,12 @@ const emit = defineEmits(['success']);
 const modelRef = ref<Record<string, any>>({});
 const isUpdate = ref(true);
 
-const [Form, { validate, setValues, getValues }] = useVbenForm({
-  showDefaultActions: false,
-  ...formOptions,
-});
+const [Form, { validate, setValues, getValues }] = useVbenForm(
+  useFormOptions({
+    schema: useSchema(),
+    wrapperClass: 'grid-cols-2',
+  }),
+);
 
 const [Modal, { close, setState, getData }] = useVbenModal({
   fullscreenButton: false,

+ 57 - 48
apps/web-baicai/src/views/system/tenant/data.config.ts

@@ -1,11 +1,14 @@
-import type { VbenFormProps } from '#/adapter/form';
-import type { VxeGridProps } from '#/adapter/vxe-table';
+import type {
+  OnActionClickFn,
+  VbenFormSchema,
+  VxeTableGridOptions,
+} from '#/adapter';
 
 import { EnumApi, TenantApi } from '#/api';
-import { dbTypeOptions, formatterStatus } from '#/api/model';
+import { dbTypeOptions } from '#/api/model';
 
-export const searchFormOptions: VbenFormProps = {
-  schema: [
+export const useSearchSchema = (): VbenFormSchema[] => {
+  return [
     {
       component: 'Input',
       fieldName: 'name',
@@ -16,22 +19,13 @@ export const searchFormOptions: VbenFormProps = {
       fieldName: 'phone',
       label: '联系人',
     },
-  ],
+  ];
 };
 
-export const gridOptions: VxeGridProps<TenantApi.RecordItem> = {
-  checkboxConfig: {
-    highlight: true,
-    labelField: 'name',
-  },
-  toolbarConfig: {
-    refresh: true,
-    print: false,
-    export: false,
-    zoom: true,
-    custom: true,
-  },
-  columns: [
+export function useColumns(
+  onActionClick?: OnActionClickFn<TenantApi.RecordItem>,
+): VxeTableGridOptions<TenantApi.RecordItem>['columns'] {
+  return [
     { title: '序号', type: 'seq', width: 50 },
     {
       align: 'left',
@@ -46,41 +40,57 @@ export const gridOptions: VxeGridProps<TenantApi.RecordItem> = {
     {
       field: 'status',
       title: '状态',
-      width: 60,
-      formatter: formatterStatus,
+      width: 82,
+      cellRender: { name: 'CellTag' },
     },
     { field: 'sort', title: '排序', width: 50 },
     { align: 'left', field: 'remark', title: '备注' },
     {
-      field: 'action',
+      align: 'right',
+      cellRender: {
+        attrs: {
+          nameField: 'name',
+          nameTitle: '租户名称',
+          onClick: onActionClick,
+        },
+        name: 'CellAction',
+        options: [
+          {
+            code: 'createDb',
+            label: '创建库',
+            auth: ['tenant:createDb'],
+            confirm: {
+              title: '提示',
+              content: `确定创建/更新租户数据库?`,
+            },
+          },
+          {
+            code: 'edit',
+            auth: ['role:edit'],
+          },
+          {
+            code: 'resetPwd',
+            label: '重置密码',
+            auth: ['tenant:resetPwd'],
+          },
+          {
+            code: 'delete',
+            auth: ['tenant:delete'],
+          },
+        ],
+      },
+      field: 'operation',
       fixed: 'right',
-      slots: { default: 'action' },
+      headerAlign: 'center',
+      showOverflow: false,
       title: '操作',
-      width: 150,
-    },
-  ],
-  height: 'auto',
-  keepSource: true,
-  proxyConfig: {
-    ajax: {
-      query: async ({ page }, formValues) => {
-        return await TenantApi.getPage({
-          pageIndex: page.currentPage,
-          pageSize: page.pageSize,
-          ...formValues,
-        });
-      },
+      width: 100,
     },
-  },
-};
+  ];
+}
 
-export const formOptions: VbenFormProps = {
-  commonConfig: {
-    componentProps: {
-      class: 'w-full',
-    },
-  },
-  schema: [
+export const useSchema = (): VbenFormSchema[] => {
+  return [
     {
       component: 'ApiRadio',
       componentProps: {
@@ -180,6 +190,5 @@ export const formOptions: VbenFormProps = {
       label: '备注',
       formItemClass: 'col-span-2 items-baseline',
     },
-  ],
-  wrapperClass: 'grid-cols-2',
+  ];
 };

+ 61 - 68
apps/web-baicai/src/views/system/tenant/index.vue

@@ -1,52 +1,29 @@
 <script lang="ts" setup>
-import { useAccess } from '@vben/access';
+import type { OnActionClickParams, VxeTableGridOptions } from '#/adapter';
+
 import { Page, useVbenModal } from '@vben/common-ui';
 
-import { Button, message, Modal } from 'ant-design-vue';
+import { Button, message } from 'ant-design-vue';
 
-import { useVbenVxeGrid } from '#/adapter/vxe-table';
+import { useTableGridOptions, useVbenVxeGrid } from '#/adapter';
 import { TenantApi } from '#/api';
-import { TableAction } from '#/components/table-action';
 
 import FormEdit from './components/edit.vue';
-import { gridOptions, searchFormOptions } from './data.config';
-
-const { hasAccessByCodes } = useAccess();
-
-const [Grid, { reload }] = useVbenVxeGrid({
-  formOptions: searchFormOptions,
-  gridOptions,
-});
+import { useColumns, useSearchSchema } from './data.config';
 
 const [FormEditModal, formEditApi] = useVbenModal({
   connectedComponent: FormEdit,
 });
 
-const handleCreateDb = (id: number) => {
-  Modal.confirm({
-    iconType: 'info',
-    title: '提示',
-    content: `确定创建/更新租户数据库?`,
-    cancelText: `关闭`,
-    onOk: async () => {
-      await TenantApi.createDb(id);
-      message.success('创建/更新租户数据库成功');
-    },
-  });
+const handleCreateDb = async (id: number) => {
+  await TenantApi.createDb(id);
+  message.success('创建/更新租户数据库成功');
 };
 
-const handleDelete = (id: number) => {
-  Modal.confirm({
-    iconType: 'info',
-    title: '删除提示',
-    content: `确定要删除选择的记录吗?`,
-    cancelText: `关闭`,
-    onOk: async () => {
-      await TenantApi.deleteDetail(id);
-      message.success('数据删除成功');
-      reload();
-    },
-  });
+const handleDelete = async (id: number) => {
+  await TenantApi.deleteDetail(id);
+  message.success('数据删除成功');
+  reload();
 };
 
 const handleEdit = (record: any, isUpdate: boolean) => {
@@ -61,6 +38,55 @@ const handleEdit = (record: any, isUpdate: boolean) => {
 const handelSuccess = () => {
   reload();
 };
+
+const handleActionClick = async ({
+  code,
+  row,
+}: OnActionClickParams<TenantApi.RecordItem>) => {
+  switch (code) {
+    case 'createDb': {
+      await handleCreateDb(row.id);
+      break;
+    }
+    case 'delete': {
+      await handleDelete(row.id);
+      break;
+    }
+    case 'edit': {
+      handleEdit(row, true);
+      break;
+    }
+    case 'resetPwd': {
+      break;
+    }
+  }
+};
+
+const [Grid, { reload }] = useVbenVxeGrid(
+  useTableGridOptions({
+    formOptions: {
+      schema: useSearchSchema(),
+    },
+    gridOptions: {
+      checkboxConfig: {
+        highlight: true,
+        labelField: 'name',
+      },
+      columns: useColumns(handleActionClick),
+      proxyConfig: {
+        ajax: {
+          query: async ({ page }, formValues) => {
+            return await TenantApi.getPage({
+              pageIndex: page.currentPage,
+              pageSize: page.pageSize,
+              ...formValues,
+            });
+          },
+        },
+      },
+    } as VxeTableGridOptions,
+  }),
+);
 </script>
 
 <template>
@@ -77,39 +103,6 @@ const handelSuccess = () => {
           新增租户
         </Button>
       </template>
-      <template #action="{ row }">
-        <TableAction
-          :actions="[
-            {
-              label: '创建库',
-              type: 'text',
-              disabled:
-                !hasAccessByCodes(['tenant:createDb']) || row.tenantType === 0,
-              onClick: handleCreateDb.bind(null, row),
-            },
-            {
-              label: '编辑',
-              type: 'text',
-              danger: true,
-              disabled: !hasAccessByCodes(['tenant:edit']),
-              onClick: handleEdit.bind(null, row, true),
-            },
-          ]"
-          :drop-down-actions="[
-            {
-              label: '重置密码',
-              type: 'link',
-              disabled: !hasAccessByCodes(['tenant:resetPwd']),
-            },
-            {
-              label: '删除',
-              type: 'link',
-              disabled: !hasAccessByCodes(['tenant:delete']),
-              onClick: handleDelete.bind(null, row.id),
-            },
-          ]"
-        />
-      </template>
     </Grid>
   </Page>
 </template>