551 lines
14 KiB
Vue
551 lines
14 KiB
Vue
<template>
|
|
<div class="page-layout navbar product-list">
|
|
<t-navbar
|
|
fixed
|
|
:z-index="20"
|
|
title="产品库"
|
|
left-arrow
|
|
@left-click="$router.back()"
|
|
>
|
|
<template #right>
|
|
<div
|
|
class="nav-actions"
|
|
v-if="(route.name === 'ProductList' && canCreate) && 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 ref="productListRef" v-can="permissionCodes.product.read" class="page-content scroll list">
|
|
<t-pull-down-refresh
|
|
class="pull-refresh"
|
|
v-model="isRefreshing"
|
|
:refresh-timeout="30000"
|
|
@refresh="refresh"
|
|
@scrolltolower="loadMore"
|
|
>
|
|
<div class="filters glass">
|
|
<t-input
|
|
v-model="keyword"
|
|
class="keyword"
|
|
type="search"
|
|
borderless
|
|
placeholder="搜索产品名称(拼音)或编码"
|
|
/>
|
|
<t-dropdown-menu class="menus">
|
|
<t-dropdown-item
|
|
v-model="status"
|
|
label="状态"
|
|
:options="statusOptions"
|
|
@change="selectStatus"
|
|
/>
|
|
<t-dropdown-item
|
|
v-model="productType"
|
|
label="类型"
|
|
:options="productTypeOptions"
|
|
@change="selectProductType"
|
|
/>
|
|
<t-dropdown-item
|
|
:model-value="categoryIdList"
|
|
label="分类"
|
|
:options="categoryOptions"
|
|
multiple
|
|
:options-columns="2"
|
|
@change="selectCategory"
|
|
@confirm="confirmCategory"
|
|
@reset="resetCategory"
|
|
@update:model-value="updateCategory"
|
|
/>
|
|
</t-dropdown-menu>
|
|
</div>
|
|
<t-cell-group
|
|
v-if="productList.length"
|
|
class="list fix-last-cell-line empty-cell-title-margin"
|
|
theme="card"
|
|
>
|
|
<t-cell v-for="item in productList" :key="item.id" class="item" arrow hover v-entrance-transition="{ step: 30 }" @click="toDetail(item)">
|
|
<template #leftIcon>
|
|
<t-image
|
|
v-if="getProductPhotoURL(item)"
|
|
:src="getProductPhotoURL(item)"
|
|
:alt="item.name || '产品图片'"
|
|
fit="cover"
|
|
class="avatar"
|
|
/>
|
|
<div
|
|
v-else
|
|
class="avatar fallback"
|
|
v-text="(item.name || '产').slice(0, 1)"
|
|
/>
|
|
</template>
|
|
<template #title>
|
|
<div class="text-bold" v-text="item.name || '未命名产品'" />
|
|
</template>
|
|
<template #note>
|
|
<t-tag
|
|
size="small"
|
|
variant="outline"
|
|
:theme="getProductStatusTheme(item.status)"
|
|
:content="getProductStatusText(item.status)"
|
|
/>
|
|
</template>
|
|
<template #description>
|
|
<div class="description">
|
|
<t-tag
|
|
size="small"
|
|
variant="outline"
|
|
:content="getProductTypeText(item.productType)"
|
|
/>
|
|
</div>
|
|
</template>
|
|
</t-cell>
|
|
</t-cell-group>
|
|
<center-tips v-else :content="emptyText" />
|
|
<center-tips v-if="productList.length" :content="isLoading ? '正在加载更多' : isNoMore ? '已经到底了' : '上拉加载更多'" />
|
|
</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 { useUserStore } from "timi-web-api";
|
|
import { onBeforeRouteLeave } from "vue-router";
|
|
import ProductAPI from "@/api/ProductAPI";
|
|
import type { Product, ProductCategory, ProductStatus, ProductType } from "@/types/Product";
|
|
import { getErrorMessage } from "@/utils/error";
|
|
import { getProductPhotoURL, getProductStatusText, getProductStatusTheme, getProductTypeText } from "@/utils/product";
|
|
import { events, isCurrentStoreEvent, listDataEventKeys, type ListDataChangedPayload } from "@/utils/listDataEvents";
|
|
import { Message } from "@/utils/message";
|
|
import { permissionCodes } from "@/utils/permission";
|
|
|
|
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();
|
|
}
|
|
|
|
|
|
defineOptions({ name: "ProductList" });
|
|
|
|
const router = useRouter();
|
|
const route = useRoute();
|
|
const productListRef = ref<HTMLElement | null>(null);
|
|
const userStore = useUserStore();
|
|
const canCreate = computed(() => userStore.hasPermissions(permissionCodes.product.create));
|
|
const actionList = computed<NavBarAction[]>(() => canCreate.value ? [{
|
|
key: "create",
|
|
text: "新增产品",
|
|
icon: "add",
|
|
theme: "primary",
|
|
action: toCreate
|
|
}] : []);
|
|
|
|
const keyword = ref("");
|
|
const status = ref("ALL");
|
|
const productType = ref("ALL");
|
|
const categoryIdList = ref<string[]>(["ALL"]);
|
|
const categoryList = ref<ProductCategory[]>([]);
|
|
const productList = ref<Product[]>([]);
|
|
const pageIndex = ref(0);
|
|
const total = ref(0);
|
|
const isLoading = ref(false);
|
|
const isRefreshing = ref(false);
|
|
const isNoMore = ref(false);
|
|
let keywordTimer: number | undefined;
|
|
let requestToken = 0;
|
|
let savedPageScrollTop: number | undefined;
|
|
let eventRefreshPromise: Promise<void> | undefined;
|
|
let hasPendingEventRefresh = false;
|
|
let unregisterProductListEvent: (() => void) | undefined;
|
|
let isDisposed = false;
|
|
|
|
function getProductListPage(): HTMLElement | null {
|
|
return productListRef.value?.closest<HTMLElement>(".page") || null;
|
|
}
|
|
|
|
function savePageScrollPosition(): void {
|
|
const page = getProductListPage();
|
|
if (!page) {
|
|
return;
|
|
}
|
|
|
|
savedPageScrollTop = page.scrollTop;
|
|
}
|
|
|
|
function restorePageScrollPosition(): void {
|
|
if (savedPageScrollTop === undefined) {
|
|
return;
|
|
}
|
|
|
|
const scrollTop = savedPageScrollTop;
|
|
requestAnimationFrame(() => {
|
|
const page = getProductListPage();
|
|
if (!page) {
|
|
return;
|
|
}
|
|
|
|
page.scrollTop = scrollTop;
|
|
savedPageScrollTop = undefined;
|
|
});
|
|
}
|
|
|
|
function syncNavBar(): void {
|
|
|
|
}
|
|
|
|
const statusOptions = [
|
|
{ label: "全部状态", value: "ALL" },
|
|
{ label: "启用", value: "ACTIVE" },
|
|
{ label: "停用", value: "INACTIVE" }
|
|
];
|
|
const productTypeOptions = [
|
|
{ label: "全部类型", value: "ALL" },
|
|
{ label: "服务", value: "SERVICE" },
|
|
{ label: "实物", value: "GOODS" },
|
|
{ label: "消耗品", value: "CONSUMABLE" }
|
|
];
|
|
const categoryOptions = computed(() => [{ label: "全部分类", value: "ALL" }, ...categoryList.value.map(item => ({
|
|
label: item.name || "未命名分类",
|
|
value: item.id || ""
|
|
}))]);
|
|
const emptyText = computed(() => isLoading.value ? "正在加载产品" : keyword.value ? "未找到匹配产品" : "暂无产品");
|
|
|
|
async function loadCategories(): Promise<void> {
|
|
const result = await ProductAPI.categoryList({ index: 0, size: 1000, status: "ACTIVE" });
|
|
categoryList.value = result.list || [];
|
|
}
|
|
|
|
async function fetch(reset = false, preserveScroll = false): Promise<void> {
|
|
if (!reset && (isLoading.value || isNoMore.value)) {
|
|
return;
|
|
}
|
|
const loadedPageCount = reset && preserveScroll ? Math.max(pageIndex.value, 1) : 1;
|
|
const preserveScrollTop = preserveScroll ? getProductListPage()?.scrollTop : undefined;
|
|
if (reset) {
|
|
pageIndex.value = 0;
|
|
total.value = 0;
|
|
isNoMore.value = false;
|
|
if (!preserveScroll) {
|
|
const page = getProductListPage();
|
|
if (page) {
|
|
page.scrollTop = 0;
|
|
}
|
|
}
|
|
}
|
|
const token = ++requestToken;
|
|
isLoading.value = true;
|
|
try {
|
|
const pageSize = 15;
|
|
if (reset && preserveScroll) {
|
|
const nextList: Product[] = [];
|
|
let nextTotal = 0;
|
|
let nextPageIndex = 0;
|
|
for (let index = 0; index < loadedPageCount; index += 1) {
|
|
const result = await ProductAPI.list({
|
|
index,
|
|
size: pageSize,
|
|
keyword: keyword.value || undefined,
|
|
status: status.value === "ALL" ? undefined : status.value as ProductStatus,
|
|
productType: productType.value === "ALL" ? undefined : productType.value as ProductType,
|
|
categoryIdList: categoryIdList.value.includes("ALL") ? undefined : categoryIdList.value
|
|
});
|
|
if (token !== requestToken) {
|
|
return;
|
|
}
|
|
const list = result.list || [];
|
|
nextList.push(...list);
|
|
nextTotal = result.total || 0;
|
|
nextPageIndex += 1;
|
|
if (list.length < pageSize || nextTotal <= nextList.length) {
|
|
break;
|
|
}
|
|
}
|
|
productList.value = nextList;
|
|
total.value = nextTotal;
|
|
pageIndex.value = nextPageIndex;
|
|
isNoMore.value = nextList.length < pageSize || nextList.length === nextTotal || nextTotal < nextList.length;
|
|
} else {
|
|
const result = await ProductAPI.list({
|
|
index: pageIndex.value,
|
|
size: pageSize,
|
|
keyword: keyword.value || undefined,
|
|
status: status.value === "ALL" ? undefined : status.value as ProductStatus,
|
|
productType: productType.value === "ALL" ? undefined : productType.value as ProductType,
|
|
categoryIdList: categoryIdList.value.includes("ALL") ? undefined : categoryIdList.value
|
|
});
|
|
if (token !== requestToken) {
|
|
return;
|
|
}
|
|
const list = result.list || [];
|
|
productList.value = reset ? list : productList.value.concat(list);
|
|
total.value = result.total || 0;
|
|
pageIndex.value += 1;
|
|
isNoMore.value = list.length < pageSize || productList.value.length === total.value || total.value < productList.value.length;
|
|
}
|
|
if (preserveScrollTop !== undefined) {
|
|
requestAnimationFrame(() => {
|
|
const page = getProductListPage();
|
|
if (page) {
|
|
page.scrollTop = preserveScrollTop;
|
|
}
|
|
});
|
|
}
|
|
} finally {
|
|
if (token === requestToken) {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
function startEventRefresh(): void {
|
|
if (isDisposed) {
|
|
return;
|
|
}
|
|
eventRefreshPromise = fetch(true, true)
|
|
.catch(error => Message.error(getErrorMessage(error, "产品列表加载失败")))
|
|
.finally(() => {
|
|
eventRefreshPromise = undefined;
|
|
if (!isDisposed && hasPendingEventRefresh) {
|
|
hasPendingEventRefresh = false;
|
|
startEventRefresh();
|
|
}
|
|
});
|
|
}
|
|
|
|
function requestEventRefresh(payload?: ListDataChangedPayload): void {
|
|
if (!isCurrentStoreEvent(payload)) {
|
|
return;
|
|
}
|
|
if (eventRefreshPromise) {
|
|
hasPendingEventRefresh = true;
|
|
return;
|
|
}
|
|
startEventRefresh();
|
|
}
|
|
|
|
async function refresh(): Promise<void> {
|
|
try {
|
|
await fetch(true);
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "产品列表加载失败"));
|
|
} finally {
|
|
isRefreshing.value = false;
|
|
}
|
|
}
|
|
|
|
async function loadMore(): Promise<void> {
|
|
try {
|
|
await fetch();
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "产品列表加载失败"));
|
|
}
|
|
}
|
|
|
|
async function selectStatus(value: DropdownValue): Promise<void> {
|
|
if (!Array.isArray(value)) {
|
|
status.value = String(value);
|
|
await fetch(true);
|
|
}
|
|
}
|
|
|
|
async function selectProductType(value: DropdownValue): Promise<void> {
|
|
if (!Array.isArray(value)) {
|
|
productType.value = String(value);
|
|
await fetch(true);
|
|
}
|
|
}
|
|
|
|
function getDropdownStringList(value: DropdownValue): string[] {
|
|
const valueList = Array.isArray(value) ? value : [value];
|
|
return valueList
|
|
.filter((item): item is string | number => !Array.isArray(item))
|
|
.map(item => String(item));
|
|
}
|
|
|
|
function normalizeCategoryIdList(value: DropdownValue): string[] {
|
|
const idList = getDropdownStringList(value).filter(Boolean);
|
|
if (!idList.length) {
|
|
return ["ALL"];
|
|
}
|
|
return idList.includes("ALL") && idList.length > 1 ? idList.filter(item => item !== "ALL") : idList;
|
|
}
|
|
|
|
function selectCategory(value: DropdownValue): void {
|
|
categoryIdList.value = normalizeCategoryIdList(value);
|
|
}
|
|
|
|
async function confirmCategory(value: DropdownValue): Promise<void> {
|
|
categoryIdList.value = normalizeCategoryIdList(value);
|
|
await fetch(true);
|
|
}
|
|
|
|
async function resetCategory(): Promise<void> {
|
|
categoryIdList.value = ["ALL"];
|
|
await fetch(true);
|
|
}
|
|
|
|
function updateCategory(value: DropdownValue): void {
|
|
categoryIdList.value = normalizeCategoryIdList(value);
|
|
}
|
|
|
|
async function toCreate(): Promise<void> {
|
|
await router.push("/management/products/create");
|
|
}
|
|
|
|
async function toDetail(item: Product): Promise<void> {
|
|
if (item.id) {
|
|
await router.push(`/management/products/${item.id}`);
|
|
}
|
|
}
|
|
|
|
onMounted(async () => {
|
|
unregisterProductListEvent = events.register(listDataEventKeys.productChanged, (payload: ListDataChangedPayload) => requestEventRefresh(payload));
|
|
syncNavBar();
|
|
try {
|
|
await Promise.all([loadCategories(), fetch(true)]);
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "产品列表加载失败"));
|
|
}
|
|
});
|
|
|
|
onActivated(() => {
|
|
restorePageScrollPosition();
|
|
syncNavBar();
|
|
});
|
|
|
|
onDeactivated(() => {
|
|
savePageScrollPosition();
|
|
});
|
|
|
|
watch(keyword, () => {
|
|
window.clearTimeout(keywordTimer);
|
|
keywordTimer = window.setTimeout(async () => {
|
|
try {
|
|
await fetch(true);
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "产品列表加载失败"));
|
|
}
|
|
}, 300);
|
|
});
|
|
|
|
onBeforeRouteLeave(() => {
|
|
savePageScrollPosition();
|
|
});
|
|
|
|
onUnmounted(() => {
|
|
isDisposed = true;
|
|
unregisterProductListEvent?.();
|
|
unregisterProductListEvent = undefined;
|
|
window.clearTimeout(keywordTimer);
|
|
requestToken += 1;
|
|
});
|
|
</script>
|
|
|
|
<style scoped lang="less">
|
|
.product-list {
|
|
padding: var(--app-nav-height) 0 1rem;
|
|
|
|
|
|
.list {
|
|
|
|
.item {
|
|
padding: 10px;
|
|
|
|
.avatar {
|
|
width: 3.5rem;
|
|
height: 3.5rem;
|
|
}
|
|
|
|
.description {
|
|
gap: .5rem;
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
.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>
|