feat: 添加租户创建功能,支持设置管理员及有效期,新增订单统计接口
This commit is contained in:
8
frontend/superadmin/src/service/OrderService.js
Normal file
8
frontend/superadmin/src/service/OrderService.js
Normal file
@@ -0,0 +1,8 @@
|
||||
import { requestJson } from './apiClient';
|
||||
|
||||
export const OrderService = {
|
||||
async getOrderStatistics() {
|
||||
return requestJson('/super/v1/orders/statistics');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -22,6 +22,17 @@ export const TenantService = {
|
||||
items: normalizeItems(data?.items)
|
||||
};
|
||||
},
|
||||
async createTenant({ code, name, admin_user_id, duration } = {}) {
|
||||
return requestJson('/super/v1/tenants', {
|
||||
method: 'POST',
|
||||
body: {
|
||||
code,
|
||||
name,
|
||||
admin_user_id,
|
||||
duration
|
||||
}
|
||||
});
|
||||
},
|
||||
async renewTenantExpire({ tenantID, duration }) {
|
||||
return requestJson(`/super/v1/tenants/${tenantID}`, {
|
||||
method: 'PATCH',
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<script setup>
|
||||
import StatisticsStrip from '@/components/StatisticsStrip.vue';
|
||||
import { OrderService } from '@/service/OrderService';
|
||||
import { TenantService } from '@/service/TenantService';
|
||||
import { UserService } from '@/service/UserService';
|
||||
import { useToast } from 'primevue/usetoast';
|
||||
@@ -13,6 +14,15 @@ const tenantLoading = ref(false);
|
||||
const statistics = ref([]);
|
||||
const statisticsLoading = ref(false);
|
||||
|
||||
const orderStats = ref(null);
|
||||
const orderStatsLoading = ref(false);
|
||||
|
||||
function formatCny(amountInCents) {
|
||||
const amount = Number(amountInCents) / 100;
|
||||
if (!Number.isFinite(amount)) return '-';
|
||||
return new Intl.NumberFormat('zh-CN', { style: 'currency', currency: 'CNY' }).format(amount);
|
||||
}
|
||||
|
||||
const statisticsItems = computed(() => {
|
||||
const total = (statistics.value || []).reduce((sum, row) => sum + (Number(row?.count) || 0), 0);
|
||||
const statusIcon = (status) => {
|
||||
@@ -59,6 +69,26 @@ const tenantItems = computed(() => [
|
||||
}
|
||||
]);
|
||||
|
||||
const orderItems = computed(() => {
|
||||
const byStatus = new Map((orderStats.value?.by_status || []).map((row) => [row?.status, row]));
|
||||
const getCount = (status) => byStatus.get(status)?.count ?? 0;
|
||||
|
||||
const totalCount = orderStats.value?.total_count ?? 0;
|
||||
const totalAmountPaidSum = orderStats.value?.total_amount_paid_sum ?? 0;
|
||||
const refundingCount = getCount('refunding');
|
||||
const refundedCount = getCount('refunded');
|
||||
const refundRequestedCount = refundingCount + refundedCount;
|
||||
|
||||
return [
|
||||
{ key: 'orders-total', label: '订单总数:', value: orderStatsLoading.value ? '-' : totalCount, icon: 'pi-shopping-cart' },
|
||||
{ key: 'orders-paid', label: '已支付:', value: orderStatsLoading.value ? '-' : getCount('paid'), icon: 'pi-check-circle' },
|
||||
{ key: 'orders-refunding', label: '退款中:', value: orderStatsLoading.value ? '-' : refundingCount, icon: 'pi-spin pi-spinner' },
|
||||
{ key: 'orders-refund-requested', label: '退款申请:', value: orderStatsLoading.value ? '-' : refundRequestedCount, icon: 'pi-undo' },
|
||||
{ key: 'orders-refunded', label: '已退款:', value: orderStatsLoading.value ? '-' : getCount('refunded'), icon: 'pi-undo' },
|
||||
{ key: 'orders-amount', label: '实付总额:', value: orderStatsLoading.value ? '-' : formatCny(totalAmountPaidSum), icon: 'pi-wallet' }
|
||||
];
|
||||
});
|
||||
|
||||
async function loadTenantTotal() {
|
||||
tenantLoading.value = true;
|
||||
try {
|
||||
@@ -82,15 +112,28 @@ async function loadStatistics() {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadOrderStatistics() {
|
||||
orderStatsLoading.value = true;
|
||||
try {
|
||||
orderStats.value = await OrderService.getOrderStatistics();
|
||||
} catch (error) {
|
||||
toast.add({ severity: 'error', summary: '加载失败', detail: error?.message || '无法加载订单统计信息', life: 4000 });
|
||||
} finally {
|
||||
orderStatsLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
loadTenantTotal();
|
||||
loadStatistics();
|
||||
loadOrderStatistics();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<StatisticsStrip :items="tenantItems" containerClass="card mb-4" />
|
||||
<StatisticsStrip :items="orderItems" containerClass="card mb-4" />
|
||||
<StatisticsStrip :items="statisticsItems" containerClass="card mb-4" />
|
||||
|
||||
<div class="card">
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
<script setup>
|
||||
import { TenantService } from '@/service/TenantService';
|
||||
import { UserService } from '@/service/UserService';
|
||||
import SearchField from '@/components/SearchField.vue';
|
||||
import SearchPanel from '@/components/SearchPanel.vue';
|
||||
import { useToast } from 'primevue/usetoast';
|
||||
import { onMounted, ref } from 'vue';
|
||||
import { computed, onMounted, ref } from 'vue';
|
||||
|
||||
const toast = useToast();
|
||||
|
||||
@@ -19,6 +20,17 @@ const status = ref('');
|
||||
const sortField = ref('id');
|
||||
const sortOrder = ref(-1);
|
||||
|
||||
const createDialogVisible = ref(false);
|
||||
const creating = ref(false);
|
||||
const createCode = ref('');
|
||||
const createName = ref('');
|
||||
const createDuration = ref(365);
|
||||
const createAdminUserID = ref(null);
|
||||
|
||||
const adminSearchUsername = ref('');
|
||||
const adminSearchLoading = ref(false);
|
||||
const adminSearchResults = ref([]);
|
||||
|
||||
function formatDate(value) {
|
||||
if (!value) return '-';
|
||||
const date = new Date(value);
|
||||
@@ -123,6 +135,63 @@ const durationOptions = [
|
||||
{ label: '365 天', value: 365 }
|
||||
];
|
||||
|
||||
const canCreateTenant = computed(() => {
|
||||
return Boolean(createCode.value?.trim()) && Boolean(createName.value?.trim()) && Number(createAdminUserID.value) > 0 && Number(createDuration.value) > 0;
|
||||
});
|
||||
|
||||
function openCreateDialog() {
|
||||
createCode.value = '';
|
||||
createName.value = '';
|
||||
createDuration.value = 365;
|
||||
createAdminUserID.value = null;
|
||||
adminSearchUsername.value = '';
|
||||
adminSearchResults.value = [];
|
||||
createDialogVisible.value = true;
|
||||
}
|
||||
|
||||
async function searchAdminUsers() {
|
||||
const keyword = String(adminSearchUsername.value || '').trim();
|
||||
if (!keyword) {
|
||||
adminSearchResults.value = [];
|
||||
return;
|
||||
}
|
||||
|
||||
adminSearchLoading.value = true;
|
||||
try {
|
||||
const result = await UserService.listUsers({ page: 1, limit: 10, username: keyword });
|
||||
adminSearchResults.value = result?.items || [];
|
||||
} catch (error) {
|
||||
toast.add({ severity: 'error', summary: '搜索失败', detail: error?.message || '无法搜索用户', life: 4000 });
|
||||
} finally {
|
||||
adminSearchLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function pickAdmin(user) {
|
||||
createAdminUserID.value = user?.id ?? null;
|
||||
}
|
||||
|
||||
async function confirmCreateTenant() {
|
||||
if (!canCreateTenant.value) return;
|
||||
|
||||
creating.value = true;
|
||||
try {
|
||||
await TenantService.createTenant({
|
||||
code: createCode.value.trim(),
|
||||
name: createName.value.trim(),
|
||||
admin_user_id: Number(createAdminUserID.value),
|
||||
duration: Number(createDuration.value)
|
||||
});
|
||||
toast.add({ severity: 'success', summary: '创建成功', detail: `租户:${createName.value.trim()}`, life: 3000 });
|
||||
createDialogVisible.value = false;
|
||||
await loadTenants();
|
||||
} catch (error) {
|
||||
toast.add({ severity: 'error', summary: '创建失败', detail: error?.message || '无法创建租户', life: 4000 });
|
||||
} finally {
|
||||
creating.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function openRenewDialog(item) {
|
||||
renewTenant.value = item;
|
||||
renewDuration.value = 30;
|
||||
@@ -209,6 +278,7 @@ onMounted(() => {
|
||||
<div class="card">
|
||||
<div class="flex items-center justify-between mb-4">
|
||||
<h4 class="m-0">租户列表</h4>
|
||||
<Button label="创建租户" icon="pi pi-plus" @click="openCreateDialog" />
|
||||
</div>
|
||||
|
||||
<SearchPanel :loading="loading" @search="onSearch" @reset="onReset">
|
||||
@@ -318,5 +388,61 @@ onMounted(() => {
|
||||
<Button label="确认" icon="pi pi-check" @click="confirmUpdateTenantStatus" :loading="tenantStatusUpdating" :disabled="!tenantStatusValue" />
|
||||
</template>
|
||||
</Dialog>
|
||||
|
||||
<Dialog v-model:visible="createDialogVisible" :modal="true" :style="{ width: '720px' }">
|
||||
<template #header>
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="font-medium">创建租户</span>
|
||||
<span class="text-muted-color">并关联租户管理员</span>
|
||||
</div>
|
||||
</template>
|
||||
<div class="flex flex-col gap-4">
|
||||
<div class="grid grid-cols-12 gap-4">
|
||||
<div class="col-span-12 md:col-span-6">
|
||||
<label class="block font-medium mb-2">租户 Code</label>
|
||||
<InputText v-model="createCode" placeholder="例如:acme" class="w-full" :disabled="creating" />
|
||||
<small class="text-muted-color">用于 URL 标识(建议小写字母/数字),创建后不建议修改</small>
|
||||
</div>
|
||||
<div class="col-span-12 md:col-span-6">
|
||||
<label class="block font-medium mb-2">租户名称</label>
|
||||
<InputText v-model="createName" placeholder="例如:ACME 工作室" class="w-full" :disabled="creating" />
|
||||
</div>
|
||||
<div class="col-span-12 md:col-span-6">
|
||||
<label class="block font-medium mb-2">有效期</label>
|
||||
<Select v-model="createDuration" :options="durationOptions" optionLabel="label" optionValue="value" placeholder="选择有效期" fluid :disabled="creating" />
|
||||
</div>
|
||||
<div class="col-span-12 md:col-span-6">
|
||||
<label class="block font-medium mb-2">租户管理员 UserID</label>
|
||||
<InputNumber v-model="createAdminUserID" :min="1" placeholder="输入用户ID" class="w-full" :disabled="creating" />
|
||||
<small class="text-muted-color">下方可通过用户名搜索并一键填入</small>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Divider />
|
||||
|
||||
<div class="flex items-end gap-2">
|
||||
<div class="flex-1">
|
||||
<label class="block font-medium mb-2">按用户名搜索管理员</label>
|
||||
<InputText v-model="adminSearchUsername" placeholder="输入用户名关键字" class="w-full" :disabled="creating" @keyup.enter="searchAdminUsers" />
|
||||
</div>
|
||||
<Button label="搜索" icon="pi pi-search" :loading="adminSearchLoading" :disabled="creating" @click="searchAdminUsers" />
|
||||
</div>
|
||||
|
||||
<DataTable :value="adminSearchResults" dataKey="id" size="small" :loading="adminSearchLoading" scrollable scrollHeight="220px">
|
||||
<Column field="id" header="ID" style="width: 7rem" />
|
||||
<Column field="username" header="用户名" />
|
||||
<Column field="status_description" header="状态" style="width: 10rem" />
|
||||
<Column header="操作" style="width: 8rem">
|
||||
<template #body="{ data }">
|
||||
<Button label="设为管理员" size="small" severity="secondary" @click="pickAdmin(data)" />
|
||||
</template>
|
||||
</Column>
|
||||
</DataTable>
|
||||
</div>
|
||||
<template #footer>
|
||||
<Button label="取消" icon="pi pi-times" text @click="createDialogVisible = false" :disabled="creating" />
|
||||
<Button label="确认创建" icon="pi pi-check" @click="confirmCreateTenant" :loading="creating" :disabled="!canCreateTenant" />
|
||||
</template>
|
||||
</Dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user