Merge branch 'dev' of gitlab.corp.qunar.com:mfe/yapi into dev

This commit is contained in:
zwjamnsss 2017-08-10 20:08:52 +08:00
commit 94d4bf2378
29 changed files with 2696 additions and 523 deletions

View File

@ -1,98 +1,17 @@
# YApi
## 平台介绍
## 一、平台介绍
<p style='text-indent:2em;line-height:1.8em'>YApi是<strong>高效</strong><strong>易用</strong><strong>功能强大</strong>、的api管理平台旨在为开发、产品、测试人员提供更优雅的接口管理服务。可以帮助开发者轻松创建、发布、维护、监控和保护任意规模的 API而且yapi为用户提供了优秀的交互体验开发人员只需利用平台提供的接口数据写入工具以及简单的点击操作就可以实现接口的创建</p>
<p style='text-indent:2em;line-height:1.8em'>YApi是<strong>高效</strong><strong>易用</strong><strong>功能强大</strong>、的api管理平台旨在为开发、产品、测试人员提供更优雅的接口管理服务。可以帮助开发者轻松创建、发布、维护、监控和保护任意规模的 APIyapi为用户提供了优秀的交互体验,开发人员只需利用平台提供的接口数据写入工具以及简单的点击操作就可以实现接口的管理</p>
## 功能
1. 项目接口管理
提供基本的项目分组,项目管理,接口管理功能
2. mockServer服务
用户只需在项目配置线上域名和接口基本路径通过将线上域名指到我们的yapi平台服务器就可使用mockServer服务
3. 用户管理
提供基本的用户注册登录管理等功能集成了去哪儿QSSO登录
## 快速开始
### 1 接口管理架构
平台以**项目分组** -> **项目** -> **接口**的划分进行接口组织管理。
<img src="http://upload-images.jianshu.io/upload_images/842107-305ba49a60ee1ff5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width = "800" style="margin:0px auto;display:block;" alt="图片名称" align=center />
#### 1.1 项目分组
登录之后进到项目首页,左边侧边栏显示的即分组列表。
<img src="http://upload-images.jianshu.io/upload_images/842107-d90ca4b3242fa760.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width = "200" style="margin-left:170px;display:block;" alt="图片名称" align=center />
管理员有权限添加或删除分组。
<img src="http://upload-images.jianshu.io/upload_images/842107-a0d4d9a98003896a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width = "500" style="margin-left:170px;display:block;" alt="图片名称" align=center />
> 分组名称具有唯一性
#### 1.2 项目
选中不同的分组,右边会显示该分组下的项目列表。
<img src="http://upload-images.jianshu.io/upload_images/842107-137bcae58b84715e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width = "800" style="margin:0px auto;display:block;" alt="图片名称" align=center />
创建项目需要填写项目名称,项目线上域名(添加完成后可配置项目其他环境域名),项目接口基本路径(接口路径前面相同的部分)以及项目描述。
<img src="http://upload-images.jianshu.io/upload_images/842107-360a50ddb746f73d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width = "800" style="margin:0px auto;display:block;" alt="图片名称" align=center />
> 项目『线上域名 + 基本路径』具有唯一性
#### 1.3 接口
点击项目名称,进入该项目接口列表。
<img src="http://upload-images.jianshu.io/upload_images/842107-e858005f714f4889.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width = "800" style="margin:0px auto;display:block;" alt="图片名称" align=center />
点击编辑,进入接口详情页(之后接口详情页和编辑也会分开),可以编辑接口或者请求测试真实接口。
<img src="http://upload-images.jianshu.io/upload_images/842107-78c0ea839619d068.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width = "800" style="margin:0px auto;display:block;" alt="图片名称" align=center />
<img src="http://upload-images.jianshu.io/upload_images/842107-2ee7171d707e91ff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width = "800" style="margin:0px auto;display:block;" alt="图片名称" align=center />
## 二、特性
* 完善的项目接口管理功能
* 易用的MockServer
* 类gitlab的用户管理和权限管理功能
* 强大的接口集功能
### 2 Mock功能
<p style='text-indent:2em;line-height:1.8em'>yapi的Mock功能可以根据用户的输入接口信息如协议、URL、接口名、请求头、请求参数、mock规则生成Mock接口这些接口会自动生成模拟数据支持复杂的生成逻辑创建者可以自由构造需要的数据。而且与常见的Mock方式如将Mock写在代码里和JS拦截等相比yapi的Mock在使用场景和效率和复杂度上是相差甚远的正是由于yapi的Mock是一个第三方平台那么在 团队开发时任何人都可以权限许可下创建、修改接口信息等操作,这对于团队开发是很有好处的。</p>
#### 2.1 添加接口
通过点击页面上的"+添加接口"
<img src="http://note.youdao.com/yws/api/personal/file/WEB613bd4f29db038f2b41c03dcfceda2b6?method=download&shareKey=29bfc2b855f6f26ce0079baf567e54cc" width = "800" style="margin:0px auto;display:block;" alt="图片名称" align=center />
输入协议、URL、接口名、请求头、请求参数、mock规则等信息然后点击右上角的"Mock"按钮。
<img src="http://upload-images.jianshu.io/upload_images/842107-78c0ea839619d068.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width = "800" style="margin:0px auto;display:block;" alt="图片名称" align=center />
#### 2.2 Mock
当点击"Mock"按钮之后就会在页面下方生成一个mock结果并产生一个API接口。点击"复制"按钮即可复制,用户拿到接口后就可以发请求了。
<img src="http://note.youdao.com/yws/api/personal/file/WEBf9b154cb88d21daa8206e8c4a3d76042?method=download&shareKey=1999f6c2cf197b5b6d775c312e34073d" width = "800" style="margin:0px auto;display:block;" alt="图片名称" align=center />
将请求的信息填写完善如请求方法post、get等、URL、请求头、请求的数据等。然后就点击"发送",然后在"返回结果"出可以看到接口返回的数据。
<img src="http://upload-images.jianshu.io/upload_images/842107-2ee7171d707e91ff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width = "800" style="margin:0px auto;display:block;" alt="图片名称" align=center />
#### 2.3 成员管理
你也可以通过点击"管理成员"来添加和删除项目的成员,便于团队管理。
<img src="http://note.youdao.com/yws/api/personal/file/WEB1b9defdf0cb884f46c2bd6c30ceb02fb?method=download&shareKey=218b9326659208ec564b9fff3ea8c6c3" width = "800" style="margin:0px auto;display:block;" alt="图片名称" align=center />
## 未来计划推出功能
1. 可视化JSON编辑器可定义JSON_Schema和mockjs
2. 支持HTTP和RPC协议
3. 自动化测试
4. 多人协作

View File

@ -2,7 +2,7 @@ import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { Route, HashRouter, Redirect, Switch } from 'react-router-dom';
import { Home, Group, Project, News, AddInterface, Follows } from './containers/index';
import { Home, Group, Project, News, AddInterface, Follows, AddProject } from './containers/index';
import User from './containers/User/User.js';
import Header from './components/Header/Header';
import Footer from './components/Footer/Footer';
@ -66,6 +66,7 @@ export default class App extends Component {
<Route path="/news" component={requireAuthentication(News)} />
<Route path="/add-interface" component={requireAuthentication(AddInterface)} />
<Route path="/follow" component={requireAuthentication(Follows)} />
<Route path="/add-project" component={requireAuthentication(AddProject)} />
</div>
<Footer />
</div>

View File

@ -44,7 +44,7 @@ const ToolUser = (props)=> (
<li className="toolbar-li item-search">
<Srch groupList={props.groupList}/>
</li>
<Link to="/">
<Link to="/add-project">
<Tooltip placement="bottom" title={'新建项目'}>
<li className="toolbar-li">
<Icon className="dropdown-link" style={{ fontSize: 16 }} type="plus-circle" />

View File

@ -0,0 +1,274 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Button, Form, Input, Icon, Tooltip, Select, message } from 'antd';
import { addProject, fetchProjectList, delProject, changeUpdateModal, changeTableLoading } from '../../../reducer/modules/project';
// import { Link } from 'react-router-dom'
// import variable from '../../../constants/variable';
// import common from '../../../common';
import { autobind } from 'core-decorators';
const { TextArea } = Input;
const FormItem = Form.Item;
const Option = Select.Option;
import './Addproject.scss'
// 确认删除项目 handleDelete, currGroup._id, fetchProjectList
// const deleteConfirm = (id, props) => {
// const { delProject, currGroup, fetchProjectList } = props;
// const handle = () => {
// delProject(id).then((res) => {
// if (res.payload.data.errcode == 0) {
// message.success('删除成功!')
// fetchProjectList(currGroup._id).then(() => {
// });
// } else {
// message.error(res.payload.data.errmsg);
// }
// });
// }
// return handle;
// };
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 6 }
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 14 }
}
};
@connect(
state => {
return {
projectList: state.project.projectList,
userInfo: state.project.userInfo,
tableLoading: state.project.tableLoading,
currGroup: state.group.currGroup,
total: state.project.total,
currPage: state.project.currPage
}
},
{
fetchProjectList,
addProject,
delProject,
changeUpdateModal,
changeTableLoading
}
)
class ProjectList extends Component {
constructor(props) {
super(props);
this.state = {
visible: false,
protocol: 'http:\/\/',
projectData: []
}
}
static propTypes = {
form: PropTypes.object,
fetchProjectList: PropTypes.func,
addProject: PropTypes.func,
delProject: PropTypes.func,
changeUpdateModal: PropTypes.func,
changeTableLoading: PropTypes.func,
projectList: PropTypes.array,
userInfo: PropTypes.object,
tableLoading: PropTypes.bool,
currGroup: PropTypes.object,
total: PropTypes.number,
currPage: PropTypes.number
}
// 显示模态框 - 创建项目
@autobind
showAddProjectModal() {
this.setState({
visible: true
});
}
// 确认添加项目
@autobind
handleOk(e) {
const { form, currGroup, changeTableLoading, addProject, fetchProjectList } = this.props;
const that = this;
e.preventDefault();
form.validateFields((err, values) => {
if (!err) {
values.protocol = this.state.protocol.split(':')[0];
// 获取当前分组id传入values
values.group_id = currGroup._id;
changeTableLoading(true);
addProject(values).then((res) => {
// 添加项目成功后再次请求列表
if (res.payload.data.errcode == 0) {
that.setState({
visible: false
});
form.resetFields();
message.success('创建成功! ');
fetchProjectList(currGroup._id, this.props.currPage).then(() => {
changeTableLoading(false);
});
} else {
changeTableLoading(false);
message.error(res.payload.data.errmsg);
}
}).catch(() => {
changeTableLoading(false);
});
}
});
}
// 取消修改
@autobind
handleCancel() {
this.props.form.resetFields();
this.setState({
visible: false
});
}
// 修改线上域名的协议类型 (http/https)
@autobind
protocolChange(value) {
this.setState({
protocol: value
})
}
// 分页逻辑
@autobind
paginationChange(pageNum) {
this.props.fetchProjectList(this.props.currGroup._id, pageNum).then((res) => {
if (res.payload.data.errcode) {
message.error(res.payload.data.errmsg);
} else {
this.props.changeTableLoading(false);
}
});
}
componentWillReceiveProps(nextProps) {
// 切换分组
if (this.props.currGroup !== nextProps.currGroup) {
if (nextProps.currGroup._id) {
this.props.fetchProjectList(nextProps.currGroup._id, this.props.currPage).then((res) => {
if (res.payload.data.errcode) {
message.error(res.payload.data.errmsg);
} else {
this.props.changeTableLoading(false);
}
});
} else {
// 无分组的时候停止loading状态
this.props.changeTableLoading(false);
}
}
// 切换项目列表
if (this.props.projectList !== nextProps.projectList) {
// console.log(nextProps.projectList);
const data = nextProps.projectList.map((item, index) => {
item.key = index;
return item;
});
this.setState({
projectData: data
});
}
}
render() {
const { getFieldDecorator } = this.props.form;
return (
<div className="g-row m-container">
<Form>
<FormItem
{...formItemLayout}
label="项目名称"
>
{getFieldDecorator('name', {
rules: [{
required: true, message: '请输入项目名称!'
}]
})(
<Input />
)}
</FormItem>
<FormItem
{...formItemLayout}
label={(
<span>
线上域名&nbsp;
<Tooltip title="将根据配置的线上域名访问mock数据">
<Icon type="question-circle-o" />
</Tooltip>
</span>
)}
>
{getFieldDecorator('prd_host', {
rules: [{
required: true,
message: '请输入项目线上域名!'
}]
})(
<Input addonBefore={(
<Select defaultValue="http://" onChange={this.protocolChange}>
<Option value="http://">{'http:\/\/'}</Option>
<Option value="https://">{'https:\/\/'}</Option>
</Select>)} />
)}
</FormItem>
<FormItem
{...formItemLayout}
label={(
<span>
基本路径&nbsp;
<Tooltip title="基本路径为空是根路径">
<Icon type="question-circle-o" />
</Tooltip>
</span>
)}
>
{getFieldDecorator('basepath', {
rules: [{
required: false, message: '请输入项目基本路径'
}]
})(
<Input />
)}
</FormItem>
<FormItem
{...formItemLayout}
label="描述"
>
{getFieldDecorator('desc', {
rules: [{
required: false, message: '请输入描述!'
}]
})(
<TextArea rows={4} />
)}
</FormItem>
</Form>
<Button className="m-btn" icon="plus" type="primary"
onClick={this.showAddProjectModal}
disabled={this.props.currGroup._id ? false : true}>创建项目</Button>
</div>
);
}
}
export default Form.create()(ProjectList);

View File

@ -0,0 +1,6 @@
@import '../../../styles/common.scss';
.m-container {
margin: .24rem auto !important;
padding: .24rem !important;
}

View File

@ -8,6 +8,7 @@ import News from './News/News.js'
import AddInterface from './AddInterface/AddInterface.js'
import DevTools from './DevTools/DevTools.js'
import Follows from './Follows/Follows.js'
import AddProject from './Project/AddProject/AddProject.js'
export {
Header,
@ -19,5 +20,6 @@ export {
AddInterface,
News,
DevTools,
Follows
Follows,
AddProject
}

View File

@ -17,7 +17,8 @@ const initialState = {
userInfo: {},
tableLoading: true,
total: 0,
currPage: 1
currPage: 1,
curProject: {}
};
export default (state = initialState, action) => {
@ -94,7 +95,6 @@ export function addProject(data) {
};
return {
type: PROJECT_ADD,
// payload 可以返回 Promise异步请求使用 axios 即可
payload: axios.post('/project/add', param)
};
}
@ -112,7 +112,6 @@ export function updateProject(data) {
};
return {
type: PROJECT_UPDATE,
// payload 可以返回 Promise异步请求使用 axios 即可
payload: axios.post('/project/up', param)
};
}
@ -121,7 +120,6 @@ export function delProject(id) {
const param = { id };
return {
type: PROJECT_DEL,
// payload 可以返回 Promise异步请求使用 axios 即可
payload: axios.post('/project/del', param)
};
}

View File

@ -61,6 +61,7 @@ em {
padding: 0 .24rem;
}
.nav-tooltip {
color: red;
.m-container {
margin: .24rem auto;
padding: .24rem;
}

View File

@ -19,6 +19,7 @@ class baseController {
async init(ctx) {
this.$user = null;
console.log(111111)
let ignoreRouter = [
'/user/login_by_token',
'/user/login',
@ -26,7 +27,7 @@ class baseController {
'/user/status',
'/user/logout'
];
if (ignoreRouter.indexOf(ctx.path) > -1) {
if (ignoreRouter.indexOf(ctx.path) > -1) {
this.$auth = true;
} else {
await this.checkLogin(ctx);
@ -61,10 +62,14 @@ class baseController {
}
}
/**
*
* @param {*} ctx
*/
async getLoginStatus(ctx) {
if (await this.checkLogin(ctx) === true) {
let result = yapi.commons.fieldSelect(this.$user, ['_id', 'username', 'email', 'up_time', 'add_time', 'role']);
let result = yapi.commons.fieldSelect(this.$user, ['_id', 'username', 'email', 'up_time', 'add_time', 'role', 'type']);
result.server_ip = yapi.WEBCONFIG.server_ip;
return ctx.body = yapi.commons.resReturn(result);
}

View File

@ -525,30 +525,60 @@ class projectController extends baseController {
* @param {String} project_id
*/
async download(ctx) {
const project_id = ctx.request.query.project_id;
let interfaceInst = yapi.getInst(interfaceModel);
let count = await interfaceInst.list(project_id);
console.log(count);
const arr = JSON.stringify(count.map(function(item) {
// 返回的json模板数据: item.res_body
const mockData = Mock.mock(
yapi.commons.json_parse(item.res_body)
);
return {
path: item.path,
mock: mockData
const project_id = ctx.request.query.project_id;
let interfaceInst = yapi.getInst(interfaceModel);
// 根据 project_id 获取接口数据
let count = await interfaceInst.list(project_id);
if (!project_id) {
return ctx.body = yapi.commons.resReturn(null, 405, '项目id不能为空');
} else if (!count) {
return ctx.body = yapi.commons.resReturn(null, 401, '项目id不存在');
}
}));
// console.log(arr);
const fileName = 'mock.js';
ctx.attachment(fileName);
await send(ctx, fileName, { root: __dirname + '/public' });
const arr = JSON.stringify(count.map(function(item) {
// 返回的json模板数据: item.res_body
const mockData = Mock.mock(
yapi.commons.json_parse(item.res_body)
);
return {
path: item.path,
mock: mockData
}
}));
const res = `
var data = ${arr}`
.trim();
return ctx.body = res;
const fileName = 'mock.js';
ctx.attachment(fileName);
await send(ctx, fileName, { root: __dirname + '/public' });
const res = `
var Mock = require('mockjs');
var xhook = require('xhook');
var data = ${arr};
function run() {
xhook.before(function(request, callback) {
setTimeout(function() {
var res;
data.forEach((item) => {
// 请求的接口在 data 中存在
if(request.url === item.path) {
res = {
status: 200,
text: Mock.mock(item.mock)
}
}
});
if (res) {
callback(res);
}else {
callback({ status: 405, text: '接口不存在' });
}
}, 500);
});
}
module.exports = run;`
.trim();
return ctx.body = res;
}
}

View File

@ -54,8 +54,8 @@ class userController extends baseController {
email: result.email,
add_time: result.add_time,
up_time: result.up_time,
server_ip: yapi.WEBCONFIG.server_ip
server_ip: yapi.WEBCONFIG.server_ip,
type: 'site'
}, 0, 'logout success...');
} else {
return ctx.body = yapi.commons.resReturn(null, 405, '密码错误');
@ -555,26 +555,24 @@ class userController extends baseController {
}
/**
* 根据路由id获取面包屑数据
* @interface /user/nav
* 根据路由id初始化项目数据
* @interface /user/project
* @method GET
* @category user
* @foldnumber 10
* @param {String} type 可选group|interface|project
* @param {Number} id
* @return {Object}
* @example ./api/user/nav.json
* @example
*/
async nav(ctx) {
async project(ctx) {
let { id, type } = ctx.request.query;
let result = {};
try {
if (type === 'interface') {
let interfaceInst = yapi.getInst(interfaceModel);
let interfaceData = await interfaceInst.get(id)
result["interface_id"] = interfaceData._id;
result["interface_name"] = interfaceData.path;
result.interface = interfaceData;
type = 'project';
id = interfaceData.project_id;
}
@ -582,17 +580,38 @@ class userController extends baseController {
if (type === 'project') {
let projectInst = yapi.getInst(projectModel);
let projectData = await projectInst.get(id);
result["project_id"] = projectData._id;
result["project_name"] = projectData.prd_host + projectData.basepath;
result.project = projectData.toObject();
let ownerAuth = await this.checkAuth(id, 'project', 'danger'), devAuth;
if(ownerAuth){
result.project.role = 'owner'
}else{
devAuth = await this.checkAuth(id, 'project', 'site');
if(devAuth){
result.project.role = 'dev'
}else{
result.project.role = 'member'
}
}
type = 'group';
id = projectData.group_id
id = projectData.group_id;
}
if (type === 'group') {
let groupInst = yapi.getInst(groupModel);
let groupData = await groupInst.get(id);
result["group_id"] = groupData._id;
result["group_name"] = groupData.group_name;
result.group = groupData.toObject();
let ownerAuth = await this.checkAuth(id, 'group', 'danger'), devAuth;
if(ownerAuth){
result.group.role = 'owner'
}else{
devAuth = await this.checkAuth(id, 'group', 'site');
if(devAuth){
result.group.role = 'dev'
}else{
result.group.role = 'member'
}
}
}
return ctx.body = yapi.commons.resReturn(result)

View File

@ -1,17 +0,0 @@
// const jwt = require('jsonwebtoken');
//检查token是否过期
module.exports = async ( ctx, next ) => {
// const authorization = ctx.get('Authorization');
// if (authorization === '') {
// ctx.throw(401, 'no token detected in http header ');
// }
// const token = authorization.split(' ')[1];
// let tokenContent;
// try {
// tokenContent = await jwt.verify(token, 'qunar'); //如果token过期或验证失败将抛出错误
// } catch (err) {
// ctx.throw(401, 'invalid token');
// }
await next();
};

View File

@ -1,6 +0,0 @@
module.exports = async (ctx, next) => {
let path = ctx.path;
console.log(path); // eslint-disable-line
console.log(ctx.hostname); // eslint-disable-line
if (next) await next();
};

View File

@ -74,7 +74,6 @@ class interfaceModel extends baseModel {
}
list(project_id) {
console.log(project_id);
return this.model.find({
project_id: project_id
})

View File

@ -145,8 +145,8 @@ const routerConfig = {
"method": "get"
},
{
"action": "nav",
"path": "nav",
"action": "project",
"path": "project",
"method": "get"
},{
"action": "avatar",

View File

@ -66,22 +66,23 @@ var baseController = function () {
switch (_context.prev = _context.next) {
case 0:
this.$user = null;
console.log(111111);
ignoreRouter = ['/user/login_by_token', '/user/login', '/user/reg', '/user/status', '/user/logout'];
if (!(ignoreRouter.indexOf(ctx.path) > -1)) {
_context.next = 6;
_context.next = 7;
break;
}
this.$auth = true;
_context.next = 8;
_context.next = 9;
break;
case 6:
_context.next = 8;
case 7:
_context.next = 9;
return this.checkLogin(ctx);
case 8:
case 9:
case 'end':
return _context.stop();
}
@ -162,6 +163,11 @@ var baseController = function () {
return checkLogin;
}()
/**
*
* @param {*} ctx
*/
}, {
key: 'getLoginStatus',
value: function () {
@ -182,7 +188,7 @@ var baseController = function () {
break;
}
result = _yapi2.default.commons.fieldSelect(this.$user, ['_id', 'username', 'email', 'up_time', 'add_time', 'role']);
result = _yapi2.default.commons.fieldSelect(this.$user, ['_id', 'username', 'email', 'up_time', 'add_time', 'role', 'type']);
result.server_ip = _yapi2.default.WEBCONFIG.server_ip;
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(result));

View File

@ -1192,13 +1192,30 @@ var projectController = function (_baseController) {
case 0:
project_id = ctx.request.query.project_id;
interfaceInst = _yapi2.default.getInst(_interface2.default);
// 根据 project_id 获取接口数据
_context12.next = 4;
return interfaceInst.list(project_id);
case 4:
count = _context12.sent;
console.log(count);
if (project_id) {
_context12.next = 9;
break;
}
return _context12.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '项目id不能为空'));
case 9:
if (count) {
_context12.next = 11;
break;
}
return _context12.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '项目id不存在'));
case 11:
arr = (0, _stringify2.default)(count.map(function (item) {
// 返回的json模板数据: item.res_body
var mockData = _mockjs2.default.mock(_yapi2.default.commons.json_parse(item.res_body));
@ -1207,19 +1224,17 @@ var projectController = function (_baseController) {
mock: mockData
};
}));
// console.log(arr);
fileName = 'mock.js';
ctx.attachment(fileName);
_context12.next = 11;
_context12.next = 16;
return send(ctx, fileName, { root: __dirname + '/public' });
case 11:
res = ('\n var data = ' + arr).trim();
case 16:
res = ('\n var Mock = require(\'mockjs\');\n var xhook = require(\'xhook\');\n var data = ' + arr + ';\n function run() {\n xhook.before(function(request, callback) {\n setTimeout(function() {\n var res;\n data.forEach((item) => {\n // \u8BF7\u6C42\u7684\u63A5\u53E3\u5728 data \u4E2D\u5B58\u5728\n if(request.url === item.path) {\n res = {\n status: 200,\n text: Mock.mock(item.mock)\n }\n }\n });\n if (res) {\n callback(res);\n }else {\n callback({ status: 405, text: \'\u63A5\u53E3\u4E0D\u5B58\u5728\' });\n }\n }, 500);\n });\n }\n module.exports = run;').trim();
return _context12.abrupt('return', ctx.body = res);
case 13:
case 18:
case 'end':
return _context12.stop();
}

View File

@ -155,8 +155,8 @@ var userController = function (_baseController) {
email: result.email,
add_time: result.add_time,
up_time: result.up_time,
server_ip: _yapi2.default.WEBCONFIG.server_ip
server_ip: _yapi2.default.WEBCONFIG.server_ip,
type: 'site'
}, 0, 'logout success...'));
case 19:
@ -1157,22 +1157,22 @@ var userController = function (_baseController) {
}()
/**
* 根据路由id获取面包屑数据
* @interface /user/nav
* 根据路由id初始化项目数据
* @interface /user/project
* @method GET
* @category user
* @foldnumber 10
* @param {String} type 可选group|interface|project
* @param {Number} id
* @return {Object}
* @example ./api/user/nav.json
* @example
*/
}, {
key: 'nav',
key: 'project',
value: function () {
var _ref14 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee14(ctx) {
var _ctx$request$query, id, type, result, interfaceInst, interfaceData, projectInst, projectData, groupInst, groupData;
var _ctx$request$query, id, type, result, interfaceInst, interfaceData, projectInst, projectData, ownerAuth, devAuth, groupInst, groupData, _ownerAuth, _devAuth;
return _regenerator2.default.wrap(function _callee14$(_context14) {
while (1) {
@ -1183,7 +1183,7 @@ var userController = function (_baseController) {
_context14.prev = 2;
if (!(type === 'interface')) {
_context14.next = 12;
_context14.next = 11;
break;
}
@ -1194,67 +1194,121 @@ var userController = function (_baseController) {
case 7:
interfaceData = _context14.sent;
result["interface_id"] = interfaceData._id;
result["interface_name"] = interfaceData.path;
result.interface = interfaceData;
type = 'project';
id = interfaceData.project_id;
case 12:
case 11:
if (!(type === 'project')) {
_context14.next = 21;
_context14.next = 31;
break;
}
projectInst = _yapi2.default.getInst(_project2.default);
_context14.next = 16;
_context14.next = 15;
return projectInst.get(id);
case 16:
case 15:
projectData = _context14.sent;
result["project_id"] = projectData._id;
result["project_name"] = projectData.prd_host + projectData.basepath;
result.project = projectData.toObject();
_context14.next = 19;
return this.checkAuth(id, 'project', 'danger');
case 19:
ownerAuth = _context14.sent;
devAuth = void 0;
if (!ownerAuth) {
_context14.next = 25;
break;
}
result.project.role = 'owner';
_context14.next = 29;
break;
case 25:
_context14.next = 27;
return this.checkAuth(id, 'project', 'site');
case 27:
devAuth = _context14.sent;
if (devAuth) {
result.project.role = 'dev';
} else {
result.project.role = 'member';
}
case 29:
type = 'group';
id = projectData.group_id;
case 21:
case 31:
if (!(type === 'group')) {
_context14.next = 28;
_context14.next = 49;
break;
}
groupInst = _yapi2.default.getInst(_group2.default);
_context14.next = 25;
_context14.next = 35;
return groupInst.get(id);
case 25:
case 35:
groupData = _context14.sent;
result["group_id"] = groupData._id;
result["group_name"] = groupData.group_name;
result.group = groupData.toObject();
_context14.next = 39;
return this.checkAuth(id, 'group', 'danger');
case 28:
case 39:
_ownerAuth = _context14.sent;
_devAuth = void 0;
if (!_ownerAuth) {
_context14.next = 45;
break;
}
result.group.role = 'owner';
_context14.next = 49;
break;
case 45:
_context14.next = 47;
return this.checkAuth(id, 'group', 'site');
case 47:
_devAuth = _context14.sent;
if (_devAuth) {
result.group.role = 'dev';
} else {
result.group.role = 'member';
}
case 49:
return _context14.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(result));
case 31:
_context14.prev = 31;
case 52:
_context14.prev = 52;
_context14.t0 = _context14['catch'](2);
return _context14.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(result, 422, _context14.t0.message));
case 34:
case 55:
case 'end':
return _context14.stop();
}
}
}, _callee14, this, [[2, 31]]);
}, _callee14, this, [[2, 52]]);
}));
function nav(_x15) {
function project(_x15) {
return _ref14.apply(this, arguments);
}
return nav;
return project;
}()
}]);
return userController;

View File

@ -117,7 +117,6 @@ var interfaceModel = function (_baseModel) {
}, {
key: 'list',
value: function list(project_id) {
console.log(project_id);
return this.model.find({
project_id: project_id
}).sort({ _id: -1 }).exec();

View File

@ -162,8 +162,8 @@ var routerConfig = {
"path": "search",
"method": "get"
}, {
"action": "nav",
"path": "nav",
"action": "project",
"path": "project",
"method": "get"
}, {
"action": "avatar",

File diff suppressed because it is too large Load Diff

View File

@ -28,6 +28,10 @@
import yapi from '../yapi.js';
import projectModel from '../models/project.js';
import userModel from '../models/user.js';
import interfaceModel from '../models/interface.js'
import groupModel from '../models/group.js'
import _ from 'underscore'
const jwt = require('jsonwebtoken');
class baseController {
@ -87,7 +91,7 @@ class baseController {
async getLoginStatus(ctx) {
if (await this.checkLogin(ctx) === true) {
let result = yapi.commons.fieldSelect(this.$user, ['_id', 'username', 'email', 'up_time', 'add_time','role']);
let result = yapi.commons.fieldSelect(this.$user, ['_id', 'username', 'email', 'up_time', 'add_time', 'role']);
result.server_ip = yapi.WEBCONFIG.server_ip;
return ctx.body = yapi.commons.resReturn(result);
}
@ -98,44 +102,77 @@ class baseController {
return this.$user.role;
}
async jungeProjectAuth(id) {
let model = yapi.getInst(projectModel);
/**
*
* @param {*} id type对应的id
* @param {*} type enum[interface, project, group]
* @param {*} action enum[ danger , edit ] danger只有owner或管理员才能操作,edit只要是dev或以上就能执行
*/
async checkAuth(id, type, action) {
let result = {};
try {
if (this.getRole() === 'admin') {
return true;
}
if (type === 'interface') {
let interfaceInst = yapi.getInst(interfaceModel);
let interfaceData = await interfaceInst.get(id)
result.interfaceData = interfaceData;
if (interfaceData.uid === this.getUid()) {
return true;
}
type = 'project';
id = interfaceData.project_id;
}
if (this.getRole() === 'admin') {
return true;
}
if (type === 'project') {
let projectInst = yapi.getInst(projectModel);
let projectData = await projectInst.get(id);
if(projectData.uid === this.getUid()){
return true;
}
let memberData = _.find(projectData.members, (m) => {
if (m.uid === this.getUid()) {
return true;
}
})
if (memberData && memberData.role) {
if(action === 'danger' && memberData.role === 'owner'){
return true;
}
if(action === 'edit'){
return true;
}
}
type = 'group';
id = projectData.group_id
}
if (type === 'group') {
let groupInst = yapi.getInst(groupModel);
let groupData = await groupInst.get(id);
let groupMemberData = _.find(groupData.members, (m) => {
if (m.uid === this.getUid()) {
return true;
}
})
if (groupMemberData && groupMemberData.role) {
if(action === 'danger' && groupMemberData.role === 'owner'){
return true;
}
if(action === 'edit'){
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) {
catch (e) {
yapi.commons.log(e.message, 'error')
return false;
}
let result = await model.checkMemberRepeat(id, member_uid);
if (result > 0) {
return true;
}
return false;
}
}

View File

@ -0,0 +1,213 @@
<!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/follow.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/follow.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';
import baseController from './base.js';
import followModel from '../models/follow';
class followController extends baseController {
constructor(ctx) {
super(ctx);
this.Model = yapi.getInst(followModel);
// try{
// var res = this.Model.save({
// uid: 107,
// projectid: 221,
// projectname: 'Flight',
// icon: 'code'
// });
// // var res = this.Model.del(107);
// ctx.body = yapi.commons.resReturn(null, 200,res);
// }catch(err){
// ctx.body = yapi.commons.resReturn(null, 402, err.message);
// }
}
/**
* 获取关注项目列表
* @interface /follow/list
* @method GET
* @category follow
* @foldnumber 10
* @param {Number} uid 用户id 不能为空
* @param {Number} [page] 分页页码
* @param {Number} [limit] 分页大小
* @returns {Object}
* @example /follow/list
*/
async list(ctx) {
let uid = ctx.request.query.uid,
page = ctx.request.query.page || 1,
limit = ctx.request.query.limit || 10;
if (!uid) {
return ctx.body = yapi.commons.resReturn(null, 400, '用户id不能为空');
}
try {
let result = await this.Model.listWithPaging(uid, page, limit);
let count = await this.Model.listCount(uid);
ctx.body = yapi.commons.resReturn({
total: Math.ceil(count / limit),
list: result
});
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 402, err.message);
}
}
/**
* 取消关注
* @interface /follow/list
* @method POST
* @category follow
* @foldnumber 10
* @param {Number} id 关注id
* @returns {Object}
* @example /follow/del
*/
async del(ctx) {
let params = ctx.request.body;
if(params.followid){
return ctx.body = yapi.commons.resReturn(null, 400, '关注id不能为空');
}
try {
let result = await this.Model.del(params.id);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 添加关注
* @interface /follow/list
* @method POST
* @category follow
* @foldnumber 10
* @param {Number} uid 用户id
* @param {Number} projectid 项目id
* @param {String} projectname 项目名
* @param {String} icon 项目icon
* @returns {Object}
* @example /follow/add
*/
async add(ctx) {
let params = ctx.request.body;
params = yapi.commons.handleParams(params, {
uid: 'number',
projectid: 'number',
projectname: 'string',
icon: 'string'
});
if (!params.uid) {
return ctx.body = yapi.commons.resReturn(null, 400, '用户id不为空');
}
if (!params.projectid) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
let checkRepeat = await this.Model.checkProjectRepeat(params.uid,params.projectid);
if (checkRepeat) {
return ctx.body = yapi.commons.resReturn(null, 401, '项目已关注');
}
if (!params.projectname) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目名不能为空');
}
if (!params.icon) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目图标标志不能为空');
}
let data = {
uid: params.uid,
projectid: params.projectid,
projectname: params.projectname,
icon: params.icon
};
try {
let result = await this.Model.save(data);
result = yapi.commons.fieldSelect(result, ['_id', 'uid', 'projectid', 'projectname', 'icon']);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
}
module.exports = followController;
</pre>
</div>
</div>
</div>
</div>
<!-- <div class="docs-header" id="content" tabindex="-1">
<div class="container">
<h1>YApi : ./server/controllers/follow.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>

View File

@ -29,6 +29,7 @@
import yapi from '../yapi.js';
import baseController from './base.js';
import projectModel from '../models/project.js';
import userModel from '../models/user.js';
class groupController extends baseController {
constructor(ctx) {
@ -42,7 +43,8 @@ class groupController extends baseController {
* @category group
* @foldnumber 10
* @param {String} group_name 项目分组名称,不能为空
* @param {String} [group_desc] 项目分组描述
* @param {String} [group_desc] 项目分组描述
* @param {String} owner_uid 组长uid
* @returns {Object}
* @example ./api/group/add.json
*/
@ -51,7 +53,8 @@ class groupController extends baseController {
params = yapi.commons.handleParams(params, {
group_name: 'string',
group_desc: 'string'
group_desc: 'string',
owner_uid: 'number'
});
if (this.getRole() !== 'admin') {
@ -62,6 +65,14 @@ class groupController extends baseController {
return ctx.body = yapi.commons.resReturn(null, 400, '项目分组名不能为空');
}
if(!params.owner_uid){
return ctx.body = yapi.commons.resReturn(null, 400, '项目分组必须添加一个组长');
}
let groupUserdata = await this.getUserdata(params.owner_uid, 'owner');
if(groupUserdata === null){
return ctx.body = yapi.commons.resReturn(null, 400, '组长uid不存在')
}
let groupInst = yapi.getInst(groupModel);
let checkRepeat = await groupInst.checkRepeat(params.group_name);
@ -75,13 +86,14 @@ class groupController extends baseController {
group_desc: params.group_desc,
uid: this.getUid(),
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
up_time: yapi.commons.time(),
members: [groupUserdata]
};
try {
let result = await groupInst.save(data);
result = yapi.commons.fieldSelect(result, ['_id', 'group_name', 'group_desc', 'uid']);
result = yapi.commons.fieldSelect(result, ['_id', 'group_name', 'group_desc', 'uid', 'members']);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
@ -89,6 +101,114 @@ class groupController extends baseController {
}
async getUserdata(uid, role){
role = role || 'dev';
let userInst = yapi.getInst(userModel);
let userData = await userInst.findById(uid);
if(!userData){
return null;
}
return {
role: role,
uid: userData._id,
username: userData.username,
email: userData.email
}
}
async addMember(ctx){
let params = ctx.request.body;
let groupInst = yapi.getInst(groupModel);
if (!params.member_uid) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组成员uid不能为空');
}
if (!params.id) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组id不能为空');
}
var check = await groupInst.checkMemberRepeat(params.id, params.member_uid);
if (check > 0) {
return ctx.body = yapi.commons.resReturn(null, 400, '成员已存在');
}
let groupUserdata = await this.getUserdata(params.member_uid);
if(groupUserdata === null){
return ctx.body = yapi.commons.resReturn(null, 400, '组长uid不存在')
}
try {
let result = await groupInst.addMember(params.id, groupUserdata);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
async changeMemberRole(ctx){
let params = ctx.request.body;
let groupInst = yapi.getInst(groupModel);
if (!params.member_uid) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组成员uid不能为空');
}
if (!params.id) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组id不能为空');
}
var check = await groupInst.checkMemberRepeat(params.id, params.member_uid);
if (check === 0) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组成员不存在');
}
if (await this.checkAuth(id, 'group', 'danger') !== true) {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
params.role = params.role === 'owner' ? 'owner' : 'dev';
try {
let result = await groupInst.changeMemberRole(params.id, params.member_uid, params.role);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
async getMemberList(ctx) {
let params = ctx.request.query;
if (!params.id) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
try {
let groupInst = yapi.getInst(groupModel);
let group = await groupInst.get(params.id);
ctx.body = yapi.commons.resReturn(group.members);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
async delMember(ctx) {
let params = ctx.request.body;
let groupInst = yapi.getInst(groupModel);
if (!params.member_uid) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组成员uid不能为空');
}
if (!params.id) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组id不能为空');
}
var check = await groupInst.checkMemberRepeat(params.id, params.member_uid);
if (check === 0) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组成员不存在');
}
if (await this.checkAuth(id, 'group', 'danger') !== true) {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
try {
let result = await groupInst.delMember(params.id, params.member_uid);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 获取项目分组列表
* @interface /group/list
@ -158,12 +278,10 @@ class groupController extends baseController {
* @example ./api/group/up.json
*/
async up(ctx) {
if (this.getRole() !== 'admin') {
return ctx.body = yapi.commons.resReturn(null, 401, '没有权限');
if (await this.checkAuth(id, 'group', 'danger') !== true) {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
try {
ctx.request.body = yapi.commons.handleParams(ctx.request.body, {
id: 'number',
group_name: 'string',

View File

@ -0,0 +1,376 @@
<!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/interfaceCol.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/interfaceCol.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 interfaceColModel from '../models/interfaceCol.js';
import interfaceCaseModel from '../models/interfaceCase.js';
import baseController from './base.js';
import yapi from '../yapi.js';
class interfaceColController extends baseController{
constructor(ctx) {
super(ctx);
this.colModel = yapi.getInst(interfaceColModel);
this.caseModel = yapi.getInst(interfaceCaseModel);
}
/**
* 获取所有接口集
* @interface /col/list
* @method GET
* @category col
* @foldnumber 10
* @param {String} project_id email名称不能为空
* @returns {Object}
* @example
*/
async list(ctx){
try {
let id = ctx.query.project_id;
let inst = this.colModel(interfaceColModel);
let result = await inst.list(id);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 增加接口集
* @interface /col/add_col
* @method POST
* @category col
* @foldnumber 10
* @param {Number} project_id
* @param {String} name
* @param {String} desc
* @returns {Object}
* @example
*/
async addCol(ctx){
try{
let params = ctx.request.body;
params = yapi.commons.handleParams(params, {
name: 'string',
project_id: 'number',
desc: 'string'
});
if (!params.project_id) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
if(!params.name){
return ctx.body = yapi.commons.resReturn(null, 400, '名称不能为空');
}
let result = await this.colModel.save({
name: params.name,
project_id: params.project_id,
desc: params.desc,
uid: this.getUid(),
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
})
ctx.body = yapi.commons.resReturn(result);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 获取一个接口集下的所有的接口用例
* @interface /col/case_list
* @method GET
* @category col
* @foldnumber 10
* @param {String} col_id 接口集id
* @returns {Object}
* @example
*/
async getCaseList(ctx){
try {
let id = ctx.query.col_id;
let inst = yapi.getInst(interfaceCaseModel);
let result = await inst.list(id);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 增加一个接口用例
* @interface /col/add_case
* @method POST
* @category col
* @foldnumber 10
* @param {String} casename
* @param {Number} col_id
* @param {Number} project_id
* @param {String} env
* @param {String} domain
* @param {String} path
* @param {String} method
* @param {Object} req_query
* @param {Object} req_headers
* @param {String} req_body_type
* @param {Array} req_body_form
* @param {String} req_body_other
* @returns {Object}
* @example
*/
async addCase(ctx){
try{
let params = ctx.request.body;
params = yapi.commons.handleParams(params, {
casename: 'string',
project_id: 'number',
col_id: 'number',
env: 'string',
domain: 'string',
method: 'string'
});
if (!params.project_id) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
if (!params.col_id) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
if (!params.env) {
return ctx.body = yapi.commons.resReturn(null, 400, '缺少环境配置');
}
if (!params.path) {
return ctx.body = yapi.commons.resReturn(null, 400, 'path 不能为空');
}
if(!params.casename){
return ctx.body = yapi.commons.resReturn(null, 400, '用例名称不能为空');
}
params.uid = this.getUid();
params.index = 0;
params.add_time = yapi.commons.time();
params.up_time = yapi.commons.time();
let result = await this.caseModel.save(params);
ctx.body = yapi.commons.resReturn(result);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 获取一个接口用例详情
* @interface /col/case
* @method GET
* @category col
* @foldnumber 10
* @param {String} caseid
* @returns {Object}
* @example
*/
async getCase(ctx){
try{
let id = ctx.query.caseid;
let result = await this.caseModel.get(id);
ctx.body = yapi.commons.resReturn(result);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 400, e.message)
}
}
/**
* 更新一个接口集name或描述
* @interface /col/up_col
* @method POST
* @category col
* @foldnumber 10
* @param {String} name
* @param {String} desc
* @returns {Object}
* @example
*/
async upCol(ctx){
try{
let params = ctx.request.body;
let result = await this.caseModel.up(params.col_id, {
name: params.col_name,
desc: params.col_desc,
up_time: yapi.commons.time()
})
ctx.body = yapi.commons.resReturn(result)
}catch(e){
ctx.body = yapi.commons.resReturn(null, 400, e.message)
}
}
/**
* 更新多个接口case index
* @interface /col/up_col_index
* @method POST
* @category col
* @foldnumber 10
* @param {Array} [id, index]
* @returns {Object}
* @example
*/
async upCaseIndex(ctx){
try{
let params = ctx.request.body;
if(!params || !Array.isArray(params)){
ctx.body = yapi.commons.resReturn(null, 400, "请求参数必须是数组")
}
params.forEach((item) => {
if(item.id && item.index){
this.caseModel.upCaseIndex(item.id, item.index).then((res) => {}, (err) => {
yapi.commons.log(err.message, 'error')
})
}
})
return ctx.body = yapi.commons.resReturn('success')
}catch(e){
ctx.body = yapi.commons.resReturn(null, 400, e.message)
}
}
/**
* 删除一个接口集
* @interface /col/del_col
* @method GET
* @category col
* @foldnumber 10
* @param {String}
* @returns {Object}
* @example
*/
async delCol(ctx){
try{
let id = ctx.request.body.colid;
let colData = await this.colModel.get(id);
if(!colData){
ctx.body = yapi.commons.resReturn(null, 400, "不存在的id")
}
if(colData.uid !== this.getUid()){
let auth = await this.checkAuth(colData.project_id, 'project', 'danger')
if(!auth){
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
}
}
let result = await this.colModel.del(caseid);
return ctx.body = yapi.commons.resReturn(result);
}catch(e){
yapi.commons.resReturn(null, 400, e.message)
}
}
/**
*
* @param {*} ctx
*/
async delCase(ctx){
try{
let caseid = ctx.request.body.caseid;
let caseData = await this.caseModel.get(caseid);
if(!caseData){
ctx.body = yapi.commons.resReturn(null, 400, "不存在的caseid")
}
if(caseData.uid !== this.getUid()){
let auth = await this.checkAuth(caseData.project_id, 'project', 'danger')
if(!auth){
return ctx.body = yapi.commons.resReturn(null, 400, '没有权限');
}
}
let result = await this.caseModel.del(caseid);
return ctx.body = yapi.commons.resReturn(result);
}catch(e){
yapi.commons.resReturn(null, 400, e.message)
}
}
}
module.exports = interfaceColController
</pre>
</div>
</div>
</div>
</div>
<!-- <div class="docs-header" id="content" tabindex="-1">
<div class="container">
<h1>YApi : ./server/controllers/interfaceCol.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>

View File

@ -35,33 +35,49 @@ class logController extends baseController {
super(ctx);
this.Model = yapi.getInst(logModel);
this.groupModel = yapi.getInst(groupModel);
try{
// var res = this.Model.save({
// uid: 107,
// typeid: 21,
// type: 'project',
// username: '小明明宝宝',
// content: '小明应该修改了的项目宝宝',
// time: yapi.commons.time()
// });
// var res = this.Model.del(107);
// ctx.body = yapi.commons.resReturn(null, 200,res);
}catch(err){
// ctx.body = yapi.commons.resReturn(null, 402, err.message);
}
}
/**
* 获取节点列表
* @interface /node/list
* 获取动态列表
* @interface /log/list
* @method GET
* @category node
* @category log
* @foldnumber 10
* @param {Number} uid 用户id 不能为空
* @param {Number} [page] 分页页码
* @param {Number} [limit] 分页大小
* @returns {Object}
* @example ./api/project/list.json
* @example /log/list
*/
async list(ctx) {
let uid = ctx.request.query.uid,
let typeid = ctx.request.query.typeid,
page = ctx.request.query.page || 1,
limit = ctx.request.query.limit || 10;
if (!uid) {
return ctx.body = yapi.commons.resReturn(null, 400, '用户id不能为空');
if (!typeid) {
return ctx.body = yapi.commons.resReturn(null, 400, 'typeid不能为空');
}
try {
let result = await this.Model.listWithPaging(uid, page, limit);
let count = await this.Model.listCount(uid);
let result = await this.Model.listWithPaging(typeid, page, limit);
let count = await this.Model.listCount(typeid);
ctx.body = yapi.commons.resReturn({
total: Math.ceil(count / limit),

View File

@ -32,6 +32,8 @@ import interfaceModel from '../models/interface.js';
import groupModel from '../models/group';
import commons from '../utils/commons.js';
import userModel from '../models/user.js';
import Mock from 'mockjs';
const send = require('koa-send');
class projectController extends baseController {
@ -85,6 +87,11 @@ class projectController extends baseController {
group_id: 'number',
desc: 'string'
});
if (await this.checkAuth(params.group_id, 'group', 'edit') !== true) {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
if (!params.group_id) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目分组id不能为空');
}
@ -99,7 +106,7 @@ class projectController extends baseController {
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的项目名');
}
if (!params.prd_host) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目domain不能为空');
}
@ -120,13 +127,16 @@ class projectController extends baseController {
return ctx.body = yapi.commons.resReturn(null, 401, '已存在domain和basepath');
}
let data = {
name: params.name,
desc: params.desc,
prd_host: params.prd_host,
basepath: params.basepath,
protocol: params.protocol || 'http',
members: [this.getUid()],
members: [],
project_type: params.project_type || 'private',
uid: this.getUid(),
group_id: params.group_id,
add_time: yapi.commons.time(),
@ -161,12 +171,23 @@ class projectController extends baseController {
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
if (await this.checkAuth(params.id, 'project', 'edit') !== true) {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
var check = await this.Model.checkMemberRepeat(params.id, params.member_uid);
if (check > 0) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目成员已存在');
}
let userdata = await this.getUserdata(params.member_uid);
if(userdata === null){
return ctx.body = yapi.commons.resReturn(null, 400, '成员uid不存在')
}
try {
let result = await this.Model.addMember(params.id, params.member_uid);
let result = await this.Model.addMember(params.id, userdata);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
@ -198,6 +219,10 @@ class projectController extends baseController {
return ctx.body = yapi.commons.resReturn(null, 400, '项目成员不存在');
}
if (await this.checkAuth(params.id, 'project', 'danger') !== true) {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
try {
let result = await this.Model.delMember(params.id, params.member_uid);
ctx.body = yapi.commons.resReturn(result);
@ -206,6 +231,22 @@ class projectController extends baseController {
}
}
async getUserdata(uid, role){
role = role || 'dev';
let userInst = yapi.getInst(userModel);
let userData = await userInst.findById(uid);
if(!userData){
return null;
}
return {
role: role,
uid: userData._id,
username: userData.username,
email: userData.email
}
}
/**
* 获取项目成员列表
* @interface /project/get_member_list
@ -225,10 +266,7 @@ class projectController extends baseController {
try {
let project = await this.Model.get(params.id);
let userInst = yapi.getInst(userModel);
let result = await userInst.findByUids(project.members);
ctx.body = yapi.commons.resReturn(result);
ctx.body = yapi.commons.resReturn(project.members);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
@ -265,24 +303,20 @@ class projectController extends baseController {
* @category project
* @foldnumber 10
* @param {Number} group_id 项目group_id不能为空
* @param {Number} [page] 分页页码
* @param {Number} [limit] 每页数据条目默认为10
* @returns {Object}
* @example ./api/project/list.json
*/
async list(ctx) {
let group_id = ctx.request.query.group_id,
page = ctx.request.query.page || 1,
limit = ctx.request.query.limit || 10;
let group_id = ctx.request.query.group_id
if (!group_id) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目分组id不能为空');
}
let auth =await this.checkAuth(group_id, 'group', 'edit')
try {
let result = await this.Model.listWithPaging(group_id, page, limit);
let count = await this.Model.listCount(group_id);
let result = await this.Model.list(group_id, auth);
let uids = [];
result.forEach((item) => {
if (uids.indexOf(item.uid) === -1) {
@ -295,7 +329,6 @@ class projectController extends baseController {
_users[item._id] = item;
});
ctx.body = yapi.commons.resReturn({
total: Math.ceil(count / limit),
list: result,
userinfo: _users
});
@ -327,7 +360,7 @@ class projectController extends baseController {
return ctx.body = yapi.commons.resReturn(null, 400, '请先删除该项目下所有接口');
}
if (await this.jungeProjectAuth(id) !== true) {
if (await this.checkAuth(id, 'project', 'danger') !== true) {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
let result = await this.Model.del(id);
@ -337,6 +370,33 @@ class projectController extends baseController {
}
}
async changeMemberRole(ctx){
let params = ctx.request.body;
let groupInst = yapi.getInst(groupModel);
if (!params.member_uid) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组成员uid不能为空');
}
if (!params.id) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组id不能为空');
}
var check = await groupInst.checkMemberRepeat(params.id, params.member_uid);
if (check === 0) {
return ctx.body = yapi.commons.resReturn(null, 400, '分组成员不存在');
}
if (await this.checkAuth(id, 'group', 'danger') !== true) {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
params.role = params.role === 'owner' ? 'owner' : 'dev';
try {
let result = await groupInst.changeMemberRole(params.id, params.member_uid, params.role);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 编辑项目
* @interface /project/up
@ -372,7 +432,7 @@ class projectController extends baseController {
return ctx.body = yapi.commons.resReturn(null, 405, '项目id不能为空');
}
if (await this.jungeMemberAuth(id, this.getUid()) !== true) {
if (await this.checkAuth(id, 'project', 'edit') !== true) {
return ctx.body = yapi.commons.resReturn(null, 405, '没有权限');
}
@ -409,7 +469,6 @@ class projectController extends baseController {
}
let data = {
uid: this.getUid(),
up_time: yapi.commons.time()
};
@ -483,6 +542,71 @@ class projectController extends baseController {
return ctx.body = yapi.commons.resReturn(queryList, 0, 'ok');
}
/**
* 下载项目的 Mock 数据
* @interface /project/download
* @method GET
* @category project
* @foldnumber 10
* @param {String} project_id
*/
async download(ctx) {
const project_id = ctx.request.query.project_id;
let interfaceInst = yapi.getInst(interfaceModel);
// 根据 project_id 获取接口数据
let count = await interfaceInst.list(project_id);
if (!project_id) {
return ctx.body = yapi.commons.resReturn(null, 405, '项目id不能为空');
} else if (!count) {
return ctx.body = yapi.commons.resReturn(null, 401, '项目id不存在');
}
const arr = JSON.stringify(count.map(function(item) {
// 返回的json模板数据: item.res_body
const mockData = Mock.mock(
yapi.commons.json_parse(item.res_body)
);
return {
path: item.path,
mock: mockData
}
}));
const fileName = 'mock.js';
ctx.attachment(fileName);
await send(ctx, fileName, { root: __dirname + '/public' });
const res = `
var Mock = require('mockjs');
var xhook = require('xhook');
var data = ${arr};
function run() {
xhook.before(function(request, callback) {
setTimeout(function() {
var res;
data.forEach((item) => {
// 请求的接口在 data 中存在
if(request.url === item.path) {
res = {
status: 200,
text: Mock.mock(item.mock)
}
}
});
if (res) {
callback(res);
}else {
callback({ status: 405, text: '接口不存在' });
}
}, 500);
});
}
module.exports = run;`;
.trim();
return ctx.body = res;
}
}
module.exports = projectController;

View File

@ -34,6 +34,7 @@ import common from '../utils/commons.js';
import interfaceModel from '../models/interface.js'
import groupModel from '../models/group.js'
import projectModel from '../models/project.js'
import avatarModel from '../models/avatar.js'
const jwt = require('jsonwebtoken');
@ -168,12 +169,13 @@ class userController extends baseController {
passsalt: passsalt,
role: 'member',
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
up_time: yapi.commons.time(),
type: 'third'
};
user = await userInst.save(data);
yapi.commons.sendMail({
to: email,
contents: `<h3>亲爱的用户:</h3><p>您好感谢使用YApi,系统检测您是第一次用Qsso账号登录YApi服务,您的Email是 ${email} ,初始化密码为:${passsalt}</p>`
contents: `<h3>亲爱的用户:</h3><p>您好感谢使用YApi平台.</p>`
});
}
@ -237,10 +239,6 @@ class userController extends baseController {
}
}
async forgetPassword() { }
async resetPassword() { }
setLoginCookie(uid, passsalt) {
let token = jwt.sign({ uid: uid }, passsalt, { expiresIn: '7 days' });
@ -298,7 +296,8 @@ class userController extends baseController {
passsalt: passsalt,
role: 'member',
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
up_time: yapi.commons.time(),
type: "site"
};
if (!data.username) {
@ -460,10 +459,6 @@ class userController extends baseController {
up_time: yapi.commons.time()
};
if (this.getRole() === 'admin') {
params.role && (data.role = params.role);
}
params.username && (data.username = params.username);
params.email && (data.email = params.email);
@ -482,6 +477,65 @@ class userController extends baseController {
}
}
/**
*
* @param {*} basecode base64编码通过h5 api传给后端
*/
async uploadAvatar(ctx) {
try {
let basecode = ctx.request.body.basecode;
if(!basecode){
return ctx.body = yapi.commons.resReturn(null, 400, 'basecode不能为空')
}
let pngPrefix = 'data:image/png;base64,';
let jpegPrefix = 'data:image/jpeg;base64,';
let type;
if(basecode.substr(0, pngPrefix.length ) === pngPrefix){
basecode = basecode.substr(pngPrefix.length);
type = 'image/png';
}else if(basecode.substr(0, jpegPrefix.length ) === jpegPrefix){
basecode = basecode.substr(jpegPrefix.length);
type = 'image/jpeg';
}else{
return ctx.body = yapi.commons.resReturn(null, 400, '仅支持jpeg和png格式的图片')
}
let strLength = basecode.length;
if(parseInt(strLength-(strLength/8)*2) > 200000){
return ctx.body = yapi.commons.resReturn(null, 400, '图片大小不能超过200kb');
}
let avatarInst = yapi.getInst(avatarModel);
let result = await avatarInst.up(this.getUid(), basecode, type)
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 401, e.message);
}
}
async avatar(ctx) {
try{
let avatarInst = yapi.getInst(avatarModel);
let data = await avatarInst.get(this.getUid());
let dataBuffer, type;
if(!data || !data.basecode){
dataBuffer = yapi.fs.readFileSync(yapi.path.join(yapi.WEBROOT, 'static/image/avatar.png'));
type = 'image/png'
}else{
type = data.type;
dataBuffer = new Buffer(data.basecode, 'base64');
}
ctx.set('Content-type', type);
ctx.body = dataBuffer;
}catch(err){
ctx.body = 'error:' + err.message
}
}
/**
* 模糊搜索用户名或者email
* @interface /user/search
@ -523,7 +577,6 @@ class userController extends baseController {
];
let filteredRes = common.filterRes(queryList, rules);
console.log(queryList); // eslint-disable-line
return ctx.body = yapi.commons.resReturn(filteredRes, 0, 'ok');
}
@ -548,6 +601,7 @@ class userController extends baseController {
let interfaceData = await interfaceInst.get(id)
result["interface_id"] = interfaceData._id;
result["interface_name"] = interfaceData.path;
type = 'project';
id = interfaceData.project_id;
}

95
ykit.js
View File

@ -6,6 +6,67 @@ var assetsPluginInstance = new AssetsPlugin({
return 'window.WEBPACK_ASSETS = ' + JSON.stringify(assets);
}
})
function handleCommonsChunk(webpackConfig) {
var commonsChunk = {
//filename: 'scripts/[name]@[chunkhash][ext]',
vendors: {
lib: ['react', 'redux',
'redux-thunk',
'react-dom',
'redux-promise',
'react-router-dom',
'prop-types'
],
lib2: [
'axios',
'moment'
]
}
},
chunks = [],
filenameTpl = webpackConfig.output[this.env],
vendors;
if (typeof commonsChunk === 'object' && commonsChunk !== undefined) {
if (typeof commonsChunk.name === 'string' && commonsChunk) {
chunks.push(commonsChunk.name);
}
vendors = commonsChunk.vendors;
if (typeof vendors === 'object' && vendors !== undefined) {
var i = 0;
for (var name in vendors) {
if (vendors.hasOwnProperty(name) && vendors[name]) {
i++;
chunks.push(name);
webpackConfig.entry[name] = Array.isArray(vendors[name]) ? vendors[name] : [vendors[name]];
}
}
if (i > 0) {
chunks.push('manifest');
}
}
if (chunks.length > 0) {
let chunkFilename = filenameTpl.filename
chunkFilename = chunkFilename.replace("[ext]", '.js')
webpackConfig.plugins.push(
new this.webpack.optimize.CommonsChunkPlugin({
name: chunks,
filename: chunkFilename,
minChunks: commonsChunk.minChunks ? commonsChunk.minChunks : 2
})
);
}
}
}
module.exports = {
plugins: [{
name: 'antd',
@ -28,45 +89,35 @@ module.exports = {
exports: [
'./index.js'
],
commonsChunk: {
//filename: 'scripts/[name]@[chunkhash][ext]',
vendors: {
lib: ['react', 'redux',
'redux-thunk',
'react-dom',
'redux-promise',
'react-router-dom',
'prop-types'
],
lib2:[
'axios',
'moment'
]
}
},
modifyWebpackConfig: function (baseConfig) {
var ENV_PARAMS = {};
switch (this.env) {
case 'local':
ENV_PARAMS = {development: true};
ENV_PARAMS = { development: true };
break;
case 'dev':
ENV_PARAMS = {development: true};
ENV_PARAMS = { development: true };
break;
case 'prd':
ENV_PARAMS = {development: false};
ENV_PARAMS = { development: false };
break;
default:
}
}
baseConfig.plugins.push(new this.webpack.DefinePlugin({
ENV_PARAMS: JSON.stringify(ENV_PARAMS)
ENV_PARAMS: JSON.stringify(ENV_PARAMS)
}))
//初始化配置
baseConfig.devtool = 'cheap-module-eval-source-map'
baseConfig.context = path.resolve(__dirname, "client");
baseConfig.output.prd.path = 'static/prd';
baseConfig.output.prd.publicPath = '';
baseConfig.output.prd.filename = '[name]@[chunkhash][ext]'
//commonsChunk
handleCommonsChunk.call(this, baseConfig)
baseConfig.module.loaders.push({
test: /\.(sass|scss)$/,
loader: ykit.ExtractTextPlugin.extract(