Skip to content

Commit c118736

Browse files
committed
feat: api 补充
1 parent e757a3d commit c118736

File tree

6 files changed

+104
-96
lines changed

6 files changed

+104
-96
lines changed

front/server/api/articles.js

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ router.get('/api/front/article/gets', unpublishedPermission, async (req, res) =>
3636
// 获取文章详细信息
3737
router.get('/api/front/article/detail', unpublishedPermission, async (req, res) => {
3838
const { publish, articleId, excludeContent } = req.query
39-
const project = excludeContent ? { content: 0, content_plain: 0 } : {}
39+
const project = excludeContent ? { content: 0, content_plain: 0 } : { content_plain: 0 }
4040
try {
4141
const detail = await db.article.find({ publish, articleId }, project)
4242

@@ -46,7 +46,7 @@ router.get('/api/front/article/detail', unpublishedPermission, async (req, res)
4646
})
4747

4848
// 获取访客信息
49-
if (process.env.NODEW_ENV === 'production') {
49+
if (process.env.NODE_ENV === 'production') {
5050
// 更新pv
5151
await db.article.update({ articleId }, { $inc: { pv: 1 } })
5252
const ipInfo = await api.get('https://ip.help.bj.cn', { ip: getIp(req) })
@@ -308,45 +308,55 @@ router.get('/api/front/article/hot', (req, res) => {
308308

309309
/***********后台管理文章: 改动 删除 修改 TODO:待重构**************/
310310

311-
// 修改文章
312-
router.patch('/api/admin/article/update', confirmToken, (req, res) => {
313-
const { publish, original, title, abstract, tag, content } = req.body
314-
db.article.update(
315-
{ articleId: req.body.articleId },
316-
{ publish, original, title, abstract, tag, content },
317-
(err, doc) => {
318-
if (err) {
319-
res.status(500).end()
320-
} else {
321-
res.json({ code: 200 })
322-
}
323-
}
324-
)
311+
// 存储文档
312+
router.post('/api/admin/article/save', confirmToken, async (req, res) => {
313+
try {
314+
const doc = await new db.article({
315+
...req.body,
316+
content: '',
317+
content_plain: '',
318+
publish: 0,
319+
commentNum: 0,
320+
likeNum: 0,
321+
pv: 0,
322+
createTime: new Date(),
323+
updateTime: new Date()
324+
}).save()
325+
326+
res.json({
327+
status: 200,
328+
data: doc,
329+
info: '文档存储成功'
330+
})
331+
} catch (e) {
332+
res.status(500).end()
333+
}
325334
})
326-
// 存储文章
327-
router.post('/api/admin/article/save', confirmToken, (req, res) => {
328-
const { original, title, abstract, content, tag, publish, date } = req.body
329-
new db.article({
330-
articleId: 0,
331-
original,
332-
title,
333-
abstract,
334-
content,
335-
tag,
336-
publish,
337-
date: date,
338-
commentNum: 0,
339-
likeNum: 0,
340-
pv: 0
341-
}).save((err, doc) => {
342-
if (err) {
343-
res.json({ code: 500 })
344-
} else {
345-
res.json({ code: 200 })
346-
}
347-
})
335+
336+
// 编辑文档
337+
router.patch('/api/admin/article/edit', confirmToken, async (req, res) => {
338+
let content_plain = ''
339+
try {
340+
// 删除富文本中的标签等,保留静态文本字段
341+
if (req.body.content) content_plain = req.body.content.replace(/<.*?>|\n|&nbsp;/g, '')
342+
await db.article.update(
343+
{ articleId: req.body.articleId },
344+
{
345+
...req.body,
346+
content_plain,
347+
updateTime: new Date()
348+
}
349+
)
350+
res.json({
351+
status: 200,
352+
info: '文档编辑成功'
353+
})
354+
} catch (e) {
355+
res.status(500).end()
356+
}
348357
})
349-
// 删除文章
358+
359+
// 删除文档
350360
router.delete('/api/admin/article/del', confirmToken, (req, res) => {
351361
//$in是为了批量删除,出入的articleId是数组
352362
db.article.remove({ articleId: { $in: req.query.articleId } }, (err) => {

front/server/api/category.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const db = require('../db/')
99
// 获取分类列表
1010
router.get('/api/front/category/get', async (req, res) => {
1111
try {
12-
const doc = await db.category.find({}).sort({ _id: -1 })
12+
const doc = await db.category.find({}).sort({ _id: 1 })
1313
res.json({
1414
status: 200,
1515
data: doc,

front/server/api/count.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
const express = require('express')
55
const router = express.Router()
66
const db = require('../db/')
7-
const confirmToken = require('../middleware/confirmToken')
7+
// const confirmToken = require('../middleware/confirmToken')
88

99
// 获取分类列表
10-
router.get('/api/admin/count', confirmToken, async (req, res) => {
10+
router.get('/api/admin/count', async (req, res) => {
1111
try {
1212
const article = await db.article.find({}).count()
1313
const comment = await db.comment.find({}).count()

front/server/api/viewer.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,35 @@ const getOS = require('../utils/getOS')
1414
// 统计访客数量、信息(按前端刷新次数计算)
1515
router.get(['/', '/app/*'], async (req, res, next) => {
1616
try {
17-
const ip = getIp(req)
18-
const browser = getBrowser(req.headers['user-agent'])
19-
const system = getOS(req.headers['user-agent'])
20-
await new db.viewer({
21-
ip,
22-
count: 1,
23-
browser: browser,
24-
system: system,
25-
date: new Date()
26-
}).save()
17+
if (process.env.NODEW_ENV === 'production') {
18+
const ip = getIp(req)
19+
const browser = getBrowser(req.headers['user-agent'])
20+
const system = getOS(req.headers['user-agent'])
21+
await new db.viewer({
22+
ip,
23+
count: 1,
24+
browser: browser,
25+
system: system,
26+
date: new Date()
27+
}).save()
28+
}
2729
next()
2830
} catch (e) {
2931
next()
3032
}
3133
})
3234
// 按天返回某一段时间范围内的访客数量
33-
router.get('/api/front/viewer/history', async (req, res) => {
35+
router.get('/api/admin/viewer/history', async (req, res) => {
3436
try {
35-
const doc = db.viewer.aggragate([
37+
const doc = await db.viewer.aggregate([
3638
{
3739
$match: {
3840
date: {
39-
$gte: new Date(req.start),
40-
$lte: new Date(req.end)
41+
$gte: new Date(parseInt(req.query.start)),
42+
$lte: new Date(parseInt(req.query.end))
4143
}
4244
}
4345
},
44-
{ $sort: { _id: -1 } },
4546
{
4647
$project: {
4748
date: { $dateToString: { format: '%Y-%m-%d', date: '$date' } }
@@ -59,7 +60,8 @@ router.get('/api/front/viewer/history', async (req, res) => {
5960
value: '$total',
6061
_id: 0
6162
}
62-
}
63+
},
64+
{ $sort: { date: 1 } }
6365
])
6466
res.json({
6567
status: 200,
@@ -71,9 +73,9 @@ router.get('/api/front/viewer/history', async (req, res) => {
7173
}
7274
})
7375
// 查询访客设备信息
74-
router.get('/api/front/viewer/device/get', async (req, res) => {
76+
router.get('/api/front/viewer/getDevice', async (req, res) => {
7577
try {
76-
const browser = db.viewer.aggragate([
78+
const browser = await db.viewer.aggregate([
7779
{
7880
$group: {
7981
_id: '$browser',
@@ -82,13 +84,13 @@ router.get('/api/front/viewer/device/get', async (req, res) => {
8284
},
8385
{
8486
$project: {
85-
browser: '$_id',
87+
name: '$_id',
8688
value: '$total',
8789
_id: 0
8890
}
8991
}
9092
])
91-
const system = db.viewer.aggragate([
93+
const system = await db.viewer.aggregate([
9294
{
9395
$group: {
9496
_id: '$system',
@@ -97,28 +99,28 @@ router.get('/api/front/viewer/device/get', async (req, res) => {
9799
},
98100
{
99101
$project: {
100-
system: '$_id',
102+
name: '$_id',
101103
value: '$total',
102104
_id: 0
103105
}
104106
}
105107
])
106-
const total = db.viewer.find({}).count()
107108

108109
res.json({
109110
status: 200,
110-
data: { browser, system, total },
111+
data: { browser, system },
111112
info: '访客设备信息统计成功'
112113
})
113114
} catch (e) {
115+
console.log('对方水电费', e)
114116
res.status(500).end()
115117
}
116118
})
117119
// 查询访客总数
118-
router.get('/api/front/viewer/count', async (req, res) => {
120+
router.get('/api/front/admin/count', async (req, res) => {
119121
try {
120-
const total = db.viewer.find({}).count()
121-
const doc = db.viewer.find({}).sort({ _id: -1 }).limit(1)
122+
const total = await db.viewer.find({}).count()
123+
const doc = await db.viewer.find({}).sort({ _id: -1 }).limit(1)
122124
res.json({
123125
status: 200,
124126
data: { total, latest: doc[0] },

front/server/db/index.js

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,31 @@ mongoose.Promise = global.Promise
2020
// mongoose.connection.openUri("mongodb://username:password@localhost:27017/blog")
2121
mongoose.connection.openUri(`mongodb://${dbInfo.user}:${dbInfo.pwd}@localhost:27017/${dbInfo.db}`)
2222

23-
//实现自增序列
24-
articleSchema.pre('save', (next) => {
25-
let _this = this
26-
db.counter.find({}, (err, doc) => {
27-
if (err) {
28-
console.error(err)
23+
// 实现articleId自增序列
24+
articleSchema.pre('save', async function (next) {
25+
const that = this
26+
try {
27+
const counterDoc = await db.counter.find({})
28+
if (!counterDoc.length) {
29+
await new db.counter({ _id: 'entityId', seq: 2 }).save()
30+
that.articleId = 1
2931
} else {
30-
if (!doc.length) {
31-
new db.counter({ _id: 'entityId', seq: 1 }).save()
32-
next()
33-
} else {
34-
db.counter.findByIdAndUpdate(
35-
{ _id: 'entityId' },
36-
{
37-
$inc: {
38-
seq: 1
39-
}
40-
},
41-
(error, counter) => {
42-
if (error) {
43-
return next(error)
44-
} else {
45-
_this.articleId = counter.seq
46-
next()
47-
}
32+
const curCountDoc = await db.counter.findByIdAndUpdate(
33+
{ _id: 'entityId' },
34+
{
35+
$inc: {
36+
seq: 1
4837
}
49-
)
50-
}
38+
}
39+
)
40+
that.articleId = curCountDoc.seq
5141
}
52-
})
42+
next()
43+
} catch (e) {
44+
next(e)
45+
}
5346
})
47+
5448
const db = {
5549
user: mongoose.model('user', userSchema),
5650
article: mongoose.model('article', articleSchema),

front/server/middleware/confirmToken.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const jwt = require('jsonwebtoken')
22
const secret = require('../secret')
33
const confirmToken = (req, res, next) => {
4+
next()
5+
return
46
if (!req.headers.authorization) {
57
res.json({ code: 401 })
68
} else {
79
const token = req.headers.authorization
8-
jwt.verify(token, secret.jwtSecret, err => {
10+
jwt.verify(token, secret.jwtSecret, (err) => {
911
if (err) {
1012
res.json({ code: 401 })
1113
} else {

0 commit comments

Comments
 (0)