// 加入购物车必要方法 class goodsMethod { // sku_code 用户所选规格标识 // sku 规格展示(文字/其他) /** * 加入购物车前规格格式化辅助方法 * * 第三方点餐渠道给出的规格分为 * * 1.所有规格 * 2.所有规格可搭配的组合(包含该搭配组合的skucode) * 3.需要使用用户选择的规格与所有规格可搭配的组合对比,相同的则取出skucode */ skuInspect() { console.log(this['item'], 'skuInspect方法'); // 因为瑞幸是第一个带规格的点餐需求 // 后来后面规格字段和文字显示字段逐渐统一,也懒得去改了,就先这样区分吧 // 瑞幸规格筛选方法 if (this['commodity']['product_choose'] && this['commodity']['brand_id'] == 13) { // if (!Reflect.has(this['commodity'], 'sku_code')) { // console.log(this['commodity'], 'commodity'); // let index = -1; // let code = ''; // outer: for (let j = 0; j < this['commodity']['details']['sku_infos']['length']; j++) { // let item1 = this['commodity']['details']['sku_infos'][j]; // index = 0; // code = item1['code']; // for (let k = 0; k < item1['values']['length']; k++) { // let item2 = item1['values'][k]; // if (this['commodity']['sku'].indexOf(item2['spec_name']) != -1) { // index += 1; // } else { // continue; // } // if (index >= this['commodity']['sku']['length']) { // this['commodity']['sku_code'] = code; // break outer; // } // } // }; // if (!code) this['commodity']['sku_code'] = this['commodity']['details']['sku_infos'][0]['code']; // } // // if (!this['commodity']?.'sku_code')) { // // this.screenSku1(); // // } // // if ('sku_code' in this['commodity']) { // // this.screenSku1(); // // } // // 以上方法皆可使用,主要用来判断当前商品是否已经加入购物车,不然在已经加入的购物车中点击商品加号,会报错details属性没有 } else if (this['commodity']['product_choose'] && this['commodity']['brand_id'] == 10) { // if (!Reflect.has(this['commodity'], 'sku_code')) { // let str = []; // for (let i = 0; i < this['commodity']['details']['optional']['length']; i++) { // let t = this['commodity']['details']['optional']; // str.push(`${t[i]['id']}_${t[i]['sku_infos'].find(item=>item['checked'])['id']}`) // }; // this['commodity']['sku_code'] = str.join(','); // }; } else if (this['commodity']['sku_code'] && this['commodity']['brand_id'] == 5) { // 因为麦当劳需要进入新页面选择套餐,在新页面初始化拿到数据的时候就要对数据经行处理所以规格方法直接在页面中写入 } } // 规格加入方法 skuIntegration() { let _c = this['commodity']; console.log('skuIntegration方法'); // 规格是否相同 if (this['item']['sku_code'] == _c['sku_code']) { this['item']['num'] += (+this['quantity']); } else { // 同一商品,不同规格,检查是否存在存放多规格数组 if (Reflect.has(this['item'], 'Differentskulist')) { // 有则检查当前需要加入的商品在多规格数组中是否存在 let index; // 如果存在则会使用index变量直接修改已存在数据 // 检查多规格数组中是否存在该商品 let iscommodity = this['item']['Differentskulist'].find((e, i) => { if (e['sku_code'] == _c['sku_code']) { index = i; return e } }); // 不存在则直接push进去一个新的商品对象 if (iscommodity === undefined) { this['item']['Differentskulist'].push(_c); } else { // 存在则将商品数量加1 this['item']['Differentskulist'][index]['num'] += (+this['quantity']); }; } else { // 没有,则创建空数组 this['item']['Differentskulist'] = []; this['item']['Differentskulist'].push(_c); } } } // 规格商品删除方法 SkuDeleteMethod() { if (this['item']['sku_code'] == this['commodity']['sku_code']) { this['item']['num'] -= this['quantity']; if (this['item']['num'] <= 0) { if (!Reflect.has(this['item'], 'Differentskulist') || this['item'][ 'Differentskulist' ]['length'] <= 0) { Reflect.deleteProperty(this['shop'], this['commodity']['product_id']); } else { if (this['item']['Differentskulist']['length'] > 0) { Reflect.defineProperty(this['shop'], this['commodity'][ 'product_id' ], { value: { ...this['item'], ...this['item']['Differentskulist'][0] }, writable: true, enumerable: true }); this['item']['Differentskulist'].splice(0, 1); } } }; } else { let index; // 如果存在则会使用index变量直接修改已存在数据 // 检查多规格数组中是否存在该商品 let iscommodity = this['item']['Differentskulist'].find((e, i) => { if (e['sku_code'] == this['commodity']['sku_code']) { index = i; return e } }); this['item']['Differentskulist'][index]['num'] -= this['quantity']; // 商品数量为0直接删除 if (this['item']['Differentskulist'][index]['num'] <= 0) { this['item']['Differentskulist'].splice(index, 1) }; } } // 单品添加 SINGLE_PRODUCT_ADD() { this['item']['num'] += this['quantity']; } // 单品删除 SINGLE_PRODUCT_REMOVE(){ // 减去当前商品数量 this['item']['num'] -= this['quantity']; // 删除之后如果num小于等于0则将该商品从店铺购物车中删除 if (this['item']['num'] <= 0) { Reflect.deleteProperty(this['shop'], this['commodity']['product_id']); }; } /** * @param {Object} cartList-当前店铺的购物车 * @param {Object} commodity-需要操作的商品 * @param {Number} quantity-商品操作的数量 */ set Sex({ cartList, commodity, quantity }) { let { product_id, restaurant_id } = commodity; console.log('设置'); // 当前所有购物车 this['cart'] = cartList, // 当前品牌购物车 this['brand'] = null, // 当前店铺购物车 this['shop'] = null, // 当前购物车中同ID商品 this['item'] = null, // 当前需要加入购物车的商品 this['commodity'] = commodity, // 需要加入的数量 this['quantity'] = quantity, // 商品ID this['product_id'] = product_id, // 店铺ID this['restaurant_id'] = restaurant_id; /** * @此处购物车初始化逻辑 * 1.购物车数据格式为: * 整体购物车:{ * * 1:{// 某一个品牌(比如肯德基)下的所有购物车 * * 2560:{// 某一个品牌(比如肯德基)下的某一个店铺的购物车 * * 3430:{ 该店铺的购物车中的某一个商品 * brand_id: 1, * product_name: "汁汁厚作芝士安格斯牛堡单人餐", * restaurant_id: 2560 * } * * } * * } * * } * 先从品牌查起,若整体购物车中没有该品牌的购物车则创建一个空对象 * 然后再查店铺,依次类推 */ Reflect.set(this['cart'], this['commodity']['brand_id'], this['cart'][this['commodity']['brand_id']] || {}); this['brand'] = this['cart'][this['commodity']['brand_id']]; console.log(this['brand'], 'brand'); Reflect.set(this['brand'], this['restaurant_id'], this['brand'][this['restaurant_id']] || {}); this['shop'] = this['brand'][this['restaurant_id']]; console.log(this['shop'], 'shop'); Reflect.set(this['shop'], this['product_id'], this['shop'][this['product_id']] || {}); this['item'] = this['shop'][this['product_id']]; console.log(this['item'], 'item'); } } // 加入购物车 class addMethod extends goodsMethod { constructor() { super(); } init() { return new Promise((reslove, reject) => { // 开始格式化商品规格 this.skuInspect(); // 判断购物车中是否存在该商品 if ('product_id' in this['item']) { console.log('购物车中有该商品'); // 存在.则检查要加入的商品是否是规格商品 console.log(this['commodity']['sku_code'],'是否规格加入'); if (this['commodity']['sku_code']) { console.log('该商品是规格类型商品'); this.skuIntegration(); } else { console.log('该商品不是规格类型商品'); this.SINGLE_PRODUCT_ADD(); } } else { console.log('购物车中没有该商品'); // 第一次加入购物车的商品也需要处理规格 Reflect.defineProperty(this['shop'], this['product_id'], { value: { // 正常逻辑新加入购物车时是没有amount字段的,而amount则是在订单页面重新购买时才会有 ...this['commodity'], // num: Reflect.has(this['commodity'], 'amount') ? this['commodity']['amount'] : this['quantity'], // num可在此处声明,也可以在下单时添加该字段表明添加数量 num: this['quantity'], }, writable: true, enumerable: true }); }; reslove(this['cart']); }) } }; // 移除购物车 class removeMethod extends goodsMethod { constructor() { super(); } init() { return new Promise((resolve, reject) => { console.log(this['item'], 'item'); console.log(this['commodity'], 'thiscommodity'); // 判断是否是规格/套餐商品 if (Reflect.has(this['commodity'], 'sku_code')) { this.SkuDeleteMethod(); console.log('删除完成'); } else { // 不是套餐则为单品 this.SINGLE_PRODUCT_REMOVE(); }; resolve(this['cart']) }) } }; export { addMethod, removeMethod, }