actions.js
8.58 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import * as Types from './types';
import { get } from 'lodash';
import Vue from 'vue';
export default {
ensureProduct({ commit, state }, { productId }) {
commit(Types.ENSURE_PRODUCT_DETAIL, { productId });
return state.products[productId];
},
async fetchProductInfo({ commit, state, rootGetters }, { productId }) {
const queryUrls = ['', '/resource', '/activity', '/recommend', '/vedioImage']
if(rootGetters.getLogin) {
queryUrls.push('/limit/info')
}
const queryTasks = queryUrls.map(path => {
let params = { product_id: productId };
if (path === '/resource') {
params = {
content_code: state.resourceContentCode,
};
}
return this.$api.post(`/api/ufo/product${path}`, params).then(result => {
if (result.code === 200) {
return result.data;
} else {
return null;
}
});
});
let [detail, resource, activity, recommend, videoResource, limitInfo] = await Promise.all(queryTasks);
if(videoResource && videoResource.vedioUrl) {
videoResource = {
src: videoResource.vedioImageUrl,
url: videoResource.vedioUrl
}
} else {
// 视频资源位
const videoResourceInfo = resource.find(r=> /(\.mp4)/.test(r.data[0].url));
videoResource = get(videoResourceInfo, 'data[0]', {});
if (videoResourceInfo) {
const {template_id, template_name} = videoResourceInfo;
videoResource = {...videoResource, template_id, template_name }
}
}
const resourceImgInfo = get(resource, '[0]', null);
resource = get(resource, '[0].data[0]', {});
if(resourceImgInfo) {
const {template_id, template_name} = resourceImgInfo;
resource = {...resource, template_id, template_name }
}
const {product_info = {}} = detail || {};
commit(Types.UPDATE_PRODUCT_DETAIL, Object.assign(product_info, {
resource,
videoResource,
activity: activity || [],
recommend: recommend && recommend.product_list || [],
limitInfo: limitInfo || {}
}));
},
async fetchProductCoupon({ commit }, { productId }) {
let params = {
productId: productId,
page: 1,
limit: 500,
}
const result = await this.$api.post('/api/ufo/product/coupon', params);
if (result.code === 200) {
const couponList = result.data.coupons || [];
commit(Types.UPDATE_PRODUCT_COUPON_LIST, { productId, couponList: couponList});
}
},
async fetchProductCouponTopList({ commit }, { productId }) {
let params = {
productId: productId,
}
const result = await this.$api.post('/api/ufo/product/couponTopList', params);
if (result.code === 200) {
const couponTopList = result.data || [];
commit(Types.UPDATE_PRODUCT_COUPON_TOP_LIST, { productId, couponTopList: couponTopList});
}
},
async fetchRecommendProduct({ commit }, { productId }) {
const result = await this.$api.post('/api/ufo/product/recommend', { product_id: productId })
return result?.data?.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.post('/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 fetchBrandTop({ commit }, { productId }) {
const result = await this.$api.post('/api/ufo/product/top', { product_id: productId });
if (result.code === 200) {
const productList = result.data.product_list || [];
commit(Types.UPDATE_BRAND_PRODUCT_TOP_LIST, { productId, topList: productList});
}
},
async setupInitialProductInfo({ commit, state, dispatch }, payload) {
const { id: productId } = payload;
if (!productId) {
return;
}
const productInfo = await dispatch('ensureProduct', {productId});
if (productInfo.product_name === null) {
commit(Types.SETUP_INITIAL_PRODUCT_INFO, payload);
}
},
async toggleFav({ commit }, { productId, isFav }) {
const result = await this.$api.post(`/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.products[productId]) {
await dispatch('fetchProductInfo', { productId });
}
commit(Types.UPDATE_SELECTED_PRODUCT_SIZE, { productId, storageId });
return state.selectedProductInfo;
},
resetSelectedSize({commit}) {
commit(Types.RESET_SELECTED_PRODUCT_SIZE);
},
async requestSize({ state }, { product_id, goods_id, size_ids }) {
const selectedProduct = state.selectedProductInfo;
await this.$api.post('/api/ufo/product/addsize', {
product_id,
goods_id,
size_ids,
});
// 忽略错误
},
async payment(context, { skup }) {
return this.$api.post('/api/ufo/product/order/payment', { skup, api_version: 1 });
},
// 获取详情页展示的社区内容
async fecthGrassAricles({state, commit}, {productSkn}) {
let articleResult = await this.$api.get('/api/grass/product/article', {
productSkn,
productType: 2,
fromXianYu: 'Y'
});
if (articleResult && articleResult.data && articleResult.data.list && articleResult.data.list.length > 0) {
let articles = articleResult.data.list || [];
let articleIds = [];
let newArticleList = [];
let newImages = [];
// 找出所有ariticleId
articles.forEach(item => {
articleIds.push(item.articleId);
newArticleList.push(item);
});
// 传入articleId获取所有需要展示的图片列表
let imagesResult = await this.$api.get(
'/api/grass/product/articleImages',
{
articleIds: articleIds.join(',')
});
// 处理数据,把对应数据整合到articles
if (imagesResult && imagesResult.data && imagesResult.data.imageMap) {
let imageData = imagesResult.data.imageMap;
let imageIndex = 0;
// 处理每篇文章下面的图片
newArticleList.forEach(articleItem => {
articleItem.imageindex = imageIndex;
if (imageData[articleItem.articleId]) {// 如果找到对应id的图片列表
imageIndex += imageData[articleItem.articleId].length;
let imageList = [];
imageData[articleItem.articleId].forEach(listItem => {
listItem.contentData = listItem.contentData.replace(/{mode}/, 2).replace(/{width}/, 750).replace(/\/h\/{height}/, '/q/60');
imageList.push(listItem);
newImages.push(listItem.contentData);
});
articleItem.coverImage = articleItem.coverImage.split('?')[0] + '?imageMogr2/thumbnail/200x/gravity/Center/crop/200x200/quality/60';
articleItem.imageList = imageList;
} else { // 没找到对应id的图片列表则把自己的图插入
let imageList = [
{
articleId: articleItem.articleId,
contentData: articleItem.coverImage.replace(/{mode}/, 2).replace(/{width}/, 750).replace(/\/h\/{height}/, '/q/60')
}
];
articleItem.imageList = imageList;
articleItem.coverImage = articleItem.coverImage.split('?')[0] + '?imageMogr2/thumbnail/200x/gravity/Center/crop/200x200/quality/60';
imageIndex += 1;
newImages = newImages.push(imageList.contentData);
}
});
articleResult.data.imageList = newImages;
}
}
// console.log('articleFinalResult=', articleResult);
return articleResult;
},
// 获取详情页尺码图片
async getProductSizeImage({state}, {product_id, brand_id}) {
let result = await this.$api.get('/api/ufo/product/sizeImage', {
product_id,
brand_id
});
return result;
},
};