actions.js
2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import * as Types from './types';
import { get } from 'lodash';
import Vue from 'vue';
export default {
async fetchProductInfo({commit}, {productId}) {
const queryTasks = ['', '/resource', '/activity', '/recommend'].map(path => {
return this.$api.get(`/api/ufo/product${path}`, {product_id: productId}).then(result => {
if (result.code === 200) {
return result.data;
} else {
return null;
}
});
});
const [detail, resource, activity, recommend] = await Promise.all(queryTasks);
commit(Types.UPDATE_PRODUCT_DETAIL, Object.assign(detail.product_info, {
resource: get(resource, '[0].data[0]', null),
activity: activity || [],
recommend: recommend && recommend.product_list || [],
}));
},
async fetchFav({commit, rootGetters}, {productId}) {
let isLogin = rootGetters.getLogin;
if (!isLogin) {
const user = await Vue.$sdk.getUser();
isLogin = user && user.uid > 1;
}
if (!isLogin) {
return false;
}
const isFav = await this.$api.get('/api/ufo/product/fav', {productId}).then(result => {
if (result.code === 200) {
return result.data;
} else {
return false;
}
});
commit(Types.UPDATE_PRODUCT_FAV, {productId, isFav});
},
async fetchTop3({commit}, {productId}) {
const result = await this.$api.get('/api/ufo/product/top', {product_id: productId});
if (result.code === 200) {
const productList = result.data.product_list || [];
commit(Types.UPDATE_PRODUCT_TOP3, {productId, topList: productList.slice(0, 3)});
}
},
async toggleFav({commit}, {productId, isFav}) {
const result = await this.$api.get(`/api/ufo/product/favorite/${isFav ? 'add' : 'cancel'}`, {productId});
if (result && result.code === 200) {
commit(Types.UPDATE_PRODUCT_FAV, {productId, isFav});
}
},
async updateTradeInfo({commit, state}, {productId, sizeInfo}) {
if (sizeInfo) {
commit(Types.UPDATE_SELECTED_PRODUCT_SIZE, {productId, sizeId: sizeInfo.size_id});
}
return {
productId,
sizeId: state.selectedProductInfo.sizeId,
storageId: state.selectedProductInfo.storageId,
};
},
async getSelectedTradeProduct({state, commit, dispatch}, {productId, storageId}) {
productId = parseInt(productId, 10);
storageId = parseInt(storageId, 10);
if (state.selectedProductInfo.productId !== productId && state.product.product_id !== productId) {
await dispatch('fetchProductInfo', {productId});
}
commit(Types.UPDATE_SELECTED_PRODUCT_SIZE, {productId, storageId});
return state.selectedProductInfo;
},
async requestSize({state}, { sizeIds }) {
const selectedProduct = state.selectedProductInfo;
await this.$api.get('/api/ufo/product/addsize', {
product_id: selectedProduct.productId,
goods_id: get(selectedProduct.product, 'goods_list[0].goods_id'),
size_ids: sizeIds
});
// 忽略错误
},
};