• store/index.js 加入购物车 本地存储 vuex里面操作


    import Vue from 'vue'
    import Vuex from 'vuex'
    import Axios from '../service/axios'

    Vue.use(Vuex)

    export default new Vuex.Store({
    state: {
    shopCart:{
    items:{},
    totalMoney:0,
    num:0,
    },
    detailProList:{},
    showShopcart:false,
    showDetailFood:false,
    storeName:{},
    delivery:'',
    self_mention:'',
    order_no:'',
    userInfo:'',
    cardInfo:'',
    addressList:'',
    ifAddNewAddress:false,
    getCurrentMoney (money){
    let money1 = money * 100;
    let intMoney1 = parseInt(money1);
    let diff = money1 - intMoney1;
    let exit=String(diff).indexOf('e')
    let decDiff = parseInt(diff * 10);
    console.log(decDiff)
    if (decDiff > 5&&exit=='-1') {
    intMoney1 += 1;
    }
    return intMoney1/100;
    },
    addressType:'1',
    itemIndex:'',
    // payType:'',
    cartStyle:'1',
    },
    getters: {
    shopCart:state=>{
    return state.shopCart
    },
    cartStyle:state=>{
    return state.cartStyle
    },
    // payType:state=>{
    // return state.payType
    // },
    addressType:state=>{
    return state.addressType
    },
    allAddressList:state=>{
    return state.addressList
    },
    ifAddNewAddress:state=>{
    return state.ifAddNewAddress
    },
    cardInfo:state=>{
    return state.cardInfo
    },
    userInfo:state=>{
    return state.userInfo
    },
    detailProList:state=>{
    return state.detailProList
    },
    showShopcart:state=>{
    return state.showShopcart
    },
    showDetailFood:state=>{
    return state.showDetailFood
    },
    storeName:state=>{
    return state.storeName
    },
    orderNo:state=>{
    return state.order_no
    },
    delivery:state=>{
    return state.delivery
    },
    self_mention:state=>{
    return state.self_mention
    },
    },
    mutations: {
    saveItemIndex(state,itemIndex){
    state.itemIndex=itemIndex
    },
    controlCartStyle(state,cartStyle){
    state.cartStyle=cartStyle
    },
    saveShopCart(state,shopCart){
    state.shopCart=shopCart
    },
    // savePayType(state,payType){
    // state.payType=payType
    // },
    saveAddressType(state,type){
    state.addressType=type
    window.sessionStorage.setItem('addressType',JSON.stringify(state.addressType));
    },
    saveUserInfo(state,userInfo){
    state.userInfo=userInfo;
    },
    selfMention(state,item){
    state.self_mention=item
    window.sessionStorage.setItem('self_mention',JSON.stringify(state.self_mention));
    },
    delivery(state,item){
    state.delivery=item
    window.sessionStorage.setItem('delivery',JSON.stringify(state.delivery));
    },
    saveMyCard(state,cardInfo){
    state.cardInfo=cardInfo
    },
    saveOrderNo(state,order_no){
    state.order_no=order_no
    },
    addShopCart(state,item){
    let cart =state.shopCart
    if(cart.items[item.date] === undefined){
    cart.items[item.date]=[]
    }
    let isExist=false
    for(let i in cart.items[item.date]){
    if(cart.items[item.date][i].id==item.id){
    cart.items[item.date][i].num +=1;
    isExist=true;
    break;
    }
    }
    if(!isExist){
    item.num=1
    cart.items[item.date].push(item)
    }
    cart.num +=1;
    cart.totalMoney +=Number(item.price);
    cart.totalMoney=state.getCurrentMoney(cart.totalMoney);
    window.sessionStorage.setItem('shopCart',JSON.stringify(cart));
    },
    saveAddressList(state,addressList){
    state.addressList=addressList
    state.ifAddNewAddress=false
    },
    ifAddNewAddress(state,ifAddNewAddress){
    state.ifAddNewAddress=ifAddNewAddress
    },
    minusShopCart(state,item){
    if(item.num==0){
    return
    }
    item.num -=1;
    let cart=state.shopCart
    cart.num -=1;
    cart.totalMoney -=Number(item.price);
    cart.totalMoney=state.getCurrentMoney(cart.totalMoney);
    if(item.num==0){
    var deleteShopCart=cart.items[item.date];
    for(var j=0;j<deleteShopCart.length;j++){
    if(item.id==deleteShopCart[j].id){
    deleteShopCart.splice(j,1);
    }
    }
    }
    if(cart.num==0){
    state.showShopcart=false
    }
    window.sessionStorage.setItem('shopCart',JSON.stringify(cart));

    },
    deleteShopCart(state,item){
    let cart=state.shopCart
    cart.num -=item.num
    cart.totalMoney -=(item.num*item.price)
    cart.totalMoney=state.getCurrentMoney(cart.totalMoney);
    /** item.number = 0 是为了清空商品详情里面number */
    item.num=0
    var deleteShopCart=cart.items[item.date];
    for(var j=0;j<deleteShopCart.length;j++){
    if(item.id==deleteShopCart[j].id){
    deleteShopCart.splice(j,1);
    }
    }
    if(cart.num==0){
    state.showShopcart=false
    }
    window.sessionStorage.setItem('shopCart',JSON.stringify(cart));
    },
    emptyShopCart(state){
    state.shopCart={
    items:{},
    totalMoney:0,
    num:0,
    }
    state.detailProList.num=0
    state.showShopcart=false
    window.sessionStorage.setItem('shopCart',JSON.stringify(state.shopCart));
    },
    detailProList(state,item){
    state.detailProList=item

    },
    showShopcart(state){
    state.showShopcart=!state.showShopcart
    },
    showDetail(state){
    state.showDetailFood=!state.showDetailFood
    },
    storageStore(state,item){
    state.storeName=item
    window.sessionStorage.setItem('storeName',JSON.stringify(state.storeName));
    },

    },
    actions: {
    addCart({commit},item){
    commit("addShopCart",item)
    },
    minusCart({commit},item){
    commit("minusShopCart",item)
    },
    deleteCart({commit},item){
    commit("deleteShopCart",item)
    },
    emptyCart({commit}){
    commit("emptyShopCart")
    },
    watchDetail({commit},item){
    commit("detailProList",item)
    },
    storageStore({commit},item){
    commit("storageStore",item)
    },
    showCart({commit}){
    commit("showShopcart")
    },
    showDetailFood({commit}){
    commit("showDetail")
    },
    getCardInfo({commit}){
    Axios.get('/shop/api/get-wx-user-customer',{
    params:{
    customerId:_global.customerId
    }
    }).then((response) => {
    if(response.data.status){
    console.log(11111)
    commit('saveMyCard',response.data.data);
    }
    })
    }
    }
    })
  • 相关阅读:
    你知道吗,Flutter内置了10多种show
    强大的Flutter App升级功能
    Flutter 日期时间DatePicker控件及国际化
    你知道吗,Flutter内置了10多种Button控件
    Flutter Form表单控件超全总结
    Flutter 裁剪类组件 最全总结
    Flutter 拖拽控件Draggable看这一篇就够了
    Python 浮点数的冷知识
    Python 为了提升性能,竟运用了共享经济
    Python 之父的解析器系列之六:给 PEG 语法添加动作
  • 原文地址:https://www.cnblogs.com/MR-cui/p/8919137.html
Copyright © 2020-2023  润新知