forked from wangtunan/vue-mooc
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcommon.js
147 lines (141 loc) · 2.9 KB
/
common.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
import Router from 'koa-router'
import Hot from '../models/hot.js'
import History from '../models/history.js'
import Nav from '../models/nav.js'
import Footer from '../models/footer.js'
import { ERR_OK } from '../config.js'
import { getGuid } from '../../src/utils/utils.js'
const router = new Router({
prefix: '/common'
})
// 头部导航接口
router.get('/nav', async (ctx) => {
try {
const result = await Nav.find()
if (result.length >= 0) {
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.get('/hot', async (ctx) => {
try {
const result = await Hot.find().sort({
time: -1
})
if (result.length >= 0) {
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.get('/history', async (ctx) => {
try {
const result = await History.find().sort({
time: -1
}).limit(5)
if (result.length >= 0) {
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.get('/history/create', async (ctx) => {
try {
const result = await History.create({
id: getGuid(),
value: ctx.query.keyword,
time: new Date().toISOString()
})
if (result) {
ctx.body = {
code: ERR_OK,
msg: '搜索历史生成成功',
data: null
}
} else {
ctx.body = {
code: -1,
msg: '搜索历史生成失败',
data: null
}
}
} catch (e) {
ctx.body = {
code: -1,
msg: e.message || '服务器异常'
}
}
})
// 底部链接接口
router.get('/footer', async (ctx) => {
try {
const result = await Footer.find().sort({
sort: 'ascending'
})
if (result.length >= 0) {
ctx.body = {
code: ERR_OK,
msg: '获取底部链接数据成功',
data: result
}
} else {
ctx.body = {
code: -1,
msg: '获取底部链接数据失败',
data: []
}
}
} catch (e) {
ctx.body = {
code: -1,
msg: e.message || '服务器异常'
}
}
})
export default router