add package

This commit is contained in:
Timi
2026-09-01 01:14:54 +08:00
parent 100d4c4c44
commit 2d94abfb32
39 changed files with 4425 additions and 59 deletions
+233
View File
@@ -0,0 +1,233 @@
<template>
<div v-can="permissionCodes.packageConfig.read" class="package-config-list">
<div class="filters">
<t-input
v-model.trim="keyword"
class="keyword"
borderless
clearable
placeholder="搜索套餐名称或说明"
@enter="reload"
/>
<t-button theme="primary" size="small" @click="reload">搜索</t-button>
</div>
<t-dropdown-menu>
<t-dropdown-item v-model="status" label="状态" :options="statusOptions" @change="selectStatus" />
</t-dropdown-menu>
<t-pull-down-refresh v-model="isRefreshing" :refresh-timeout="30000" @refresh="reload" @scrolltolower="loadMore">
<t-cell-group v-if="configList.length" theme="card" class="list">
<t-swipe-cell v-for="(item, index) in configList" :key="item.id" v-entrance-transition="index * 30">
<t-cell class="item" :arrow="!!item.id" :hover="!!item.id" @click="toDetail(item)">
<template #title>
<div class="title-row">
<strong v-text="item.name || '未命名套餐'" />
<t-tag :content="getPackageConfigStatusText(item.status)" variant="light-outline" :theme="getStatusTheme(item.status)" />
</div>
</template>
<template #description>
<div class="description">
<span v-text="formatPackageAmount(item.standardAmount)" />
<span v-text="`${item.itemList?.length || 0} 项权益`" />
<span v-text="formatValidity(item)" />
</div>
</template>
</t-cell>
<template v-if="canDelete && item.id" #right>
<t-button class="delete" theme="danger" @click.stop="confirmRemove(item)">删除</t-button>
</template>
</t-swipe-cell>
</t-cell-group>
<div v-else class="empty" v-text="loading ? '正在加载套餐配置' : '暂无套餐配置'" />
<div class="load-state" v-text="loadStateText" />
</t-pull-down-refresh>
</div>
<nav-bar-action-menu v-if="canCreate" :actions="actionList" />
</template>
<script setup lang="ts">
import { DialogPlugin, type DropdownValue } from "tdesign-mobile-vue";
import { Time } from "timi-web";
import { useUserStore } from "timi-web-api";
import PackageAPI from "@/api/PackageAPI";
import type { GaoPackageConfig, PackageConfigStatus } from "@/types/Package";
import type { NavBarAction } from "@/types/NavBarAction";
import NavBarActionMenu from "@/components/NavBarActionMenu.vue";
import { useNavBarStore } from "@/store/navBarStore";
import { getErrorMessage } from "@/utils/error";
import { formatPackageAmount, getPackageConfigStatusText } from "@/utils/package";
import { Message } from "@/utils/message";
import { permissionCodes } from "@/utils/permission";
defineOptions({ name: "PackageConfigList" });
const router = useRouter();
const navBarStore = useNavBarStore();
const userStore = useUserStore();
const canCreate = computed(() => userStore.hasPermissions(permissionCodes.packageConfig.create));
const canDelete = computed(() => userStore.hasPermissions(permissionCodes.packageConfig.delete));
const actionList = computed<NavBarAction[]>(() => canCreate.value ? [{
key: "create",
text: "新增套餐配置",
icon: "add",
theme: "primary",
action: toCreate
}] : []);
const keyword = ref("");
const status = ref("ALL");
const configList = ref<GaoPackageConfig[]>([]);
const pageIndex = ref(0);
const total = ref(0);
const loading = ref(false);
const isRefreshing = ref(false);
const noMore = ref(false);
const statusOptions = [
{ label: "全部状态", value: "ALL" },
{ label: "草稿", value: "DRAFT" },
{ label: "启用", value: "ACTIVE" },
{ label: "停用", value: "INACTIVE" }
];
const loadStateText = computed(() => loading.value ? "正在加载" : noMore.value ? "已经到底了" : "上拉加载更多");
async function loadMore(): Promise<void> {
if (loading.value || noMore.value) {
return;
}
loading.value = true;
try {
const result = await PackageAPI.configList({
index: pageIndex.value,
size: 15,
keyword: keyword.value || undefined,
status: status.value === "ALL" ? undefined : status.value as PackageConfigStatus
});
const list = result.list || [];
configList.value = configList.value.concat(list);
total.value = result.total || 0;
pageIndex.value += 1;
noMore.value = list.length < 15 || total.value <= configList.value.length;
} finally {
loading.value = false;
}
}
async function reload(): Promise<void> {
if (loading.value) {
return;
}
pageIndex.value = 0;
configList.value = [];
total.value = 0;
noMore.value = false;
await loadMore();
}
async function selectStatus(value: DropdownValue): Promise<void> {
if (Array.isArray(value)) {
return;
}
status.value = String(value);
await reload();
}
async function toCreate(): Promise<void> {
await router.push("/management/package-configs/create");
}
async function toDetail(item: GaoPackageConfig): Promise<void> {
if (item.id) {
await router.push(`/management/package-configs/${item.id}`);
}
}
function getStatusTheme(statusValue?: PackageConfigStatus): "success" | "warning" | "default" {
return statusValue === "ACTIVE" ? "success" : statusValue === "INACTIVE" ? "warning" : "default";
}
function formatValidity(item: GaoPackageConfig): string {
const begin = item.beginAt ? Time.toDateTime(item.beginAt) : "不限起始";
const end = item.endAt ? Time.toDateTime(item.endAt) : "不限结束";
return `${begin} 至 ${end}`;
}
function confirmRemove(item: GaoPackageConfig): void {
if (!item.id || !canDelete.value) {
return;
}
const dialog = DialogPlugin.confirm({
title: "删除套餐配置",
content: `确定删除“${item.name || "未命名套餐"}”?删除后不可再用于购买。`,
confirmBtn: { content: "删除", theme: "danger" },
onConfirm: async () => {
try {
await PackageAPI.configRemove(item.id!);
configList.value = configList.value.filter(config => config.id !== item.id);
total.value = Math.max(0, total.value - 1);
Message.success("套餐配置已删除");
} catch (error) {
Message.error(getErrorMessage(error, "套餐配置删除失败"));
} finally {
dialog.destroy();
}
}
});
}
onMounted(async () => {
navBarStore.clearRight();
navBarStore.setCustomRightVisible(canCreate.value);
try {
await reload();
} catch (error) {
Message.error(getErrorMessage(error, "套餐配置加载失败"));
}
});
onUnmounted(() => navBarStore.clearRight());
</script>
<style scoped lang="less">
.package-config-list {
padding: calc(var(--app-nav-height) + 1rem) 0 1rem;
.filters {
gap: .5rem;
margin: 0 1rem .75rem;
display: flex;
.keyword {
flex: 1;
border: 1px solid var(--td-component-border);
border-radius: .75rem;
}
}
.item {
.title-row,
.description {
gap: .5rem;
display: flex;
align-items: center;
}
.description {
color: var(--td-text-color-secondary);
margin-top: .35rem;
font-size: .8125rem;
flex-wrap: wrap;
}
.delete {
height: 100%;
padding: 0 1rem;
border-radius: 0;
}
}
.empty {
color: var(--td-text-color-secondary);
padding: 2rem 1rem;
text-align: center;
}
}
</style>