mutations.js 6.8 KB
Newer Older
M
maguohua 已提交
1 2 3 4 5 6 7 8 9
import {
	RECORD_ADDRESS,
	ADD_CART,
	REDUCE_CART,
	INIT_BUYCART,
	CLEAR_CART,
	RECORD_SHOPDETAIL,
	RECORD_USERINFO,
	GET_USERINFO,
M
maguohua 已提交
10
	CONFIRM_REMARK,
M
confrim  
maguohua 已提交
11 12 13 14
	CONFIRM_INVOICE,
	CHOOSE_SEARCH_ADDRESS,
	SAVE_GEOHASH,
	CHOOSE_ADDRESS,
M
maguohua 已提交
15 16 17 18 19 20
	NEED_VALIDATION,
	SAVE_CART_ID_SIG,
	SAVE_ORDER_PARAM,
	CHANGE_ORDER_PARAM,
	ORDER_SUCCESS,
	SAVE_SHOPID,
M
maguohua 已提交
21 22 23 24 25
} from './mutation-types.js'
import {
	setStore,
	getStore,
} from '../config/mUtils'
M
udpata  
maguohua 已提交
26 27

export default {
M
maguohua 已提交
28
	// 记录当前经度纬度
M
maguohua 已提交
29 30 31 32
	[RECORD_ADDRESS](state, {
		latitude,
		longitude
	}) {
M
maguohua 已提交
33 34
		state.latitude = latitude;
		state.longitude = longitude;
M
udpata  
maguohua 已提交
35
	},
M
maguohua 已提交
36 37

	[RECORD_SHOPDETAIL](state, detail) {
M
updata  
maguohua 已提交
38 39
		state.shopDetail = detail;
	},
M
maguohua 已提交
40
	// 加入购物车
M
maguohua 已提交
41 42 43 44 45 46 47 48 49 50 51 52
	[ADD_CART](state, {
		shopid,
		category_id,
		item_id,
		food_id,
		name,
		price,
		specs,
		packing_fee,
		sku_id,
		stock
	}) {
M
maguohua 已提交
53
		let cart = state.cartList;
M
maguohua 已提交
54 55 56
		if (cart[shopid] && cart[shopid][category_id] && cart[shopid][category_id][item_id] && cart[shopid][category_id][item_id][food_id]) {
			cart[shopid][category_id][item_id][food_id]['num']++;
		} else if (cart[shopid] && cart[shopid][category_id] && cart[shopid][category_id][item_id]) {
M
maguohua 已提交
57 58
			cart[shopid][category_id][item_id][food_id] = {};
			cart[shopid][category_id][item_id][food_id]['num'] = 1;
M
maguohua 已提交
59
			cart[shopid][category_id][item_id][food_id]['id'] = food_id;
M
maguohua 已提交
60 61 62
			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 已提交
63 64 65 66
			cart[shopid][category_id][item_id][food_id]['packing_fee'] = packing_fee;
			cart[shopid][category_id][item_id][food_id]['sku_id'] = sku_id;
			cart[shopid][category_id][item_id][food_id]['stock'] = stock;
		} else if (cart[shopid] && cart[shopid][category_id]) {
M
maguohua 已提交
67
			cart[shopid][category_id][item_id] = {};
M
maguohua 已提交
68
			cart[shopid][category_id][item_id][food_id] = {};
M
maguohua 已提交
69
			cart[shopid][category_id][item_id][food_id]['num'] = 1;
M
maguohua 已提交
70
			cart[shopid][category_id][item_id][food_id]['id'] = food_id;
M
maguohua 已提交
71 72 73
			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 已提交
74 75 76 77
			cart[shopid][category_id][item_id][food_id]['packing_fee'] = packing_fee;
			cart[shopid][category_id][item_id][food_id]['sku_id'] = sku_id;
			cart[shopid][category_id][item_id][food_id]['stock'] = stock;
		} else if (cart[shopid]) {
M
maguohua 已提交
78 79
			cart[shopid][category_id] = {};
			cart[shopid][category_id][item_id] = {};
M
maguohua 已提交
80 81
			cart[shopid][category_id][item_id][food_id] = {};
			cart[shopid][category_id][item_id][food_id]['num'] = 1;
M
maguohua 已提交
82
			cart[shopid][category_id][item_id][food_id]['id'] = food_id;
M
maguohua 已提交
83 84 85
			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 已提交
86 87 88 89
			cart[shopid][category_id][item_id][food_id]['packing_fee'] = packing_fee;
			cart[shopid][category_id][item_id][food_id]['sku_id'] = sku_id;
			cart[shopid][category_id][item_id][food_id]['stock'] = stock;
		} else {
M
maguohua 已提交
90 91 92
			cart[shopid] = {};
			cart[shopid][category_id] = {};
			cart[shopid][category_id][item_id] = {};
M
maguohua 已提交
93 94
			cart[shopid][category_id][item_id][food_id] = {};
			cart[shopid][category_id][item_id][food_id]['num'] = 1;
M
maguohua 已提交
95
			cart[shopid][category_id][item_id][food_id]['id'] = food_id;
M
maguohua 已提交
96 97 98
			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 已提交
99 100 101
			cart[shopid][category_id][item_id][food_id]['packing_fee'] = packing_fee;
			cart[shopid][category_id][item_id][food_id]['sku_id'] = sku_id;
			cart[shopid][category_id][item_id][food_id]['stock'] = stock;
M
maguohua 已提交
102
		}
M
maguohua 已提交
103
		//返回一个新的对象,否则计算属性无法监听到数据的变化
M
maguohua 已提交
104
		state.cartList = Object.assign({}, cart);
M
maguohua 已提交
105
		//存入localStorage
M
maguohua 已提交
106 107
		setStore('buyCart', state.cartList);
	},
M
maguohua 已提交
108
	// 移出购物车
M
maguohua 已提交
109 110 111 112 113 114 115
	[REDUCE_CART](state, {
		shopid,
		category_id,
		item_id,
		food_id,
		name,
		price,
M
maguohua 已提交
116
		specs,
M
maguohua 已提交
117
	}) {
M
maguohua 已提交
118
		let cart = state.cartList;
M
maguohua 已提交
119
		if (cart[shopid] && cart[shopid][category_id] && cart[shopid][category_id][item_id] && cart[shopid][category_id][item_id][food_id]) {
M
maguohua 已提交
120
			if (cart[shopid][category_id][item_id][food_id]['num'] > 0) {
M
maguohua 已提交
121
				cart[shopid][category_id][item_id][food_id]['num']--;
M
maguohua 已提交
122
				//返回一个新的对象,否则计算属性无法监听到数据的变化
M
maguohua 已提交
123
				state.cartList = Object.assign({}, cart);
M
maguohua 已提交
124
				//存入localStorage
M
maguohua 已提交
125
				setStore('buyCart', state.cartList);
M
maguohua 已提交
126
			} else {
M
maguohua 已提交
127
				//商品数量为0,则清空当前商品的信息
M
maguohua 已提交
128
				cart[shopid][category_id][item_id][food_id] = null;
M
maguohua 已提交
129 130 131
			}
		}
	},
M
maguohua 已提交
132
	//网页初始化时从本地缓存获取购物车数据
M
maguohua 已提交
133
	[INIT_BUYCART](state) {
M
maguohua 已提交
134 135 136 137
		let initCart = getStore('buyCart');
		if (initCart) {
			state.cartList = JSON.parse(initCart);
		}
M
maguohua 已提交
138
	},
M
maguohua 已提交
139
	//清空当前商品的购物车信息
M
maguohua 已提交
140
	[CLEAR_CART](state, shopid) {
M
maguohua 已提交
141 142 143
		state.cartList[shopid] = null;
		state.cartList = Object.assign({}, state.cartList);
		setStore('buyCart', state.cartList);
M
maguohua 已提交
144
	},
M
maguohua 已提交
145
	// 记录用户信息
M
maguohua 已提交
146
	[RECORD_USERINFO](state, info) {
M
maguohua 已提交
147
		state.userInfo = info;
M
confrim  
maguohua 已提交
148 149 150 151 152 153
		let validity = 30;
		let now = new Date();
		now.setTime(now.getTime() + validity * 24 * 60 * 60 * 1000);

		document.cookie = "USERID=" + info.user_id + ";expires=" + now.toGMTString();
		document.cookie = "SID=huRyTRd9QLij7NkbpHJoj3PQrx1eRiO6bAiw" + ";expires=" + now.toGMTString();
M
maguohua 已提交
154
	},
M
confrim  
maguohua 已提交
155 156 157 158 159 160
	//获取用户信息存入vuex
	[GET_USERINFO](state, info) {
		if (!info.message) {
			state.userInfo = info;
		} else {
			state.userInfo = null;
M
maguohua 已提交
161 162
		}
	},
M
maguohua 已提交
163 164 165 166
	//保存商铺id
	[SAVE_SHOPID](state, shopid) {
		state.shopid = shopid;
	},
M
maguohua 已提交
167 168 169 170 171 172 173
	//记录订单页面用户选择的备注, 传递给订单确认页面
	[CONFIRM_REMARK](state, {
		remarkText,
		inputText
	}) {
		state.remarkText = remarkText;
		state.inputText = inputText;
M
confrim  
maguohua 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	},
	//是否开发票
	[CONFIRM_INVOICE](state, invoice) {
		state.invoice = invoice;
	},
	//选择搜索的地址
	[CHOOSE_SEARCH_ADDRESS](state, place) {
		state.searchAddress = place;
	},
	//保存geohash
	[SAVE_GEOHASH](state, geohash) {
		state.geohash = geohash;
	},
	//选择的地址
	[CHOOSE_ADDRESS](state, {
		address,
		index
	}) {
		state.choosedAddress = address;
		state.addressIndex = index;
M
maguohua 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	},
	//保存下单需要验证的返回值
	[NEED_VALIDATION](state, needValidation) {
		state.needValidation = needValidation;
	},
	//保存下单后购物id 和 sig
	[SAVE_CART_ID_SIG](state, {
		cart_id,
		sig
	}) {
		state.cart_id = cart_id;
		state.sig = sig;
	},
	//保存下单参数,用户验证页面调用
	[SAVE_ORDER_PARAM](state, orderParam) {
		state.orderParam = orderParam;
	},
	//修改下单参数
	[CHANGE_ORDER_PARAM](state, newParam) {
		state.orderParam = Object.assign({}, state.orderParam, newParam);
	},
	//下单成功,保存订单返回信息
	[ORDER_SUCCESS](state, order) {
		state.orderMessage = order;
	},
	}