fix PackageConfig*.vue style

This commit is contained in:
Timi
2026-09-06 01:10:22 +08:00
parent 3698f7151f
commit d929dbe622
7 changed files with 472 additions and 142 deletions
+211 -57
View File
@@ -1,9 +1,6 @@
<template>
<div v-can="savePermissionCode" class="package-config-edit">
<t-form class="form" label-align="left" label-width="6rem" v-entrance-transition>
<t-form-item v-if="isGlobalManager" label="所属门店" arrow @click="openStorePicker">
<span class="picker-text" v-text="selectedStoreName" />
</t-form-item>
<t-form-item label="套餐名称" required-mark>
<t-input v-model="form.name" borderless maxlength="100" placeholder="请输入套餐名称" />
</t-form-item>
@@ -36,33 +33,75 @@
label-width="6rem"
>
<div class="header">
<span class="no" v-text="String(index + 1).padStart(2, '0')" />
<strong v-text="item.productNameSnapshot || '未选择产品'" />
<t-button theme="danger" variant="text" size="small" @click="removeItem(index)">删除</t-button>
<t-button
v-if="1 < form.itemList.length"
theme="danger"
variant="text"
size="small"
@click="removeItem(index)"
>删除</t-button>
</div>
<product-selector
:model-value="item.productId && item.productUnitId ? {productId: item.productId, productUnitId: item.productUnitId} : undefined"
label="产品单位"
:model-value="item.productId ? {productId: item.productId, productUnitId: item.productUnitId || ''} : undefined"
:selected-product="getProduct(item)"
label="产品"
placeholder="请选择产品"
required-mark
@update:model-value="applyProduct(index, $event)"
/>
<t-form-item label="默认配额" required-mark>
<t-input v-model.trim="item.defaultQuantity" type="number" borderless />
<t-form-item v-if="item.productId" label="产品单位" required-mark>
<div v-if="getSaleableUnits(item).length" class="unit-list">
<t-button
v-for="unit in getSaleableUnits(item)"
:key="unit.id"
:theme="item.productUnitId === unit.id ? 'primary' : 'default'"
:variant="item.productUnitId === unit.id ? 'base' : 'outline'"
size="extra-small"
:content="unit.name"
@click="selectProductUnit(index, unit)"
/>
</div>
<span v-else class="unit-empty" v-text="'暂无可售单位'" />
</t-form-item>
<t-form-item label="默认配额" content-align="right" required-mark>
<t-stepper
v-model="item.defaultQuantity"
size="large"
:max="Number.MAX_SAFE_INTEGER"
:min="getQuantityStep(item)"
:step="getQuantityStep(item)"
:integer="!getProduct(item)?.allowDecimalQuantity"
theme="outline"
/>
</t-form-item>
<t-form-item label="备注">
<t-input v-model="item.remark" borderless />
<t-input v-model.trim="item.remark" borderless placeholder="选填" />
</t-form-item>
<div class="price">
<span>预估价(原价)</span>
<strong class="red" v-text="getItemOriginalAmountText(item)" />
</div>
</t-form>
<t-button
class="cell-button"
theme="primary"
class="add"
type="button"
theme="light"
variant="dashed"
@click="addItem"
content="添加产品权益"
/>
>
<template #icon>
<t-icon name="add" size="1.125rem" />
</template>
</t-button>
<div class="summary">
<span>产品权益预估价(原价)</span>
<strong class="red" v-text="estimatedOriginalAmountText" />
</div>
</div>
</card-container>
<t-popup v-model="storePickerVisible" placement="bottom">
<t-picker v-model="storePickerDraft" :columns="storePickerColumns" title="选择门店" @cancel="storePickerVisible = false" @confirm="confirmStore" />
</t-popup>
<t-popup v-model="statusPickerVisible" placement="bottom">
<t-picker v-model="statusPickerDraft" :columns="statusOptions" title="状态" @cancel="statusPickerVisible = false" @confirm="confirmStatus" />
</t-popup>
@@ -83,13 +122,13 @@ import { useCurrentStoreStore } from "@/store/currentStoreStore";
import { useNavBarStore } from "@/store/navBarStore";
import type { GaoPackageConfig, PackageConfigItem, PackageConfigStatus } from "@/types/Package";
import ProductAPI from "@/api/ProductAPI";
import type { Product } from "@/types/Product";
import type { Product, ProductUnit } from "@/types/Product";
import { getErrorMessage } from "@/utils/error";
import { formatPackageAmountInput, getPackageConfigStatusText, normalizePackageQuantity, parsePackageAmount } from "@/utils/package";
import { Message } from "@/utils/message";
import { gaoRoleCodes, permissionCodes } from "@/utils/permission";
import { formatProductAmount, getProductQuantityStep } from "@/utils/product";
type PickerValue = string | number;
type PackageConfigFormItem = Partial<PackageConfigItem> & { clientKey: string };
defineOptions({ name: "PackageConfigEdit" });
@@ -117,10 +156,8 @@ const form = reactive({
const standardAmountText = ref("");
const beginAtText = ref("");
const endAtText = ref("");
const storePickerVisible = ref(false);
const storePickerDraft = ref<PickerValue[]>([]);
const statusPickerVisible = ref(false);
const statusPickerDraft = ref<PickerValue[]>([]);
const statusPickerDraft = ref<Array<string | number>>([]);
const beginPickerVisible = ref(false);
const endPickerVisible = ref(false);
const beginDraft = ref("");
@@ -130,8 +167,6 @@ const statusOptions = [
{ label: "启用", value: "ACTIVE" },
{ label: "停用", value: "INACTIVE" }
];
const storePickerColumns = computed(() => [[...currentStoreStore.storeList].map(item => ({ label: item.name || item.code || item.id || "未知门店", value: item.id || "" }))]);
const selectedStoreName = computed(() => currentStoreStore.storeList.find(item => item.id === form.storeId)?.name || form.storeId || "请选择门店");
function createItem(item?: Partial<PackageConfigItem>): PackageConfigFormItem {
return {
@@ -144,29 +179,87 @@ function createItem(item?: Partial<PackageConfigItem>): PackageConfigFormItem {
const productCache = new Map<string, Product>();
function getProduct(item: PackageConfigFormItem): Product | undefined {
return productCache.get(item.productId || "");
}
function getQuantityStep(item: PackageConfigFormItem): number {
return getProductQuantityStep(getProduct(item));
}
const estimatedOriginalAmount = computed<number | undefined>(() => {
let amount = 0;
for (const item of form.itemList) {
const itemAmount = calculateOriginalAmount(item);
if (itemAmount === undefined) {
return undefined;
}
amount += itemAmount;
}
return amount;
});
const estimatedOriginalAmountText = computed(() => estimatedOriginalAmount.value === undefined ? "待完善" : `¥${formatProductAmount(estimatedOriginalAmount.value)}`);
async function applyProduct(index: number, value?: { productId: string; productUnitId: string }): Promise<void> {
if (!value) {
return;
}
const product = productCache.get(value.productId) || await ProductAPI.detail(value.productId);
productCache.set(value.productId, product);
const unit = product.unitList?.find(item => item.id === value.productUnitId);
const saleableUnits = (product.unitList || []).filter(unit => unit.allowSale !== false && unit.id);
const productUnitId = saleableUnits.length < 2 ? saleableUnits[0]?.id || "" : value.productUnitId;
const unit = saleableUnits.find(item => item.id === productUnitId);
Object.assign(form.itemList[index], {
productId: value.productId,
productUnitId: value.productUnitId,
productUnitId,
productNameSnapshot: product.name,
unitSnapshot: unit?.name || "",
baseQuantityPerUnitSnapshot: unit?.baseQuantityPerUnit
});
}
function addItem(): void {
form.itemList.push(createItem());
function getSaleableUnits(item: PackageConfigFormItem): ProductUnit[] {
return (productCache.get(item.productId || "")?.unitList || []).filter(unit => unit.allowSale !== false && unit.id);
}
function openStorePicker(): void {
storePickerDraft.value = [form.storeId];
storePickerVisible.value = true;
function selectProductUnit(index: number, unit: ProductUnit): void {
const item = form.itemList[index];
const product = productCache.get(item.productId || "");
if (!product?.id || !unit.id) {
return;
}
Object.assign(item, {
productUnitId: unit.id,
productNameSnapshot: product.name,
unitSnapshot: unit.name,
baseQuantityPerUnitSnapshot: unit.baseQuantityPerUnit
});
}
function calculateOriginalAmount(item: PackageConfigFormItem): number | undefined {
const quantity = normalizePackageQuantity(item.defaultQuantity ?? "");
const salePrice = productCache.get(item.productId || "")?.unitList?.find(unit => unit.id === item.productUnitId)?.salePrice;
if (!quantity || quantity === "0" || salePrice === undefined || salePrice === null) {
return undefined;
}
const [integerPart, fractionPart = ""] = quantity.split(".");
const quantityScale = 10n ** BigInt(fractionPart.length);
const quantityValue = BigInt(`${integerPart}${fractionPart}`);
const amount = BigInt(salePrice) * quantityValue;
if (amount % quantityScale !== 0n) {
return undefined;
}
const result = Number(amount / quantityScale);
return Number.isSafeInteger(result) ? result : undefined;
}
function getItemOriginalAmountText(item: PackageConfigFormItem): string {
const amount = calculateOriginalAmount(item);
return amount === undefined ? "待完善" : `¥${formatProductAmount(amount)}`;
}
function addItem(): void {
form.itemList.push(createItem());
}
function openStatusPicker(): void {
@@ -182,14 +275,6 @@ function removeItem(index: number): void {
Message.warning("套餐至少需要一项产品权益");
}
function confirmStore(value: Array<string | number>): void {
const storeId = String(value[0] || "");
if (storeId) {
form.storeId = storeId;
}
storePickerVisible.value = false;
}
function confirmStatus(value: Array<string | number>): void {
const status = String(value[0] || "");
if (status === "DRAFT" || status === "ACTIVE" || status === "INACTIVE") {
@@ -227,8 +312,8 @@ function resolveTimestamp(value: string): number | undefined {
}
function validateForm(): boolean {
if (isGlobalManager.value && !form.storeId) {
Message.warning("请选择所属门店");
if (!form.storeId) {
Message.warning("当前门店未就绪");
return false;
}
if (!form.name.trim()) {
@@ -251,6 +336,10 @@ function validateForm(): boolean {
Message.warning("产品单位和正数量不能为空");
return false;
}
if (getProduct(item) && !getProduct(item)!.allowDecimalQuantity && quantity.includes(".")) {
Message.warning("该产品不允许小数数量");
return false;
}
if (keySet.has(key)) {
Message.warning("产品名称和单位组合不能重复");
return false;
@@ -311,23 +400,27 @@ async function loadData(): Promise<void> {
} else {
await currentStoreStore.loadLoginStore();
}
if (!form.storeId) {
form.storeId = currentStoreStore.currentStoreId;
}
const currentStoreId = currentStoreStore.currentStoreId;
if (isCreateMode.value) {
form.storeId = currentStoreId;
addItem();
return;
}
const result = await PackageAPI.configDetail(routeId.value);
const itemList = (result.itemList || []).map(createItem);
const productIdList = [...new Set(itemList.map(item => item.productId).filter((id): id is string => !!id))];
await Promise.all(productIdList.map(async productId => {
productCache.set(productId, await ProductAPI.detail(productId));
}));
Object.assign(form, {
id: result.id || routeId.value,
storeId: result.storeId || form.storeId,
storeId: result.storeId || currentStoreId,
name: result.name || "",
description: result.description || "",
status: result.status || "DRAFT",
beginAt: result.beginAt || undefined,
endAt: result.endAt || undefined,
itemList: (result.itemList || []).map(createItem)
itemList
});
standardAmountText.value = formatPackageAmountInput(result.standardAmount);
beginAtText.value = result.beginAt ? Time.toDateTime(result.beginAt) : "";
@@ -367,28 +460,89 @@ onUnmounted(() => navBarStore.clearRight());
}
.items {
padding: 1rem 0;
gap: .75rem;
display: flex;
margin-top: .75rem;
flex-direction: column;
.item {
gap: .5rem;
border: 1px dashed var(--td-component-border);
padding: .5rem 1rem;
margin-bottom: .75rem;
border-radius: 1rem;
border: 1px dashed var(--td-border-level-1-color);
padding: .5rem;
border-radius: .875rem;
.header {
gap: .625rem;
display: flex;
justify-content: space-between;
align-items: center;
.no {
width: 1.75rem;
height: 1.75rem;
color: var(--td-brand-color);
flex: none;
display: flex;
align-items: center;
justify-content: center;
background: var(--td-brand-color-1);
border-radius: .625rem;
font-size: .6875rem;
font-weight: 700;
}
strong {
flex: 1;
min-width: 0;
overflow: hidden;
color: var(--td-text-color-primary);
font-size: .875rem;
white-space: nowrap;
text-overflow: ellipsis;
}
}
:deep(.t-form__item) {
padding: .25rem 0;
.unit-list {
gap: .5rem;
width: 100%;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
}
.unit-empty {
color: var(--td-text-color-secondary);
font-size: .75rem;
}
.price {
display: flex;
align-items: baseline;
justify-content: end;
.red {
font-size: 1.5rem;
}
}
}
.add {
--td-button-border-radius: 1rem;
gap: .375rem;
color: var(--td-brand-color);
margin: .75rem 1rem;
}
.summary {
margin: 0 1rem;
display: flex;
align-items: baseline;
padding-bottom: 1rem;
justify-content: space-between;
.red {
font-size: 1.25rem;
}
}
}
.add-item {
border-radius: .75rem;
}
}
</style>