fix: ProductStockCalendar.vue style

This commit is contained in:
Timi
2026-11-12 17:48:51 +08:00
parent f0672826ff
commit fd56908c57
2 changed files with 260 additions and 32 deletions
+1
View File
@@ -45,6 +45,7 @@ declare module 'vue' {
SegmentedSlider: typeof import('./src/components/SegmentedSlider.vue')['default']
StoreRemoveDialog: typeof import('./src/components/StoreRemoveDialog.vue')['default']
TagSelector: typeof import('./src/components/TagSelector.vue')['default']
TAvatar: typeof import('tdesign-mobile-vue')['Avatar']
TBadge: typeof import('tdesign-mobile-vue')['Badge']
TButton: typeof import('tdesign-mobile-vue')['Button']
TCalendar: typeof import('tdesign-mobile-vue')['Calendar']
@@ -10,18 +10,38 @@
content="产品规格"
@click="selectorVisible = true"
>
<template #icon><t-icon name="filter" /></template>
<template #icon>
<t-icon name="filter" />
</template>
</t-button>
</template>
<template #subtitle>
<div class="selection">
<span v-text="selectionText" />
<t-button v-if="selection" size="extra-small" variant="text" @click="clearSelection">清除</t-button>
<t-button
v-if="selection && 1 < units.length"
size="extra-small"
variant="text"
@click="nextUnit"
:content="`单位:${currentUnit?.name}`"
/>
<t-button
v-if="selection"
size="extra-small"
variant="outline"
content="清除"
@click="clearSelection"
/>
</div>
</template>
<template #day="{ day }">
<span v-if="daily(day)?.inQuantity" class="quantity in" v-text="`入 ${daily(day)?.inQuantity}`" />
<span v-if="daily(day)?.outQuantity" class="quantity out" v-text="`出 ${daily(day)?.outQuantity}`" />
<template v-if="selection">
<span v-if="daily(day)?.inQuantity" class="quantity in"
v-text="formatQuantity(daily(day)?.inQuantity)" />
<span v-if="daily(day)?.outQuantity" class="quantity out"
v-text="formatQuantity(daily(day)?.outQuantity)" />
</template>
<span class="movement-bar" :style="movementStyle(day)" />
</template>
</month-calendar>
<t-loading v-if="loading" text="加载中" />
@@ -31,45 +51,78 @@
v-model:visible="selectorVisible"
@select="selectProduct"
/>
<bottom-popup v-model="visible" :title="`${selectedDate} 库存流水`" left-button-text="关闭" @left-button-click="visible = false">
<div v-if="canReadRecords" class="records">
<div class="directions">
<t-button size="small" :theme="direction === 'IN' ? 'primary' : 'default'" @click="selectDirection('IN')">入库</t-button>
<t-button size="small" :theme="direction === 'OUT' ? 'primary' : 'default'" @click="selectDirection('OUT')">出库</t-button>
</div>
<t-cell-group v-if="records.length">
<t-cell
v-for="record in records"
:key="record.id"
:title="record.productNameSnapshot || '未命名产品'"
:description="record.skuNameSnapshot || ''"
:note="`${getStockOperationText(record.operationType)} ${record.onHandDelta || 0}(基础单位)`"
/>
<bottom-popup
v-model="visible"
:title="`${selectedDate} 库存流水`"
left-button-text="关闭"
@left-button-click="visible = false"
>
<div class="records">
<template v-if="canReadRecords">
<div class="directions">
<t-segmented block :value="direction" :options="directionOptions" @change="changeDirection" />
</div>
<div class="record-list">
<t-cell-group v-if="records.length" class="list empty-cell-title-margin" theme="card">
<t-cell v-for="record in records" :key="record.id" class="item">
<template #leftIcon>
<t-image v-if="getCustomerPhotoURL(record.customer, 64)" :src="getCustomerPhotoURL(record.customer, 64)"
:alt="getCustomerText(record)" fit="cover" class="avatar" />
<div v-else-if="record.customer" class="avatar fallback" v-text="getCustomerText(record).slice(0, 1)" />
<t-image v-else-if="record.operator" :src="getOperatorAvatar(record)" :alt="getOperatorText(record)" fit="cover" class="avatar" />
<span v-else class="icon" :class="getDeltaClass(record.onHandDelta)"><t-icon name="history" size="1.25rem" /></span>
</template>
<template #title>
<div class="record-title">
<span v-if="record.customer" class="actor" v-text="getCustomerText(record)" />
<span v-else-if="record.operator" class="actor" v-text="getOperatorText(record)" />
<strong class="operation" :class="getOperationClass(record.operationType)" v-text="getStockOperationText(record.operationType)" />
<strong class="product-name" v-text="record.productNameSnapshot || '未命名产品'" />
<t-tag v-if="record.skuNameSnapshot && record.productNameSnapshot !== record.skuNameSnapshot"
size="small" :content="record.skuNameSnapshot" />
<span v-if="record.quantity !== undefined && record.quantity !== null && record.quantity !== ''"
class="record-quantity" v-text="`${getProductQuantityText(record.quantity)} ${record.unitSnapshot || ''}`" />
</div>
</template>
<template #description>
<div class="description">
<time-label :timestamp="record.occurredAt" underline />
<div><span class="delta" :class="getDeltaClass(record.onHandDelta)" v-text="getDeltaText(record.onHandDelta)" /></div>
</div>
</template>
</t-cell>
</t-cell-group>
<t-empty v-else-if="!recordsLoading" description="当天暂无流水" />
<t-button v-if="!noMore" block variant="text" :loading="recordsLoading" @click="loadRecords">加载更多</t-button>
<t-button v-if="!noMore" block variant="text" :loading="recordsLoading" @click="loadRecords">加载更多
</t-button>
</div>
</template>
<t-empty v-else description="暂无库存流水查看权限" />
</div>
<t-empty v-else description="暂无库存流水查看权限" />
</bottom-popup>
</section>
</template>
<script setup lang="ts">
import { BottomPopup } from "timi-tdesign-mobile";
import { useUserStore } from "timi-web-api";
import { TimeLabel } from "timi-web";
import { UserAPI, useUserStore } from "timi-web-api";
import GaoProductChartAPI from "@/api/GaoProductChartAPI";
import GaoProductSkuChartAPI from "@/api/GaoProductSkuChartAPI";
import ProductAPI from "@/api/ProductAPI";
import StoreChartAPI from "@/api/StoreChartAPI";
import MonthCalendar from "@/components/monthCalendar/MonthCalendar.vue";
import ProductSelectPopup from "@/components/productSelector/ProductSelectPopup.vue";
import { buildMonthCalendarDays, type MonthCalendarDay } from "@/components/monthCalendar/monthCalendar";
import type { GaoProductStockMovementDailyStatView } from "@/types/GaoChart";
import type { ProductSelection, StockRecord, StockRecordDirection } from "@/types/Product";
import type { ProductSkuSelectionValue } from "@/types/ProductSkuSelection";
import { getProductBaseUnitName, getStockOperationText } from "@/utils/product";
import { getProductQuantityText, getStockOperationText } from "@/utils/product";
import { buildFallbackAvatar } from "@/utils/avatar";
import { permissionCodes } from "@/utils/permission";
import { getErrorMessage } from "@/utils/error";
import { Message } from "@/utils/message";
import { getCustomerPhotoURL } from "@/utils/customer";
const props = defineProps<{ storeId: string }>();
const userStore = useUserStore();
@@ -80,15 +133,68 @@ const selectedProductSku = ref<ProductSkuSelectionValue>();
const selection = ref<ProductSelection>();
const skuId = computed(() => selection.value?.sku.id || "");
const selectionText = computed(() => {
if (!selection.value) return "全部产品规格 · 数量按各规格基础单位汇总";
if (!selection.value) return "全部产品规格 · 颜色表示出入库情况";
const { product, sku } = selection.value;
const name = sku.specSummary || (sku.name !== product.name ? sku.name : "");
return [product.name || product.code, name, `基础单位:${getProductBaseUnitName(sku) || "未知"}`].filter(Boolean).join(" · ");
return [
product.name || product.code,
name
].filter(Boolean).join(" · ");
});
const unitIndex = ref(0);
const units = computed(() => selection.value?.sku.unitList || []);
const currentUnit = computed(() => units.value[unitIndex.value]);
function getCustomerText(record: StockRecord): string {
return record.customer?.name || record.customer?.code || "未知客户";
}
function getOperatorText(record: StockRecord): string {
return record.operator?.nick || record.operator?.name || "未知操作人";
}
function getOperatorAvatar(record: StockRecord): string {
return UserAPI.getAvatarURL(record.operator) || buildFallbackAvatar(getOperatorText(record), "#0f766e");
}
function getOperationClass(type?: string): string {
return ({
INITIAL_IN: "initial-in", MANUAL_IN: "manual-in", MANUAL_OUT: "manual-out", MANUAL_REVOKE: "manual-revoke",
ORDER_RESERVE: "order-reserve", ORDER_RELEASE: "order-release", ORDER_OUT: "order-out", ORDER_REVOKE: "order-revoke",
ORDER_RETURN_IN: "return-in", POINT_REDEMPTION_OUT: "manual-out", POINT_REDEMPTION_RETURN_IN: "return-in",
STOCKTAKE_GAIN: "stocktake-gain", STOCKTAKE_LOSS: "stocktake-loss"
} as Record<string, string>)[type || ""] || "unknown";
}
function getDeltaClass(value?: number | string): string {
const amount = Number(value || 0);
return 0 < amount ? "positive" : amount < 0 ? "negative" : "gray";
}
function getDeltaText(value?: number | string): string {
const amount = Number(value || 0);
return `${0 < amount ? "+" : amount < 0 ? "-" : ""}${getProductQuantityText(String(value || "0").replace(/^-/, ""))}`;
}
function nextUnit(): void {
unitIndex.value = (unitIndex.value + 1) % units.value.length;
}
function formatQuantity(value: number | string | undefined): string {
const ratio = Number(currentUnit.value?.baseQuantityPerUnit) || 1;
return String(Number((Number(value || 0) / ratio).toFixed(4)));
}
function movementStyle(day: MonthCalendarDay): Record<string, string> {
const item = daily(day);
const incoming = Math.abs(Number(item?.inQuantity) || 0);
const outgoing = Math.abs(Number(item?.outQuantity) || 0);
if (!incoming && !outgoing) return { background: "var(--td-component-stroke)" };
const percentage = incoming / (incoming + outgoing) * 100;
return { background: `linear-gradient(90deg, var(--tui-green) ${percentage}%, var(--tui-red) ${percentage}%)` };
}
function selectProduct(value: ProductSelection): void {
if (!value.sku.id) return;
selection.value = value;
unitIndex.value = Math.max(0, (value.sku.unitList || []).findIndex(unit => unit.id === value.unit.id));
}
function clearSelection(): void {
selection.value = undefined;
selectedProductSku.value = undefined;
@@ -98,13 +204,27 @@ const month = ref(new Date(new Date().getFullYear(), new Date().getMonth(), 1));
const stats = ref<GaoProductStockMovementDailyStatView[]>([]);
const loading = ref(false);
let monthToken = 0;
let restDayToken = 0;
const restDaySet = ref<Set<number>>(new Set());
watch([month, () => props.storeId], async () => {
const token = ++restDayToken;
const days = buildMonthCalendarDays(month.value);
try {
const list = await StoreChartAPI.businessDayCalendar(props.storeId, Number(days[0].dateText.replaceAll("-", "")), Number(days[days.length - 1].dateText.replaceAll("-", "")));
if (token === restDayToken) restDaySet.value = new Set(list.filter(item => item.status === "CLOSED" && item.dateValue).map(item => item.dateValue as number));
} catch {
if (token === restDayToken) restDaySet.value = new Set();
}
}, { immediate: true });
const calendarDays = computed(() => buildMonthCalendarDays(month.value).map(day => ({
...day,
marked: !!(daily(day)?.inQuantity || daily(day)?.outQuantity)
isRestDay: restDaySet.value.has(Number(day.dateText.replaceAll("-", "")))
})));
function daily(day: MonthCalendarDay): GaoProductStockMovementDailyStatView | undefined {
return stats.value.find(item => item.dateValue === Number(day.dateText.replaceAll("-", "")));
}
watch([month, () => props.storeId, skuId], async () => {
const token = ++monthToken;
stats.value = [];
@@ -130,11 +250,16 @@ const visible = ref(false);
const selectedDate = ref("");
const selectedStart = ref(0);
const direction = ref<StockRecordDirection>("IN");
const directionOptions = [{ label: "入库", value: "IN" }, { label: "出库", value: "OUT" }];
async function changeDirection(event: { value: string | number }): Promise<void> {
if (event.value === "IN" || event.value === "OUT") await selectDirection(event.value);
}
const records = ref<StockRecord[]>([]);
const recordsLoading = ref(false);
const noMore = ref(false);
let page = 0;
let recordToken = 0;
async function openDay(day: MonthCalendarDay): Promise<void> {
const [year, monthNumber, dateNumber] = day.dateText.split("-").map(Number);
const date = new Date(year, monthNumber - 1, dateNumber);
@@ -143,6 +268,7 @@ async function openDay(day: MonthCalendarDay): Promise<void> {
visible.value = true;
if (canReadRecords.value) await selectDirection("IN");
}
async function selectDirection(value: StockRecordDirection): Promise<void> {
direction.value = value;
recordToken++;
@@ -152,6 +278,7 @@ async function selectDirection(value: StockRecordDirection): Promise<void> {
recordsLoading.value = false;
await loadRecords();
}
async function loadRecords(): Promise<void> {
if (recordsLoading.value || noMore.value) return;
const token = ++recordToken;
@@ -159,7 +286,16 @@ async function loadRecords(): Promise<void> {
try {
const start = new Date(selectedStart.value);
const endAt = new Date(start.getFullYear(), start.getMonth(), start.getDate() + 1).getTime() - 1;
const result = await ProductAPI.recordList({ storeId: props.storeId, skuId: skuId.value || undefined, beginAt: selectedStart.value, endAt, direction: direction.value, index: page, size: 15 });
const result = await ProductAPI.recordList({
storeId: props.storeId,
productId: selection.value?.product.id || undefined,
skuId: skuId.value || undefined,
beginAt: selectedStart.value,
endAt,
direction: direction.value,
index: page,
size: 15
});
if (token !== recordToken) return;
const list = result.list || [];
records.value.push(...list);
@@ -171,6 +307,7 @@ async function loadRecords(): Promise<void> {
if (token === recordToken) recordsLoading.value = false;
}
}
watch([() => props.storeId, skuId], () => {
visible.value = false;
recordToken++;
@@ -193,19 +330,109 @@ watch(() => props.storeId, () => {
color: var(--td-text-color-secondary);
font-size: .75rem;
align-items: center;
justify-content: space-between;
justify-content: end;
}
.movement-bar {
width: 100%;
height: .25rem;
flex-shrink: 0;
margin-top: auto;
border-radius: .125rem;
}
.quantity {
margin-bottom: .125rem;
}
.quantity {
overflow: hidden;
max-width: 100%;
font-size: .625rem;
white-space: nowrap;
text-overflow: ellipsis;
&.in { color: var(--td-success-color); }
&.out { color: var(--td-error-color); }
&.in {
color: var(--td-success-color);
}
&.out {
color: var(--td-error-color);
}
}
.records { overflow: auto; max-height: 60vh; }
.directions { display: flex; gap: .5rem; padding: .75rem; }
}
.records {
height: 60vh;
display: flex;
min-height: 0;
flex-direction: column;
.directions {
padding: .75rem;
min-width: 0;
flex-shrink: 0;
}
.record-list {
overflow-x: hidden;
overflow-y: auto;
min-width: 0;
min-height: 0;
flex: 1;
}
.avatar {
width: 3.5rem;
height: 3.5rem;
}
.list { margin: 0; }
.item { padding: .625rem; min-width: 0; }
.icon { width: 2.25rem; height: 2.25rem; display: inline-flex; background: var(--td-bg-color-secondarycontainer); align-items: center; border-radius: 50%; justify-content: center; }
.record-title {
gap: .25rem;
display: flex;
min-width: 0;
max-width: 100%;
font-size: .75rem;
align-items: center;
white-space: nowrap;
.actor { flex: 0 0 auto; color: var(--td-text-color-brand); }
.operation, .record-quantity { flex: 0 0 auto; }
.operation {
color: var(--operation-color, var(--td-text-color-secondary));
&.initial-in { --operation-color: #2563eb; }
&.manual-in { --operation-color: #15803d; }
&.manual-out { --operation-color: #c2410c; }
&.manual-revoke { --operation-color: #be123c; }
&.order-reserve { --operation-color: #7c3aed; }
&.order-release { --operation-color: #0891b2; }
&.order-out { --operation-color: #dc2626; }
&.order-revoke { --operation-color: #4f46e5; }
&.return-in { --operation-color: #15803d; }
&.stocktake-gain { --operation-color: #0f766e; }
&.stocktake-loss { --operation-color: #a16207; }
}
.product-name { overflow: hidden; flex: 0 1 auto; text-overflow: ellipsis; }
.record-quantity { color: var(--td-text-color-secondary); }
}
.description { gap: .5rem; display: flex; font-size: .75rem; align-items: center; justify-content: space-between; }
.delta { font-size: 1.25rem; }
.positive { color: var(--td-success-color); }
.negative { color: var(--td-error-color); }
}
:global(html.theme-dark) .records .operation {
&.initial-in { --operation-color: #93c5fd; }
&.manual-in, &.return-in { --operation-color: #86efac; }
&.manual-out { --operation-color: #fdba74; }
&.manual-revoke { --operation-color: #fda4af; }
&.order-reserve { --operation-color: #c4b5fd; }
&.order-release { --operation-color: #67e8f9; }
&.order-out { --operation-color: #fca5a5; }
&.order-revoke { --operation-color: #a5b4fc; }
&.stocktake-gain { --operation-color: #5eead4; }
&.stocktake-loss { --operation-color: #fcd34d; }
}
</style>