163 lines
3.9 KiB
TypeScript
Executable File
163 lines
3.9 KiB
TypeScript
Executable File
import { defineFakeRoute } from "vite-plugin-fake-server/client";
|
|
|
|
// 模拟数据
|
|
|
|
const mockContracts = [
|
|
{
|
|
id: "1",
|
|
name: "铜矿采购合同",
|
|
contractType: "1",
|
|
contractCode: "SC2024001",
|
|
buyParty: "中尚鹏贸易有限公司",
|
|
sellParty: "矿业公司",
|
|
commodity: "铜矿",
|
|
count: "1000",
|
|
unitPrice: "5000",
|
|
BLNumber: "BL001;BL002",
|
|
totalAmount: "5000000",
|
|
status: "active"
|
|
},
|
|
{
|
|
id: "2",
|
|
name: "铜材销售合同",
|
|
contractType: "3",
|
|
contractCode: "DX2024001",
|
|
buyParty: "华东制造有限公司",
|
|
sellParty: "中尚鹏贸易有限公司",
|
|
commodity: "铜材",
|
|
count: "800",
|
|
unitPrice: "5500",
|
|
BLNumber: "BL003",
|
|
totalAmount: "4400000",
|
|
status: "active"
|
|
},
|
|
{
|
|
id: "3",
|
|
name: "铁矿进口合同",
|
|
contractType: "2",
|
|
contractCode: "FW2024001",
|
|
buyParty: "中尚鹏贸易有限公司",
|
|
sellParty: "澳大利亚矿业公司",
|
|
commodity: "铁矿",
|
|
count: "2000",
|
|
unitPrice: "800",
|
|
BLNumber: "BL004;BL005;BL006",
|
|
totalAmount: "1600000",
|
|
status: "pending"
|
|
}
|
|
];
|
|
|
|
export default defineFakeRoute([
|
|
// 提交单个合同
|
|
{
|
|
url: "/contracts/upload",
|
|
method: "post",
|
|
response: ({ body }) => {
|
|
const newContract = {
|
|
id: Date.now().toString(),
|
|
name: body.name,
|
|
contractType: body.contractType,
|
|
contractCode: body.contractCode,
|
|
buyParty: body.buyParty,
|
|
sellParty: body.sellParty,
|
|
commodity: body.commodity,
|
|
count: body.count,
|
|
unitPrice: body.unitPrice,
|
|
BLNumber: body.BLNumber,
|
|
totalAmount: (
|
|
parseFloat(body.count || 0) * parseFloat(body.unitPrice || 0)
|
|
).toFixed(2),
|
|
status: "active",
|
|
createdAt: new Date().toISOString()
|
|
};
|
|
|
|
mockContracts.unshift(newContract);
|
|
|
|
return {
|
|
success: true,
|
|
message: `${body.name} 合同提交成功`,
|
|
data: {
|
|
id: newContract.id,
|
|
createdAt: newContract.createdAt
|
|
}
|
|
};
|
|
}
|
|
},
|
|
|
|
// 批量上传合同
|
|
{
|
|
url: "/contracts/batch-upload",
|
|
method: "post",
|
|
response: ({ body }) => {
|
|
const items = body.items || [];
|
|
const failedItems = items
|
|
.filter((item: any) => item.name && item.name.includes("fail"))
|
|
.map((item: any) => ({
|
|
name: item.name,
|
|
reason: "Invalid format"
|
|
}));
|
|
|
|
const successItems = items.filter(
|
|
(item: any) => !item.name.includes("fail")
|
|
);
|
|
|
|
// 模拟添加成功合同
|
|
successItems.forEach((item: any) => {
|
|
mockContracts.push({
|
|
id: Date.now().toString() + Math.random(),
|
|
...item,
|
|
status: "active",
|
|
createdAt: new Date().toISOString()
|
|
});
|
|
});
|
|
|
|
return {
|
|
success: true,
|
|
message: "批量上传完成",
|
|
data: {
|
|
successCount: successItems.length,
|
|
failedItems
|
|
}
|
|
};
|
|
}
|
|
},
|
|
|
|
// 获取合同列表
|
|
{
|
|
url: "/contracts/list",
|
|
method: "get",
|
|
response: ({ query }) => {
|
|
let contracts = [...mockContracts];
|
|
|
|
// 简单筛选逻辑
|
|
if (query.search) {
|
|
const search = (
|
|
Array.isArray(query.search) ? query.search[0] : query.search
|
|
).toLowerCase();
|
|
contracts = contracts.filter(
|
|
contract =>
|
|
contract.name.toLowerCase().includes(search) ||
|
|
contract.contractCode.toLowerCase().includes(search) ||
|
|
contract.buyParty.toLowerCase().includes(search) ||
|
|
contract.sellParty.toLowerCase().includes(search)
|
|
);
|
|
}
|
|
|
|
if (query.contractType) {
|
|
contracts = contracts.filter(
|
|
contract => contract.contractType === query.contractType
|
|
);
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
message: "获取合同列表成功",
|
|
data: {
|
|
items: contracts,
|
|
total: contracts.length
|
|
}
|
|
};
|
|
}
|
|
}
|
|
]);
|