yapi/server/controllers/base.js

115 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-07-27 14:12:43 +08:00
import yapi from '../yapi.js';
import projectModel from '../models/project.js';
import userModel from '../models/user.js';
2017-07-11 12:12:43 +08:00
const jwt = require('jsonwebtoken');
2017-07-27 14:12:43 +08:00
class baseController {
constructor(ctx) {
2017-07-12 17:01:27 +08:00
this.ctx = ctx;
2017-07-11 16:50:17 +08:00
//网站上线后role对象key是不能修改的value可以修改
this.roles = {
admin: 'Admin',
member: '网站会员'
2017-07-27 14:12:43 +08:00
};
2017-07-11 12:12:43 +08:00
}
2017-07-10 11:11:35 +08:00
2017-07-27 14:12:43 +08:00
async init(ctx) {
2017-07-11 12:12:43 +08:00
this.$user = null;
2017-07-12 17:01:27 +08:00
let ignoreRouter = [
'/user/login_by_token',
'/user/login',
'/user/reg',
'/user/status',
'/user/logout'
2017-07-27 14:12:43 +08:00
];
if (ignoreRouter.indexOf(ctx.path) > -1) {
2017-07-11 12:12:43 +08:00
this.$auth = true;
2017-07-27 14:12:43 +08:00
} else {
await this.checkLogin(ctx);
2017-07-11 12:12:43 +08:00
}
2017-07-27 14:12:43 +08:00
2017-07-11 12:12:43 +08:00
}
2017-07-10 11:11:35 +08:00
2017-07-27 14:12:43 +08:00
getUid() {
return parseInt(this.$uid, 10);
2017-07-05 17:47:51 +08:00
}
2017-07-27 14:12:43 +08:00
async checkLogin(ctx) {
2017-07-11 12:12:43 +08:00
let token = ctx.cookies.get('_yapi_token');
2017-07-27 14:12:43 +08:00
let uid = ctx.cookies.get('_yapi_uid');
try {
if (!token || !uid) return false;
2017-07-11 12:12:43 +08:00
let userInst = yapi.getInst(userModel); //创建user实体
let result = await userInst.findById(uid);
2017-07-27 14:12:43 +08:00
let decoded = jwt.verify(token, result.passsalt);
if (decoded.uid == uid) {
2017-07-11 12:12:43 +08:00
this.$uid = uid;
this.$auth = true;
2017-07-27 14:12:43 +08:00
this.$user = result;
2017-07-11 12:12:43 +08:00
return true;
}
2017-07-27 14:12:43 +08:00
2017-07-11 12:12:43 +08:00
return false;
2017-07-27 14:12:43 +08:00
} catch (e) {
2017-07-11 12:12:43 +08:00
return false;
}
2017-07-05 17:47:51 +08:00
}
2017-07-27 14:12:43 +08:00
async getLoginStatus(ctx) {
if (await this.checkLogin(ctx) === true) {
2017-07-27 18:00:43 +08:00
let result = yapi.commons.fieldSelect(this.$user, ['_id', 'username', 'email', 'up_time', 'add_time']);
result.server_ip = yapi.WEBCONFIG.server_ip;
return ctx.body = yapi.commons.resReturn(result);
2017-07-11 12:12:43 +08:00
}
2017-07-27 14:12:43 +08:00
return ctx.body = yapi.commons.resReturn(null, 300, 'Please login.');
2017-07-05 17:47:51 +08:00
}
2017-07-06 19:21:54 +08:00
2017-07-27 14:12:43 +08:00
getRole() {
2017-07-11 16:50:17 +08:00
return this.$user.role;
2017-07-06 19:21:54 +08:00
}
2017-07-10 11:56:53 +08:00
2017-07-27 14:12:43 +08:00
async jungeProjectAuth(id) {
2017-07-10 11:56:53 +08:00
let model = yapi.getInst(projectModel);
2017-07-27 14:12:43 +08:00
if (this.getRole() === 'admin') {
2017-07-10 11:56:53 +08:00
return true;
}
2017-07-27 14:12:43 +08:00
if (!id) {
return false;
}
let result = await model.get(id);
if (result.uid === this.getUid()) {
return true;
}
2017-07-10 11:56:53 +08:00
return false;
}
2017-07-27 14:12:43 +08:00
async jungeMemberAuth(id, member_uid) {
2017-07-10 11:56:53 +08:00
let model = yapi.getInst(projectModel);
2017-07-27 14:12:43 +08:00
if (this.getRole() === 'admin') {
return true;
}
if (!id || !member_uid) {
return false;
}
let result = await model.checkMemberRepeat(id, member_uid);
if (result > 0) {
2017-07-10 11:56:53 +08:00
return true;
}
2017-07-27 14:12:43 +08:00
2017-07-10 11:56:53 +08:00
return false;
}
2017-07-05 17:47:51 +08:00
}
2017-07-27 14:12:43 +08:00
module.exports = baseController;