index.js
5.5 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import Vue from 'vue';
import {
FETCH_PRODUCT_DETAIL,
PRODUCT_FAVORITE,
FETCH_CART_COUNT,
PRODUCT_ADD_CART,
FETCH_PRODUCT_INTRO,
FETCH_SEARCH_REQUEST,
FETCH_SEARCH_SUCCESS,
FETCH_SEARCH_FAILURE
} from './types';
export function createProduct() {
return {
state: {
list: [],
items: {},
isFetching: false,
cartCount: 0,
isSearching: false,
searchError: false
},
mutations: {
[FETCH_PRODUCT_DETAIL](state, {product, product: { product_id }}) {
Vue.set(state.items, product_id, product);
},
[FETCH_PRODUCT_INTRO](state, {intro, product_id}) {
Vue.set(state.items[product_id], 'intro', intro);
},
[PRODUCT_FAVORITE](state, {product_id, is_collect}) {
state.items[product_id].is_collect = is_collect;
},
[FETCH_CART_COUNT](state, {cart_goods_count}) {
state.cartCount = cart_goods_count;
},
[FETCH_SEARCH_REQUEST](state) {
state.searchError = false;
state.isSearching = true;
},
[FETCH_SEARCH_SUCCESS](state) {
state.searchError = false;
state.isSearching = false;
},
[FETCH_SEARCH_FAILURE](state) {
state.searchError = true;
state.isSearching = false;
}
},
actions: {
[FETCH_PRODUCT_DETAIL]({commit, state}, {product_id}) {
// let cacheItem = state.items[product_id];
// if (cacheItem && Date.now() - cacheItem.__lasttime < 1000 * 60 * 3) {
// return Promise.resolve();
// }
return this.$api.get('/api/product/data', {
product_id: parseInt(product_id, 10)
}).then(res => {
if (res.code === 200) {
return Promise.all([
this.$api.get('/api/product/refundExchange', {
product_skn: res.data.product_skn
}),
this.$api.get('/api/favorite/isFavoriteNew', {
id: res.data.product_id
})]).then(result => {
if (result[0].code === 200) {
res.data.supportRefundExchange = result[0].data[res.data.product_skn] === 'N' ? 'Y' : 'N';
}
res.data.is_collect = (result[1].code === 200 && result[1].data === 'Y') ? 'Y' : 'N';
res.data.__lasttime = Date.now();
commit(FETCH_PRODUCT_DETAIL, {product: res.data});
});
}
});
},
[PRODUCT_FAVORITE]({commit}, {product_id, is_collect}) {
return this.$api.post('/product/favorite.json', {
operation: is_collect === 'Y' ? 'remove' : 'add',
id: product_id
}).then(res => {
if (res.code === 200) {
commit(PRODUCT_FAVORITE, {product_id, is_collect: is_collect === 'Y' ? 'N' : 'Y'});
}
return res;
});
},
[FETCH_CART_COUNT]({commit}) {
return this.$api.get('/product/cart-count.json').then(res => {
if (res.code === 200) {
commit(FETCH_CART_COUNT, res.data);
}
});
},
[PRODUCT_ADD_CART]({commit, rootState}, {productSku, buyNumber}) {
return this.$api.post('/product/cart.json', {
productSku: productSku,
buyNumber: buyNumber
}).then(res=> {
if (res.code === 200) {
if (Vue.$yoho.goShopingKey && res.data && res.data.shopping_key) {
Vue.$yoho.goShopingKey({shoppingKey: res.data.shopping_key});
}
commit(FETCH_CART_COUNT, {cart_goods_count: res.data.goods_count});
} else {
return Promise.reject();
}
});
},
[FETCH_PRODUCT_INTRO]({commit, state}, {skn, pid}) {
let cacheItem = state.items[pid];
if (cacheItem && cacheItem.intro && cacheItem.intro.__lasttime < 1000 * 60 * 3) {
return Promise.resolve();
}
return this.$api.get(`/product/product/intro_${pid}.json`, {skn}).then(res => {
res.__lasttime = Date.now();
commit(FETCH_PRODUCT_INTRO, {intro: res, product_id: pid});
});
},
async [FETCH_SEARCH_REQUEST]({commit}, {keyword}) {
commit(FETCH_SEARCH_REQUEST);
try {
const result = await this.$api.get('/api/search/fuzzy', {keyword});
if (result.code === 200) {
commit(FETCH_SEARCH_SUCCESS);
return result;
}
commit(FETCH_SEARCH_FAILURE);
} catch (e) {
commit(FETCH_SEARCH_FAILURE);
}
}
}
};
}