zero-help.js
8.23 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
export const FETCH_ZERO_HELP_OTHER_GOODS = 'FETCH_ZERO_HELP_OTHER_GOODS';
export const FETCH_POOL_GOODS = 'FETCH_POOL_GOODS';
export const FETCH_ZERO_HELP_COMMAND = 'FETCH_ZERO_HELP_COMMAND';
export const IS_LOADING = 'IS_LOADING';
export const FETCH_ZERO_HELP_GOOD = 'FETCH_ZERO_HELP_GOOD';
export const SET_API_ERROR = 'SET_API_ERROR';
export const FETCH_RESOURCE_IFNO = 'FETCH_RESOURCE_IFNO';
// 730 测试环境 39194 线上
const productPool = 39194;
const resourceCode = 'c41ccdd910fcf0840f18755de4d85ee9';
import { isArray, isEmpty } from 'lodash';
import { getImgUrl } from 'common/utils';
import cookie from 'yoho-cookie';
export default {
namespaced: true,
state: {
isLoading: true,
poolGoodsInfo: {
page: 0,
page_size: 20,
page_total: 0,
total: 0,
product_list: [],
},
zeroHelpOtherGoods: {
page: 0,
page_size: 10,
page_total: 0,
total: 0,
product_list: [],
},
zeroHelpGood: {},
code: '',
countdownTimeList: [],
error: null,
resourceList: [],
},
getters: {
poolProductList: state => state.poolGoodsInfo.product_list || [],
zeroHelpOtherProductList: state => state.zeroHelpOtherGoods.product_list,
productSkn: state => state.zeroHelpGood.productSkn,
isActivityEnd: state => {
const { nowTime, endTime } = state.zeroHelpGood;
return nowTime - endTime < 0;
},
reportParam: state => {
const { userActivityId, productSkn } = state.zeroHelpGood;
return { userActivityId, productSkn };
},
},
actions: {
async init({ dispatch }, params = {}) {
const actions = [
dispatch('fetchPoolGoods'),
dispatch('fetchRecourseInfo'),
];
const { actId } = params;
if (actId) {
actions.push(dispatch('fetchZeroHelpInfo', params));
} else {
actions.push(dispatch('fetchZeroHelpItemInfo', params));
}
Promise.all(actions);
},
// 获取资源池商品
async fetchPoolGoods({ commit, state: { poolGoodsInfo } }) {
let { page_total: pageTotal, page } = poolGoodsInfo;
// 数据完全获取
if (page > 0 && (pageTotal === 0 || page === pageTotal)) {
return true;
}
page++;
let res = await this.$api.get('/api/zero/help/poll/productList', {
page,
limit: 30,
productPool,
order: 'pools_sort_desc',
});
if (res) {
if (res.code === 200) {
/**
* 数据格式兼容
* res: {
* code: 200,
* data: [] | {total, page_total, page_size, page, product_list}
* }
* 无数据返回 []
*/
if (isArray(res.data) && isEmpty(res.data)) {
res = { ...res, data: { product_list: [], page } };
}
commit(FETCH_POOL_GOODS, res);
} else {
commit(SET_API_ERROR, new Error(res.message));
}
}
},
/**
*
* @param {*} param0
* @param {*} productSkn 商品编号
* 获取更多0元助力商品
*/
async fetchZeroHelpOtherGoods(
{
commit,
state: { zeroHelpOtherGoods },
getters,
},
productSkn,
) {
let {
page_total: pageTotal,
page,
page_size: limit,
} = zeroHelpOtherGoods;
// 数据完全获取
if (page > 0 && (pageTotal === 0 || page === pageTotal)) {
return true;
}
page++;
const res = await this.$api.get('/api/zero/help/otherProductList', {
page,
limit,
productSkn: getters.productSkn || productSkn,
});
if (res) {
if (res.code === 200) {
commit('FETCH_ZERO_HELP_OTHER_GOODS', res.data);
} else {
commit(SET_API_ERROR, new Error(res.message));
}
}
},
/**
*
* @param {*} param0
* @param {*} userActivityId 0元助力活动Id
* 获取App使用口令
*/
async fetchZeroHelpCommand({ commit }, { userActivityId, userId } = {}) {
const res = await this.$api.get('/api/zero/help/command', {
// 业务来源,如1-0元免单
source: 1,
key: userActivityId,
uid: userId,
});
if (res) {
if (res.code === 200) {
commit(FETCH_ZERO_HELP_COMMAND, res.data);
} else {
commit(SET_API_ERROR, new Error(res.message));
}
}
},
/**
*
* @param {*} param0
* @param {*} params 获取0元助力商品信息
*/
async fetchZeroHelpItemInfo({ commit, dispatch }, params) {
commit(IS_LOADING, true);
const { skn, id: helpProductId } = params;
const res = await this.$api.get('/api/zero/help/canHelp', {
skn,
helpProductId,
});
if (res) {
if (res.code === 200) {
commit(FETCH_ZERO_HELP_GOOD, res.data);
await dispatch('fetchZeroHelpOtherGoods', skn);
} else {
commit(SET_API_ERROR, new Error(res.message));
}
}
commit(IS_LOADING, false);
},
/**
*
* @param {*} param0
* @param {*} params {actId: 活动Id分享参数, activityId: 自定义活动Id}
*/
async fetchZeroHelpInfo({ commit, dispatch }, params) {
commit(IS_LOADING, true);
const { actId: userActivityId = '' } = params;
const res = await this.$api.get('/api/zero/help/info', {
// 1-发起人调用,0-助力者调用
type: 0,
userActivityId,
});
if (res) {
if (res.code === 200) {
const { userId } = res.data || {};
commit(FETCH_ZERO_HELP_GOOD, res.data);
await dispatch('fetchZeroHelpOtherGoods', res.data.productSkn);
await dispatch('fetchZeroHelpCommand', { userActivityId, userId });
} else {
commit(SET_API_ERROR, new Error(res.message));
}
}
commit(IS_LOADING, false);
},
/**
* 获取资源位信息
*/
async fetchRecourseInfo({ commit }) {
const res = await this.$api.get('/api/yoho/resource', {
content_code: resourceCode,
});
if (res && res.code === 200) {
commit(FETCH_RESOURCE_IFNO, res.data);
}
},
/**
* 埋点上报事件
*/
reportYas(_, params) {
let { appop, param } = params;
if (window._yas && window._yas.sendAppLogs) {
param = param || {};
if (!param.C_ID) {
const channel = {
men: 1,
women: 2,
}[cookie.get('_Channel') || 'men'];
param.C_ID = channel;
}
window._yas.sendAppLogs({
appop,
param: param ? JSON.stringify(param) : '{}',
});
}
},
},
mutations: {
[FETCH_ZERO_HELP_GOOD](state, data) {
state.zeroHelpGood = data;
},
[IS_LOADING](state, status = false) {
state.isLoading = status;
},
[SET_API_ERROR](state, error) {
state.error = error;
},
// eslint-disable-next-line no-unused-vars
[FETCH_POOL_GOODS](state, res) {
const { product_list: oldProductList = [] } = state.poolGoodsInfo;
const { product_list: newProductList = [] } = res.data;
const product_list = oldProductList.concat(newProductList);
state.poolGoodsInfo = {
...state.poolGoodsInfo,
...res.data,
product_list,
};
},
[FETCH_ZERO_HELP_OTHER_GOODS](state, data) {
const { product_list: oldProductList = [] } = state.zeroHelpOtherGoods;
const { product_list: newProductList = [] } = data;
const product_list = oldProductList.concat(newProductList);
state.zeroHelpOtherGoods = {
...state.zeroHelpOtherGoods,
...data,
product_list,
};
},
[FETCH_ZERO_HELP_COMMAND](state, { code }) {
state.code = code;
},
[FETCH_RESOURCE_IFNO](state, resources = []) {
resources = resources.map(resourceInfo => {
const {
data: { src = '', urls = [] } = {},
image_height = 1000,
image_width = 1000,
is_extend,
} = resourceInfo;
const [to = ''] = urls;
const url = getImgUrl(src, image_width || 1000, image_height || 1000);
return { url, to, isExtend: is_extend === '1' };
});
state.resourceList = resources;
},
},
};