Files
gao-customer-h5/src/pages/recycle/RecycleItemList.vue
T
2026-11-08 20:06:15 +08:00

254 lines
6.1 KiB
Vue

<template>
<div class="tui-page-layout tui-navbar recycle-item-list">
<t-navbar
fixed
:z-index="20"
title="回收物配置"
left-arrow
@left-click="$router.back()"
>
<template #right>
<navbar-actions :actions="actions" />
</template>
</t-navbar>
<master-detail-page
v-can="permissionCodes.recycleExchange.configManage"
class="list"
:split="isWide"
:selected="!!selectedId"
:direction="paneDirection"
>
<template #master>
<t-pull-down-refresh
class="pull-refresh"
v-model="refreshing"
:refresh-timeout="30000"
@refresh="reload"
@scrolltolower="loadMore"
>
<div class="filters glass">
<t-input
v-model.trim="keyword"
class="keyword"
type="search"
borderless
clearable
placeholder="搜索回收物名称"
/>
</div>
<t-cell-group v-if="list.length" theme="card">
<t-cell
v-for="item in list"
:key="item.id"
class="item"
:arrow="!!item.id"
v-entrance-transition="{ step: 30 }"
@click="toEdit(item)"
>
<template #left-icon>
<div class="avatar fallback" v-text="(item.name || '回').slice(0, 1)" />
</template>
<template #title>
<span class="clip-text" v-text="item.name || '未命名回收物'" />
</template>
<template #note>
<t-tag
size="small"
:theme="getRecycleStatusTheme(item.status)"
:content="getRecycleStatusText(item.status)"
/>
</template>
<template #description>
<div class="description">
<t-tag size="small" variant="light-outline" :content="`按${item.unitName || '单位'}计量`" />
<span v-if="getSourceText(item)" class="source" v-text="getSourceText(item)" />
</div>
</template>
</t-cell>
</t-cell-group>
<center-tips v-else :content="loading ? '正在加载回收物' : '暂无回收物配置'" />
<center-tips :content="loading ? '正在加载' : noMore ? `共 ${total} 条配置` : '上拉加载更多'" />
</t-pull-down-refresh>
</template>
<template #detail>
<recycle-item-edit
:key="`item-${selectedId}`"
:item-id="selectedId"
@saved="reload"
/>
</template>
</master-detail-page>
</div>
</template>
<script setup lang="ts">
import { MasterDetailPage, useMasterDetailPane } from "timi-tdesign-mobile";
import { Toolkit } from "timi-web";
import RecycleAPI from "@/api/RecycleAPI";
import RecycleItemEdit from "@/pages/recycle/RecycleItemEdit.vue";
import type { NavbarAction } from "@/types/NavbarAction";
import type { RecycleItem } from "@/types/Recycle";
import { getErrorMessage } from "@/utils/error";
import { Message } from "@/utils/message";
import { permissionCodes } from "@/utils/permission";
import { getRecycleStatusText, getRecycleStatusTheme } from "@/utils/recycle";
defineOptions({ name: "RecycleItemList" });
const router = useRouter();
// 回收物没有独立详情,选中即进编辑
const { selectedId, isWide, paneDirection, openDetail } = useMasterDetailPane({
path: "/management/recycle-items",
detail: id => `/management/recycle-items/${id}`
});
const actions = computed<NavbarAction[]>(() => [
{
key: "create",
text: "新增回收物",
icon: "add",
theme: "primary",
action: toCreate
},
{
key: "rules",
text: "兑换规则",
icon: "swap",
theme: "primary",
action: toRuleList
}
]);
// ---------- 列表 ----------
const list = ref<RecycleItem[]>([]);
const keyword = ref("");
const pageIndex = ref(0);
const total = ref(0);
const loading = ref(false);
const refreshing = ref(false);
const noMore = ref(false);
const pageSize = 20;
async function fetchList(reset: boolean): Promise<void> {
if (loading.value || (!reset && noMore.value)) {
return;
}
if (reset) {
pageIndex.value = 0;
total.value = 0;
noMore.value = false;
list.value = [];
}
loading.value = true;
try {
const result = await RecycleAPI.itemList({
offset: pageIndex.value * pageSize,
limit: pageSize,
keyword: keyword.value.trim() || undefined
});
const rows = result.list || [];
list.value = reset ? rows : list.value.concat(rows);
total.value = result.total || 0;
pageIndex.value += 1;
noMore.value = rows.length < pageSize || total.value <= list.value.length;
} finally {
loading.value = false;
}
}
async function reload(): Promise<void> {
try {
await fetchList(true);
} catch (error) {
Message.error(getErrorMessage(error, "回收物配置加载失败"));
} finally {
refreshing.value = false;
}
}
async function loadMore(): Promise<void> {
try {
await fetchList(false);
} catch (error) {
Message.error(getErrorMessage(error, "回收物配置加载失败"));
}
}
const reloadByKeyword = Toolkit.debounce(async () => {
await reload();
}, false);
/** 来源产品名称由服务端回填,仅用于识别,不做库存和销售校验。 */
function getSourceText(item: RecycleItem): string {
const productName = item.sourceProductName || "";
const skuText = item.sourceSkuName || "";
return [productName, skuText && skuText !== productName ? skuText : ""].filter(Boolean).join(" · ");
}
// ---------- 跳转 ----------
function toEdit(item: RecycleItem): void {
openDetail(item.id || "");
}
async function toCreate(): Promise<void> {
await router.push({ name: "RecycleItemCreate" });
}
async function toRuleList(): Promise<void> {
await router.push({ name: "RecycleRuleList" });
}
onMounted(reload);
watch(keyword, () => {
reloadByKeyword();
});
onUnmounted(() => {
reloadByKeyword.cancel();
});
</script>
<style scoped lang="less">
.recycle-item-list {
display: flex;
padding: var(--tui-nav-height) 0 5rem;
flex-direction: column;
.item {
padding: 10px;
.avatar {
width: 2.5rem;
height: 2.5rem;
font-size: 1rem;
}
.description {
gap: .5rem;
display: flex;
min-width: 0;
align-items: center;
.source {
min-width: 0;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
}
}
}
:global(html.theme-dark) {
.recycle-item-list {
.fallback {
color: #ccfbf1;
background: #115e59;
}
}
}
</style>