This commit is contained in:
ah--desktop
2025-12-11 08:04:56 +08:00
parent a3b8797955
commit 5129657f7f
69 changed files with 1695 additions and 545 deletions
@@ -1,9 +1,15 @@
<script lang="ts" setup>
import type { OrderDTO, OrderVO } from '#/api/ecommerce/order';
import type { OrderStatusVO } from '#/api/ecommerce/order-status';
import type {
ProductSpecDTO,
ProductSpecVO,
} from '#/api/ecommerce/product-spec/types';
import type { ProductDTO, ProductVO } from '#/api/ecommerce/product/types';
import type { ShopVO } from '#/api/ecommerce/shop/types';
import type { PageQueryDTO } from '#/types/common';
import { ref } from 'vue';
import { ref, watch } from 'vue';
import { useVbenModal } from '@vben/common-ui';
@@ -14,11 +20,14 @@ import {
InputNumber,
message,
Select,
Spin,
} from 'ant-design-vue';
import dayjs from 'dayjs';
import { createOrderApi, updateOrderApi } from '#/api/ecommerce/order';
import { getActiveOrderStatusListApi } from '#/api/ecommerce/order-status';
import { getProductPageApi } from '#/api/ecommerce/product';
import { getProductSpecPageApi } from '#/api/ecommerce/product-spec';
import { getShopPageApi } from '#/api/ecommerce/shop';
const emit = defineEmits<{
@@ -63,6 +72,20 @@ const formData = ref<Partial<OrderDTO.Create>>({
const shopOptions = ref<{ label: string; value: number }[]>([]);
const orderStatusOptions = ref<{ label: string; value: number }[]>([]);
// 商品选项数据
const productOptions = ref<
{ label: string; product: ProductVO.Info; value: number }[]
>([]);
const productLoading = ref(false);
const productSearchKeyword = ref('');
// 规格选项数据
const productSpecOptions = ref<
{ label: string; spec: ProductSpecVO.Info; value: number }[]
>([]);
const productSpecLoading = ref(false);
const productSpecSearchKeyword = ref('');
// 表单验证规则
const rules = {
shopId: [{ required: true, message: '请选择店铺', trigger: 'change' }],
@@ -71,7 +94,7 @@ const rules = {
{ required: true, message: '请输入实收金额', trigger: 'blur' },
],
orderDate: [{ required: true, message: '请选择下单时间', trigger: 'change' }],
productId: [{ required: true, message: '请输入商品ID', trigger: 'blur' }],
productId: [{ required: true, message: '请选择商品', trigger: 'change' }],
productName: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
quantity: [{ required: true, message: '请输入数量', trigger: 'blur' }],
unitPrice: [{ required: true, message: '请输入销售单价', trigger: 'blur' }],
@@ -112,8 +135,114 @@ async function fetchOrderStatusOptions() {
}
}
// 搜索商品
async function searchProducts(keyword: string) {
productSearchKeyword.value = keyword;
productLoading.value = true;
try {
const params: PageQueryDTO<ProductDTO.Filters> = {
page: 1,
pageSize: 50,
filters: {
name: keyword || undefined,
},
};
const data = await getProductPageApi(params);
productOptions.value = data.records.map((product: ProductVO.Info) => ({
label: `${product.name}${product.productCode ? ` (${product.productCode})` : ''}`,
value: product.id,
product,
}));
} catch (error) {
console.error('搜索商品失败:', error);
message.error('搜索商品失败');
} finally {
productLoading.value = false;
}
}
// 搜索规格
async function searchProductSpecs(keyword: string) {
if (!formData.value.productId) {
productSpecOptions.value = [];
return;
}
productSpecSearchKeyword.value = keyword;
productSpecLoading.value = true;
try {
const params: PageQueryDTO<ProductSpecDTO.Filters> = {
page: 1,
pageSize: 50,
filters: {
productId: formData.value.productId,
name: keyword || undefined,
},
};
const data = await getProductSpecPageApi(params);
productSpecOptions.value = data.records.map((spec: ProductSpecVO.Info) => ({
label: `${spec.name}${spec.specCode ? ` (${spec.specCode})` : ''}`,
value: spec.id,
spec,
}));
} catch (error) {
console.error('搜索规格失败:', error);
message.error('搜索规格失败');
} finally {
productSpecLoading.value = false;
}
}
// 处理商品选择
function handleProductChange(productId: number | undefined) {
if (productId) {
const selectedProduct = productOptions.value.find(
(opt) => opt.value === productId,
);
if (selectedProduct) {
formData.value.productName = selectedProduct.product.name;
// 加载该商品的规格列表
searchProductSpecs('');
}
} else {
formData.value.productName = '';
formData.value.productSpecId = undefined;
formData.value.specName = '';
formData.value.costPrice = 0;
productSpecOptions.value = [];
}
}
// 处理规格选择
function handleProductSpecChange(specId: number | undefined) {
if (specId) {
const selectedSpec = productSpecOptions.value.find(
(opt) => opt.value === specId,
);
if (selectedSpec) {
formData.value.specName = selectedSpec.spec.name;
formData.value.costPrice = selectedSpec.spec.costPrice;
}
} else {
formData.value.specName = '';
formData.value.costPrice = 0;
}
}
// 监听商品ID变化,加载规格列表
watch(
() => formData.value.productId,
(newProductId) => {
if (newProductId) {
searchProductSpecs('');
} else {
productSpecOptions.value = [];
formData.value.productSpecId = undefined;
}
},
);
// 打开模态框
function open(edit: boolean, record?: OrderVO.Info) {
async function open(edit: boolean, record?: OrderVO.Info) {
isEdit.value = edit;
currentRecord.value = record || null;
@@ -156,6 +285,41 @@ function open(edit: boolean, record?: OrderVO.Info) {
unitPrice: 0,
costPrice: 0,
};
// 如果是编辑模式且有商品ID,加载商品和规格选项
if (edit && record?.productId) {
await searchProducts(record.productName || '');
// 确保选中的商品在选项中
if (!productOptions.value.some((opt) => opt.value === record.productId)) {
productOptions.value.push({
label: record.productName,
value: record.productId,
product: {
id: record.productId,
name: record.productName,
} as ProductVO.Info,
});
}
if (record.productSpecId) {
await searchProductSpecs(record.specName || '');
// 确保选中的规格在选项中
if (
!productSpecOptions.value.some(
(opt) => opt.value === record.productSpecId,
)
) {
productSpecOptions.value.push({
label: record.specName || '',
value: record.productSpecId,
spec: {
id: record.productSpecId,
name: record.specName || '',
costPrice: record.costPrice,
} as ProductSpecVO.Info,
});
}
}
}
}
// 关闭模态框
@@ -272,22 +436,57 @@ defineExpose({
</div>
<div class="grid grid-cols-2 gap-4">
<Form.Item label="商品ID" name="productId">
<InputNumber
<Form.Item label="商品" name="productId">
<Select
v-model:value="formData.productId"
placeholder="请输入商品ID"
style="width: 100%"
:min="1"
/>
placeholder="请搜索并选择商品"
show-search
:filter-option="false"
:loading="productLoading"
:options="productOptions"
@search="searchProducts"
@change="(value) => handleProductChange(value as number)"
>
<template #notFoundContent>
<div v-if="productLoading" class="py-4 text-center">
<Spin size="small" />
<div class="mt-2">加载中...</div>
</div>
<div v-else class="py-4 text-center text-gray-400">
{{ productSearchKeyword ? '未找到商品' : '请输入商品名称搜索' }}
</div>
</template>
</Select>
</Form.Item>
<Form.Item label="商品规格ID" name="productSpecId">
<InputNumber
<Form.Item label="商品规格" name="productSpecId">
<Select
v-model:value="formData.productSpecId"
placeholder="请输入商品规格ID(可选)"
style="width: 100%"
:min="1"
/>
placeholder="请先选择商品,然后搜索规格"
show-search
:filter-option="false"
:loading="productSpecLoading"
:disabled="!formData.productId"
:options="productSpecOptions"
@search="searchProductSpecs"
@change="(value) => handleProductSpecChange(value as number)"
>
<template #notFoundContent>
<div v-if="productSpecLoading" class="py-4 text-center">
<Spin size="small" />
<div class="mt-2">加载中...</div>
</div>
<div v-else class="py-4 text-center text-gray-400">
{{
formData.productId
? productSpecSearchKeyword
? '未找到规格'
: '请输入规格名称搜索'
: '请先选择商品'
}}
</div>
</template>
</Select>
</Form.Item>
</div>