consult.js
3.08 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
/**
* home-我的咨询 业务逻辑
* @author 陈轩 <xuan.chen@yoho.cn>
*/
'use strict';
const path = require('path');
const Promise = require('bluebird');
const moment = require('moment');
const _ = require('lodash');
const imgUtils = require(path.join(global.utils, 'images'));
const api = global.yoho.API;
const searchApi = global.yoho.SearchAPI;
const helpers = global.yoho.helpers;
const co = Promise.coroutine;
// 分页 函数
const pagerPath = path.join(global.appRoot, '/apps/product/models/public-handler.js');
const pager = require(pagerPath).handlePagerData;
const NO_CONSULT = '您尚未咨询任何内容';
/**
* 查询多个skn的默认商品
* @param skns array
* @return array
*/
function getProductGoodsInfo(skns) {
const query = skns.join(',');
return searchApi.get('/search.json', {
data: {
query: query
}
}).then(result => {
let resData = {};
_.forEach(_.get(result, 'data.product_list', []), good => {
resData[good.product_skn] = good;
});
return resData;
});
}
/**
* 获取咨询列表
*/
exports.consultList = (uid, page, limit) => {
page = Number.parseInt(page, 10) || 1;
limit = Number.parseInt(limit, 10) || 10;
const data = {
uid: uid,
page: page,
limit: limit
};
const fetchConsults = co(function*(params) {
let res = yield api.get('', Object.assign({
method: 'web.personCen.buyConsult'
}, params));
return res;
});
// 处理fetchConsults的数据
const processData = co(function*(res) {
// 处理结果
const result = {
consults: [],
pager: {}
};
let origin = res.data; // 原始数据
const skns = [];
if (res.code === 200 && origin && origin.consult_list && origin.consult_list.length) {
origin.consult_list.forEach(consult => {
skns.push(consult.skn);
});
let goodInfos = yield getProductGoodsInfo(skns);
origin.consult_list.forEach(consult => {
let goodInfo = goodInfos[consult.skn];
if (!goodInfo) {
return;
}
let info = {
href: helpers.getUrlBySkc(goodInfo.productId, goodInfo.goodsId, goodInfo.cn_alphabet),
thumb: imgUtils.getImageUrl(goodInfo.images_url, 60, 60, 1),
name: consult.productName,
question: consult.ask || '',
consultTime: moment(consult.askTime).format('YYYY-MM-DD H:m:s')
};
if (consult.answer) {
info.reply = consult.answer;
}
result.consults.push(info);
});
result.pager = pager(origin.total, {
page: origin.page,
limit: limit
});
}
if (!result.consults.length) {
result.empty = NO_CONSULT;
}
return result;
});
return fetchConsults(data).then(processData);
};