599 lines
15 KiB
Vue
599 lines
15 KiB
Vue
<template>
|
||
<div class="tui-page-layout tui-navbar point-ledger-list">
|
||
<t-navbar
|
||
fixed
|
||
:z-index="20"
|
||
title="积分流水"
|
||
left-arrow
|
||
@left-click="$router.back()"
|
||
/>
|
||
<main v-can="permissionCodes.point.ledgerRead" class="tui-page-content scroll column list">
|
||
<t-pull-down-refresh
|
||
class="pull-refresh"
|
||
v-model="isRefreshing"
|
||
:refresh-timeout="30000"
|
||
@refresh="reload"
|
||
@scrolltolower="load"
|
||
>
|
||
<div class="filters glass">
|
||
<t-input
|
||
v-model="customerKeyword"
|
||
class="customer"
|
||
type="search"
|
||
borderless
|
||
placeholder="输入客户编码或姓名"
|
||
@clear="searchCustomer"
|
||
@enter="searchCustomer"
|
||
@input="searchCustomer"
|
||
>
|
||
<template #suffix-icon>
|
||
<t-button
|
||
size="extra-small"
|
||
theme="primary"
|
||
variant="outline"
|
||
content="选择客户"
|
||
@click.stop="selectorVisible = true"
|
||
/>
|
||
</template>
|
||
</t-input>
|
||
<t-dropdown-menu class="menus">
|
||
<t-dropdown-item
|
||
v-model="changeTypeFilter"
|
||
label="流水类型"
|
||
:options="changeTypeOptions"
|
||
@change="selectChangeType"
|
||
/>
|
||
<t-dropdown-item
|
||
v-model="dateFilter"
|
||
:label="dateFilterLabel"
|
||
:options="dateFilterOptions"
|
||
@change="selectDateFilter"
|
||
/>
|
||
</t-dropdown-menu>
|
||
</div>
|
||
<div class="list-wrap">
|
||
<card-container v-if="customer" class="customer">
|
||
<customer-profile-card :customer="customer" />
|
||
</card-container>
|
||
<t-cell-group class="summary" theme="card">
|
||
<t-cell
|
||
title="开始时间"
|
||
:note="dateRange[0] || '-'"
|
||
arrow
|
||
@click="openStartDate"
|
||
/>
|
||
<t-cell
|
||
title="结束时间"
|
||
:note="dateRange[1] || '-'"
|
||
arrow
|
||
@click="openEndDate"
|
||
/>
|
||
</t-cell-group>
|
||
<t-cell-group v-if="pageResult.list.length" class="list" theme="card">
|
||
<t-cell
|
||
v-entrance-transition="{ step: 30 }"
|
||
v-for="item in pageResult.list"
|
||
:key="item.id"
|
||
class="item"
|
||
:arrow="!!item.id"
|
||
:hover="!!item.id"
|
||
@click="toDetail(item)"
|
||
>
|
||
<template #leftIcon>
|
||
<t-image
|
||
v-if="getCustomerPhotoURL(item.customer!, 64)"
|
||
:src="getCustomerPhotoURL(item.customer!, 64)"
|
||
fit="cover"
|
||
class="avatar"
|
||
/>
|
||
<div v-else class="avatar fallback" v-text="(item.customer?.name || item.customer?.code || '客').slice(0, 1)" />
|
||
</template>
|
||
<template #title>
|
||
<div class="title">
|
||
<t-tag
|
||
class="type-icon"
|
||
:theme="item.pointDelta && 0 < item.pointDelta ? 'success' : 'danger'"
|
||
:content="getLedgerShortType(item)"
|
||
variant="outline"
|
||
/>
|
||
<div class="name" v-text="item.customer?.name" />
|
||
</div>
|
||
</template>
|
||
<template #description>
|
||
<div class="description">
|
||
<div>
|
||
<span>时间:</span>
|
||
<time-label :timestamp="item.occurredAt" underline />
|
||
</div>
|
||
<div v-text="`余额:${formatPointValue(item.balanceBefore)} → ${formatPointValue(item.balanceAfter)}`" />
|
||
<div v-text="item.remark" />
|
||
</div>
|
||
</template>
|
||
<template #note>
|
||
<div class="delta keep-text" :class="getLedgerTheme(item)" v-text="getLedgerDelta(item)" />
|
||
</template>
|
||
</t-cell>
|
||
</t-cell-group>
|
||
<center-tips :content="loadStateText" />
|
||
</div>
|
||
</t-pull-down-refresh>
|
||
</main>
|
||
<customer-selector
|
||
v-model="selectedCustomerId"
|
||
v-model:visible="selectorVisible"
|
||
@change="handleCustomerChange"
|
||
/>
|
||
<t-popup v-model="startDateVisible" placement="bottom">
|
||
<t-date-time-picker
|
||
v-if="startDateVisible"
|
||
v-model="startDraft"
|
||
format="YYYY-MM-DD HH:mm"
|
||
:mode="['date', 'minute']"
|
||
title="开始时间"
|
||
:default-value="dateRange[0]"
|
||
@cancel="cancelStartDate"
|
||
@confirm="confirmStartDate"
|
||
/>
|
||
</t-popup>
|
||
<t-popup v-model="endDateVisible" placement="bottom">
|
||
<t-date-time-picker
|
||
v-if="endDateVisible"
|
||
v-model="endDraft"
|
||
format="YYYY-MM-DD HH:mm"
|
||
:mode="['date', 'minute']"
|
||
title="结束时间"
|
||
:default-value="dateRange[1]"
|
||
@cancel="cancelEndDate"
|
||
@confirm="confirmEndDate"
|
||
/>
|
||
</t-popup>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import type { DropdownValue } from "tdesign-mobile-vue";
|
||
import { TimeLabel, Toolkit } from "timi-web";
|
||
import { type PageResult, useUserStore } from "timi-web-api";
|
||
import CustomerAPI from "@/api/CustomerAPI";
|
||
import PointAPI from "@/api/PointAPI";
|
||
import CustomerSelector from "@/components/CustomerSelector.vue";
|
||
import { useCurrentStoreStore } from "@/store/currentStoreStore";
|
||
import type { Customer } from "@/types/Customer";
|
||
import type { PointLedger, PointLedgerChangeType, PointLedgerPage } from "@/types/Point";
|
||
import { getCustomerPhotoURL } from "@/utils/customer";
|
||
import { buildPresetDateRange, parseDateTime } from "@/utils/date";
|
||
import { DATE_FILTER_OPTIONS as dateFilterOptions, type DateFilterType } from "@/utils/dateFilter";
|
||
import { Message } from "@/utils/message";
|
||
import { formatWanInteger } from "@/utils/money";
|
||
import { gaoRoleCodes, permissionCodes } from "@/utils/permission";
|
||
|
||
defineOptions({
|
||
name: "PointLedgerList"
|
||
});
|
||
|
||
const route = useRoute();
|
||
const router = useRouter();
|
||
const userStore = useUserStore();
|
||
const currentStoreStore = useCurrentStoreStore();
|
||
|
||
// ---------- 筛选 ----------
|
||
|
||
const isGlobalManager = computed(() => userStore.hasRoles(gaoRoleCodes.globalManager));
|
||
const selectedCustomerId = ref(String(route.query.customerId || ""));
|
||
const selectedAccountId = ref(String(route.query.accountId || ""));
|
||
const customer = ref<Customer>();
|
||
const customerKeyword = ref("");
|
||
const selectorVisible = ref(false);
|
||
const changeTypeFilter = ref("ALL");
|
||
const dateFilter = ref<DateFilterType>("thisMonth");
|
||
const dateRange = ref<[string, string]>(buildPresetDateRange("thisMonth"));
|
||
const startDraft = ref("");
|
||
const endDraft = ref("");
|
||
const startDateVisible = ref(false);
|
||
const endDateVisible = ref(false);
|
||
const changeTypeOptions = [
|
||
{ label: "全部类型", value: "ALL" },
|
||
{ label: "获得", value: "EARN" },
|
||
{ label: "消费", value: "CONSUME" },
|
||
{ label: "撤销", value: "REVOKE" },
|
||
{ label: "调整", value: "ADJUST" }
|
||
];
|
||
const dateFilterLabel = computed(() => dateFilterOptions.find(item => item.value === dateFilter.value)?.label || "时间范围");
|
||
|
||
async function handleCustomerChange(value: string | string[]): Promise<void> {
|
||
selectedCustomerId.value = Array.isArray(value) ? String(value[0] || "") : value;
|
||
if (selectedCustomerId.value) {
|
||
customer.value = await CustomerAPI.detail(selectedCustomerId.value);
|
||
customerKeyword.value = getCustomerSearchKeyword(customer.value);
|
||
} else {
|
||
customer.value = undefined;
|
||
customerKeyword.value = "";
|
||
}
|
||
await reload();
|
||
}
|
||
|
||
function getCustomerSearchKeyword(value?: Customer): string {
|
||
return value?.name?.trim() || value?.code?.trim() || "";
|
||
}
|
||
|
||
const searchCustomer = Toolkit.debounce(async () => {
|
||
const keyword = customerKeyword.value.trim();
|
||
if (!keyword) {
|
||
selectedCustomerId.value = "";
|
||
customer.value = undefined;
|
||
await reload();
|
||
return;
|
||
}
|
||
if (keyword === getCustomerSearchKeyword(customer.value)) {
|
||
return;
|
||
}
|
||
try {
|
||
const matched = await CustomerAPI.findByCode(keyword, currentStoreStore.currentStoreId || undefined)
|
||
|| await CustomerAPI.findByName(keyword);
|
||
if (!matched?.id) {
|
||
Message.warning("未找到客户");
|
||
return;
|
||
}
|
||
selectedCustomerId.value = matched.id;
|
||
customer.value = matched;
|
||
customerKeyword.value = getCustomerSearchKeyword(matched);
|
||
await reload();
|
||
} catch {
|
||
Message.error("客户信息加载失败");
|
||
}
|
||
}, false);
|
||
|
||
async function selectChangeType(value: DropdownValue): Promise<void> {
|
||
if (Array.isArray(value)) {
|
||
return;
|
||
}
|
||
changeTypeFilter.value = String(value);
|
||
await reload();
|
||
}
|
||
|
||
async function selectDateFilter(value: DropdownValue): Promise<void> {
|
||
if (Array.isArray(value)) {
|
||
return;
|
||
}
|
||
dateFilter.value = String(value) as DateFilterType;
|
||
if (dateFilter.value !== "custom") {
|
||
dateRange.value = buildPresetDateRange(dateFilter.value);
|
||
await reload();
|
||
}
|
||
}
|
||
|
||
function openStartDate(): void {
|
||
startDraft.value = dateRange.value[0];
|
||
startDateVisible.value = true;
|
||
}
|
||
|
||
function openEndDate(): void {
|
||
endDraft.value = dateRange.value[1];
|
||
endDateVisible.value = true;
|
||
}
|
||
|
||
function cancelStartDate(): void {
|
||
startDateVisible.value = false;
|
||
startDraft.value = dateRange.value[0];
|
||
}
|
||
|
||
function cancelEndDate(): void {
|
||
endDateVisible.value = false;
|
||
endDraft.value = dateRange.value[1];
|
||
}
|
||
|
||
async function confirmStartDate(value?: string | number): Promise<void> {
|
||
startDateVisible.value = false;
|
||
dateRange.value = [String(value || startDraft.value), dateRange.value[1]];
|
||
await confirmDateRange();
|
||
}
|
||
|
||
async function confirmEndDate(value?: string | number): Promise<void> {
|
||
endDateVisible.value = false;
|
||
dateRange.value = [dateRange.value[0], String(value || endDraft.value)];
|
||
await confirmDateRange();
|
||
}
|
||
|
||
async function confirmDateRange(): Promise<void> {
|
||
startDateVisible.value = false;
|
||
endDateVisible.value = false;
|
||
dateFilter.value = "custom";
|
||
if (dateRange.value[0] && dateRange.value[1] && dateRange.value[1] < dateRange.value[0]) {
|
||
dateRange.value = [dateRange.value[1], dateRange.value[0]];
|
||
}
|
||
await reload();
|
||
}
|
||
|
||
// ---------- 分页 ----------
|
||
|
||
const page = reactive<PointLedgerPage>({
|
||
index: 0,
|
||
size: 20
|
||
});
|
||
const pageResult = reactive<PageResult<PointLedger>>({
|
||
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.listLedger(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(): PointLedgerPage {
|
||
const [beginAt, endAt] = buildUnixRange();
|
||
return {
|
||
index: page.index,
|
||
size: page.size,
|
||
customerId: selectedCustomerId.value || undefined,
|
||
accountId: selectedAccountId.value || undefined,
|
||
changeType: changeTypeFilter.value === "ALL" ? undefined : changeTypeFilter.value as PointLedgerChangeType,
|
||
beginAt,
|
||
endAt
|
||
};
|
||
}
|
||
|
||
function buildListOptions(): { storeId: string } {
|
||
return { storeId: currentStoreStore.currentStoreId };
|
||
}
|
||
|
||
// ---------- 展示与导航 ----------
|
||
|
||
function getLedgerDelta(item: PointLedger): string {
|
||
const value = item.pointDelta ?? 0;
|
||
return `${0 < value ? "+" : "-"}${formatWanInteger(Math.abs(value))} 分`;
|
||
}
|
||
|
||
function formatPointValue(value?: number): string {
|
||
return value === undefined || value === null ? "-" : formatWanInteger(value);
|
||
}
|
||
|
||
function getLedgerShortType(item: PointLedger): string {
|
||
if (item.changeType === "EARN") {
|
||
return "获得";
|
||
}
|
||
if (item.changeType === "CONSUME") {
|
||
return "消耗";
|
||
}
|
||
return item.changeType === "REVOKE" ? "撤销" : "调整";
|
||
}
|
||
|
||
function getLedgerTheme(item: PointLedger): string {
|
||
return item.pointDelta && 0 < item.pointDelta ? "increase" : "decrease";
|
||
}
|
||
|
||
async function toDetail(item: PointLedger): Promise<void> {
|
||
if (!item.id) {
|
||
return;
|
||
}
|
||
|
||
const snapshot: PointLedger = {
|
||
id: item.id,
|
||
accountId: item.accountId,
|
||
storeId: item.storeId,
|
||
customerId: item.customerId,
|
||
changeType: item.changeType,
|
||
direction: item.direction,
|
||
pointDelta: item.pointDelta,
|
||
balanceBefore: item.balanceBefore,
|
||
balanceAfter: item.balanceAfter,
|
||
ruleId: item.ruleId,
|
||
triggerSourceType: item.triggerSourceType,
|
||
triggerSourceId: item.triggerSourceId,
|
||
salesOrderId: item.salesOrderId,
|
||
groovyScriptVersionId: item.groovyScriptVersionId,
|
||
ruleSnapshot: item.ruleSnapshot,
|
||
sourcePayload: item.sourcePayload,
|
||
idempotencyKey: item.idempotencyKey,
|
||
remark: item.remark,
|
||
operatorUserId: item.operatorUserId,
|
||
occurredAt: item.occurredAt
|
||
};
|
||
await router.push({
|
||
name: "PointLedgerDetail",
|
||
params: { id: item.id },
|
||
query: { snapshot: JSON.stringify(snapshot) }
|
||
});
|
||
}
|
||
|
||
function buildUnixRange(): [number, number] {
|
||
if (!dateRange.value[0] && !dateRange.value[1]) {
|
||
return [0, Date.now()];
|
||
}
|
||
return [
|
||
parseDateTime(dateRange.value[0], false).getTime(),
|
||
parseDateTime(dateRange.value[1], true).getTime()
|
||
];
|
||
}
|
||
|
||
// ---------- 页面数据 ----------
|
||
|
||
async function preparePage(): Promise<void> {
|
||
if (isGlobalManager.value) {
|
||
await currentStoreStore.loadStoreList();
|
||
} else {
|
||
await currentStoreStore.loadLoginStore();
|
||
}
|
||
if (selectedCustomerId.value) {
|
||
customer.value = await CustomerAPI.detail(selectedCustomerId.value);
|
||
customerKeyword.value = getCustomerSearchKeyword(customer.value);
|
||
}
|
||
await reload();
|
||
}
|
||
|
||
onMounted(async () => {
|
||
try {
|
||
await preparePage();
|
||
} catch {
|
||
Message.error("积分流水页面加载失败");
|
||
}
|
||
});
|
||
|
||
onUnmounted(() => {
|
||
searchCustomer.cancel();
|
||
});
|
||
</script>
|
||
|
||
<style scoped lang="less">
|
||
.point-ledger-list {
|
||
display: flex;
|
||
padding: var(--tui-nav-height) 0 1rem;
|
||
flex-direction: column;
|
||
|
||
|
||
.summary {
|
||
overflow: hidden;
|
||
|
||
:deep(.t-cell__title) {
|
||
flex: 0 0 4.5rem;
|
||
}
|
||
|
||
:deep(.t-cell__note) {
|
||
flex: 1 1 0;
|
||
min-width: 0;
|
||
overflow: hidden;
|
||
text-align: right;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
}
|
||
|
||
.list-wrap {
|
||
gap: 1rem;
|
||
display: flex;
|
||
flex-direction: column;
|
||
|
||
.list {
|
||
|
||
.item {
|
||
|
||
.avatar {
|
||
width: 3.5rem;
|
||
height: 3.5rem;
|
||
}
|
||
|
||
.title {
|
||
gap: .5rem;
|
||
display: flex;
|
||
|
||
.type-icon {
|
||
display: flex;
|
||
}
|
||
}
|
||
|
||
.description {
|
||
font-size: .75rem;
|
||
line-height: 1.25;
|
||
}
|
||
|
||
.delta {
|
||
font-weight: 700;
|
||
}
|
||
|
||
.increase {
|
||
color: #059669;
|
||
}
|
||
|
||
.decrease {
|
||
color: #dc2626;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
.actions {
|
||
left: 1rem;
|
||
right: 1rem;
|
||
gap: .75rem;
|
||
z-index: 20;
|
||
bottom: calc(.75rem + var(--tui-safe-bottom, 0px));
|
||
display: flex;
|
||
position: fixed;
|
||
|
||
.t-button {
|
||
flex: 1;
|
||
}
|
||
}
|
||
}
|
||
|
||
:global(html.theme-dark) {
|
||
.point-ledger-list {
|
||
.customer-id {
|
||
color: #94a3b8;
|
||
}
|
||
|
||
.customer-summary {
|
||
background: rgba(15, 23, 42, .88);
|
||
box-shadow: 0 .75rem 2rem rgba(2, 6, 23, .32);
|
||
}
|
||
|
||
.increase {
|
||
color: #6ee7b7;
|
||
background: rgba(6, 78, 59, .56);
|
||
}
|
||
|
||
.name {
|
||
color: #e2e8f0;
|
||
}
|
||
|
||
.code,
|
||
.remark {
|
||
color: #94a3b8;
|
||
}
|
||
|
||
.decrease {
|
||
color: #fca5a5;
|
||
background: rgba(127, 29, 29, .56);
|
||
}
|
||
}
|
||
}
|
||
</style>
|