2017-07-06 19:21:54 +08:00
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="zh-CN">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8">
|
|
|
|
|
<meta name="viewport" content="initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" />
|
|
|
|
|
<title>yapi : ./server/controllers/base.js</title>
|
|
|
|
|
<link type="text/css" rel="stylesheet" href="../../../source/code.css"/>
|
|
|
|
|
<script type="text/javascript" src="../../../source/shCore.js"></script>
|
|
|
|
|
<script type="text/javascript" src="../../../source/shBrush-js.js"></script>
|
|
|
|
|
<style>
|
|
|
|
|
.syntaxhighlighter .number1 .spaces,.syntaxhighlighter .toolbar{ display: none;}
|
|
|
|
|
.syntaxhighlighter table td.gutter .line.highlight { background-color: #6ce26c !important; color: white; }
|
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<div class="ydoc">
|
|
|
|
|
<div class="ydoc-banner-bg">
|
|
|
|
|
<div class="ydoc-banner" id="content" tabindex="-1">
|
|
|
|
|
<div class="ydoc-banner-area">
|
|
|
|
|
<h1>yapi : ./server/controllers/base.js</h1>
|
|
|
|
|
<p>源代码</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="ydoc-container">
|
|
|
|
|
<div class="ydoc-container-content">
|
|
|
|
|
<div class="static-code-content" role="main">
|
|
|
|
|
<pre class="brush: js;">
|
|
|
|
|
import yapi from '../yapi.js'
|
2017-07-10 11:56:53 +08:00
|
|
|
|
import projectModel from '../models/project.js'
|
2017-07-11 18:22:20 +08:00
|
|
|
|
import userModel from '../models/user.js'
|
|
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
|
|
|
|
|
|
|
2017-07-06 19:21:54 +08:00
|
|
|
|
class baseController{
|
|
|
|
|
constructor(ctx){
|
2017-07-18 15:35:32 +08:00
|
|
|
|
this.ctx = ctx;
|
2017-07-11 18:22:20 +08:00
|
|
|
|
//网站上线后,role对象key是不能修改的,value可以修改
|
|
|
|
|
this.roles = {
|
|
|
|
|
admin: 'Admin',
|
|
|
|
|
member: '网站会员'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async init(ctx){
|
|
|
|
|
this.$user = null;
|
2017-07-18 15:35:32 +08:00
|
|
|
|
let ignoreRouter = [
|
|
|
|
|
'/user/login_by_token',
|
|
|
|
|
'/user/login',
|
|
|
|
|
'/user/reg',
|
|
|
|
|
'/user/status',
|
|
|
|
|
'/user/logout'
|
|
|
|
|
]
|
|
|
|
|
if(ignoreRouter.indexOf(ctx.path) > -1){
|
2017-07-11 18:22:20 +08:00
|
|
|
|
this.$auth = true;
|
|
|
|
|
}else{
|
|
|
|
|
await this.checkLogin(ctx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getUid(ctx){
|
|
|
|
|
return this.$uid;
|
2017-07-06 19:21:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 18:22:20 +08:00
|
|
|
|
async checkLogin(ctx){
|
|
|
|
|
let token = ctx.cookies.get('_yapi_token');
|
|
|
|
|
let uid = ctx.cookies.get('_yapi_uid');
|
|
|
|
|
try{
|
|
|
|
|
if(!token || !uid) return false;
|
|
|
|
|
let userInst = yapi.getInst(userModel); //创建user实体
|
|
|
|
|
let result = await userInst.findById(uid);
|
|
|
|
|
let decoded = jwt.verify(token, result.passsalt)
|
|
|
|
|
if(decoded.uid == uid){
|
|
|
|
|
this.$uid = uid;
|
|
|
|
|
this.$auth = true;
|
|
|
|
|
this.$user = result;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}catch(e){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-06 19:21:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 18:22:20 +08:00
|
|
|
|
async getLoginStatus(ctx){
|
2017-07-18 15:35:32 +08:00
|
|
|
|
if(await this.checkLogin(ctx) === true){
|
2017-07-11 18:22:20 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(yapi.commons.fieldSelect(this.$user,['_id','username','email', 'up_time', 'add_time']));
|
|
|
|
|
}
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 300 , 'Please login.');
|
2017-07-10 11:11:35 +08:00
|
|
|
|
|
2017-07-06 19:21:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getRole(){
|
2017-07-11 18:22:20 +08:00
|
|
|
|
return this.$user.role;
|
2017-07-06 19:21:54 +08:00
|
|
|
|
}
|
2017-07-10 11:56:53 +08:00
|
|
|
|
|
|
|
|
|
async jungeProjectAuth(id){
|
|
|
|
|
let model = yapi.getInst(projectModel);
|
|
|
|
|
if(this.getRole() === 'admin') return true;
|
|
|
|
|
if(!id) return false;
|
|
|
|
|
let result = await model.get(id);
|
|
|
|
|
if(result.uid === this.getUid()){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async jungeMemberAuth(id, member_uid){
|
|
|
|
|
let model = yapi.getInst(projectModel);
|
|
|
|
|
if(this.getRole() === 'admin') return true;
|
|
|
|
|
if(!id || !member_uid) return false;
|
|
|
|
|
let result = await model.checkMemberRepeat(id, member_uid);
|
|
|
|
|
if(result > 0){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-07-06 19:21:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = baseController
|
|
|
|
|
</pre>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<!-- <div class="docs-header" id="content" tabindex="-1">
|
|
|
|
|
<div class="container">
|
|
|
|
|
<h1>yapi : ./server/controllers/base.js</h1>
|
|
|
|
|
<p>源代码</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div> -->
|
|
|
|
|
|
|
|
|
|
<footer class="docs-footer" role="contentinfo">
|
|
|
|
|
<div class="container">
|
|
|
|
|
<p></p>
|
|
|
|
|
</div>
|
|
|
|
|
</footer>
|
|
|
|
|
</div>
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
SyntaxHighlighter.all();
|
|
|
|
|
|
|
|
|
|
function getTop(node){
|
|
|
|
|
return node.offsetTop + (node.offsetParent ? getTop(node.offsetParent) : 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
try {
|
|
|
|
|
var lineNum = (parseInt(location.hash.replace(/#/g, '')) - 1) || 0,
|
|
|
|
|
node = document.querySelectorAll('div.line')[lineNum];
|
|
|
|
|
document.body.scrollTop = getTop(node);
|
|
|
|
|
node.className += ' highlight';
|
|
|
|
|
} catch(e) {}
|
|
|
|
|
}, 500);
|
|
|
|
|
}, false);
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|