mutations.js 8.0 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
	SAVE_ORDER,
M
update  
maguohua 已提交
22
	OUT_LOGIN,
M
update  
maguohua 已提交
23 24 25
	RETSET_NAME,
	SAVE_AVANDER,
	SAVE_ADDRESS,
M
update  
maguohua 已提交
26
	SAVE_ADDDETAIL,
M
update  
maguohua 已提交
27
	SAVE_QUESTION,
M
update  
maguohua 已提交
28
	ADD_ADDRESS,
M
update  
maguohua 已提交
29
	BUY_CART,
M
maguohua 已提交
30
} from './mutation-types.js'
M
udpate  
maguohua 已提交
31

M
maguohua 已提交
32 33 34 35
import {
	setStore,
	getStore,
} from '../config/mUtils'
M
udpata  
maguohua 已提交
36 37

export default {
M
maguohua 已提交
38
	// 记录当前经度纬度
M
maguohua 已提交
39 40 41 42
	[RECORD_ADDRESS](state, {
		latitude,
		longitude
	}) {
M
maguohua 已提交
43 44
		state.latitude = latitude;
		state.longitude = longitude;
M
udpata  
maguohua 已提交
45
	},
M
maguohua 已提交
46 47

	[RECORD_SHOPDETAIL](state, detail) {
M
updata  
maguohua 已提交
48 49
		state.shopDetail = detail;
	},
M
maguohua 已提交
50
	// 加入购物车
M
maguohua 已提交
51 52 53 54 55 56 57 58 59 60 61 62
	[ADD_CART](state, {
		shopid,
		category_id,
		item_id,
		food_id,
		name,
		price,
		specs,
		packing_fee,
		sku_id,
		stock
	}) {
M
maguohua 已提交
63
		let cart = state.cartList;
M
maguohua 已提交
64 65 66
		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 已提交
67 68
			cart[shopid][category_id][item_id][food_id] = {};
			cart[shopid][category_id][item_id][food_id]['num'] = 1;
M
maguohua 已提交
69
			cart[shopid][category_id][item_id][food_id]['id'] = food_id;
M
maguohua 已提交
70 71 72
			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 已提交
73 74 75 76
			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 已提交
77
			cart[shopid][category_id][item_id] = {};
M
maguohua 已提交
78
			cart[shopid][category_id][item_id][food_id] = {};
M
maguohua 已提交
79
			cart[shopid][category_id][item_id][food_id]['num'] = 1;
M
maguohua 已提交
80
			cart[shopid][category_id][item_id][food_id]['id'] = food_id;
M
maguohua 已提交
81 82 83
			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 已提交
84 85 86 87
			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 已提交
88 89
			cart[shopid][category_id] = {};
			cart[shopid][category_id][item_id] = {};
M
maguohua 已提交
90 91
			cart[shopid][category_id][item_id][food_id] = {};
			cart[shopid][category_id][item_id][food_id]['num'] = 1;
M
maguohua 已提交
92
			cart[shopid][category_id][item_id][food_id]['id'] = food_id;
M
maguohua 已提交
93 94 95
			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 已提交
96 97 98 99
			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 已提交
100 101 102
			cart[shopid] = {};
			cart[shopid][category_id] = {};
			cart[shopid][category_id][item_id] = {};
M
maguohua 已提交
103 104
			cart[shopid][category_id][item_id][food_id] = {};
			cart[shopid][category_id][item_id][food_id]['num'] = 1;
M
maguohua 已提交
105
			cart[shopid][category_id][item_id][food_id]['id'] = food_id;
M
maguohua 已提交
106 107 108
			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 已提交
109 110 111
			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 已提交
112
		}
M
update  
maguohua 已提交
113
		state.cartList = {...cart};
M
maguohua 已提交
114
		//存入localStorage
M
maguohua 已提交
115 116
		setStore('buyCart', state.cartList);
	},
M
maguohua 已提交
117
	// 移出购物车
M
maguohua 已提交
118 119 120 121 122 123 124
	[REDUCE_CART](state, {
		shopid,
		category_id,
		item_id,
		food_id,
		name,
		price,
M
maguohua 已提交
125
		specs,
M
maguohua 已提交
126
	}) {
M
maguohua 已提交
127
		let cart = state.cartList;
M
maguohua 已提交
128
		if (cart[shopid] && cart[shopid][category_id] && cart[shopid][category_id][item_id] && cart[shopid][category_id][item_id][food_id]) {
M
maguohua 已提交
129
			if (cart[shopid][category_id][item_id][food_id]['num'] > 0) {
M
maguohua 已提交
130
				cart[shopid][category_id][item_id][food_id]['num']--;
M
update  
maguohua 已提交
131
				state.cartList = {...cart};
M
maguohua 已提交
132
				//存入localStorage
M
maguohua 已提交
133
				setStore('buyCart', state.cartList);
M
maguohua 已提交
134
			} else {
M
maguohua 已提交
135
				//商品数量为0,则清空当前商品的信息
M
maguohua 已提交
136
				cart[shopid][category_id][item_id][food_id] = null;
M
maguohua 已提交
137 138 139
			}
		}
	},
M
maguohua 已提交
140
	//网页初始化时从本地缓存获取购物车数据
M
maguohua 已提交
141
	[INIT_BUYCART](state) {
M
maguohua 已提交
142 143 144 145
		let initCart = getStore('buyCart');
		if (initCart) {
			state.cartList = JSON.parse(initCart);
		}
M
maguohua 已提交
146
	},
M
maguohua 已提交
147
	//清空当前商品的购物车信息
M
maguohua 已提交
148
	[CLEAR_CART](state, shopid) {
M
maguohua 已提交
149
		state.cartList[shopid] = null;
M
update  
maguohua 已提交
150
		state.cartList = {...state.cartList};
M
maguohua 已提交
151
		setStore('buyCart', state.cartList);
M
maguohua 已提交
152
	},
M
maguohua 已提交
153
	// 记录用户信息
M
maguohua 已提交
154
	[RECORD_USERINFO](state, info) {
M
maguohua 已提交
155
		state.userInfo = info;
M
update  
maguohua 已提交
156
		state.login = true;
M
confrim  
maguohua 已提交
157 158 159 160 161
		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 已提交
162
	},
M
confrim  
maguohua 已提交
163 164
	//获取用户信息存入vuex
	[GET_USERINFO](state, info) {
M
update  
maguohua 已提交
165 166 167
		if (state.userInfo && (state.userInfo.username !== info.username)) {
			return;
		};
M
update  
maguohua 已提交
168 169 170
		if (!state.login) {
			return
		}
M
confrim  
maguohua 已提交
171 172
		if (!info.message) {
			state.userInfo = info;
M
update  
maguohua 已提交
173 174 175 176 177
			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
confrim  
maguohua 已提交
178 179
		} else {
			state.userInfo = null;
M
maguohua 已提交
180 181
		}
	},
M
update  
maguohua 已提交
182 183 184 185
	//修改用户名
	[RETSET_NAME](state,username) {
		state.userInfo = Object.assign({}, state.userInfo,{username})
	},
M
maguohua 已提交
186 187 188 189
	//保存商铺id
	[SAVE_SHOPID](state, shopid) {
		state.shopid = shopid;
	},
M
maguohua 已提交
190 191 192 193 194 195 196
	//记录订单页面用户选择的备注, 传递给订单确认页面
	[CONFIRM_REMARK](state, {
		remarkText,
		inputText
	}) {
		state.remarkText = remarkText;
		state.inputText = inputText;
M
confrim  
maguohua 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	},
	//是否开发票
	[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 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	},
	//保存下单需要验证的返回值
	[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) {
M
update  
maguohua 已提交
240
		state.cartPrice = null;
M
maguohua 已提交
241 242
		state.orderMessage = order;
	},
M
maguohua 已提交
243 244 245 246
	//进入订单详情页前保存该订单信息
	[SAVE_ORDER](state, orderDetail) {
		state.orderDetail = orderDetail;
	},
M
update  
maguohua 已提交
247 248 249 250 251
	//退出登陆
	[OUT_LOGIN](state) {
		state.userInfo = null;
		state.login = false;
	},
M
update  
maguohua 已提交
252 253 254 255 256 257 258 259
	//保存图片
	[SAVE_AVANDER](state, imgPath) {
		state.imgPath = imgPath;
	},
	//删除地址列表
	[SAVE_ADDRESS](state, newAdress) {
		state.removeAddress = newAdress
	},
M
update  
maguohua 已提交
260 261 262 263
	//添加地址name
	[SAVE_ADDDETAIL](state, addAddress){
		state.addAddress=addAddress;
	},
M
update  
maguohua 已提交
264 265 266
	//保存所选问题标题和详情
	[SAVE_QUESTION](state, question) {
		state.question = {...question};
M
update  
maguohua 已提交
267
	},
M
update  
maguohua 已提交
268
	//增加地址
M
udpate  
maguohua 已提交
269
	[ADD_ADDRESS](state, obj) {
M
update  
maguohua 已提交
270
		state.removeAddress = [obj, ...state.removeAddress];
M
update  
maguohua 已提交
271 272 273 274
	},
	//会员卡价格纪录
	[BUY_CART](state, price) {
		state.cartPrice = price;
M
update  
maguohua 已提交
275
	}
M
maguohua 已提交
276
}