515 lines
15 KiB
Vue
515 lines
15 KiB
Vue
<template>
|
||
<div class="tui-page-layout tui-navbar">
|
||
<t-navbar
|
||
fixed
|
||
:z-index="20"
|
||
title="回收兑换"
|
||
left-arrow
|
||
@left-click="$router.back()"
|
||
>
|
||
<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.create" class="recycle-exchange-create">
|
||
<t-form class="form card" label-width="7.5rem" v-entrance-transition>
|
||
<t-form-item label="客户" required-mark>
|
||
<t-input
|
||
:model-value="customerName || '请选择客户'"
|
||
class="customer"
|
||
readonly
|
||
borderless
|
||
placeholder="选择客户"
|
||
@click="customerVisible = true"
|
||
>
|
||
<template #suffix-icon>
|
||
<div class="customer-actions">
|
||
<t-button
|
||
v-if="customerId"
|
||
shape="circle"
|
||
theme="danger"
|
||
variant="text"
|
||
size="extra-small"
|
||
aria-label="清除客户选择"
|
||
@click.stop="clearCustomer"
|
||
>
|
||
<t-icon name="close" size="1rem" />
|
||
</t-button>
|
||
<t-button
|
||
theme="primary"
|
||
variant="outline"
|
||
size="extra-small"
|
||
content="选择"
|
||
@click.stop="customerVisible = true"
|
||
/>
|
||
</div>
|
||
</template>
|
||
</t-input>
|
||
</t-form-item>
|
||
<customer-profile-card
|
||
v-if="customer"
|
||
class="customer-profile"
|
||
:customer="customer"
|
||
v-entrance-transition
|
||
/>
|
||
<t-form-item label="兑换规则" required-mark content-align="right" arrow @click="openRulePicker">
|
||
<span class="clip-text" v-text="ruleDisplayText" />
|
||
</t-form-item>
|
||
<t-form-item label="倍数" required-mark>
|
||
<t-stepper
|
||
v-model="multiple"
|
||
size="large"
|
||
:min="1"
|
||
:max="9999"
|
||
:step="1"
|
||
integer
|
||
theme="outline"
|
||
/>
|
||
</t-form-item>
|
||
<t-form-item label="预设数量" content-align="right">
|
||
<span v-text="defaultQuantityText" />
|
||
</t-form-item>
|
||
<template v-if="canAdjust">
|
||
<t-form-item label="实际回收">
|
||
<t-input
|
||
v-model.trim="recycleQuantityText"
|
||
type="number"
|
||
align="right"
|
||
borderless
|
||
:placeholder="defaultRecycleQuantity"
|
||
/>
|
||
</t-form-item>
|
||
<t-form-item label="实际兑换">
|
||
<t-input
|
||
v-model.trim="exchangeQuantityText"
|
||
type="number"
|
||
align="right"
|
||
borderless
|
||
:placeholder="defaultExchangeQuantity"
|
||
/>
|
||
</t-form-item>
|
||
<t-form-item label-width="0">
|
||
<div class="adjust-tips">
|
||
<span class="tips" v-text="selectedRule ? adjustTipText : '选择兑换规则后可调整实际数量'" />
|
||
<t-button
|
||
v-if="selectedRule"
|
||
size="extra-small"
|
||
theme="primary"
|
||
variant="text"
|
||
content="恢复预设"
|
||
@click="resetQuantity"
|
||
/>
|
||
</div>
|
||
</t-form-item>
|
||
</template>
|
||
<t-form-item label="兑换时间" content-align="right">
|
||
<date-time-now-picker v-model="occurredAtText" title="兑换时间" />
|
||
</t-form-item>
|
||
<t-form-item label="备注">
|
||
<t-textarea v-model.trim="remark" borderless autosize maxlength="500" placeholder="可不填" />
|
||
</t-form-item>
|
||
<div class="photo-wrap">
|
||
<attachment-selector
|
||
v-model:items="photoItems"
|
||
:upload="uploadPhotoList"
|
||
title="兑换凭证照片"
|
||
required
|
||
/>
|
||
</div>
|
||
</t-form>
|
||
<div class="tips footer-tips" v-text="footerTipText" />
|
||
</div>
|
||
</main>
|
||
<customer-selector
|
||
v-model="customerId"
|
||
v-model:visible="customerVisible"
|
||
:enable-scan="false"
|
||
@change="handleCustomerChange"
|
||
/>
|
||
<bottom-popup
|
||
v-model="ruleVisible"
|
||
title="选择兑换规则"
|
||
height="70vh"
|
||
right-button-text=""
|
||
@left-button-click="ruleVisible = false"
|
||
>
|
||
<div class="rule-picker">
|
||
<t-cell
|
||
v-for="item in ruleList"
|
||
:key="item.id"
|
||
class="item"
|
||
:class="{ active: item.id === selectedRule?.id }"
|
||
@click="selectRule(item)"
|
||
>
|
||
<template #title>
|
||
<span class="clip-text" v-text="getRuleTitle(item)" />
|
||
</template>
|
||
<template #note>
|
||
<span v-text="getRuleRatioText(item)" />
|
||
</template>
|
||
<template #description>
|
||
<div class="description">
|
||
<span v-text="getRuleProductText(item)" />
|
||
<t-tag
|
||
v-if="isStockEnabled(item)"
|
||
size="small"
|
||
theme="warning"
|
||
variant="light-outline"
|
||
content="占用库存"
|
||
/>
|
||
</div>
|
||
</template>
|
||
</t-cell>
|
||
<center-tips v-if="!ruleList.length" :content="ruleLoading ? '正在加载兑换规则' : '暂无启用的兑换规则'" />
|
||
</div>
|
||
</bottom-popup>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { BottomPopup } from "timi-tdesign-mobile";
|
||
import { Time } from "timi-web";
|
||
import { useUserStore } from "timi-web-api";
|
||
import CustomerAPI from "@/api/CustomerAPI";
|
||
import RecycleAPI from "@/api/RecycleAPI";
|
||
import CustomerProfileCard from "@/components/CustomerProfileCard.vue";
|
||
import CustomerSelector from "@/components/CustomerSelector.vue";
|
||
import DateTimeNowPicker from "@/components/DateTimeNowPicker.vue";
|
||
import AttachmentSelector from "@/components/attachmentSelector/AttachmentSelector.vue";
|
||
import type { Customer } from "@/types/Customer";
|
||
import type { PhotoItem } from "@/types/Photo";
|
||
import type { RecycleExchangeRule } from "@/types/Recycle";
|
||
import { parseDateTime } from "@/utils/date";
|
||
import { getErrorMessage } from "@/utils/error";
|
||
import { Message } from "@/utils/message";
|
||
import { normalizePackageQuantity } from "@/utils/package";
|
||
import { permissionCodes } from "@/utils/permission";
|
||
import { multiplyRecycleQuantity, getRecycleQuantityText } from "@/utils/recycle";
|
||
import { uploadPhotoList } from "@/utils/photo";
|
||
|
||
defineOptions({ name: "RecycleExchangeCreate" });
|
||
|
||
const router = useRouter();
|
||
const userStore = useUserStore();
|
||
|
||
// ---------- 客户 ----------
|
||
|
||
const customerId = ref("");
|
||
const customer = ref<Customer>();
|
||
const customerVisible = ref(false);
|
||
const customerName = computed(() => customer.value?.name || customer.value?.code || "");
|
||
|
||
async function handleCustomerChange(value: string | string[]): Promise<void> {
|
||
const id = Array.isArray(value) ? String(value[0] || "") : value;
|
||
customerId.value = id;
|
||
if (!id) {
|
||
customer.value = undefined;
|
||
return;
|
||
}
|
||
try {
|
||
customer.value = await CustomerAPI.detail(id);
|
||
} catch (error) {
|
||
customer.value = undefined;
|
||
Message.error(getErrorMessage(error, "客户资料加载失败"));
|
||
}
|
||
}
|
||
|
||
function clearCustomer(): void {
|
||
customerId.value = "";
|
||
customer.value = undefined;
|
||
}
|
||
|
||
// ---------- 规则 ----------
|
||
|
||
const ruleVisible = ref(false);
|
||
const ruleLoading = ref(false);
|
||
const ruleList = ref<RecycleExchangeRule[]>([]);
|
||
const selectedRule = ref<RecycleExchangeRule>();
|
||
const multiple = ref(1);
|
||
|
||
/**
|
||
* 规则行只回显「回收物 兑换 兑换物」,具体数量由下方「预设数量」表达。
|
||
*
|
||
* 带上产品名是为了区分比例相同但兑换不同产品的规则。
|
||
*/
|
||
const ruleDisplayText = computed(() => {
|
||
const rule = selectedRule.value;
|
||
if (!rule) {
|
||
return "请选择兑换规则";
|
||
}
|
||
const title = getRuleTitle(rule);
|
||
const productText = rule.productName || rule.skuName || "";
|
||
return productText ? `${title} 兑换 ${productText}` : title;
|
||
});
|
||
const defaultRecycleQuantity = computed(() => multiplyRecycleQuantity(selectedRule.value?.recycleQuantity, multiple.value));
|
||
const defaultExchangeQuantity = computed(() => multiplyRecycleQuantity(selectedRule.value?.exchangeQuantity, multiple.value));
|
||
const recycleUnitName = computed(() => selectedRule.value?.recycleUnit || "");
|
||
const exchangeUnitName = computed(() => selectedRule.value?.unitName || "");
|
||
const defaultQuantityText = computed(() => (selectedRule.value
|
||
? `${getRecycleQuantityText(defaultRecycleQuantity.value, recycleUnitName.value)} 兑换 ${getRecycleQuantityText(defaultExchangeQuantity.value, exchangeUnitName.value)}`
|
||
: "-"));
|
||
|
||
/** 规则自带回收物、产品与单位名称,选择弹层不需要再回查配置和产品库。 */
|
||
function getRuleTitle(rule: RecycleExchangeRule): string {
|
||
return rule.recycleName || getRuleProductText(rule) || "兑换规则";
|
||
}
|
||
|
||
/** 单份规则的兑换比例,弹层里产品名另起一行显示,这里不带。 */
|
||
function getRuleRatioText(rule: RecycleExchangeRule): string {
|
||
const recycleText = getRecycleQuantityText(multiplyRecycleQuantity(rule.recycleQuantity, 1), rule.recycleUnit);
|
||
const exchangeText = getRecycleQuantityText(multiplyRecycleQuantity(rule.exchangeQuantity, 1), rule.unitName);
|
||
return `${recycleText} 兑换 ${exchangeText}`;
|
||
}
|
||
|
||
function getRuleProductText(rule: RecycleExchangeRule): string {
|
||
const productName = rule.productName || "";
|
||
const skuText = rule.specSummary || rule.skuName || "";
|
||
if (!productName) {
|
||
return skuText;
|
||
}
|
||
return skuText && skuText !== productName ? `${productName}(${skuText})` : productName;
|
||
}
|
||
|
||
/** 启用规则列表可能超过一页,创建时一次取完,避免规则被分页截断。 */
|
||
async function loadRules(): Promise<void> {
|
||
ruleLoading.value = true;
|
||
try {
|
||
const rules: RecycleExchangeRule[] = [];
|
||
let offset = 0;
|
||
const limit = 100;
|
||
let total = 0;
|
||
do {
|
||
const page = await RecycleAPI.activeRuleList({ offset, limit });
|
||
const rows = page.list || [];
|
||
rules.push(...rows);
|
||
total = page.total || 0;
|
||
offset += rows.length;
|
||
if (!rows.length) {
|
||
break;
|
||
}
|
||
} while (offset < total);
|
||
ruleList.value = rules;
|
||
} catch (error) {
|
||
Message.error(getErrorMessage(error, "启用兑换规则加载失败"));
|
||
} finally {
|
||
ruleLoading.value = false;
|
||
}
|
||
}
|
||
|
||
async function openRulePicker(): Promise<void> {
|
||
ruleVisible.value = true;
|
||
if (!ruleList.value.length && !ruleLoading.value) {
|
||
await loadRules();
|
||
}
|
||
}
|
||
|
||
/** 换规则或改倍数都要把实际数量重置回预设;没选规则时留空,让占位提示生效。 */
|
||
function selectRule(rule: RecycleExchangeRule): void {
|
||
selectedRule.value = rule;
|
||
ruleVisible.value = false;
|
||
resetQuantity();
|
||
}
|
||
|
||
function resetQuantity(): void {
|
||
if (!selectedRule.value) {
|
||
recycleQuantityText.value = "";
|
||
exchangeQuantityText.value = "";
|
||
return;
|
||
}
|
||
recycleQuantityText.value = defaultRecycleQuantity.value;
|
||
exchangeQuantityText.value = defaultExchangeQuantity.value;
|
||
}
|
||
|
||
/** 库存占用标记同样由规则自带,避免再查一次产品库。 */
|
||
function isStockEnabled(rule?: RecycleExchangeRule): boolean {
|
||
return rule?.stockEnabled === true;
|
||
}
|
||
|
||
// ---------- 数量调整 ----------
|
||
|
||
const canAdjust = computed(() => userStore.hasPermissions(permissionCodes.recycleExchange.quantityAdjust));
|
||
const recycleQuantityText = ref("");
|
||
const exchangeQuantityText = ref("");
|
||
const adjustTipText = computed(() => {
|
||
const recycleText = getRecycleQuantityText(defaultRecycleQuantity.value, recycleUnitName.value);
|
||
const exchangeText = getRecycleQuantityText(defaultExchangeQuantity.value, exchangeUnitName.value);
|
||
return `有数量调整权限时可修改实际数量,预设为 ${recycleText} 兑换 ${exchangeText}。`;
|
||
});
|
||
|
||
watch([() => selectedRule.value?.id, multiple], () => {
|
||
resetQuantity();
|
||
});
|
||
|
||
// ---------- 其它字段 ----------
|
||
|
||
const occurredAtText = ref("");
|
||
const remark = ref("");
|
||
const photoItems = ref<PhotoItem[]>([]);
|
||
const requestNo = ref("");
|
||
const saving = ref(false);
|
||
const footerTipText = "创建即登记收到回收物,兑换凭证照片必传。管理库存的兑换物会在创建时预占库存,库存不足则创建失败。";
|
||
|
||
function buildAttachments(): Array<{ tempFileId: string; attachType: "PHOTO" }> {
|
||
return photoItems.value.flatMap(item => item.tempFileId ? [{
|
||
tempFileId: item.tempFileId,
|
||
attachType: "PHOTO" as const
|
||
}] : []);
|
||
}
|
||
|
||
function buildOccurredAt(): number | undefined {
|
||
return occurredAtText.value ? Time.toTimestamp(parseDateTime(occurredAtText.value, false)) : undefined;
|
||
}
|
||
|
||
// 提交内容一变就要换请求号,服务端按请求号 + 内容摘要去重
|
||
watch([customerId, () => selectedRule.value?.id, multiple, recycleQuantityText, exchangeQuantityText, occurredAtText, remark, photoItems], () => {
|
||
requestNo.value = "";
|
||
}, { deep: true });
|
||
|
||
async function save(): Promise<void> {
|
||
if (saving.value) {
|
||
return;
|
||
}
|
||
if (!customerId.value) {
|
||
Message.warning("请选择客户");
|
||
return;
|
||
}
|
||
if (!selectedRule.value?.id) {
|
||
Message.warning("请选择兑换规则");
|
||
return;
|
||
}
|
||
const multipleValue = Number(multiple.value);
|
||
if (!Number.isSafeInteger(multipleValue) || multipleValue < 1) {
|
||
Message.warning("倍数必须为正整数");
|
||
return;
|
||
}
|
||
let recycleQuantity: string | undefined;
|
||
let exchangeQuantity: string | undefined;
|
||
if (canAdjust.value) {
|
||
recycleQuantity = normalizePackageQuantity(recycleQuantityText.value || defaultRecycleQuantity.value);
|
||
exchangeQuantity = normalizePackageQuantity(exchangeQuantityText.value || defaultExchangeQuantity.value);
|
||
if (!recycleQuantity || !exchangeQuantity || recycleQuantity === "0" || exchangeQuantity === "0") {
|
||
Message.warning("实际回收数量和兑换数量必须为大于 0 的数字,最多 3 位小数");
|
||
return;
|
||
}
|
||
}
|
||
if (!photoItems.value.length) {
|
||
Message.warning("请上传兑换凭证照片");
|
||
return;
|
||
}
|
||
if (!requestNo.value) {
|
||
requestNo.value = window.crypto.randomUUID();
|
||
}
|
||
saving.value = true;
|
||
try {
|
||
const result = await RecycleAPI.create({
|
||
customerId: customerId.value,
|
||
ruleId: selectedRule.value.id,
|
||
requestNo: requestNo.value,
|
||
multiple: multipleValue,
|
||
// 只有确实调整过才提交实际数量,无调整权限时一律不传
|
||
recycleQuantity: recycleQuantity === defaultRecycleQuantity.value ? undefined : recycleQuantity,
|
||
exchangeQuantity: exchangeQuantity === defaultExchangeQuantity.value ? undefined : exchangeQuantity,
|
||
occurredAt: buildOccurredAt(),
|
||
remark: remark.value || null,
|
||
attachmentList: buildAttachments()
|
||
});
|
||
Message.success("兑换单已创建");
|
||
const id = result.id || "";
|
||
if (id) {
|
||
await router.replace({ name: "RecycleExchangeDetail", params: { id } });
|
||
return;
|
||
}
|
||
await router.replace({ name: "RecycleExchangeList" });
|
||
} catch (error) {
|
||
Message.error(getErrorMessage(error, "兑换单创建失败,重试会复用同一请求号"));
|
||
} finally {
|
||
saving.value = false;
|
||
}
|
||
}
|
||
|
||
onMounted(async () => {
|
||
await loadRules();
|
||
});
|
||
</script>
|
||
|
||
<style scoped lang="less">
|
||
.recycle-exchange-create {
|
||
padding: 0 0 2rem;
|
||
|
||
.form {
|
||
|
||
.customer {
|
||
|
||
.customer-actions {
|
||
gap: .5rem;
|
||
display: flex;
|
||
}
|
||
}
|
||
|
||
.customer-profile {
|
||
margin: 1rem;
|
||
}
|
||
|
||
.adjust-tips {
|
||
gap: .5rem;
|
||
width: 100%;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
|
||
.tips {
|
||
color: var(--td-text-color-secondary);
|
||
font-size: .8125rem;
|
||
line-height: 1.5;
|
||
}
|
||
}
|
||
|
||
.photo-wrap {
|
||
padding: 1rem;
|
||
}
|
||
}
|
||
|
||
.footer-tips {
|
||
color: var(--td-text-color-secondary);
|
||
font-size: .8125rem;
|
||
line-height: 1.6;
|
||
padding: .75rem 2rem 0;
|
||
}
|
||
}
|
||
|
||
.rule-picker {
|
||
|
||
.item {
|
||
padding: .75rem 1rem;
|
||
border-radius: 1rem;
|
||
|
||
&.active {
|
||
background: var(--td-brand-color-1, rgba(0, 82, 217, .08));
|
||
}
|
||
|
||
.description {
|
||
gap: .5rem;
|
||
display: flex;
|
||
align-items: center;
|
||
}
|
||
}
|
||
}
|
||
|
||
:global(html.theme-dark) {
|
||
.rule-picker {
|
||
|
||
.item.active {
|
||
background: rgba(15, 118, 110, .22);
|
||
}
|
||
}
|
||
}
|
||
</style>
|