Files
gao-customer-h5/src/pages/recycle/components/RecycleItemPickerPopup.vue
T

170 lines
4.8 KiB
Vue

<template>
<bottom-popup
v-model="visible"
:title="props.title"
height="70vh"
right-button-text=""
:z-index="12100"
:overlay-props="{ zIndex: 12099 }"
@left-button-click="visible = false"
>
<template v-if="props.manage" #rightButton>
<t-button theme="primary" variant="text" size="small" content="新建" :disabled="isLoading" @click="openEdit()" />
</template>
<div class="picker-list">
<t-cell-group>
<t-swipe-cell v-for="item in displayedItems" :key="item.id">
<template #right>
<t-button v-if="props.manage" class="edit-button" theme="primary" content="编辑" :disabled="isLoading" @click.stop="openEdit(item.id)" />
</template>
<t-cell
:title="item.name || '未命名回收物'"
:description="`按${item.unitName || '单位'}计量`"
class="item"
:class="{ active: item.id === props.selectedId }"
arrow
@click="select(item)"
>
<template #note>
<t-tag v-if="props.showStatus" size="small" :theme="getRecycleStatusTheme(item.status)" :content="getRecycleStatusText(item.status)" />
</template>
<template #leftIcon>
<t-image v-if="getCoverId(item)" class="avatar" :src="getAttachmentReadURL(getCoverId(item), 96)" :alt="item.name" fit="cover" />
<div v-else class="avatar fallback" v-text="(item.name || '产').slice(0, 1)" />
</template>
</t-cell>
</t-swipe-cell>
</t-cell-group>
<center-tips v-if="!displayedItems.length" :content="isLoading ? '正在加载回收物' : props.emptyText" />
</div>
<recycle-item-edit-popup v-model:visible="editVisible" :item-id="editingId" :z-index="12300" @saved="handleSaved" />
</bottom-popup>
</template>
<script setup lang="ts">
import { BottomPopup } from "timi-tdesign-mobile";
import RecycleAPI from "@/api/RecycleAPI";
import RecycleItemEditPopup from "@/pages/recycle/components/RecycleItemEditPopup.vue";
import type { RecycleItem, RecycleStatus } from "@/types/Recycle";
import { getAttachmentReadURL } from "@/utils/attachment";
import { getErrorMessage } from "@/utils/error";
import { Message } from "@/utils/message";
import { getRecycleStatusText, getRecycleStatusTheme } from "@/utils/recycle";
defineOptions({ name: "RecycleItemPickerPopup" });
const props = withDefaults(defineProps<{
title?: string;
manage?: boolean;
showStatus?: boolean;
accountItems?: boolean;
selectedId?: string;
items?: RecycleItem[];
loading?: boolean;
status?: RecycleStatus;
emptyText?: string;
}>(), {
title: "选择回收物",
manage: false,
showStatus: false,
accountItems: false,
selectedId: "",
loading: false,
emptyText: "暂无回收物"
});
const visible = defineModel<boolean>("visible", { default: false });
const emit = defineEmits<{ select: [item: RecycleItem]; saved: [] }>();
const loadedItems = ref<RecycleItem[]>([]);
const internalLoading = ref(false);
const displayedItems = computed(() => props.items ?? loadedItems.value);
const isLoading = computed(() => props.items ? props.loading : internalLoading.value);
const editVisible = ref(false);
const editingId = ref("");
function getCoverId(item: RecycleItem): string {
return item.attachmentList?.find(attachment => attachment.id && attachment.mimeType?.startsWith("image/"))?.id || "";
}
async function load(): Promise<void> {
if (internalLoading.value) return;
internalLoading.value = true;
try {
if (props.accountItems) {
loadedItems.value = await RecycleAPI.accountItems();
return;
}
const result: RecycleItem[] = [];
let offset = 0;
let total = 0;
do {
const page = await RecycleAPI.itemList({ offset, limit: 100, status: props.status });
const rows = page.list || [];
result.push(...rows);
total = page.total || 0;
offset += rows.length;
if (!rows.length) break;
} while (offset < total);
loadedItems.value = result;
} catch (error) {
Message.error(getErrorMessage(error, "回收物加载失败"));
} finally {
internalLoading.value = false;
}
}
async function handleSaved(): Promise<void> {
if (!props.items) await load();
emit("saved");
}
function openEdit(id = ""): void {
editingId.value = id;
editVisible.value = true;
}
function select(item: RecycleItem): void {
visible.value = false;
emit("select", item);
}
watch(visible, async value => {
if (value && !props.items) await load();
});
</script>
<style scoped lang="less">
.picker-list {
.item.active {
background: var(--td-brand-color-1, rgba(0, 82, 217, .08));
}
.edit-button {
width: 4rem;
height: 100%;
}
.avatar {
width: 3.5rem;
height: 3.5rem;
}
.title {
gap: 1rem;
display: flex;
align-items: center;
.unit {
font-size: .875rem;
}
}
}
:global(html.theme-dark) .picker-list .item.active {
background: rgba(15, 118, 110, .22);
}
:deep(.avatar .t-image__img) {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: inherit;
}
</style>