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

238 lines
6.3 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';
2024-10-07 12:46:17 +08:00
import { getPopupContainer } 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 dayjs from 'dayjs';
import { useVbenVxeGrid, type VxeGridProps } from '#/adapter';
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-10-05 23:23:21 +08:00
import { downloadExcel } from '#/utils/file/download';
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,
},
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
// 区间选择器处理
if (formValues?.createTime) {
formValues.params = {
beginTime: dayjs(formValues.createTime[0]).format(
'YYYY-MM-DD 00:00:00',
),
endTime: dayjs(formValues.createTime[1]).format(
'YYYY-MM-DD 23:59:59',
),
};
Reflect.deleteProperty(formValues, 'createTime');
} else {
Reflect.deleteProperty(formValues, 'params');
}
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: (e: any) => {
checked.value = e.records.length > 0;
},
checkboxAll: (e: any) => {
checked.value = e.records.length > 0;
},
},
});
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
});
}
/**
* 与后台逻辑相同
* 只有超级管理员能访问租户相关
*/
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-05 23:23:21 +08:00
<BasicTable>
<template #toolbar-actions>
2024-10-07 12:50:35 +08:00
<span class="pl-[7px] text-[16px]">租户列表</span>
2024-10-05 23:23:21 +08:00
</template>
<template #toolbar-tools>
<Space>
<a-button
v-access:code="['system:tenant:export']"
2024-10-07 17:11:08 +08:00
@click="
downloadExcel(
tenantExport,
'租户数据',
tableApi.formApi.form.values,
)
"
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="getPopupContainer"
:title="`确认同步[${row.companyName}]的套餐吗?`"
placement="left"
@confirm="handleSync(row)"
2024-10-05 23:23:21 +08:00
>
2024-10-07 16:56:32 +08:00
<ghost-button 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="getPopupContainer"
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>