|
本帖最后由 孙森峰 于 2025-3-26 05:09 编辑
大神们,我准备做一个订单项目,具有可扩展性
请帮忙!
(附件将后缀.zip去掉即可)
首先定义一个类Order,这是项目的核心
class Order {
// 配置字段映射和常量
static CONFIG = {
fieldMapping: { // 字段映射表 (参数名 -> 类属性名)
totalValue: 'total' // 示例:将参数中的 totalValue 映射到类的 total 属性
},
constants: { // 常量配置
status: 'pending' // 示例:默认订单状态为 pending
}
};
constructor(params = {}) {
const { fieldMapping, constants } = Order.CONFIG;
// 处理基础参数及默认值
const {
id = 1,
saleId = null,
customer = null,
items = [],
// ...restParams
orderId = null,
orderDate = null,
promisedBy = null
} = params;
// 初始化基础属性
this.id = id;
this.orderId = orderId;
this.customer = customer;
this.items = [...items]; // 防御性拷贝避免外部引用
// 合并剩余参数(低优先级)
Object.assign(this, {orderId, orderDate, promisedBy});
// 处理字段映射(中优先级,覆盖直接赋值)
Object.entries(fieldMapping).forEach(([paramKey, classKey]) => {
if (paramKey in params) {
this[classKey] = params[paramKey];
}
});
// 注入常量配置(最高优先级,不可被覆盖)
Object.entries(constants).forEach(([key, value]) => {
this[key] = value;
});
}
/**
* 添加订单项
* @param {Object} item - 订单项对象
*/
itemAdd(item) {
this.items.push(item);
console.log(`Item added: ${JSON.stringify(item)}`);
}
/**
* 删除订单项
* @param {string|Object} item - 订单项ID或对象
*/
itemDelete(item) {
const targetId = typeof item === 'string' ? item : item?.id;
const index = this.items.findIndex(i => i.id === targetId);
if (index !== -1) {
const [deleted] = this.items.splice(index, 1);
console.log(`Item deleted: ${JSON.stringify(deleted)}`);
}
}
}
补充内容 (2025-4-11 18:45):
2025-4-11 : V0.41版本
补充内容 (2025-4-11 21:53):
2025-4-11: V0.42版本 扩展订单项目以及明细
项目主题具有良好的可扩展性
补充内容 (2025-4-13 21:31):
2025-4-13:V0.5版本
可以选择商品
补充内容 (2025-4-14 17:36):
2025-4-14: V0.52
增加右键功能,展开类别选择区域。保存时隐藏类别选择区域
补充内容 (2025-4-15 16:26):
2025-4-15:V0.6
新增商品表货号列右键事件,计算货号
项目框架基本完善,后期会做一些优化。请老师们多提意见 |
评分
-
1
查看全部评分
-
|