Files
gao-customer-h5/src/pages/point/PointRuleList.vue
T
2026-10-17 12:01:28 +08:00

476 lines
11 KiB
Vue

<template>
<div class="page-layout navbar point-rule-list">
<t-navbar
fixed
:z-index="20"
title="积分规则"
left-arrow
@left-click="$router.back()"
>
<template #right>
<div
class="nav-actions"
v-if="(canShowActionMenu) && pageNavBarActionList.length"
>
<t-popover
v-model:visible="pageNavBarActionVisible"
placement="bottom-right"
:show-arrow="false"
theme="light"
>
<t-button
class="trigger"
variant="text"
:aria-label="'更多操作'"
type="button"
>
<template #icon>
<t-icon name="ellipsis" />
</template>
</t-button>
<template #content>
<div class="menu">
<t-button
v-for="action in pageNavBarActionList"
:key="action.key"
class="menu-item"
variant="text"
type="button"
:theme="action.theme"
:loading="action.loading"
:disabled="action.disabled || action.loading"
@click="handlePageNavBarAction(action)"
>
<template v-if="action.icon" #icon>
<t-icon :name="action.icon" />
</template>
<span v-text="action.text" />
</t-button>
</div>
</template>
</t-popover>
</div>
</template>
</t-navbar>
<main v-can="permissionCodes.pointRule.read" class="page-content scroll list">
<t-pull-down-refresh
class="list-wrapper pull-refresh"
v-model="isRefreshing"
:refresh-timeout="30000"
@refresh="reload"
@scrolltolower="load"
>
<div class="filter-menu">
<t-dropdown-menu>
<t-dropdown-item
v-model="statusFilter"
label="规则状态"
:options="statusOptions"
@change="selectStatus"
/>
</t-dropdown-menu>
</div>
<t-cell-group
v-if="pageResult.list.length"
class="list empty-cell-title-margin"
theme="card"
>
<t-cell
v-for="item in pageResult.list"
:key="item.id"
class="item"
:arrow="!!item.id"
:hover="!!item.id"
v-entrance-transition="{ step: 30 }"
@click="toDetail(item)"
>
<template #left-icon>
<div class="point" :class="{ 'consume': item.pointValue && item.pointValue < 0 }">
<t-icon name="money" />
<span v-text="getPointText(item)" />
</div>
</template>
<template #title>
<div class="title">
<h4 class="text" v-text="item.name || '未命名规则'" />
<t-tag
:theme="getStatusTheme(item.status)"
variant="light-outline"
:content="getStatusText(item.status)"
/>
</div>
</template>
<template #description>
<div class="description">
<div v-text="`触发来源:${getTriggerSourceText(item)}`"></div>
<div v-text="`触发对象:${getTriggerRefText(item)}`"></div>
</div>
</template>
</t-cell>
</t-cell-group>
<div class="center-tips" v-text="loadStateText" />
</t-pull-down-refresh>
</main>
</div>
</template>
<script setup lang="ts">
import type { NavBarAction } from "@/types/NavBarAction";
import { type DropdownValue } from "tdesign-mobile-vue";
import EventAPI from "@/api/EventAPI";
import PointAPI from "@/api/PointAPI";
import { useCurrentStoreStore } from "@/store/currentStoreStore";
import type { PointRule, PointRuleStatus } from "@/types/Point";
import { formatWanInteger } from "@/utils/money";
import { gaoRoleCodes, permissionCodes } from "@/utils/permission";
import { type Page, type PageResult, useUserStore } from "timi-web-api";
import { Message } from "@/utils/message.ts";
const pageNavBarActionVisible = ref(false);
const pageNavBarActionList = computed<NavBarAction[]>(() => (unref(actionList) as NavBarAction[]).filter(action => action.visible !== false));
async function handlePageNavBarAction(action: NavBarAction): Promise<void> {
pageNavBarActionVisible.value = false;
await action.action();
}
import type { EventItem } from "@/types/EventItem";
defineOptions({
name: "PointRuleList"
});
const router = useRouter();
const userStore = useUserStore();
const currentStoreStore = useCurrentStoreStore();
// ---------- 筛选 ----------
const isGlobalManager = computed(() => userStore.hasRoles(gaoRoleCodes.globalManager));
const canCreate = computed(() => userStore.hasPermissions(permissionCodes.pointRule.create));
const canUpdate = computed(() => userStore.hasPermissions(permissionCodes.pointRule.update));
const canShowActionMenu = computed(() => canCreate.value || canUpdate.value);
const actionList = computed<NavBarAction[]>(() => {
const list: NavBarAction[] = [];
if (canCreate.value) {
list.push({
action: toCreate,
theme: "primary",
icon: "add",
key: "create",
text: "新增积分规则"
});
}
if (canUpdate.value) {
list.push({
action: toSort,
icon: "order",
key: "sort",
text: "排序"
});
}
return list;
});
const statusFilter = ref("ALL");
const statusOptions = [
{ label: "全部状态", value: "ALL" },
{ label: "草稿", value: "DRAFT" },
{ label: "启用", value: "ACTIVE" },
{ label: "停用", value: "INACTIVE" },
{ label: "已删除", value: "DELETED" }
];
async function selectStatus(value: DropdownValue): Promise<void> {
if (Array.isArray(value)) {
return;
}
statusFilter.value = String(value);
await reload();
}
// ---------- 规则列表 ----------
const page = reactive<Page<PointRule>>({
index: 0,
size: 20
});
const pageResult = reactive<PageResult<PointRule>>({
total: 0,
list: []
});
const isLoading = ref(false);
const isRefreshing = ref(false);
const isNoMore = ref(false);
let loadToken = 0;
const loadStateText = computed(() => {
if (isLoading.value) {
return "正在加载更多";
}
if (isNoMore.value) {
return pageResult.list.length ? `共 ${pageResult.total} 条规则` : "";
}
return "上拉加载更多";
});
async function reload(): Promise<void> {
isRefreshing.value = true;
try {
page.index = 0;
pageResult.total = 0;
pageResult.list = [];
isNoMore.value = false;
await load();
} finally {
isRefreshing.value = false;
}
}
async function load(): Promise<void> {
if (isLoading.value || isNoMore.value) {
return;
}
const token = loadToken + 1;
loadToken = token;
isLoading.value = true;
try {
const result = await PointAPI.ruleList(buildRequest(), buildListOptions());
if (token !== loadToken) {
return;
}
const list = result.list || [];
pageResult.list = pageResult.list.concat(list);
pageResult.total = result.total || 0;
page.index += 1;
isNoMore.value = pageResult.list.length >= pageResult.total || list.length < page.size;
} catch {
Message.error("积分规则加载失败");
} finally {
if (token === loadToken) {
isLoading.value = false;
}
}
}
function buildRequest(): Page<PointRule> {
return {
index: page.index,
size: page.size,
equalsExample: statusFilter.value === "ALL" ? undefined : {
status: statusFilter.value as PointRuleStatus
}
};
}
function buildListOptions(): { storeId: string } {
return { storeId: currentStoreStore.currentStoreId };
}
// ---------- 事件名称 ----------
const eventList = ref<EventItem[]>([]);
const eventNameMap = computed(() => new Map(eventList.value.filter(item => item.id).map(item => [item.id as string, item.name])));
async function loadEventList(): Promise<void> {
try {
const options = buildListOptions();
const result = await EventAPI.list({ index: 0, size: 1000 }, options);
eventList.value = result.list || [];
} catch {
eventList.value = [];
}
}
// ---------- 展示与导航 ----------
function getTriggerSourceText(rule: PointRule): string {
return rule.triggerSourceType === "SALES_ORDER_PAID" ? "订单全额实收" : "事件登记";
}
function getTriggerRefText(rule: PointRule): string {
if (rule.triggerSourceType === "SALES_ORDER_PAID") {
return "订单";
}
return rule.triggerRefId ? eventNameMap.value.get(rule.triggerRefId) || "未知事件" : "未绑定事件";
}
function getPointText(rule: PointRule): string {
if (rule.ruleMode === "GROOVY") {
return "脚本";
}
if (rule.pointValue === undefined || rule.pointValue === null) {
return "?";
}
return `${0 < rule.pointValue ? "+" : "-"}${formatWanInteger(Math.abs(rule.pointValue))}`;
}
function getStatusText(status?: PointRuleStatus): string {
return ({
DRAFT: "草稿",
ACTIVE: "启用",
INACTIVE: "停用",
DELETED: "已删除"
} as Record<string, string>)[status || ""] || "未知";
}
function getStatusTheme(status?: PointRuleStatus): "default" | "success" | "warning" | "danger" {
if (status === "ACTIVE") {
return "success";
}
if (status === "INACTIVE") {
return "warning";
}
if (status === "DELETED") {
return "danger";
}
return "default";
}
async function toCreate(): Promise<void> {
await router.push({ name: "PointRuleCreate" });
}
async function toSort(): Promise<void> {
await router.push("/management/points/rules/sort");
}
async function toDetail(rule: PointRule): Promise<void> {
if (!rule.id) {
return;
}
await router.push(`/management/points/rules/${rule.id}`);
}
async function preparePage(): Promise<void> {
if (isGlobalManager.value) {
await currentStoreStore.loadStoreList();
} else {
await currentStoreStore.loadLoginStore();
}
await loadEventList();
await reload();
}
onMounted(async () => {
try {
await preparePage();
} catch {
Message.error("积分规则页面加载失败");
}
});
</script>
<style scoped lang="less">
.point-rule-list {
padding: calc(var(--app-nav-height) + 4rem) 0 1rem;
.filter-menu {
top: var(--app-nav-height);
left: 0;
right: 0;
z-index: 10;
position: fixed;
background: var(--td-bg-color-container);
}
.list-wrapper {
min-height: 70vh;
.list {
.item {
padding: 10px;
.point {
width: 4rem;
display: flex;
align-items: center;
flex-direction: column;
justify-content: space-between;
&.consume {
color: var(--td-error-color);
}
}
.title {
width: 100%;
display: flex;
justify-content: space-between;
.text {
margin: 0;
}
}
.description {
font-size: .75rem;
line-height: 1.25;
}
}
}
}
}
:global(html.theme-dark) {
.point-rule-list {
.empty {
color: #94a3b8;
}
.list .priority {
color: #bfdbfe;
background: rgba(30, 64, 175, .45);
}
}
}
.nav-actions {
--td-popover-padding: 0 .5rem;
width: 6rem;
height: 100%;
display: flex;
align-items: center;
justify-content: flex-end;
:deep(.t-popover__content) { border-radius: .875rem; }
.trigger { padding: 0; }
.menu {
gap: .25rem;
display: grid;
padding: .5rem 0;
min-width: 9rem;
.menu-item {
--td-button-medium-height: 32px;
--td-button-medium-padding-horizontal: 10px;
gap: .25rem;
width: 100%;
font-weight: unset;
justify-content: flex-start;
}
}
}
:global(html.theme-dark) {
.nav-actions {
:deep(.t-popover__content) {
color: #e2e8f0;
background: rgba(30, 41, 59, .98);
}
.menu-item { color: #e2e8f0; }
}
}
</style>