admin-vben5/apps/web-antd/src/views/system/tenant/index.vue

224 lines
5.8 KiB
Vue
Raw Normal View History

2024-08-07 08:57:56 +08:00
<script setup lang="ts">
2024-10-05 23:23:21 +08:00
import type { Recordable } from '@vben/types';
2024-09-18 11:45:52 +08:00
import { computed, ref } from 'vue';
2024-09-24 11:23:02 +08:00
2024-10-05 23:23:21 +08:00
import { useAccess } from '@vben/access';
import { Page, useVbenDrawer, type VbenFormProps } from '@vben/common-ui';
import { Fallback } from '@vben/common-ui';
import { getVxePopupContainer } from '@vben/utils';
2024-09-24 11:23:02 +08:00
2024-10-05 23:23:21 +08:00
import { Modal, Popconfirm, Space } from 'ant-design-vue';
import {
tableCheckboxEvent,
useVbenVxeGrid,
type VxeGridProps,
} from '#/adapter/vxe-table';
2024-10-05 23:23:21 +08:00
import {
tenantExport,
tenantList,
tenantRemove,
tenantStatusChange,
2024-10-06 16:50:30 +08:00
tenantSyncPackage,
2024-10-05 23:23:21 +08:00
} from '#/api/system/tenant';
import { TableSwitch } from '#/components/table';
2024-10-06 10:28:33 +08:00
import { useTenantStore } from '#/store/tenant';
2024-11-14 14:58:42 +08:00
import { commonDownloadExcel } from '#/utils/file/download';
2024-10-05 23:23:21 +08:00
import { columns, querySchema } from './data';
2024-09-18 11:45:52 +08:00
import tenantDrawer from './tenant-drawer.vue';
2024-10-05 23:23:21 +08:00
const formOptions: VbenFormProps = {
commonConfig: {
labelWidth: 80,
componentProps: {
allowClear: true,
},
2024-10-05 23:23:21 +08:00
},
schema: querySchema(),
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};
const gridOptions: VxeGridProps = {
checkboxConfig: {
// 高亮
highlight: true,
// 翻页时保留选中状态
reserve: true,
// 点击行选中
// trigger: 'row',
2024-10-07 12:50:35 +08:00
checkMethod: ({ row }) => row?.id !== 1,
2024-10-05 23:23:21 +08:00
},
columns,
height: 'auto',
keepSource: true,
pagerConfig: {},
proxyConfig: {
ajax: {
2024-10-06 09:53:00 +08:00
query: async ({ page }, formValues = {}) => {
2024-10-05 23:23:21 +08:00
return await tenantList({
pageNum: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
rowConfig: {
isHover: true,
keyField: 'id',
},
id: 'system-tenant-index',
2024-10-05 23:23:21 +08:00
};
const checked = ref(false);
const [BasicTable, tableApi] = useVbenVxeGrid({
formOptions,
gridOptions,
gridEvents: {
checkboxChange: tableCheckboxEvent(checked),
checkboxAll: tableCheckboxEvent(checked),
2024-10-05 23:23:21 +08:00
},
});
2024-09-18 11:45:52 +08:00
const [TenantDrawer, drawerApi] = useVbenDrawer({
connectedComponent: tenantDrawer,
});
function handleAdd() {
2024-10-05 23:23:21 +08:00
drawerApi.setData({});
2024-09-18 11:45:52 +08:00
drawerApi.open();
}
2024-09-24 11:23:02 +08:00
2024-10-05 23:23:21 +08:00
async function handleEdit(record: Recordable<any>) {
drawerApi.setData({ id: record.id });
drawerApi.open();
}
2024-10-06 16:50:30 +08:00
async function handleSync(record: Recordable<any>) {
const { tenantId, packageId } = record;
await tenantSyncPackage(tenantId, packageId);
await tableApi.query();
}
2024-10-06 10:28:33 +08:00
const tenantStore = useTenantStore();
2024-10-05 23:23:21 +08:00
async function handleDelete(row: Recordable<any>) {
await tenantRemove(row.id);
await tableApi.query();
2024-10-06 10:28:33 +08:00
// 重新加载租户信息
tenantStore.initTenant();
2024-10-05 23:23:21 +08:00
}
function handleMultiDelete() {
const rows = tableApi.grid.getCheckboxRecords();
const ids = rows.map((row: any) => row.id);
Modal.confirm({
title: '提示',
okType: 'danger',
content: `确认删除选中的${ids.length}条记录吗?`,
onOk: async () => {
await tenantRemove(ids);
await tableApi.query();
checked.value = false;
2024-10-06 10:28:33 +08:00
// 重新加载租户信息
tenantStore.initTenant();
2024-09-24 11:23:02 +08:00
},
2024-10-05 23:23:21 +08:00
});
}
2024-11-14 14:58:42 +08:00
function handleDownloadExcel() {
commonDownloadExcel(tenantExport, '租户数据', tableApi.formApi.form.values);
}
/**
* 与后台逻辑相同
* 只有超级管理员能访问租户相关
*/
const { hasAccessByCodes, hasAccessByRoles } = useAccess();
const isSuperAdmin = computed(() => {
return hasAccessByRoles(['superadmin']);
});
2024-08-07 08:57:56 +08:00
</script>
<template>
<Page v-if="isSuperAdmin" :auto-content-height="true">
2024-10-25 08:09:53 +08:00
<BasicTable table-title="租户列表">
2024-10-05 23:23:21 +08:00
<template #toolbar-tools>
<Space>
<a-button
v-access:code="['system:tenant:export']"
2024-11-14 14:58:42 +08:00
@click="handleDownloadExcel"
2024-10-05 23:23:21 +08:00
>
{{ $t('pages.common.export') }}
</a-button>
<a-button
:disabled="!checked"
danger
type="primary"
v-access:code="['system:tenant:remove']"
@click="handleMultiDelete"
>
{{ $t('pages.common.delete') }}
</a-button>
<a-button
type="primary"
v-access:code="['system:tenant:add']"
@click="handleAdd"
>
{{ $t('pages.common.add') }}
</a-button>
</Space>
</template>
<template #status="{ row }">
<TableSwitch
v-model="row.status"
:api="() => tenantStatusChange(row)"
:disabled="row.id === 1 || !hasAccessByCodes(['system:tenant:edit'])"
:reload="() => tableApi.query()"
/>
</template>
<template #action="{ row }">
2024-10-07 16:56:32 +08:00
<Space v-if="row.id !== 1">
<ghost-button
2024-10-06 16:50:30 +08:00
v-access:code="['system:tenant:edit']"
2024-10-07 12:46:17 +08:00
@click="handleEdit(row)"
2024-10-06 16:50:30 +08:00
>
2024-10-07 12:46:17 +08:00
{{ $t('pages.common.edit') }}
2024-10-07 16:56:32 +08:00
</ghost-button>
2024-10-07 12:46:17 +08:00
<Popconfirm
:get-popup-container="getVxePopupContainer"
2024-10-07 12:46:17 +08:00
:title="`确认同步[${row.companyName}]的套餐吗?`"
placement="left"
@confirm="handleSync(row)"
2024-10-05 23:23:21 +08:00
>
2024-10-30 15:27:39 +08:00
<ghost-button
class="btn-success"
v-access:code="['system:tenant:edit']"
>
2024-10-07 12:46:17 +08:00
{{ $t('pages.common.sync') }}
2024-10-07 16:56:32 +08:00
</ghost-button>
2024-10-07 12:46:17 +08:00
</Popconfirm>
<Popconfirm
:get-popup-container="getVxePopupContainer"
2024-10-07 12:46:17 +08:00
placement="left"
title="确认删除?"
@confirm="handleDelete(row)"
>
2024-10-07 16:56:32 +08:00
<ghost-button
2024-10-07 12:46:17 +08:00
danger
v-access:code="['system:tenant:remove']"
@click.stop=""
>
{{ $t('pages.common.delete') }}
2024-10-07 16:56:32 +08:00
</ghost-button>
2024-10-07 12:46:17 +08:00
</Popconfirm>
2024-10-07 16:56:32 +08:00
</Space>
2024-10-05 23:23:21 +08:00
</template>
</BasicTable>
<TenantDrawer @reload="tableApi.query()" />
2024-09-18 11:45:52 +08:00
</Page>
<Fallback v-else description="您没有租户的访问权限" status="403" />
2024-08-07 08:57:56 +08:00
</template>