SOURCE

var productList = {
  "deviceId": "测试名字没问题",
  "productDTO": {
    "productId": 1, //商品ID
    "productName": "西蓝花",
    //商品
    "productImage": "http://img.netbian.com/file/2020/0914/125f86647f0c75c770ec5ceaf69c4e55.jpg",
    "price": "10.00",
    "quantity": 1,
    //数量
    "unit": "件",
    "weight": 200,
    //重量 g
    "type": 1,
    // 类型 //JIN(0,"斤"),JIAN(1,"件"),PING(2,"瓶"),GONGJIN(3,"公斤");
    "money": "8.00",
    "vipPrice": "8.00" //会员价
  },
  "status": 'ADD'
};
var productList2 = {
  "deviceId": "测试名字没问题",
  "productDTO": {
    "productId": 1, //商品ID
    "productName": "西蓝花",
    //商品
    "productImage": "http://img.netbian.com/file/2020/0914/125f86647f0c75c770ec5ceaf69c4e55.jpg",
    "price": "10.00",
    "quantity": 0,
    //数量
    "unit": "件",
    "weight": 200,
    //重量 g
    "type": 1,
    // 类型 //JIN(0,"斤"),JIAN(1,"件"),PING(2,"瓶"),GONGJIN(3,"公斤");
    "money": "8.00",
    "vipPrice": "8.00" //会员价
  },
  "status": 'UPDATE'
};
function mockSocket(data) {
  let type = data.status; //0 开门 1 关门
  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')
    // this.setData({
    //   productList: list,
    //   oldProductList: oldProductList,
    //   orderCountMoney: orderCountMoney
    // });
  }
};

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)
        // realList.push(data)
      } 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)
          }
        }

      }
      // 如果游标为-1 说明此商品未在oldList中存储 需要插入新的栈中更新上一次的数据堆
    });
    realStore = realList;
    console.log(realIndex, '查找到同类商品的游标');
    // let multiCountMoney = realStore.reduce((total, item) => {
    //   console.log(item, '计算') return total += Number(item.productDTO.money) == 0 ? 0 : parseInt(item.productDTO.money)
    // },
    // 0) 
    productList = realStore;
    console.log(productList,'realTimeComputed')
  }
}

mockSocket(productList)
mockSocket(productList2)
console 命令行工具 X clear

                    
>
console