yapi/doc/build/static/server/controllers/interface.js.html

292 lines
11 KiB
HTML
Raw Normal View History

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/interface.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/interface.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;">
2017-07-10 11:56:53 +08:00
import interfaceModel from '../models/interface.js'
import baseController from './base.js'
import yapi from '../yapi.js'
class interfaceController extends baseController{
constructor(ctx){
super(ctx)
this.Model = yapi.getInst(interfaceModel);
}
/**
* 添加项目分组
* @interface /interface/add
* @method POST
* @category interface
* @foldnumber 10
* @param {Number} project_id 项目id不能为空
* @param {String} path 接口请求路径,不能为空
* @param {String} method 请求方式
* @param {Array} [req_headers] 请求的header信息
* @param {String} [req_headers[].name] 请求的header信息名
* @param {String} [req_headers[].value] 请求的header信息值
* @param {Boolean} [req_headers[].required] 是否是必须,默认为否
* @param {String} [req_headers[].desc] header描述
* @param {String} [req_params_type] 请求参数方式,有["form", "json", "text", "xml"]四种
* @param {Mixed} [req_params] 请求参数,如果请求方式是form参数是Array数组其他格式请求参数是字符串
* @param {String} [req_params[].name] 请求参数名
* @param {String} [req_params[].value] 请求参数值可填写生成规则mock。如@email随机生成一条email
* @param {String} [req_params[].type] 请求参数类型,有["text", "file"]两种
* @param {String} [res_body_type] 相应信息的数据格式,有["json", "text", "xml"]三种
* @param {String} [res_body] 响应信息可填写任意字符串如果res_body_type是json,则会调用mock功能
* @param {String} [desc] 接口描述
* @returns {Object}
* @example ./api/interface/add.json
*/
async add(ctx){
let params = ctx.request.body;
params.method = params.method || 'GET';
params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json';
if(!params.project_id){
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
if(!params.path){
return ctx.body = yapi.commons.resReturn(null, 400, '接口请求路径不能为空');
}
let checkRepeat = await this.Model.checkRepeat(params.path, params.method);
if(checkRepeat > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']');
}
try{
let data = {
project_id: params.project_id,
path: params.path,
desc: params.desc,
method: params.method,
req_headers: params.req_headers,
req_params_type: params.req_params_type,
res_body: params.res_body,
res_body_type: params.res_body_type,
uid: this.getUid(),
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
}
if(data.req_params_type === 'form') data.req_params_form = params.req_params;
else data.req_params_other = params.req_params;
let result = await this.Model.save(data);
ctx.body = yapi.commons.resReturn(result);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
2017-07-11 18:22:20 +08:00
/**
* 添加项目分组
* @interface /interface/get
* @method GET
* @category interface
* @foldnumber 10
* @param {Number} id 接口id不能为空
* @returns {Object}
* @example ./api/interface/get.json
*/
2017-07-10 11:56:53 +08:00
async get(ctx){
let params = ctx.request.query;
if(!params.id){
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
}
try{
let result = await this.Model.get(params.id);
ctx.body = yapi.commons.resReturn(result);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
2017-07-11 18:22:20 +08:00
/**
* 添加项目分组
* @interface /interface/list
* @method GET
* @category interface
* @foldnumber 10
* @param {Number} project_id 项目id不能为空
* @returns {Object}
* @example ./api/interface/list.json
*/
2017-07-10 11:56:53 +08:00
async list(ctx){
let project_id = ctx.request.query.project_id;
if(!project_id){
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
try{
let result = await this.Model.list(project_id);
ctx.body = yapi.commons.resReturn(result)
}catch(err){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
2017-07-11 18:22:20 +08:00
/**
* 添加项目分组
* @interface /interface/up
* @method POST
* @category interface
* @foldnumber 10
* @param {Number} id 接口id不能为空
* @param {String} [path] 接口请求路径
* @param {String} [method] 请求方式
* @param {Array} [req_headers] 请求的header信息
* @param {String} [req_headers[].name] 请求的header信息名
* @param {String} [req_headers[].value] 请求的header信息值
* @param {Boolean} [req_headers[].required] 是否是必须,默认为否
* @param {String} [req_headers[].desc] header描述
* @param {String} [req_params_type] 请求参数方式,有["form", "json", "text", "xml"]四种
* @param {Mixed} [req_params] 请求参数,如果请求方式是form参数是Array数组其他格式请求参数是字符串
* @param {String} [req_params[].name] 请求参数名
* @param {String} [req_params[].value] 请求参数值可填写生成规则mock。如@email随机生成一条email
* @param {String} [req_params[].type] 请求参数类型,有["text", "file"]两种
* @param {String} [res_body_type] 相应信息的数据格式,有["json", "text", "xml"]三种
* @param {String} [res_body] 响应信息可填写任意字符串如果res_body_type是json,则会调用mock功能
* @param {String} [desc] 接口描述
* @returns {Object}
* @example ./api/interface/up.json
*/
2017-07-10 11:56:53 +08:00
async up(ctx){
let params = ctx.request.body;
params.method = params.method || 'GET';
let id = ctx.request.body.id;
if(!id){
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
}
if(params.path){
let checkRepeat = await this.Model.checkRepeat(params.path, params.method);
if(checkRepeat > 0){
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']');
}
}
2017-07-06 19:21:54 +08:00
let data = {
2017-07-10 11:56:53 +08:00
up_time: yapi.commons.time()
}
if(params.path) data.path = params.path;
if(params.desc) data.desc = params.desc;
if(params.method) data.method = params.method;
if(params.req_headers) data.req_headers = params.req_headers;
if(params.req_params_type === 'form') data.req_params_form = params.req_params;
else data.req_params_other = params.req_params;
if(params.res_body_type) data.res_body_type = params.res_body_type;
if(params.res_body) data.res_body = params.res_body;
try{
let result = await this.Model.up(id, data);
ctx.body = yapi.commons.resReturn(result)
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
2017-07-11 18:22:20 +08:00
/**
* 删除接口
* @interface /interface/del
* @method GET
* @category interface
* @foldnumber 10
* @param {Number} id 接口id不能为空
* @returns {Object}
* @example ./api/interface/del.json
*/
2017-07-10 11:56:53 +08:00
async del(ctx){
try{
2017-07-11 18:22:20 +08:00
let id = ctx.request.body.id;
2017-07-10 11:56:53 +08:00
if(!id){
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
}
2017-07-11 18:22:20 +08:00
let data = await this.Model.get(params.id);
if(data.uid != this.getUid()){
if(await this.jungeProjectAuth(data.project_id) !== true){
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
2017-07-10 11:56:53 +08:00
}
2017-07-11 18:22:20 +08:00
2017-07-10 11:56:53 +08:00
let result = await this.Model.del(id);
ctx.body = yapi.commons.resReturn(result)
}catch(err){
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
2017-07-06 19:21:54 +08:00
}
}
2017-07-10 11:56:53 +08:00
module.exports = interfaceController;
2017-07-06 19:21:54 +08:00
</pre>
</div>
</div>
</div>
</div>
<!-- <div class="docs-header" id="content" tabindex="-1">
<div class="container">
<h1>yapi : ./server/controllers/interface.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>