consult-comment.js
5.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
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
/**
* 商品详情相关models
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2016/6/27
*/
'use strict';
const _ = require('lodash');
const api = global.yoho.API;
const helpers = global.yoho.helpers;
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 获取默认咨询列表
*/
getCommonConsult() {
let params = {
method: 'app.consult.common'
};
return this.get({
data: params,
param: {code: 200, cache: true}
}).then(result => {
let data = {};
if (result.data) {
data.faq = result.data;
}
return data;
});
}
/**
* 处理咨询列表数据
* @data {[object]} 咨询列表原始数据
* @return {[object]}
*/
_formatConsultsList(data) {
data = data || [];
return data.map(function(value) {
return {
question: value.ask,
time: value.ask_time,
answer: value.answer,
id: value.id,
isLike: value.is_like === 'Y',
like: _.toNumber(value.like),
isUseful: value.is_useful === 'Y',
useful: _.toNumber(value.useful)
};
});
}
/**
* 获取咨询数据
* @id {[number]} 商品id
* @page {[number]} 页码
* @limit {[number]} 每页咨询数量
* @return {[object]}
*/
getConsults(id, page, limit, uid) {
let params = {
method: 'app.consult.li',
product_id: id,
page: page ? page : 1,
limit: limit ? limit : 300,
uid
};
return this.get({
data: params,
param: {code: 200, cache: true}
}).then(result => {
let data = {};
if (result && result.data && result.data.list) {
Object.assign(data, {
list: this._formatConsultsList(result.data.list),
pageTotal: result.data.page_total,
total: result.data.total
});
}
return data;
});
}
/**
* 处理评论数据
*/
_processComment(data) {
let result = {
commentsNum: (data.pageResponse && data.pageResponse.totalCount) || 0,
comments: []
};
if (data && data.pageResponse) {
let color, size;
_.forEach(data.pageResponse.list, (item) => {
if (item.goods) {
color = item.goods.factory_goods_name ? item.goods.factory_goods_name : item.goods.color_name;
size = item.goods.size_name;
}
result.comments.push({
userName: item.userInfo ? item.userInfo.nickName : '',
color: color || '',
size: size || '',
content: item.content,
time: helpers.dateFormat('YYYY-MM-DD HH:mm:ss', new Date(item.createTime * 1000))
});
});
}
return result;
}
/**
* 获取评论信息
*/
getCommentInfo(params) {
return this.get({
data: Object.assign({
method: 'show.productShareOrderList',
limit: '1',
page: '1',
filterId: '7'
}, params),
param: {cache: true}
}).then((result) => {
if (result && result.code === 200) {
return this._processComment(result.data);
}
return {};
});
}
/**
* 购买评价列表
* @param {[object]} 查询参数
* @return {[object]}
*/
comments(params) {
return this.getCommentInfo({
productId: params.product_id,
limit: '60'
}).then(result => {
let data = {};
if (result.comments && result.comments.length) {
if (result.commentsNum) {
_.set(data, 'navTitle', `购买评价(${result.commentsNum})`);
}
data.comments = result.comments;
}
return data;
});
}
/**
* 购买咨询列表
* @params {[object]} 查询参数
* @return {[object]}
*/
consults(params, uid) {
return api.all([
this.getCommonConsult(),
this.getConsults(params.product_id, 1, 60, uid)
]).then(result => {
let data = {
link: `/product/detail/consultform?product_id=${params.product_id}`
};
Object.assign(data, result[0]);
if (result[1].list && result[1].list.length) {
if (result[1].total) {
_.set(data, 'navTitle', `购买咨询(${result[1].total})`);
}
data.consults = result[1].list;
}
data.showReadMore = _.get(result[1], 'list.length', 0) > 2;
return data;
});
}
upvoteConsult(params) {
return this.get({
data: {
method: params.isUpvote ? 'app.consult.like' : 'app.consult.useful',
id: params.id,
uid: params.uid
}
});
}
/**
* 购买咨询列表
* @uid {[number]} 用户id
* @productId {[number]} 商品id
* @content {[string]} 咨询内容
* @return {[object]}
*/
addConsult(uid, productId, content) {
let params = {
method: 'app.consult.add',
product_id: productId,
content: content,
uid: uid
};
return this.post({
data: params
}).then(result => {
if (!result || !result.code || result.code !== 200) {
return false;
}
return result;
});
}
};