Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3f77a2a

Browse files
refactor: some api modules
1 parent 13ca186 commit 3f77a2a

19 files changed

+101
-267
lines changed

‎front/server/api/articles.js‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ const db = require('../db/')
99
const getIp = require('../utils/getIp')
1010
const api = require('../http/')
1111
const confirmToken = require('../middleware/confirmToken')
12-
const unpublishedPermission = require('../middleware/unpublishedPermission')
12+
const confirmUnpublish = require('../middleware/confirmUnpublish')
1313

1414
// 获取文章列表 / 按分类 / 按标签 筛选文章
15-
router.get('/api/front/article/gets', unpublishedPermission, async (req, res) => {
15+
router.get('/api/front/article/gets', confirmUnpublish, async (req, res) => {
1616
const params = {}
1717
if (req.query.publish) params.publish = req.query.publish
1818
if (req.query.categoryId) params.categoryId = req.query.categoryId
@@ -164,7 +164,7 @@ router.patch('/api/front/article/like', async (req, res) => {
164164
})
165165

166166
// 文章搜索
167-
router.get('/api/front/article/search', unpublishedPermission, async (req, res) => {
167+
router.get('/api/front/article/search', confirmUnpublish, async (req, res) => {
168168
const limit = 8
169169
const skip = req.query.page * limit - limit
170170
try {
@@ -231,6 +231,7 @@ router.get('/api/front/article/archives', async (req, res) => {
231231
// 按月筛选
232232
if (req.query.filter) {
233233
const pipe = [
234+
{ $match: { publish: 1 } },
234235
{
235236
$project: {
236237
month: { $dateToString: { format: '%Y-%m', date: '$createTime' } },
@@ -249,7 +250,7 @@ router.get('/api/front/article/archives', async (req, res) => {
249250
total = totalRes[0].total
250251
} else {
251252
doc = await db.article.aggregate([
252-
{ $match: {} },
253+
{ $match: {publish: 1} },
253254
{ $sort: { _id: -1 } },
254255
{ $skip: skip },
255256
{ $limit: limit },
@@ -294,7 +295,7 @@ router.get('/api/front/article/hot', (req, res) => {
294295
/***********后台管理文章: 改动 删除 修改 TODO:待重构**************/
295296

296297
// 获取文章详细信息
297-
router.get('/api/admin/article/getDraft', unpublishedPermission, async (req, res) => {
298+
router.get('/api/admin/article/getDraft', confirmToken, async (req, res) => {
298299
const { articleId, excludeContent } = req.query
299300
const project = excludeContent ? { content: 0, content_plain: 0, content_draft: 0 } : { content_plain: 0 }
300301
try {
@@ -337,7 +338,7 @@ router.post('/api/admin/article/save', confirmToken, async (req, res) => {
337338
}
338339
})
339340

340-
// 编辑文档
341+
// 编辑文档(保存、更新/发布)
341342
router.patch('/api/admin/article/edit', confirmToken, async (req, res) => {
342343
const updates = {}
343344
try {
@@ -354,7 +355,12 @@ router.patch('/api/admin/article/edit', confirmToken, async (req, res) => {
354355
updates.content = updates.content_draft
355356
}
356357
}
357-
358+
// 数据存储前获取原始发布状态
359+
const oldDoc = await db.article.find(
360+
{ articleId: req.body.articleId },
361+
{ content: 0, content_plain: 0, content_draft: 0 }
362+
)
363+
const oldPublish = oldDoc[0].publish
358364
await db.article.update(
359365
{ articleId: req.body.articleId },
360366
{
@@ -367,6 +373,12 @@ router.patch('/api/admin/article/edit', confirmToken, async (req, res) => {
367373
{ articleId: req.body.articleId },
368374
{ content: 0, content_plain: 0, content_draft: 0 }
369375
)
376+
// 状态不一致说明发布状态已变更,更新分类表的统计字段
377+
if (oldPublish != doc[0].publish) {
378+
const inc = doc[0].publish ? 1 : -1
379+
await db.category.update({ _id: doc[0].categoryId }, { $inc: { total: inc } })
380+
}
381+
370382
res.json({
371383
status: 200,
372384
data: doc[0],
@@ -381,7 +393,17 @@ router.patch('/api/admin/article/edit', confirmToken, async (req, res) => {
381393
router.delete('/api/admin/article/del', confirmToken, async (req, res) => {
382394
try {
383395
const params = typeof req.query.id === 'string' ? { _id: req.query.id } : { _id: { $in: req.query.id } }
396+
// 维护分类表
397+
const articlesDoc = await db.article.find(params)
398+
if (articlesDoc.length) {
399+
articlesDoc.forEach(async (doc) => {
400+
if (doc.publish) {
401+
await db.category.update({ _id: doc.categoryId }, { $inc: { total: -1 } })
402+
}
403+
})
404+
}
384405
await db.article.remove(params)
406+
385407
res.json({
386408
status: 200,
387409
info: '删除文档成功'

‎front/server/api/category.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
const express = require('express')
77
const router = express.Router()
88
const db = require('../db/')
9-
// const confirmToken = require('../middleware/confirmToken')
109

1110
// 获取分类列表
1211
router.get('/api/front/category/get', async (req, res) => {

‎front/server/api/count.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/**
22
* @desc 统计类
3+
* @author justJokee
34
*/
45
const express = require('express')
56
const router = express.Router()
67
const db = require('../db/')
7-
// const confirmToken = require('../middleware/confirmToken')
8+
const confirmToken = require('../middleware/confirmToken')
89

9-
// 获取分类列表
10-
router.get('/api/admin/count', async (req, res) => {
10+
// 管理端首页统计项
11+
router.get('/api/admin/count', confirmToken,async (req, res) => {
1112
try {
1213
const article = await db.article.find({}).count()
1314
const comment = await db.comment.find({}).count()

‎front/server/api/donload.js‎

Lines changed: 0 additions & 39 deletions
This file was deleted.

‎front/server/api/getCount.js‎

Lines changed: 0 additions & 60 deletions
This file was deleted.

‎front/server/api/getTime.js‎

Lines changed: 0 additions & 68 deletions
This file was deleted.

‎front/server/api/index.js‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
11
const articles = require('./articles')
22
const tags = require('./tags')
3-
const getCount = require('./getCount')
43
const comments = require('./comments')
5-
const getTime = require('./getTime')
64
const msgBoard = require('./msgBoard')
75
const visitor = require('./visitor')
86
const login = require('./login')
9-
const copyData = require('./copyData')
7+
const mongodump = require('./mongodump')
108
const news = require('./news')
11-
const reviseKey = require('./reviseKey')
12-
const donload = require('./donload')
9+
const resetpwd = require('./resetpwd')
1310
const category = require('./category')
1411
const movies = require('./movies')
1512
const qiniu = require('./qiniu')
1613
const viewer = require('./viewer')
1714
const count = require('./count')
1815

19-
// const confirmToken = require('../middleware/confirmToken')
20-
2116
module.exports = (app) => {
22-
// app.use(confirmToken)
2317
app.use(viewer)
2418
app.use(articles)
2519
app.use(tags)
26-
app.use(getCount)
2720
app.use(comments)
28-
app.use(getTime)
2921
app.use(msgBoard)
3022
app.use(login)
3123
app.use(visitor)
32-
app.use(copyData)
24+
app.use(mongodump)
3325
app.use(news)
34-
app.use(reviseKey)
35-
app.use(donload)
26+
app.use(resetpwd)
3627
app.use(category)
3728
app.use(movies)
3829
app.use(qiniu)

‎front/server/api/login.js‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
const express = require('express')
66
const router = express.Router()
77
const jwt = require('jsonwebtoken')
8-
const md5 = require('md5')
98
const db = require('../db/')
109
const { userSecret } = require('../db/secret')
1110

1211
const createToken = (id, account) => {
13-
const secret = `${userSecret.pwd}.${userSecret.salt}`
12+
const secret = `${userSecret.salt}`
1413
return jwt.sign(
1514
{
1615
id: id,
@@ -21,25 +20,25 @@ const createToken = (id, account) => {
2120
)
2221
}
2322
// 登录验证
24-
router.post('/api/login', async (req, res) => {
23+
router.post('/api/admin/login', async (req, res) => {
2524
try {
2625
const user = await db.user.find({ account: req.body.account })
2726
// 用户名不存在,返回401
2827
if (!user.length) {
2928
res.json({
30-
status: 401,
29+
status: 402,
3130
info: '用户名或密码不正确'
3231
})
3332
return
3433
}
3534
const pwd = user[0].password
36-
console.log('body-----', req.body, md5(req.body.password))
37-
if (md5(req.body.password) === pwd) {
35+
if (req.body.password === pwd) {
3836
const token = createToken(user[0]._id, user[0].account)
3937
res.json({
4038
status: 200,
4139
data: {
4240
token,
41+
uid: user[0]._id,
4342
account: user[0].account,
4443
avatar: user[0].avatar,
4544
lastLoginTime: user[0].lastLoginTime
@@ -50,7 +49,7 @@ router.post('/api/login', async (req, res) => {
5049
await db.user.update({ user: req.body.user }, { lastLoginTime: new Date() })
5150
} else {
5251
res.json({
53-
status: 401,
52+
status: 402,
5453
info: '用户名或密码不正确'
5554
})
5655
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /