403 lines
11 KiB
Vue
403 lines
11 KiB
Vue
<template>
|
|
<t-navbar
|
|
fixed
|
|
:z-index="20"
|
|
title="积分规则详情"
|
|
left-arrow
|
|
@left-click="$router.back()"
|
|
>
|
|
<template #right>
|
|
<t-button
|
|
v-if="rule?.id && canUpdate"
|
|
@click="toEdit"
|
|
theme="primary"
|
|
variant="text"
|
|
size="small"
|
|
content="编辑"
|
|
/>
|
|
</template>
|
|
</t-navbar>
|
|
<div v-can="permissionCodes.pointRule.read" class="point-rule-detail">
|
|
<section v-if="rule" class="rule-header" v-entrance-transition>
|
|
<div class="point-icon" :class="{ consume: isConsumeRule }">
|
|
<t-icon name="money" size="1.75rem" />
|
|
</div>
|
|
<div class="header-content">
|
|
<div class="title-row">
|
|
<h2 v-text="rule.name || '未命名规则'" />
|
|
<t-tag :theme="getStatusTheme(rule.status)" variant="light-outline" :content="getStatusText(rule.status)" />
|
|
</div>
|
|
<div class="point-value" v-text="rulePointText" />
|
|
</div>
|
|
</section>
|
|
<t-cell-group v-if="rule" title="基本信息" theme="card" v-entrance-transition>
|
|
<t-cell
|
|
v-if="rule.triggerSourceType === 'EVENT_RECORD'"
|
|
title="触发事件"
|
|
:note="event?.name || '未绑定事件'"
|
|
:arrow="canReadEvent && !!event?.id"
|
|
:hover="canReadEvent && !!event?.id"
|
|
@click="toEventDetail"
|
|
/>
|
|
<t-cell v-else-if="rule.triggerSourceType === 'SALES_ORDER_PAID'" title="触发来源" note="订单首次全额实收" />
|
|
<t-cell title="规则名称" :note="rule.name || '未命名规则'" />
|
|
<t-cell title="规则状态">
|
|
<template #note>
|
|
<t-tag :theme="getStatusTheme(rule.status)" variant="light-outline" :content="getStatusText(rule.status)" />
|
|
</template>
|
|
</t-cell>
|
|
<t-cell title="规则备注" :note="Text.display(rule.remark)" />
|
|
</t-cell-group>
|
|
<t-cell-group
|
|
v-if="rule"
|
|
class="script"
|
|
title="积分配置"
|
|
theme="card"
|
|
v-entrance-transition
|
|
>
|
|
<t-cell title="规则模式" :note="rule.ruleMode === 'GROOVY' ? '脚本规则' : '简单规则'" />
|
|
<t-cell v-if="rule.ruleMode !== 'GROOVY'" title="固定积分" :note="formatPointValue(rule.pointValue)" />
|
|
<template v-if="rule?.ruleMode === 'GROOVY'">
|
|
<t-cell title="执行脚本" :note="scriptName" />
|
|
<t-cell class="description empty-cell-title-margin">
|
|
<template #title>
|
|
<div class="title">
|
|
<span>脚本说明</span>
|
|
<span class="tips orange">如需调整规则请联系技术支持</span>
|
|
</div>
|
|
</template>
|
|
<template #description>
|
|
<markdown-view :content="rule.groovyScript?.description || '暂无脚本说明'" />
|
|
</template>
|
|
</t-cell>
|
|
<t-cell class="empty-cell-title-margin" title="脚本代码">
|
|
<template #description>
|
|
<code-editor
|
|
v-if="scriptContent"
|
|
:value="scriptContent"
|
|
language="groovy"
|
|
readonly
|
|
line-numbers
|
|
class="code"
|
|
/>
|
|
<center-tips v-else :content="'暂无脚本代码'" />
|
|
</template>
|
|
</t-cell>
|
|
</template>
|
|
</t-cell-group>
|
|
<t-cell-group v-if="rule" title="其他配置" theme="card" v-entrance-transition>
|
|
<t-cell title="规则优先级" :note="formatNumber(rule.priority)" />
|
|
<t-cell title="命中后停止匹配" :note="rule.exclusive ? '是' : '否'" />
|
|
<t-cell title="生效开始" :note="formatDate(rule.beginAt)" />
|
|
<t-cell title="生效结束" :note="formatDate(rule.endAt)" />
|
|
<t-cell title="创建时间" :note="formatDate(rule.createdAt, '未知时间')" />
|
|
<t-cell title="更新时间" :note="formatDate(rule.updatedAt, '未知时间')" />
|
|
</t-cell-group>
|
|
<t-button
|
|
v-if="rule && canDelete"
|
|
class="cell-button"
|
|
:loading="isDeleting"
|
|
theme="danger"
|
|
@click="confirmRemove"
|
|
>
|
|
删除
|
|
</t-button>
|
|
<t-empty v-if="!rule" :description="isLoading ? '正在加载积分规则' : '积分规则不存在'" />
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { DialogPlugin } from "tdesign-mobile-vue";
|
|
import { CodeEditor } from "timi-tdesign-mobile";
|
|
import { MarkdownView, Text, Time } from "timi-web";
|
|
import { useUserStore } from "timi-web-api";
|
|
import EventAPI from "@/api/EventAPI";
|
|
import PointAPI from "@/api/PointAPI";
|
|
import { useCurrentStoreStore } from "@/store/currentStoreStore";
|
|
import type { PointRule, PointRuleStatus } from "@/types/Point";
|
|
import { getErrorMessage } from "@/utils/error";
|
|
import { Message } from "@/utils/message";
|
|
import { formatWanInteger } from "@/utils/money";
|
|
import { gaoRoleCodes, permissionCodes } from "@/utils/permission";
|
|
|
|
|
|
|
|
|
|
|
|
import type { EventItem } from "@/types/EventItem";
|
|
|
|
defineOptions({
|
|
name: "PointRuleDetail"
|
|
});
|
|
|
|
const route = useRoute();
|
|
const router = useRouter();
|
|
const userStore = useUserStore();
|
|
const currentStoreStore = useCurrentStoreStore();
|
|
|
|
// ---------- 权限与门店 ----------
|
|
|
|
const canUpdate = computed(() => userStore.hasPermissions(permissionCodes.pointRule.update));
|
|
const canDelete = computed(() => userStore.hasPermissions(permissionCodes.pointRule.delete));
|
|
const canReadEvent = computed(() => userStore.hasPermissions(permissionCodes.event.read));
|
|
const isGlobalManager = computed(() => userStore.hasRoles(gaoRoleCodes.globalManager));
|
|
|
|
// ---------- 规则资料 ----------
|
|
|
|
const rule = ref<PointRule>();
|
|
const event = ref<EventItem>();
|
|
const isLoading = ref(false);
|
|
const isDeleting = ref(false);
|
|
|
|
const isConsumeRule = computed(() => (rule.value?.pointValue ?? 0) < 0);
|
|
const rulePointText = computed(() => {
|
|
if (rule.value?.ruleMode === "GROOVY") {
|
|
return "脚本规则";
|
|
}
|
|
return formatPointValue(rule.value?.pointValue);
|
|
});
|
|
const scriptName = computed(() => rule.value?.groovyScript?.name || rule.value?.groovyScriptId || "未绑定脚本");
|
|
const scriptContent = computed(() => rule.value?.groovyScript?.latest?.content || "");
|
|
|
|
async function loadStoreScope(): Promise<void> {
|
|
if (isGlobalManager.value) {
|
|
await currentStoreStore.loadStoreList();
|
|
return;
|
|
}
|
|
await currentStoreStore.loadLoginStore();
|
|
}
|
|
|
|
async function loadOptionalEvent(eventId: string, message: string): Promise<EventItem | undefined> {
|
|
try {
|
|
return await EventAPI.detail(eventId);
|
|
} catch (error) {
|
|
console.warn(message, error);
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
async function loadRule(): Promise<void> {
|
|
const ruleId = String(route.params.id || "");
|
|
if (!ruleId) {
|
|
rule.value = undefined;
|
|
event.value = undefined;
|
|
return;
|
|
}
|
|
|
|
rule.value = undefined;
|
|
event.value = undefined;
|
|
isLoading.value = true;
|
|
try {
|
|
await loadStoreScope();
|
|
rule.value = await PointAPI.ruleDetail(ruleId);
|
|
if (!canReadEvent.value || !rule.value) {
|
|
return;
|
|
}
|
|
|
|
const triggerRefId = rule.value.triggerRefId || "";
|
|
event.value = triggerRefId && rule.value.triggerSourceType === "EVENT_RECORD"
|
|
? await loadOptionalEvent(triggerRefId, "积分规则触发事件加载失败")
|
|
: undefined;
|
|
} catch (error) {
|
|
rule.value = undefined;
|
|
event.value = undefined;
|
|
console.warn("积分规则详情加载失败", error);
|
|
Message.error("积分规则加载失败");
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
// ---------- 展示 ----------
|
|
|
|
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";
|
|
}
|
|
|
|
function formatPointValue(value?: number | null): string {
|
|
if (value === undefined || value === null) {
|
|
return "未配置";
|
|
}
|
|
if (value === 0) {
|
|
return "不产生积分流水";
|
|
}
|
|
return `${0 < value ? "发放" : "消耗"} ${formatWanInteger(Math.abs(value))} 分`;
|
|
}
|
|
|
|
function formatNumber(value?: number): string {
|
|
return value === undefined || value === null ? "未配置" : String(value);
|
|
}
|
|
|
|
function formatDate(timestamp?: number, emptyText = "不限"): string {
|
|
return timestamp === undefined || timestamp === null ? emptyText : Time.toDateTime(timestamp);
|
|
}
|
|
|
|
// ---------- 删除 ----------
|
|
|
|
async function confirmRemove(): Promise<void> {
|
|
if (!rule.value?.id || isDeleting.value) {
|
|
return;
|
|
}
|
|
const dialog = DialogPlugin.confirm({
|
|
title: "删除积分规则",
|
|
content: `确定删除规则 ${rule.value.name || "未命名规则"} 吗?删除只影响后续登记,不会改变历史流水。`,
|
|
cancelBtn: "取消",
|
|
confirmBtn: "删除",
|
|
onCancel() {
|
|
dialog.destroy();
|
|
},
|
|
async onConfirm() {
|
|
dialog.destroy();
|
|
await removeRule();
|
|
}
|
|
});
|
|
}
|
|
|
|
async function removeRule(): Promise<void> {
|
|
const ruleId = rule.value?.id;
|
|
if (!ruleId || isDeleting.value) {
|
|
return;
|
|
}
|
|
|
|
isDeleting.value = true;
|
|
try {
|
|
await PointAPI.ruleRemove(ruleId);
|
|
Message.success("积分规则已删除");
|
|
await router.replace("/management/points/rules");
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "积分规则删除失败"));
|
|
} finally {
|
|
isDeleting.value = false;
|
|
}
|
|
}
|
|
|
|
// ---------- 导航 ----------
|
|
|
|
|
|
async function toEdit(): Promise<void> {
|
|
const ruleId = String(route.params.id || "");
|
|
if (!canUpdate.value || !ruleId) {
|
|
return;
|
|
}
|
|
await router.push({
|
|
name: "PointRuleEdit",
|
|
params: { id: ruleId },
|
|
query: { transitionDepth: String(Number(route.meta.depth ?? 0) + 1) }
|
|
});
|
|
}
|
|
|
|
async function toEventDetail(): Promise<void> {
|
|
if (!canReadEvent.value || !event.value?.id) {
|
|
return;
|
|
}
|
|
await router.push({
|
|
name: "EventDetail",
|
|
params: { id: event.value.id },
|
|
query: { transitionDepth: String(Number(route.meta.depth ?? 0) + 1) }
|
|
});
|
|
}
|
|
|
|
watch(() => route.params.id, async () => {
|
|
await loadRule();
|
|
}, { immediate: true });
|
|
|
|
|
|
</script>
|
|
|
|
<style scoped lang="less">
|
|
.point-rule-detail {
|
|
gap: 1rem;
|
|
min-height: 100vh;
|
|
padding: calc(var(--app-nav-height) + 1rem) 0 1rem;
|
|
display: flex;
|
|
flex-direction: column;
|
|
|
|
.rule-header {
|
|
gap: .75rem;
|
|
margin: 0 1rem;
|
|
padding: 1rem;
|
|
display: flex;
|
|
background: var(--td-bg-color-container);
|
|
box-shadow: var(--td-shadow-1);
|
|
border-radius: 1.4rem;
|
|
|
|
.point-icon {
|
|
width: 3.5rem;
|
|
height: 3.5rem;
|
|
color: var(--td-success-color);
|
|
flex: 0 0 auto;
|
|
display: grid;
|
|
place-items: center;
|
|
background: rgba(34, 197, 94, .12);
|
|
border-radius: .7rem;
|
|
|
|
&.consume {
|
|
color: var(--td-error-color);
|
|
background: rgba(239, 68, 68, .12);
|
|
}
|
|
}
|
|
|
|
.header-content {
|
|
min-width: 0;
|
|
flex: 1;
|
|
}
|
|
|
|
.title-row {
|
|
gap: .5rem;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
|
|
h2 {
|
|
margin: 0;
|
|
font-size: 1.125rem;
|
|
}
|
|
}
|
|
|
|
.point-value {
|
|
margin-top: .5rem;
|
|
color: var(--td-text-color-secondary);
|
|
}
|
|
}
|
|
|
|
.script {
|
|
|
|
.description {
|
|
|
|
.title {
|
|
flex: 1;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
|
|
.tips {
|
|
font-size: .75rem;
|
|
}
|
|
}
|
|
}
|
|
|
|
.code {
|
|
min-height: 10rem;
|
|
}
|
|
}
|
|
}
|
|
|
|
</style>
|