Files
gao-customer-h5/src/pages/point/PointRuleSort.vue
T
2026-10-22 16:17:22 +08:00

166 lines
4.6 KiB
Vue

<template>
<t-navbar
fixed
:z-index="20"
title="积分规则排序"
left-arrow
@left-click="$router.back()"
>
<template #right>
<button class="nav-action" type="button" @click="saveSort">保存</button>
</template>
</t-navbar>
<div v-can="permissionCodes.pointRule.update" class="point-rule-sort">
<div class="tip" v-text="'按住右侧拖动手柄调整规则顺序,保存后生效;仅显示启用规则'" v-entrance-transition />
<div class="items">
<t-cell-group v-if="ruleList.length" theme="card">
<vue-draggable-next
v-model="ruleList"
class="draggable"
item-key="id"
handle=".handle"
ghost-class="ghost"
chosen-class="chosen"
drag-class="dragging"
:animation="180"
:disabled="isLoading || isSaving"
@change="sortDirty = true"
>
<t-cell
class="cell"
v-for="item in ruleList"
:key="item.id"
v-entrance-transition="{ step: 30 }"
>
<template #title>
<div class="title" v-text="item.name || '未命名规则'" />
</template>
<template #description>
<div class="description">
<span v-text="'积分值:' + getPointText(item)" />
<span v-text="'触发对象:' + (item.triggerRefId ? eventNameMap.get(item.triggerRefId) || '未知事件' : '任意订单')" />
</div>
</template>
<template #note>
<t-tag :theme="item.exclusive ? 'warning' : 'default'" variant="outline" :content="item.exclusive ? '独占匹配' : '继续匹配'" />
</template>
<template #rightIcon>
<t-icon class="handle" role="button" name="move" aria-label="拖动排序" />
</template>
</t-cell>
</vue-draggable-next>
</t-cell-group>
<center-tips v-else :content="isLoading ? '正在加载启用规则' : '暂无启用规则'" />
</div>
</div>
</template>
<script setup lang="ts">
import { Message } from "@/utils/message";
import { VueDraggableNext } from "vue-draggable-next";
import EventAPI from "@/api/EventAPI";
import PointAPI from "@/api/PointAPI";
import type { PointRule } from "@/types/Point";
import type { EventItem } from "@/types/EventItem";
import { formatWanInteger } from "@/utils/money";
import { permissionCodes } from "@/utils/permission";
defineOptions({ name: "PointRuleSort" });
const router = useRouter();
const ruleList = ref<PointRule[]>([]);
const eventList = ref<EventItem[]>([]);
const isLoading = ref(false);
const isSaving = ref(false);
const sortDirty = ref(false);
const eventNameMap = computed(() => new Map(eventList.value.filter(item => item.id).map(item => [item.id as string, item.name])));
function getPointText(rule: PointRule): string {
if (rule.ruleMode === "GROOVY") {
return "脚本";
}
if (rule.pointValue === undefined || rule.pointValue === null) {
return "未配置";
}
return (0 < rule.pointValue ? "+" : "") + formatWanInteger(rule.pointValue);
}
async function loadData(): Promise<void> {
isLoading.value = true;
try {
const [ruleResult, eventResult] = await Promise.all([
PointAPI.ruleList({ index: 0, size: 1000, equalsExample: { status: "ACTIVE" } }),
EventAPI.list({ index: 0, size: 1000 })
]);
ruleList.value = ruleResult.list || [];
eventList.value = eventResult.list || [];
sortDirty.value = false;
} finally {
isLoading.value = false;
}
}
async function saveSort(): Promise<void> {
if (!sortDirty.value || isSaving.value) {
return;
}
isSaving.value = true;
try {
const idList = ruleList.value.map(item => item.id).filter((id): id is string => Boolean(id));
await PointAPI.ruleSort(idList);
ruleList.value.forEach((item, index) => { item.priority = index; });
sortDirty.value = false;
Message.success("积分规则排序已保存");
await router.back();
} catch {
Message.error("积分规则排序保存失败");
} finally {
isSaving.value = false;
}
}
onMounted(async () => {
try {
await loadData();
} catch {
Message.error("启用积分规则加载失败");
}
});
</script>
<style scoped lang="less">
.point-rule-sort {
padding: calc(var(--app-nav-height) + 1rem) 0 6rem;
.tip {
color: #52607a;
padding: 0 1rem .75rem;
font-size: .8125rem;
}
.draggable {
.cell {
.title { font-weight: 600; }
.description {
display: flex;
gap: .75rem;
font-size: .75rem;
}
.handle { color: #52607a; cursor: grab; touch-action: none; }
}
.ghost { opacity: .4; }
.chosen,
.dragging { z-index: 1; box-shadow: 0 .5rem 1rem rgba(15, 23, 42, .16); }
}
}
:global(html.theme-dark) {
.point-rule-sort {
.tip,
.handle { color: #94a3b8; }
.draggable .cell { background: rgba(15, 23, 42, .88); }
}
}
</style>