-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.controll.js
319 lines (311 loc) · 7.57 KB
/
article.controll.js
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
import articleModel from '../models/article.model';
import { responseClient } from '../../util/util.js';
import request from 'request';
import config from '../../config/config';
/**
* 添加文章
* @param {*} req
* @param {*} res
* @param {*} next
*/
exports.addArticle = async (req, res, next) => {
let {
title,
tags,
content,
coverImg,
status,
description
} = req.body;
try {
let article = new articleModel({
title,
tags,
content,
description,
coverImg,
author: req.session.userName,
status,
createTime: new Date(),
viewCount: 0,
reviewArea: 1,
fabulous: 0
})
article.save().then(data => {
responseClient(res, 200, 200, '保存成功!');
request.post({
url: `http://data.zz.baidu.com/urls?site=${config.baidu.site}&token=${config.baidu.token}`,
headers: {
'Content-Type': 'text/plain'
},
body: `${config.baidu.site}/article/${data._id}`
}, (error, response, body) => {
console.log('推送结果:', body)
})
next();
}).catch(err => {
responseClient(res, 500, 500, '系统异常!', err);
next();
})
} catch (err) {
responseClient(res, 200, 1, '保存失败!', err);
next();
};
};
/**
* 文章列表
* @param {*} req
* @param {*} res
* @param {*} next
*/
exports.articleList = async (req, res, next) => {
let userName = ''
req.originalUrl.indexOf('/admin/') === 0 ? userName = req.session.userName : userName = 'admin'
let {
page_num = 1, tag, hot = false, page_size = 20
} = req.query;
let query = {};
let options = {
sort: {
createTime: -1
},
page: Number(page_num),
limit: Number(page_size)
}
let sort = {};
if(tag) query.tags = tag ;
hot ? options.sort = {
fabulous: -1
}: options.sort = {
createTime: -1
}
let articleList = await articleModel.paginate(query, options).catch(err => responseClient(res, 500, 202, '服务器内部错误!', err))
let data = {
data: articleList.docs,
pageTotal: articleList.pages,
more: articleList.pages - articleList.page > 0 ? true : false,
page: articleList.page,
totalNum: articleList.total
};
responseClient(res, 200, 200, 'success', data);
};
/**
* 获取文章详情
* @param {*} req
* @param {*} res
* @param {*} next
*/
exports.getArticle = async (req, res, next) => {
let { articleID } = req.params;
articleModel.findOne({
_id: articleID
}).then(info => {
let viewCount = info.viewCount + 1;
articleModel.update({
_id: articleID
}, { viewCount }).then(result => {
console.log(result);
});
responseClient(res, 200, 200, '', info);
next();
}).catch(err => {
responseClient(res, 200, 202, '查询失败', err);
next();
});
};
/**
* 文章更新
* @param {*} req
* @param {*} res
* @param {*} next
*/
exports.updateArticle = async (req, res, next) => {
let { articleID } = req.params;
let {
title,
tags,
description,
content,
coverImg,
status
} = req.body;
let updateTime = new Date();
articleModel.update({
_id: articleID
}, {
title,
tags,
content,
description,
coverImg,
status,
updateTime
}).then(updateInfo => {
responseClient(res, 200, 201, '更新成功!', updateInfo);
request.post({
url: `http://data.zz.baidu.com/update?site=${config.baidu.site}&token=${config.baidu.token}`,
headers: {
'Content-Type': 'text/plain'
},
body: `${config.baidu.site}/article/${articleID}`
}, (error, response, body) => {
console.log('百度更新结果:', body)
})
}).catch(err => {
responseClient(res, 500, 202, '更新失败!', err)
})
};
/**
* 删除文章
* @param {*} req
* @param {*} res
* @param {*} next
*/
exports.delArticle = async (req, res, next) => {
let { articleID } = req.params;
try {
if (!articleID) {
responseClient(res, 200, 202, '请输入正确的ID');
next();
} else {
articleModel.remove({
_id: articleID
}).then(result => {
if (result.n === 1) {
responseClient(res, 200, 200, '删除成功!');
request.post({
url: `http://data.zz.baidu.com/del?site=${config.baidu.site}&token=${config.baidu.token}`,
headers: {
'Content-Type': 'text/plain'
},
body: `${config.baidu.site}/article/${articleID}`
}, (error, response, body) => {
console.log('百度删除结果:', body)
})
} else {
responseClient(res, 200, 202, '文章不存在!!')
}
}).catch(err => {
responseClient(res, 500, 500, '服务器异常', err)
})
}
} catch (err) {
responseClient(res, 500, 500, '服务器异常', err)
};
};
/**
* 喜欢art
* @param {*} req
* @param {*} res
* @param {*} next
*/
exports.likeArticle = async (req, res, next) => {
let {
articleID
} = req.params;
try {
if (!articleID) {
responseClient(res, 200, 202, '请输入正确的ID');
next();
} else {
articleModel.findOne({
_id: articleID
}).then(result => {
let fabulous = result.fabulous + 1 || 1;
articleModel.update({
_id: articleID
},{ fabulous }).then(info => {
responseClient(res, 200, 200, '成功', info);
next();
}).catch(err => {
responseClient(res, 500, 500, '服务器异常', err)
})
}).catch(err => {
responseClient(res, 500, 500, '服务器异常', err)
})
}
} catch (err) {
responseClient(res, 500, 500, '服务器异常', err)
};
}
/**
* search
* 搜索
*/
exports.searchArticle = async (req, res, next)=> {
const {
keyword = ''
} = req.query;
const keywordReg = new RegExp(keyword);
const query = {};
query.$or = [
{title: {$regex: keyword, $options: '$i'}},
{description: {$regex: keyword, $options: '$i'}}, // $options: '$i' 忽略大小写
{content: {$regex: keyword, $options: '$i'}}
];
const list = await articleModel.paginate(query,{}).catch(err => {
responseClient(res, 500, 500, '服务器异常', err);
})
let data = {
data: list.docs,
pageTotal: list.pages,
more: list.pages - list.page > 0 ? true : false,
page: list.page,
totalNum: list.total
};
responseClient(res, 200, 200, '成功', data);
}
exports.getRecords = async (req, res, next) => {
const article = await articleModel.aggregate([
{
$project: {
year: {
$year: '$createTime'
},
month: {
$month: '$createTime'
},
title: 1,
createTime: 1
}
},
{
$group: {
_id: {
year: '$year',
month: '$month'
},
article: {
$push: {
title: '$title',
_id: '$_id',
createTime: '$createTime'
}
}
}
}
])
if (article) {
let yearList = [...new Set(article.map(item => item._id.year))]
.sort((a, b) => b - a)
.map(item => {
let monthList = []
article.forEach(n => {
// 同一年
if (n._id.year === item) {
monthList.push({
month: n._id.month,
articleList: n.article.reverse()
})
}
})
return {
year: item,
monthList: monthList.sort((a, b) => b.month - a.month)
}
})
responseClient(res, 200, 200, '获取内容成功', yearList);
} else {
responseClient(res, 500, 201, '获取内容失败', []);
}
}