-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.js
More file actions
119 lines (115 loc) · 4.23 KB
/
Copy pathrouter.js
File metadata and controls
119 lines (115 loc) · 4.23 KB
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
const Router = require('koa-router');
const api = require('./api');
let router = new Router();
async function getCommonData() {
let individuation = await api.getIndividuationData();
let nickname = individuation.nickname || '';
let self_intro = individuation.self_intro || '';
let portrait = individuation.portrait || '';
let articleTotal = await api.getArticleTotal();
let labelTotal = await api.getLabelTotal();
let banner = await api.getBannerData();
let businessCard = {nickname, self_intro, portrait, articleTotal, labelTotal};
let sideBar = {businessCard, banner};
return {individuation, sideBar};
}
router
.get('/', async ctx => {
let commonData = await getCommonData();
let pageIndex = ctx.query.pageIndex;
let labelId = ctx.query.labelId;
let searchVal = ctx.query.searchVal;
let labelName = '';
let articleList = {};
if (searchVal) {
articleList = await api.getArticleList({searchVal, pageIndex});
} else if (labelId) {
articleList = await api.getArticleListByLabelId({labelId, pageIndex});
let label = await api.getLabelById({id: labelId});
labelName = label.name;
} else {
articleList = await api.getArticleList({pageIndex});
}
for (let i in articleList.data) {
if (articleList.data[i].label_id) {
let labelId = articleList.data[i].label_id;
articleList.data[i].label = await api.getLabelById({id: labelId});
}
}
await ctx.render('./pages/index', {
individuation: commonData.individuation,
banner: commonData.sideBar.banner,
sideBar: commonData.sideBar,
searchVal,
labelName,
articleList
});
})
.get('/archive', async ctx => {
let commonData = await getCommonData();
let pageIndex = ctx.query.pageIndex;
let pageSize = 12;
let archiveList = await api.getArticleArchive({pageIndex, pageSize});
await ctx.render('./pages/archive', {
individuation: commonData.individuation,
sideBar: commonData.sideBar,
archiveList
});
})
.get('/label', async ctx => {
let commonData = await getCommonData();
let labelList = await api.getLabelList();
await ctx.render('./pages/label', {
individuation: commonData.individuation,
sideBar: commonData.sideBar,
labelList
});
})
.get('/diary', async ctx => {
let commonData = await getCommonData();
let month = '', diaryList = [];
if (ctx.query.month) {
// client端异步请求
month = ctx.query.month;
diaryList = await api.getDiaryList({month});
ctx.body = {diaryList};
} else {
let y = new Date().getFullYear();
let m = new Date().getMonth() + 1;
m = m < 10 ? `0${m}` : m;
month = `${y}-${m}-01`;
diaryList = await api.getDiaryList({month});
await ctx.render('./pages/diary', {
individuation: commonData.individuation,
sideBar: commonData.sideBar,
diaryList
});
}
})
.get('/about', async ctx => {
let commonData = await getCommonData();
await ctx.render('./pages/about', {
individuation: commonData.individuation,
sideBar: commonData.sideBar
});
})
.get('/article', async ctx => {
let commonData = await getCommonData();
let id = ctx.query.id;
await api.updateArticleHits({id});
let articleDetail = await api.getArticleDetail({id});
if (articleDetail.id) {
await ctx.render('./pages/article', {
individuation: commonData.individuation,
sideBar: commonData.sideBar,
articleDetail
});
} else {
await ctx.render('./404', {
message: '文章不存在或已删除',
individuation: commonData.individuation,
sideBar: commonData.sideBar
});
}
});
module.exports = {router, getCommonData};