add: recycle product
This commit is contained in:
@@ -0,0 +1,400 @@
|
||||
<template>
|
||||
<div class="tui-page-layout tui-navbar">
|
||||
<t-navbar
|
||||
fixed
|
||||
:z-index="20"
|
||||
:title="isCreate ? '新增兑换规则' : '编辑兑换规则'"
|
||||
left-arrow
|
||||
@left-click="exit()"
|
||||
>
|
||||
<template #right>
|
||||
<t-button
|
||||
:loading="saving"
|
||||
theme="primary"
|
||||
variant="text"
|
||||
size="small"
|
||||
content="保存"
|
||||
@click="save()"
|
||||
/>
|
||||
</template>
|
||||
</t-navbar>
|
||||
<main class="tui-page-content scroll column">
|
||||
<div v-can="permissionCodes.recycleExchange.configManage" class="recycle-rule-edit">
|
||||
<t-form class="form card" label-width="7.5rem" v-entrance-transition>
|
||||
<t-form-item label="回收物" required-mark content-align="right" arrow @click="openItemPicker">
|
||||
<span class="clip-text" v-text="recycleItemName || '请选择回收物'" />
|
||||
</t-form-item>
|
||||
<t-form-item label="单份回收数量" required-mark>
|
||||
<t-input
|
||||
v-model.trim="form.recycleQuantity"
|
||||
type="number"
|
||||
align="right"
|
||||
borderless
|
||||
:placeholder="`按${recycleUnitName || '回收单位'}填写`"
|
||||
/>
|
||||
</t-form-item>
|
||||
<t-form-item label="兑换产品" required-mark content-align="right" arrow @click="productPopupVisible = true">
|
||||
<span class="clip-text" v-text="productDisplayName || '请选择兑换产品'" />
|
||||
</t-form-item>
|
||||
<t-form-item v-if="skuDisplayText || unitDisplayText" label="产品 SKU" content-align="right">
|
||||
<product-sku-tags :fallback="skuDisplayText" :unit="unitDisplayText" />
|
||||
</t-form-item>
|
||||
<t-form-item label="单份兑换数量" required-mark>
|
||||
<t-input
|
||||
v-model.trim="form.exchangeQuantity"
|
||||
type="number"
|
||||
align="right"
|
||||
borderless
|
||||
:placeholder="`按${unitDisplayText || '业务单位'}填写`"
|
||||
/>
|
||||
</t-form-item>
|
||||
<t-form-item label-width="0">
|
||||
<span class="tips" v-text="ratioTipText" />
|
||||
</t-form-item>
|
||||
<t-form-item label="规则状态" content-align="right" arrow @click="statusVisible = true">
|
||||
<span v-text="getRecycleStatusText(form.status)" />
|
||||
</t-form-item>
|
||||
<t-form-item label="排序值">
|
||||
<t-input
|
||||
v-model.trim="form.sortText"
|
||||
type="number"
|
||||
align="right"
|
||||
borderless
|
||||
placeholder="默认 0,越小越靠前"
|
||||
/>
|
||||
</t-form-item>
|
||||
<t-form-item label="备注">
|
||||
<t-textarea v-model.trim="form.remark" borderless autosize maxlength="500" placeholder="可不填" />
|
||||
</t-form-item>
|
||||
</t-form>
|
||||
<span class="tips footer-tips" v-text="footerTipText" />
|
||||
<t-button
|
||||
v-if="!isCreate"
|
||||
class="cell-button"
|
||||
theme="default"
|
||||
v-entrance-transition
|
||||
:content="form.status === 'ACTIVE' ? '停用规则' : '启用规则'"
|
||||
@click="toggleStatus"
|
||||
/>
|
||||
</div>
|
||||
</main>
|
||||
<bottom-popup
|
||||
v-model="itemPickerVisible"
|
||||
title="选择回收物"
|
||||
height="60vh"
|
||||
right-button-text=""
|
||||
@left-button-click="itemPickerVisible = false"
|
||||
>
|
||||
<div class="item-picker">
|
||||
<t-cell
|
||||
v-for="item in itemList"
|
||||
:key="item.id"
|
||||
class="item"
|
||||
:class="{ active: item.id === form.recycleItemId }"
|
||||
@click="selectItem(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>
|
||||
<span v-text="`按${item.unitName || '单位'}计量`" />
|
||||
</template>
|
||||
</t-cell>
|
||||
<center-tips v-if="!itemList.length" :content="itemLoading ? '正在加载回收物' : '暂无回收物,请先配置回收物'" />
|
||||
</div>
|
||||
</bottom-popup>
|
||||
<t-popup v-model="statusVisible" placement="bottom">
|
||||
<t-picker
|
||||
v-model="statusDraft"
|
||||
:columns="statusOptions"
|
||||
title="规则状态"
|
||||
@cancel="statusVisible = false"
|
||||
@confirm="confirmStatus"
|
||||
/>
|
||||
</t-popup>
|
||||
<product-select-popup
|
||||
v-model="selection"
|
||||
v-model:visible="productPopupVisible"
|
||||
@select="handleProductSelection"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { BottomPopup, useExpandToMaster, usePane } from "timi-tdesign-mobile";
|
||||
import RecycleAPI from "@/api/RecycleAPI";
|
||||
import ProductSelectPopup from "@/components/productSelector/ProductSelectPopup.vue";
|
||||
import ProductSkuTags from "@/components/ProductSkuTags.vue";
|
||||
import type { ProductSelection } from "@/types/Product";
|
||||
import type { ProductSkuSelectionValue } from "@/types/ProductSkuSelection";
|
||||
import type { RecycleItem, RecycleStatus } from "@/types/Recycle";
|
||||
import { getErrorMessage } from "@/utils/error";
|
||||
import { Message } from "@/utils/message";
|
||||
import { normalizePackageQuantity } from "@/utils/package";
|
||||
import { permissionCodes } from "@/utils/permission";
|
||||
import { getRecycleQuantityText, getRecycleStatusText, getRecycleStatusTheme } from "@/utils/recycle";
|
||||
|
||||
defineOptions({ name: "RecycleRuleEdit" });
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
/** 指定规则 ID 时忽略路由参数,用于平板主从分栏的内联渲染。 */
|
||||
ruleId?: string;
|
||||
}>(), {
|
||||
ruleId: ""
|
||||
});
|
||||
const emit = defineEmits<{
|
||||
saved: [];
|
||||
}>();
|
||||
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
const currentId = computed(() => props.ruleId || String(route.params.id || ""));
|
||||
const isCreate = computed(() => !currentId.value);
|
||||
|
||||
useExpandToMaster({
|
||||
path: "/management/recycle-rules",
|
||||
id: () => currentId.value
|
||||
});
|
||||
/** 非空说明当前被内联在主从分栏的右栏里 */
|
||||
const pane = usePane();
|
||||
|
||||
function exit(saved = false): void {
|
||||
if (pane) {
|
||||
pane.close(saved);
|
||||
return;
|
||||
}
|
||||
|
||||
void router.back();
|
||||
}
|
||||
|
||||
// ---------- 表单 ----------
|
||||
|
||||
const form = reactive({
|
||||
id: "",
|
||||
recycleItemId: "",
|
||||
recycleQuantity: "",
|
||||
exchangeQuantity: "",
|
||||
status: "ACTIVE" as RecycleStatus,
|
||||
sortText: "",
|
||||
remark: ""
|
||||
});
|
||||
const selection = ref<ProductSkuSelectionValue>();
|
||||
const picked = ref<ProductSelection>();
|
||||
const productPopupVisible = ref(false);
|
||||
// 规则详情自带产品与单位名称,未改选前直接用它展示,不必再查产品库
|
||||
const ruleSnapshot = reactive({ productName: "", skuText: "", unitName: "" });
|
||||
const itemPickerVisible = ref(false);
|
||||
const itemList = ref<RecycleItem[]>([]);
|
||||
const itemLoading = ref(false);
|
||||
const selectedItem = ref<RecycleItem>();
|
||||
const statusVisible = ref(false);
|
||||
const statusDraft = ref<Array<string | number>>(["ACTIVE"]);
|
||||
const saving = ref(false);
|
||||
const statusOptions = [
|
||||
{ label: "启用", value: "ACTIVE" },
|
||||
{ label: "停用", value: "INACTIVE" }
|
||||
];
|
||||
const recycleItemName = computed(() => selectedItem.value?.name || "");
|
||||
const recycleUnitName = computed(() => selectedItem.value?.unitName || "");
|
||||
const productDisplayName = computed(() => picked.value?.product?.name || ruleSnapshot.productName);
|
||||
const skuDisplayText = computed(() => {
|
||||
const sku = picked.value?.sku;
|
||||
return sku ? sku.specSummary?.trim() || sku.name?.trim() || "" : ruleSnapshot.skuText;
|
||||
});
|
||||
const unitDisplayText = computed(() => picked.value?.unit?.name || ruleSnapshot.unitName);
|
||||
const ratioTipText = computed(() => {
|
||||
const recycleText = getRecycleQuantityText(form.recycleQuantity || "0", recycleUnitName.value);
|
||||
const exchangeText = getRecycleQuantityText(form.exchangeQuantity || "0", unitDisplayText.value);
|
||||
return `创建兑换单时按倍数放大:预设 ${recycleText} 兑换 ${exchangeText}。数量最多 3 位小数。`;
|
||||
});
|
||||
const footerTipText = "启用规则要求回收物、产品和 SKU 均已启用,且所选业务单位允许销售。规则停用不影响已创建的兑换单。";
|
||||
|
||||
function openItemPicker(): void {
|
||||
itemPickerVisible.value = true;
|
||||
}
|
||||
|
||||
function selectItem(item: RecycleItem): void {
|
||||
form.recycleItemId = item.id || "";
|
||||
selectedItem.value = item;
|
||||
itemPickerVisible.value = false;
|
||||
}
|
||||
|
||||
function handleProductSelection(value: ProductSelection): void {
|
||||
picked.value = value;
|
||||
selection.value = { skuId: value.sku.id || "", skuUnitId: value.unit.id || "" };
|
||||
}
|
||||
|
||||
function confirmStatus(value: Array<string | number>): void {
|
||||
form.status = String(value[0]) as RecycleStatus;
|
||||
statusVisible.value = false;
|
||||
}
|
||||
|
||||
// ---------- 数据 ----------
|
||||
|
||||
/** 回收物数量不多,一次取完供选择;超过一页时继续翻页。 */
|
||||
async function loadItems(): Promise<void> {
|
||||
itemLoading.value = true;
|
||||
const loadedList: RecycleItem[] = [];
|
||||
let offset = 0;
|
||||
const limit = 100;
|
||||
let total = 0;
|
||||
try {
|
||||
do {
|
||||
const page = await RecycleAPI.itemList({ offset, limit });
|
||||
const rows = page.list || [];
|
||||
loadedList.push(...rows);
|
||||
total = page.total || 0;
|
||||
offset += rows.length;
|
||||
if (!rows.length) {
|
||||
break;
|
||||
}
|
||||
} while (offset < total);
|
||||
itemList.value = loadedList;
|
||||
} finally {
|
||||
itemLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function load(): Promise<void> {
|
||||
const id = currentId.value;
|
||||
try {
|
||||
await loadItems();
|
||||
if (!id) {
|
||||
return;
|
||||
}
|
||||
const rule = await RecycleAPI.ruleDetail(id);
|
||||
form.id = rule.id || "";
|
||||
form.recycleItemId = rule.recycleItemId || "";
|
||||
form.recycleQuantity = String(rule.recycleQuantity ?? "");
|
||||
form.exchangeQuantity = String(rule.exchangeQuantity ?? "");
|
||||
form.status = rule.status || "ACTIVE";
|
||||
form.sortText = rule.sort === null || rule.sort === undefined ? "" : String(rule.sort);
|
||||
form.remark = rule.remark || "";
|
||||
selectedItem.value = itemList.value.find(item => item.id === rule.recycleItemId);
|
||||
selection.value = { skuId: rule.skuId, skuUnitId: rule.skuUnitId };
|
||||
ruleSnapshot.productName = rule.productName || "";
|
||||
ruleSnapshot.skuText = rule.specSummary?.trim() || rule.skuName?.trim() || "";
|
||||
ruleSnapshot.unitName = rule.unitName || "";
|
||||
} catch (error) {
|
||||
Message.error(getErrorMessage(error, "兑换规则加载失败"));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 保存 ----------
|
||||
|
||||
async function save(status?: RecycleStatus): Promise<void> {
|
||||
const recycleQuantity = normalizePackageQuantity(form.recycleQuantity);
|
||||
const exchangeQuantity = normalizePackageQuantity(form.exchangeQuantity);
|
||||
const skuId = selection.value?.skuId;
|
||||
const skuUnitId = selection.value?.skuUnitId;
|
||||
if (saving.value) {
|
||||
return;
|
||||
}
|
||||
if (!form.recycleItemId || !skuId || !skuUnitId) {
|
||||
Message.warning("请选择回收物和兑换产品业务单位");
|
||||
return;
|
||||
}
|
||||
if (!recycleQuantity || !exchangeQuantity || recycleQuantity === "0" || exchangeQuantity === "0") {
|
||||
Message.warning("单份回收数量和兑换数量必须为大于 0 的数字,最多 3 位小数");
|
||||
return;
|
||||
}
|
||||
const sort = form.sortText ? Number(form.sortText) : 0;
|
||||
if (!Number.isSafeInteger(sort)) {
|
||||
Message.warning("排序值必须为整数");
|
||||
return;
|
||||
}
|
||||
saving.value = true;
|
||||
try {
|
||||
await RecycleAPI.ruleSave({
|
||||
id: form.id || undefined,
|
||||
recycleItemId: form.recycleItemId,
|
||||
recycleQuantity,
|
||||
skuId,
|
||||
skuUnitId,
|
||||
exchangeQuantity,
|
||||
status: status || form.status,
|
||||
sort,
|
||||
remark: form.remark || null
|
||||
});
|
||||
Message.success("兑换规则已保存");
|
||||
form.status = status || form.status;
|
||||
emit("saved");
|
||||
exit(true);
|
||||
} catch (error) {
|
||||
Message.error(getErrorMessage(error, "兑换规则保存失败"));
|
||||
} finally {
|
||||
saving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function toggleStatus(): Promise<void> {
|
||||
await save(form.status === "ACTIVE" ? "INACTIVE" : "ACTIVE");
|
||||
}
|
||||
|
||||
onMounted(load);
|
||||
watch(() => [props.ruleId, route.params.id], load);
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.recycle-rule-edit {
|
||||
gap: 1rem;
|
||||
display: flex;
|
||||
padding: 0 0 2rem;
|
||||
flex-direction: column;
|
||||
|
||||
.form {
|
||||
|
||||
.tips {
|
||||
color: var(--td-text-color-secondary);
|
||||
font-size: .8125rem;
|
||||
line-height: 1.6;
|
||||
}
|
||||
}
|
||||
|
||||
.footer-tips {
|
||||
padding: 0 2rem;
|
||||
}
|
||||
}
|
||||
|
||||
.item-picker {
|
||||
|
||||
.item {
|
||||
padding: .75rem 1rem;
|
||||
border-radius: 1rem;
|
||||
|
||||
&.active {
|
||||
background: var(--td-brand-color-1, rgba(0, 82, 217, .08));
|
||||
}
|
||||
|
||||
.avatar {
|
||||
font-size: 1rem;
|
||||
width: 2.5rem;
|
||||
height: 2.5rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
:global(html.theme-dark) {
|
||||
.item-picker {
|
||||
|
||||
.item.active {
|
||||
background: rgba(15, 118, 110, .22);
|
||||
}
|
||||
|
||||
.fallback {
|
||||
color: #ccfbf1;
|
||||
background: #115e59;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user