forked from wangtunan/vue-mooc
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlabel.js
152 lines (147 loc) · 3.13 KB
/
label.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
import Router from 'koa-router'
import Label from '../models/label.js'
import LableType from '../models/labelType.js'
import LabelFollow from '../models/labelFollow.js'
import checkUser from '../middleware/auth.js'
import { ERR_OK } from '../config.js'
import { getGuid } from '../../src/utils/utils.js';
const router = new Router({
prefix: '/label'
})
// 获取标签分类路由
router.get('/types', async (ctx) => {
try {
const result = await LableType.find().sort({
sort: 'asc'
})
if (result) {
ctx.body = {
code: ERR_OK,
msg: '获取标签分类成功',
data: result
}
} else {
ctx.body = {
code: -1,
msg: '获取标签分类失败',
data: []
}
}
} catch (e) {
ctx.body = {
code: -1,
msg: '服务器异常'
}
}
})
// 获取标签列表路由
router.get('/list', async (ctx) => {
const { code } = ctx.query
let where = {}
if (code) {
where['status.code'] = code
}
try {
const result = await Label.find(where).sort({
'type.code': 'asc',
sort: 'asc'
})
if (result) {
ctx.body = {
code: ERR_OK,
msg: '获取标签列表数据成功',
data: result
}
} else {
ctx.body = {
code: -1,
msg: '获取标签列表数据失败',
data: []
}
}
} catch (e) {
ctx.body = {
code: -1,
msg: e.message || '服务器异常'
}
}
})
// 用户关注标签路由
router.post('/follow', checkUser, async (ctx) => {
const userid = ctx.session.user_id
const { list } = ctx.request.body
// 验证是否有要关注的标签
if (!list || list.length === 0) {
ctx.body = {
code: -1,
msg: '待关注的标签不能为空'
}
return false
}
try {
const params = []
list.forEach(item => {
params.push({
id: getGuid(),
userid: userid,
labelid: item.id,
title: item.title
})
})
const removeResult = await LabelFollow.where({
userid: userid
}).deleteMany()
if (!removeResult) {
ctx.body = {
code: ERR_OK,
msg: '关注标签失败'
}
return false
}
const result = await LabelFollow.insertMany(params)
if (result) {
ctx.body = {
code: ERR_OK,
msg: '关注标签成功'
}
} else {
ctx.body = {
code: ERR_OK,
msg: '关注标签失败'
}
}
} catch (e) {
ctx.body = {
code: -1,
msg: e.message || '服务器异常'
}
}
})
// 用户关注标签列表路由
router.get('/follow/list', checkUser, async (ctx) => {
const userid = ctx.session.user_id
try {
const result = await LabelFollow.find({
userid: userid
})
if (result) {
ctx.body = {
code: ERR_OK,
msg: '获取用户关注标签数据成功',
data: result
}
} else {
ctx.body = {
code: ERR_OK,
msg: '获取用户关注标签数据失败',
data: []
}
}
} catch (e) {
ctx.body = {
code: -1,
msg: e.message || '服务器异常'
}
}
})
export default router