Files
gao-customer-h5/src/pages/point/PointAccountList.vue
T
2026-10-19 11:43:07 +08:00

313 lines
6.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="page-layout navbar point-account-list">
<t-navbar
fixed
:z-index="20"
title="积分账户"
left-arrow
@left-click="$router.back()"
/>
<main v-can="permissionCodes.point.accountRead" class="page-content scroll 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.trim="keyword"
class="customer"
placeholder="搜索客户编码、姓名(拼音)"
borderless
>
<template #suffix-icon>
<t-button
size="extra-small"
theme="primary"
variant="outline"
content="选择客户"
@click.stop="customerSelectorVisible = true"
/>
</template>
</t-input>
</div>
<div class="list-wrap">
<t-cell-group v-if="pageResult.list.length" class="list" theme="card">
<customer-cell
v-entrance-transition="{ step: 30 }"
v-for="item in pageResult.list"
:key="item.id"
:customer="item.customer"
class="item"
arrow
hover
@click="toDetail(item)"
>
<template #description>
<span>余额:</span>
<span
class="balance"
:class="getBalanceTheme(item.balance)"
v-text="formatPoints(item.balance)" />
</template>
</customer-cell>
</t-cell-group>
<center-tips :content="loadStateText" />
</div>
</t-pull-down-refresh>
<customer-selector
v-model="selectedCustomerId"
v-model:visible="customerSelectorVisible"
@change="handleCustomerChange"
/>
</main>
</div>
</template>
<script setup lang="ts">
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 { PointAccount, PointAccountPage } from "@/types/Point";
import { Message } from "@/utils/message";
import { formatWanInteger } from "@/utils/money";
import { gaoRoleCodes, permissionCodes } from "@/utils/permission";
defineOptions({
name: "PointAccountList"
});
const router = useRouter();
const userStore = useUserStore();
const currentStoreStore = useCurrentStoreStore();
// ---------- 门店 ----------
const isGlobalManager = computed(() => userStore.hasRoles(gaoRoleCodes.globalManager));
async function prepareStoreScope(): Promise<void> {
if (isGlobalManager.value) {
await currentStoreStore.loadStoreList();
return;
}
await currentStoreStore.loadLoginStore();
}
// ---------- 筛选 ----------
const keyword = ref("");
const selectedCustomerId = ref("");
const customerSelectorVisible = ref(false);
let selectedCustomerKeyword = "";
let keywordTimer: number | undefined;
async function handleCustomerChange(value: string | string[]): Promise<void> {
selectedCustomerId.value = Array.isArray(value) ? String(value[0] || "") : value;
if (!selectedCustomerId.value) {
selectedCustomerKeyword = "";
keyword.value = "";
await reload();
return;
}
try {
const customer = await CustomerAPI.detail(selectedCustomerId.value);
selectedCustomerKeyword = customer.name?.trim() || customer.code?.trim() || "";
keyword.value = selectedCustomerKeyword;
await reload();
} catch {
selectedCustomerKeyword = "";
Message.error("客户信息加载失败");
}
}
// ---------- 分页 ----------
const page = reactive<PointAccountPage>({
index: 0,
size: 20
});
const pageResult = reactive<PageResult<PointAccount>>({
total: 0,
list: []
});
const isLoading = ref(false);
const isRefreshing = ref(false);
const isNoMore = ref(false);
let loadToken = 0;
let reloadRequested = false;
const loadStateText = computed(() => {
if (isLoading.value) {
return "正在加载积分账户";
}
if (!pageResult.list.length) {
return "暂无积分账户";
}
if (isNoMore.value) {
return `共 ${pageResult.total} 个积分账户`;
}
return "上拉加载更多";
});
async function reload(): Promise<void> {
if (isLoading.value) {
reloadRequested = true;
return;
}
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 || !currentStoreStore.currentStoreId) {
return;
}
const token = loadToken + 1;
loadToken = token;
isLoading.value = true;
try {
const result = await PointAPI.list({
...page,
keyword: keyword.value || undefined
}, { storeId: currentStoreStore.currentStoreId });
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;
}
if (reloadRequested && token === loadToken) {
reloadRequested = false;
await reload();
}
}
}
// ---------- 展示 ----------
function formatPoints(value?: number): string {
return `${formatWanInteger(value ?? 0)} 分`;
}
function getBalanceTheme(value?: number): string {
return value && 0 < value ? "positive" : "zero";
}
// ---------- 导航 ----------
async function toDetail(item: PointAccount): Promise<void> {
if (!item.id) {
return;
}
await router.push({
name: "PointAccountDetail",
params: { id: item.id },
query: item.storeId ? { storeId: item.storeId } : undefined
});
}
// ---------- 生命周期 ----------
watch(keyword, () => {
window.clearTimeout(keywordTimer);
if (selectedCustomerKeyword && keyword.value.trim() === selectedCustomerKeyword) {
selectedCustomerKeyword = "";
return;
}
selectedCustomerKeyword = "";
keywordTimer = window.setTimeout(async () => {
await reload();
}, 300);
});
onUnmounted(() => {
window.clearTimeout(keywordTimer);
});
onMounted(async () => {
try {
await prepareStoreScope();
await reload();
} catch {
Message.error("积分账户页面加载失败");
}
});
</script>
<style scoped lang="less">
.point-account-list {
padding: var(--app-nav-height) 0 1rem;
.list-wrap {
gap: 1rem;
display: flex;
min-height: 70vh;
flex-direction: column;
.list {
.item {
padding: 10px;
.description {
color: #52607a;
font-size: .8125rem;
line-height: 1.8;
}
.balance {
font-size: .875rem;
font-weight: 600;
white-space: nowrap;
}
.positive {
color: #0f766e;
}
.zero {
color: #52607a;
}
}
}
}
}
:global(html.theme-dark) {
.point-account-list {
.description,
.zero {
color: #94a3b8;
}
.positive {
color: #5eead4;
}
}
}
</style>