mutations.js 3.8 KB
Newer Older
M
maguohua 已提交
1
import { RECORD_ADDRESS, ADD_CART, REDUCE_CART, INIT_BUYCART, CLEAR_CART, RECORD_SHOPDETAIL, RECORD_USERINFO, GET_USERINFO} from './mutation-types.js'
M
maguohua 已提交
2
import {setStore, getStore} from '../config/mUtils'
M
udpata  
maguohua 已提交
3 4

export default {
M
maguohua 已提交
5 6 7 8
	// 记录当前经度纬度
	[RECORD_ADDRESS] (state, {latitude, longitude}) {
		state.latitude = latitude;
		state.longitude = longitude;
M
udpata  
maguohua 已提交
9
	},
M
updata  
maguohua 已提交
10 11 12
	[RECORD_SHOPDETAIL](state, detail){
		state.shopDetail = detail;
	},
M
maguohua 已提交
13
	// 加入购物车
M
maguohua 已提交
14
	[ADD_CART] (state, {shopid, category_id, item_id, food_id, name, price, specs}) {
M
maguohua 已提交
15 16
		let cart = state.cartList;
		if (cart[shopid]&&cart[shopid][category_id]&&cart[shopid][category_id][item_id]&&cart[shopid][category_id][item_id][food_id]) {
M
maguohua 已提交
17
			cart[shopid][category_id][item_id][food_id]['num'] ++;
M
maguohua 已提交
18
		}else if(cart[shopid]&&cart[shopid][category_id]&&cart[shopid][category_id][item_id]){
M
maguohua 已提交
19 20 21 22 23
			cart[shopid][category_id][item_id][food_id] = {};
			cart[shopid][category_id][item_id][food_id]['num'] = 1;
			cart[shopid][category_id][item_id][food_id]['name'] = name;
			cart[shopid][category_id][item_id][food_id]['price'] = price;
			cart[shopid][category_id][item_id][food_id]['specs'] = specs;
M
maguohua 已提交
24 25
		}else if(cart[shopid]&&cart[shopid][category_id]){
			cart[shopid][category_id][item_id] = {};
M
maguohua 已提交
26
			cart[shopid][category_id][item_id][food_id] = {};
M
maguohua 已提交
27 28 29 30
			cart[shopid][category_id][item_id][food_id]['num'] = 1;
			cart[shopid][category_id][item_id][food_id]['name'] = name;
			cart[shopid][category_id][item_id][food_id]['price'] = price;
			cart[shopid][category_id][item_id][food_id]['specs'] = specs;
M
maguohua 已提交
31 32 33
		}else if(cart[shopid]){
			cart[shopid][category_id] = {};
			cart[shopid][category_id][item_id] = {};
M
maguohua 已提交
34 35 36 37 38
			cart[shopid][category_id][item_id][food_id] = {};
			cart[shopid][category_id][item_id][food_id]['num'] = 1;
			cart[shopid][category_id][item_id][food_id]['name'] = name;
			cart[shopid][category_id][item_id][food_id]['price'] = price;
			cart[shopid][category_id][item_id][food_id]['specs'] = specs;
M
maguohua 已提交
39 40 41 42
		}else{
			cart[shopid] = {};
			cart[shopid][category_id] = {};
			cart[shopid][category_id][item_id] = {};
M
maguohua 已提交
43 44 45 46 47
			cart[shopid][category_id][item_id][food_id] = {};
			cart[shopid][category_id][item_id][food_id]['num'] = 1;
			cart[shopid][category_id][item_id][food_id]['name'] = name;
			cart[shopid][category_id][item_id][food_id]['price'] = price;
			cart[shopid][category_id][item_id][food_id]['specs'] = specs;
M
maguohua 已提交
48
		}
M
maguohua 已提交
49
		//返回一个新的对象,否则计算属性无法监听到数据的变化
M
maguohua 已提交
50
		state.cartList = Object.assign({}, cart);
M
maguohua 已提交
51
		//存入localStorage
M
maguohua 已提交
52 53
		setStore('buyCart', state.cartList);
	},
M
maguohua 已提交
54
	// 移出购物车
M
maguohua 已提交
55
	[REDUCE_CART] (state, {shopid, category_id, item_id, food_id, name, price, specs}) {
M
maguohua 已提交
56 57
		let cart = state.cartList;
		if (cart[shopid]&&cart[shopid][category_id]&&cart[shopid][category_id][item_id]&&cart[shopid][category_id][item_id][food_id]) {
M
maguohua 已提交
58 59
			if (cart[shopid][category_id][item_id][food_id]['num'] > 0) {
				cart[shopid][category_id][item_id][food_id]['num'] --;
M
maguohua 已提交
60
				//返回一个新的对象,否则计算属性无法监听到数据的变化
M
maguohua 已提交
61
				state.cartList = Object.assign({}, cart);
M
maguohua 已提交
62
				//存入localStorage
M
maguohua 已提交
63
				setStore('buyCart', state.cartList);
M
maguohua 已提交
64
			}else{
M
maguohua 已提交
65
				//商品数量为0,则清空当前商品的信息
M
maguohua 已提交
66
				cart[shopid][category_id][item_id][food_id] = null;
M
maguohua 已提交
67 68 69
			}
		}
	},
M
maguohua 已提交
70
	//网页初始化时从本地缓存获取购物车数据
M
maguohua 已提交
71 72 73 74 75
	[INIT_BUYCART](state){
		let initCart = getStore('buyCart');
		if (initCart) {
			state.cartList = JSON.parse(initCart);
		}
M
maguohua 已提交
76
	},
M
maguohua 已提交
77
	//清空当前商品的购物车信息
M
maguohua 已提交
78 79 80 81
	[CLEAR_CART](state,shopid){
		state.cartList[shopid] = null;
		state.cartList = Object.assign({}, state.cartList);
		setStore('buyCart', state.cartList);
M
maguohua 已提交
82 83 84 85 86 87 88 89 90 91 92
	},
	[RECORD_USERINFO](state, info){
		state.userInfo = info;
		setStore('useInfo', info);
	},
	[GET_USERINFO](state){
		let info = getStore('useInfo');
		if (info) {
			state.userInfo = JSON.parse(info);
		}
	},
M
udpata  
maguohua 已提交
93
}