Skip to content

Commit cc54edd

Browse files
committed
refactor(server): 整理路由,重构 controller 层
1 parent 1f8cd38 commit cc54edd

File tree

16 files changed

+2003
-1736
lines changed

16 files changed

+2003
-1736
lines changed

CHANGELOG.md

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@
2424

2525
初始化以下 API:
2626

27+
- 获取图形验证码
28+
- 上传文件
29+
- 获取文件
2730
- 注册
28-
- 账户激活
31+
- 登录
2932
- 忘记密码
3033
- 重置密码
31-
- 头像上传
34+
- 获取当前登录用户信息
3235
- 更新个人信息
3336
- 修改密码
34-
- GitHub 登录
35-
- 获取图形验证码
36-
- 登录
37-
- 获取当前用户信息
37+
- 获取用户消息
38+
- 获取系统消息
3839
- 获取积分榜用户列表
3940
- 根据ID获取用户信息
4041
- 获取用户动态
@@ -44,33 +45,28 @@
4445
- 获取用户粉丝列表
4546
- 获取用户关注列表
4647
- 关注或者取消关注用户
47-
- 创建话题
48-
- 删除话题
49-
- 编辑话题
5048
- 获取话题列表
5149
- 搜索话题列表
5250
- 获取无人回复的话题
51+
- 创建话题
52+
- 删除话题
53+
- 编辑话题
5354
- 根据ID获取话题详情
5455
- 喜欢或者取消喜欢话题
5556
- 收藏或者取消收藏话题
5657
- 创建回复
57-
- 删除回复
58-
- 编辑回复
59-
- 回复点赞或者取消点赞
60-
- 获取用户消息
61-
- 获取系统消息
62-
- 获取本周新增用户数
63-
- 获取上周新增用户数
64-
- 获取用户总数
58+
- 点赞回复
59+
- 获取系统概览
6560
- 获取用户列表
6661
- 新增用户
6762
- 删除用户(超管物理删除)
63+
- 更新用户
6864
- 设为星标用户
6965
- 锁定用户(封号)
70-
- 获取本周新增话题数
71-
- 获取上周新增话题数
72-
- 获取话题总数
66+
- 获取话题列表
67+
- 创建话题
7368
- 删除话题(超管物理删除)
69+
- 更新话题
7470
- 话题置顶
7571
- 话题加精
7672
- 话题锁定(封贴)

packages/server/app.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
const Koa = require('koa');
22
const koaBody = require('koa-body');
33
const koaJwt = require('koa-jwt');
4-
const path = require('path');
5-
const { jwt: { SECRET }, SERVER_PORT, FILE_LIMIT } = require('../../config');
4+
const {
5+
jwt: { SECRET },
6+
SERVER_PORT,
7+
FILE_LIMIT,
8+
} = require('../../config');
69
const router = require('./router');
710
const logger = require('./utils/logger');
8-
const ErrorHandler = require('./middlewares/error-handler');
11+
const errorHandler = require('./middleware/error-handler');
912

1013
require('./db/mongodb');
1114

12-
const app = module.exports = new Koa();
15+
const app = (module.exports = new Koa());
1316

1417
// middleware
1518
app
16-
.use(koaBody({
17-
multipart: true,
18-
formidable: {
19-
uploadDir: `${__dirname}/uploads`,
20-
keepExtensions: true,
21-
multiples: false,
22-
maxFieldsSize: FILE_LIMIT, // 限制上传文件大小为 512kb
23-
onFileBegin(name, file) {
24-
const dir = path.dirname(file.path);
25-
file.path = path.join(dir, file.name);
26-
}
27-
}
28-
}))
29-
.use(koaJwt({
30-
secret: SECRET,
31-
passthrough: true
32-
}))
33-
.use(ErrorHandler.handleError);
19+
.use(
20+
koaBody({
21+
multipart: true,
22+
formidable: {
23+
uploadDir: `${__dirname}/upload`,
24+
keepExtensions: true,
25+
multiples: false,
26+
maxFieldsSize: FILE_LIMIT, // 限制上传文件大小为 512kb
27+
},
28+
}),
29+
)
30+
.use(
31+
koaJwt({
32+
secret: SECRET,
33+
passthrough: true,
34+
}),
35+
)
36+
.use(errorHandler);
3437

3538
// router
36-
app
37-
.use(router.rt)
38-
.use(router.v1)
39-
.use(router.v2);
39+
app.use(router.rt).use(router.be);
4040

4141
// 404
4242
app.use(ctx => {

packages/server/controller/aider.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { BMP24 } = require('gd-bmp');
4+
const moment = require('moment');
5+
const UserModel = require('../model/user');
6+
const TopicModel = require('../model/topic');
7+
8+
class Aider {
9+
constructor() {
10+
this.getCaptcha = this.getCaptcha.bind(this);
11+
}
12+
13+
// 生成随机数
14+
_rand(min, max) {
15+
return (Math.random() * (max - min + 1) + min) | 0;
16+
}
17+
18+
getCaptcha(ctx) {
19+
const {
20+
width = 100,
21+
height = 40,
22+
textColor = 'a1a1a1',
23+
bgColor = 'ffffff',
24+
} = ctx.query;
25+
26+
// 设置画布
27+
const img = new BMP24(width, height);
28+
// 设置背景
29+
img.fillRect(0, 0, width, height, `0x${bgColor}`);
30+
31+
let token = '';
32+
33+
// 随机字符列表
34+
const p = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
35+
// 组成token
36+
for (let i = 0; i < 5; i++) {
37+
token += p.charAt((Math.random() * p.length) | 0);
38+
}
39+
40+
// 字符定位于背景 x,y 轴位置
41+
let x = 10,
42+
y = 2;
43+
44+
for (let i = 0; i < token.length; i++) {
45+
y = 2 + this._rand(-4, 4);
46+
// 画字符
47+
img.drawChar(token[i], x, y, BMP24.font12x24, `0x${textColor}`);
48+
x += 12 + this._rand(4, 8);
49+
}
50+
51+
const url = `data:image/bmp;base64,${img.getFileData().toString('base64')}`;
52+
53+
ctx.body = { token, url };
54+
}
55+
56+
// 上传文件
57+
async upload(ctx) {
58+
const { id } = ctx.state.user;
59+
const { file } = ctx.request.files;
60+
61+
if (!file) {
62+
ctx.throw(400, '请上传文件');
63+
}
64+
65+
const filename = `avatar_${id}${path.extname(file.path)}`;
66+
67+
await new Promise((resolve, reject) => {
68+
fs.rename(file.path, `${path.dirname(file.path)}/${filename}`, err => {
69+
if (err) reject(err);
70+
resolve();
71+
});
72+
});
73+
74+
ctx.body = filename;
75+
}
76+
77+
// 获取文件
78+
async getFile(ctx) {
79+
const { filename } = ctx.params;
80+
const file = path.join(__dirname, '../upload', filename);
81+
ctx.set('Content-Type', 'image/png');
82+
ctx.body = fs.readFileSync(file);
83+
}
84+
85+
// 获取系统概览
86+
async dashboard(ctx) {
87+
const curWeekStart = moment().startOf('week');
88+
const curWeekEnd = moment().endOf('week');
89+
const preWeekStart = moment()
90+
.startOf('week')
91+
.subtract(1, 'w');
92+
const preWeekEnd = moment()
93+
.endOf('week')
94+
.subtract(1, 'w');
95+
96+
// 本周新增用户数, 上周新增用户数, 用户总数
97+
const [curWeekAddUser, preWeekAddUser, userTotal] = await Promise.all([
98+
UserModel.countDocuments({
99+
created_at: {
100+
$gte: curWeekStart,
101+
$lt: curWeekEnd,
102+
},
103+
}),
104+
UserModel.countDocuments({
105+
created_at: {
106+
$gte: preWeekStart,
107+
$lt: preWeekEnd,
108+
},
109+
}),
110+
UserModel.countDocuments(),
111+
]);
112+
113+
// 本周新增话题数, 上周新增话题数, 话题总数
114+
const [curWeekAddTopic, preWeekAddTopic, topicTotal] = await Promise.all([
115+
TopicModel.countDocuments({
116+
created_at: {
117+
$gte: curWeekStart,
118+
$lt: curWeekEnd,
119+
},
120+
}),
121+
TopicModel.countDocuments({
122+
created_at: {
123+
$gte: preWeekStart,
124+
$lt: preWeekEnd,
125+
},
126+
}),
127+
TopicModel.countDocuments(),
128+
]);
129+
130+
ctx.body = {
131+
curWeekAddUser,
132+
preWeekAddUser,
133+
userTotal,
134+
curWeekAddTopic,
135+
preWeekAddTopic,
136+
topicTotal,
137+
};
138+
}
139+
}
140+
141+
module.exports = new Aider();

0 commit comments

Comments
 (0)