var productList = {
"deviceId": "测试名字没问题",
"productDTO": {
"productId": 1,
"productName": "西蓝花",
"productImage": "http://img.netbian.com/file/2020/0914/125f86647f0c75c770ec5ceaf69c4e55.jpg",
"price": "10.00",
"quantity": 1,
"unit": "件",
"weight": 200,
"type": 1,
"money": "8.00",
"vipPrice": "8.00"
},
"status": 'ADD'
};
var productList2 = {
"deviceId": "测试名字没问题",
"productDTO": {
"productId": 1,
"productName": "西蓝花",
"productImage": "http://img.netbian.com/file/2020/0914/125f86647f0c75c770ec5ceaf69c4e55.jpg",
"price": "10.00",
"quantity": 0,
"unit": "件",
"weight": 200,
"type": 1,
"money": "8.00",
"vipPrice": "8.00"
},
"status": 'UPDATE'
};
function mockSocket(data) {
let type = data.status;
switch (type) {
case 'ADD':
this.initProductList(data);
break;
case 'UPDATE':
this.realTimeComputed(data);
break;
case 'CLOSE':
break;
default:
break;
}
};
function initProductList(data) {
let list = [];
console.log('走初始化这一步', data);
if (data.hasOwnProperty('productDTO') && data.productDTO !== undefined) {
list.push(data)
let orderCountMoney = data.productDTO.money;
let oldProductList = list;
productList = list;
console.log(productList,'initProductList')
}
};
function realTimeComputed(data) {
let realList = [...productList],
tempStore = [],
realStore = [];
console.log(realList, '多条数据时传递');
if (realList.length > 0) {
let realIndex = null;
realList.map((item, index) => {
if (data.hasOwnProperty('productDTO') && data.productDTO.productId === item.productDTO.productId) {
realIndex = index
}
if (realList[realIndex].quantity == 0) {
realList.splice(realList[realIndex], 1)
} else {
if (data.hasOwnProperty('productDTO') && data.productDTO.productId === item.productDTO.productId) {
if (data.productDTO.weight === item.productDTO.weight) {
realList.splice(realList[realIndex], 1);
realList.push(data)
} else {
realList.push(data)
}
}
}
});
realStore = realList;
console.log(realIndex, '查找到同类商品的游标');
productList = realStore;
console.log(productList,'realTimeComputed')
}
}
mockSocket(productList)
mockSocket(productList2)
console