/* eslint-disable arrow-parens */ import history from './../../utils/history'; import { updateGlobalData } from '../../utils/initMallList'; import { connect } from 'herculex'; import { clearCart, checkAllCar, checkOneCar, queryCarTypeList } from './service'; import { queryForCarList, updateCar } from '../mall-index/service'; import showToast from '../../utils/showToast'; import { buyIsAvailable } from '../../utils'; Component(connect({ mapStateToProps: { globalTabs: state => state.globalTabs || [], globalGoodsList: state => state.globalGoodsList || [], globalCar: state => state.$global.globalCar || [], globalShopId: state => state.$global.globalShopId || '' } })({ props: { onChange: () => null, tabBarHight: 0, showFlag: false, text: '开通会员享优惠', type: 'car' }, didMount() {}, didUpdate() {}, data: { popUpShow: false, deliveryPopShow: false, checkedNum: 0, showAuto: false, deliverTypeList: [], itemIndex: 99, memberPopUpShow: false, memberItem: null }, methods: { saveRef(ref) { // 存储自定义组件实例,方便以后调用 this.test = ref; }, cancelClick() { this.onClosePopup(); }, onClosePopup() { this.setData({ popUpShow: false, deliveryPopShow: false, deliverTypeList: [], itemIndex: 99 }); }, openPopup() { const { carts = [] } = this.data.globalCar; if (carts.length) { this.setData({ popUpShow: true }); } }, // 公共函数 回传shopId 执行fn更新 查询购物车 async publicUpdateCar(fn) { const { globalShopId } = this.data; try { my.showLoading(); await fn(globalShopId); // 获取购物车数据 const car = await queryForCarList({ shopId: globalShopId }); my.hideLoading(); updateGlobalData.call(this, car); if (car.carts && !car.carts.length) { this.onClosePopup(); } } catch (err) { my.hideLoading(); showToast({ content: err.msg, type: 'none' }); } }, // 点击清空按钮 展示二次确认弹框 clearCar() { my.confirm({ title: '', content: '确认清空购物车吗?', confirmButtonText: '确认', cancelButtonText: '取消', success: result => { if (result.confirm) { // 发起请求 this.publicUpdateCar(async globalShopId => { await clearCart({ shopId: globalShopId }); }); this.setData({ popUpShow: false }); } } }); }, // 单选 handelItemCheck(e) { const { skuId, checkedState, status } = e.target.dataset; if (status === 'N') { return; } // 发起请求 this.publicUpdateCar(async globalShopId => { await checkOneCar({ shopId: globalShopId, skuId, checkedState: checkedState === 'Y' ? 'N' : 'Y' }); }); }, // 全选 or 全不选 checkedAllChange() { // 发起请求 // todo const { globalCar: { carts: carList = [] } } = this.data; const checkedState = carList.every(v => { if (v.status === 'N') { return true; } return v.checkedState === 'Y'; }); const status = checkedState ? 'N' : 'Y'; this.publicUpdateCar(async globalShopId => { await checkAllCar({ shopId: globalShopId, checkedState: status }); }); }, // 购物车中 点击加号 handelItemChange(e) { const { skuId, number, type } = e; // 发起请求 this.publicUpdateCar(async globalShopId => { await updateCar({ shopId: globalShopId, skuId, num: number, type }); }); }, // 关闭弹框 跳转去判断配送弹窗 结算 goToConfirm({ totalPrice, carCheckedList, disCountTotalPrice, deliveryCost, deliverType, packingFee, estimatedPrice, deliverTypeItem }) { this.onClosePopup(); if (estimatedPrice) { const { componentData } = this.props; return this.setData({ memberPopUpShow: true, memberItem: { totalPrice, carCheckedList, disCountTotalPrice, deliveryCost, deliverType, packingFee, estimatedPrice, deliverTypeItem, ...componentData } }); } else { const { componentData } = this.props; history.push({ title: '订单确认', pageType: 'order-confirm', componentData: { totalPrice, carCheckedList, disCountTotalPrice, deliveryCost, deliverType, packingFee, deliverTypeItem, ...componentData }, header: 'show' }); } }, onCheckBoxClick({ itemIndex, isChecked }) { const { deliverTypeList } = this.data; const ListWithCheck = deliverTypeList.map((v, index) => { if (index === itemIndex) { if (v.deliverType === 'DELIVERY_LOGISTICS' && !v.deliveryTemplate) { my.showToast({ content: '当前门店无配送模版,暂不支持配送' }); return { ...v, isChecked: false }; } return { ...v, isChecked }; } return { ...v, isChecked: false }; }); const isPopActive = !ListWithCheck.every(v => !v.isChecked); this.setData({ deliverTypeList: ListWithCheck, itemIndex: isPopActive ? itemIndex : 99, isPopActive }); }, async queryOnceCarTypeList() { const { globalShopId } = this.data; const list = await queryCarTypeList(globalShopId); return list.map(v => ({ ...v, isChecked: false })); }, // 去结算判断是否弹框选择单一类型 async judgeDeliverType() { try { const list = await this.queryOnceCarTypeList(); if (list.length > 1) { // 两种类型 需要弹框 my.alert({ content: '已选商品支持多种配送方式,请选择结算方式', success: () => { this.setData({ deliveryPopShow: true, deliverTypeList: list }); } }); } else { // 单一类型 去结算 const { totalPrice = '', disCountTotalPrice = '', deliveryCost = 0, carts = [], deliverType = '', packingFee = 0, estimatedPrice = 0 } = list[0]; const carCheckedList = carts.filter(v => v.checkedState === 'Y' && v.status === 'Y'); // this.setData({ // memberPopUpShow: true, // }); this.goToConfirm({ totalPrice, disCountTotalPrice, deliveryCost, carCheckedList, deliverType, packingFee, estimatedPrice, deliverTypeItem: list[0] }); } } catch (err) { my.showToast({ content: err.msg || '请求失败' }); } }, // 去订单确认页面 gotoCreateOrder() { const { deliverTypeList, itemIndex } = this.data; if (!deliverTypeList[itemIndex]) { return; } const { totalPrice = '', disCountTotalPrice = '', deliveryCost = '', carts = [], packingFee = '', deliverType = '', estimatedPrice = 0 } = deliverTypeList[itemIndex]; const carCheckedList = carts.filter(v => v.checkedState === 'Y' && v.status === 'Y'); this.goToConfirm({ totalPrice, disCountTotalPrice, deliveryCost, carCheckedList, deliverType, packingFee, estimatedPrice, deliverTypeItem: deliverTypeList[itemIndex] }); }, // 去结算 async handelCreateOrder() { try { const { carts: preCar } = this.data.globalCar; const { globalShopId } = this.data; const car = await queryForCarList({ shopId: globalShopId }); updateGlobalData.call(this, car); const { carts } = car; const carCheckedList = carts.filter(v => v.checkedState === 'Y' && v.status === 'Y'); // 没预约 if (carCheckedList.some(v => v.userLimitType === 'BESPEAK' && !v.isbespeak)) { return showToast({ content: '部分勾选商品需要预约,请前往预约', type: 'none' }); } // 不同类型商品不能一起下单 if (!buyIsAvailable(carCheckedList)) { return showToast({ content: '商品发货类型不同,请分别购买', type: 'none' }); } // 选择的商品全停售 if (!carCheckedList.length) { return showToast({ content: '商品已停售,请选择其他商品', type: 'none' }); } // 部分停售 if (preCar.filter(v => v.checkedState === 'Y').length !== carCheckedList.length) { return my.confirm({ content: '部分商品已停售,已为您取消勾选', confirmButtonText: '继续购买', cancelButtonText: '重新查看', success: e => { const { confirm } = e; if (confirm) { this.judgeDeliverType(); } }, fail: () => 0 }); } // 有库存标志 但是库存不足 if (carCheckedList.filter(v => v.stockStatus === 'Y' && v.quantity > v.stock).length) { return showToast({ content: '您的订单存在库存不足商品,请核对数量', type: 'none' }); } // 有限购 超过购了 if (carCheckedList.filter(v => v.buyLimitType === 'GENERAL' && v.quantity > v.buyLimitCount).length) { return showToast({ content: '您的订单存在超出限购商品,请核对数量', type: 'none' }); } this.setData({ popUpShow: false }); // 不属于购物车有更新 判断类型 this.judgeDeliverType(); } catch (err) { showToast({ content: '查询购物车失败', type: 'fail' }); } }, // 点击了会员卡按钮弹窗 handleMemberBtnClick({ type, memberItem }) { if (type === 'none') { // 原价结算 history.push({ title: '订单确认', pageType: 'order-confirm', componentData: { ...memberItem }, header: 'show' }); } else { const { type: carMemberType } = this.props; if (carMemberType === 'car') { this.commitGlobal && this.commitGlobal({ openMallMemberCard: true }); } else { my.navigateTo({ url: '/pages/one/index?pageCode=shopIndex' }); } } }, // 关闭会员开卡弹框 onMemberPopClose() { this.setData({ memberPopUpShow: false }); } } }));