Skip to content

Commit

Permalink
refactor(server): 整理路由,重构 controller 层
Browse files Browse the repository at this point in the history
  • Loading branch information
mintsweet committed Jul 5, 2021
1 parent 1f8cd38 commit cc54edd
Show file tree
Hide file tree
Showing 16 changed files with 2,003 additions and 1,736 deletions.
36 changes: 16 additions & 20 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@

初始化以下 API:

- 获取图形验证码
- 上传文件
- 获取文件
- 注册
- 账户激活
- 登录
- 忘记密码
- 重置密码
- 头像上传
- 获取当前登录用户信息
- 更新个人信息
- 修改密码
- GitHub 登录
- 获取图形验证码
- 登录
- 获取当前用户信息
- 获取用户消息
- 获取系统消息
- 获取积分榜用户列表
- 根据ID获取用户信息
- 获取用户动态
Expand All @@ -44,33 +45,28 @@
- 获取用户粉丝列表
- 获取用户关注列表
- 关注或者取消关注用户
- 创建话题
- 删除话题
- 编辑话题
- 获取话题列表
- 搜索话题列表
- 获取无人回复的话题
- 创建话题
- 删除话题
- 编辑话题
- 根据ID获取话题详情
- 喜欢或者取消喜欢话题
- 收藏或者取消收藏话题
- 创建回复
- 删除回复
- 编辑回复
- 回复点赞或者取消点赞
- 获取用户消息
- 获取系统消息
- 获取本周新增用户数
- 获取上周新增用户数
- 获取用户总数
- 点赞回复
- 获取系统概览
- 获取用户列表
- 新增用户
- 删除用户(超管物理删除)
- 更新用户
- 设为星标用户
- 锁定用户(封号)
- 获取本周新增话题数
- 获取上周新增话题数
- 获取话题总数
- 获取话题列表
- 创建话题
- 删除话题(超管物理删除)
- 更新话题
- 话题置顶
- 话题加精
- 话题锁定(封贴)
52 changes: 26 additions & 26 deletions packages/server/app.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
const Koa = require('koa');
const koaBody = require('koa-body');
const koaJwt = require('koa-jwt');
const path = require('path');
const { jwt: { SECRET }, SERVER_PORT, FILE_LIMIT } = require('../../config');
const {
jwt: { SECRET },
SERVER_PORT,
FILE_LIMIT,
} = require('../../config');
const router = require('./router');
const logger = require('./utils/logger');
const ErrorHandler = require('./middlewares/error-handler');
const errorHandler = require('./middleware/error-handler');

require('./db/mongodb');

const app = module.exports = new Koa();
const app = (module.exports = new Koa());

// middleware
app
.use(koaBody({
multipart: true,
formidable: {
uploadDir: `${__dirname}/uploads`,
keepExtensions: true,
multiples: false,
maxFieldsSize: FILE_LIMIT, // 限制上传文件大小为 512kb
onFileBegin(name, file) {
const dir = path.dirname(file.path);
file.path = path.join(dir, file.name);
}
}
}))
.use(koaJwt({
secret: SECRET,
passthrough: true
}))
.use(ErrorHandler.handleError);
.use(
koaBody({
multipart: true,
formidable: {
uploadDir: `${__dirname}/upload`,
keepExtensions: true,
multiples: false,
maxFieldsSize: FILE_LIMIT, // 限制上传文件大小为 512kb
},
}),
)
.use(
koaJwt({
secret: SECRET,
passthrough: true,
}),
)
.use(errorHandler);

// router
app
.use(router.rt)
.use(router.v1)
.use(router.v2);
app.use(router.rt).use(router.be);

// 404
app.use(ctx => {
Expand Down
141 changes: 141 additions & 0 deletions packages/server/controller/aider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
const fs = require('fs');
const path = require('path');
const { BMP24 } = require('gd-bmp');
const moment = require('moment');
const UserModel = require('../model/user');
const TopicModel = require('../model/topic');

class Aider {
constructor() {
this.getCaptcha = this.getCaptcha.bind(this);
}

// 生成随机数
_rand(min, max) {
return (Math.random() * (max - min + 1) + min) | 0;
}

getCaptcha(ctx) {
const {
width = 100,
height = 40,
textColor = 'a1a1a1',
bgColor = 'ffffff',
} = ctx.query;

// 设置画布
const img = new BMP24(width, height);
// 设置背景
img.fillRect(0, 0, width, height, `0x${bgColor}`);

let token = '';

// 随机字符列表
const p = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
// 组成token
for (let i = 0; i < 5; i++) {
token += p.charAt((Math.random() * p.length) | 0);
}

// 字符定位于背景 x,y 轴位置
let x = 10,
y = 2;

for (let i = 0; i < token.length; i++) {
y = 2 + this._rand(-4, 4);
// 画字符
img.drawChar(token[i], x, y, BMP24.font12x24, `0x${textColor}`);
x += 12 + this._rand(4, 8);
}

const url = `data:image/bmp;base64,${img.getFileData().toString('base64')}`;

ctx.body = { token, url };
}

// 上传文件
async upload(ctx) {
const { id } = ctx.state.user;
const { file } = ctx.request.files;

if (!file) {
ctx.throw(400, '请上传文件');
}

const filename = `avatar_${id}${path.extname(file.path)}`;

await new Promise((resolve, reject) => {
fs.rename(file.path, `${path.dirname(file.path)}/${filename}`, err => {
if (err) reject(err);
resolve();
});
});

ctx.body = filename;
}

// 获取文件
async getFile(ctx) {
const { filename } = ctx.params;
const file = path.join(__dirname, '../upload', filename);
ctx.set('Content-Type', 'image/png');
ctx.body = fs.readFileSync(file);
}

// 获取系统概览
async dashboard(ctx) {
const curWeekStart = moment().startOf('week');
const curWeekEnd = moment().endOf('week');
const preWeekStart = moment()
.startOf('week')
.subtract(1, 'w');
const preWeekEnd = moment()
.endOf('week')
.subtract(1, 'w');

// 本周新增用户数, 上周新增用户数, 用户总数
const [curWeekAddUser, preWeekAddUser, userTotal] = await Promise.all([
UserModel.countDocuments({
created_at: {
$gte: curWeekStart,
$lt: curWeekEnd,
},
}),
UserModel.countDocuments({
created_at: {
$gte: preWeekStart,
$lt: preWeekEnd,
},
}),
UserModel.countDocuments(),
]);

// 本周新增话题数, 上周新增话题数, 话题总数
const [curWeekAddTopic, preWeekAddTopic, topicTotal] = await Promise.all([
TopicModel.countDocuments({
created_at: {
$gte: curWeekStart,
$lt: curWeekEnd,
},
}),
TopicModel.countDocuments({
created_at: {
$gte: preWeekStart,
$lt: preWeekEnd,
},
}),
TopicModel.countDocuments(),
]);

ctx.body = {
curWeekAddUser,
preWeekAddUser,
userTotal,
curWeekAddTopic,
preWeekAddTopic,
topicTotal,
};
}
}

module.exports = new Aider();
Loading

0 comments on commit cc54edd

Please sign in to comment.