From f56d0cdf831281a60e9b9a995b6d39481f3dd733 Mon Sep 17 00:00:00 2001 From: "wenbo.dong" Date: Thu, 10 Aug 2017 17:08:39 +0800 Subject: [PATCH 1/6] fix: merge --- server/controllers/project.js | 33 +- server_dist/controllers/project.js | 29 +- static/doc/api.html | 833 +++++++++++++++++- .../static/server/controllers/base.js.html | 103 ++- .../static/server/controllers/group.js.html | 134 ++- .../server/controllers/interfaceCol.js.html | 376 ++++++++ .../doc/static/server/controllers/log.js.html | 28 +- .../static/server/controllers/project.js.html | 139 ++- .../static/server/controllers/user.js.html | 1 + 9 files changed, 1560 insertions(+), 116 deletions(-) create mode 100644 static/doc/static/server/controllers/interfaceCol.js.html diff --git a/server/controllers/project.js b/server/controllers/project.js index 9840f2c1..b6d889b5 100644 --- a/server/controllers/project.js +++ b/server/controllers/project.js @@ -527,26 +527,35 @@ class projectController extends baseController { 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); - console.log(count); + + 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('cont',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 - } - })); - // console.log(arr); + // 返回的json模板数据: item.res_body + const mockData = Mock.mock( + yapi.commons.json_parse(item.res_body) + ); + return { + path: item.path, + mock: mockData + } + })); + // console.log(arr); const fileName = 'mock.js'; ctx.attachment(fileName); await send(ctx, fileName, { root: __dirname + '/public' }); const res = ` - var data = ${arr}` + var data = ${arr}; + module.exports = data;` .trim(); return ctx.body = res; } diff --git a/server_dist/controllers/project.js b/server_dist/controllers/project.js index 5a575fc0..dceb6e94 100644 --- a/server_dist/controllers/project.js +++ b/server_dist/controllers/project.js @@ -1192,13 +1192,32 @@ 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: + + console.log('cont', count); 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)); @@ -1212,14 +1231,14 @@ var projectController = function (_baseController) { fileName = 'mock.js'; ctx.attachment(fileName); - _context12.next = 11; + _context12.next = 17; return send(ctx, fileName, { root: __dirname + '/public' }); - case 11: - res = ('\n var data = ' + arr).trim(); + case 17: + res = ('\n var data = ' + arr + ';\n module.exports = data;').trim(); return _context12.abrupt('return', ctx.body = res); - case 13: + case 19: case 'end': return _context12.stop(); } diff --git a/static/doc/api.html b/static/doc/api.html index f587c9de..9255827d 100644 --- a/static/doc/api.html +++ b/static/doc/api.html @@ -191,6 +191,10 @@
  • /project/search
  • + +
  • + /project/download +
  • @@ -224,6 +228,48 @@ + +
  • + + col +
  • + + +
  • @@ -266,7 +312,7 @@

    源码位置: - ./server/controllers/group.js:10 + ./server/controllers/group.js:11

    @@ -313,6 +359,18 @@ + + owner_uid + String + 组长uid + + + + + + + + @@ -349,7 +407,7 @@

    源码位置: - ./server/controllers/group.js:64 + ./server/controllers/group.js:184

    @@ -411,7 +469,7 @@

    源码位置: - ./server/controllers/group.js:83 + ./server/controllers/group.js:203

    @@ -483,7 +541,7 @@

    源码位置: - ./server/controllers/group.js:120 + ./server/controllers/group.js:240

    @@ -1351,7 +1409,7 @@

    源码位置: - ./server/controllers/project.js:35 + ./server/controllers/project.js:37

    @@ -1492,7 +1550,7 @@

    源码位置: - ./server/controllers/project.js:116 + ./server/controllers/project.js:126

    @@ -1576,7 +1634,7 @@

    源码位置: - ./server/controllers/project.js:148 + ./server/controllers/project.js:169

    @@ -1660,7 +1718,7 @@

    源码位置: - ./server/controllers/project.js:181 + ./server/controllers/project.js:222

    @@ -1738,7 +1796,7 @@

    源码位置: - ./server/controllers/project.js:209 + ./server/controllers/project.js:247

    @@ -1821,7 +1879,7 @@

    源码位置: - ./server/controllers/project.js:233 + ./server/controllers/project.js:271

    @@ -1858,26 +1916,6 @@ - - page - Number - 分页页码 - - - - - - - - limit - Number - 每页数据条目,默认为10 - - - - - - @@ -2068,7 +2106,7 @@

    源码位置: - ./server/controllers/project.js:279 + ./server/controllers/project.js:312

    @@ -2140,7 +2178,7 @@

    源码位置: - ./server/controllers/project.js:312 + ./server/controllers/project.js:372

    @@ -2300,7 +2338,7 @@

    源码位置: - ./server/controllers/project.js:404 + ./server/controllers/project.js:463

    @@ -2375,6 +2413,68 @@ } + + +
    +
    + +
    +

    + 描述: + 下载项目的 Mock 数据 +

    + +

    + 源码位置: + ./server/controllers/project.js:518 +

    + + +

    + 参数: +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数名类型描述必选支持版本
    project_idString + + + +
    +
    + + +
    @@ -3124,6 +3224,671 @@ } + + + + + +
    +
    + +
    +

    + 描述: + 获取所有接口集 +

    + +

    + 源码位置: + ./server/controllers/interfaceCol.js:12 +

    + + +

    + 参数: +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数名类型描述必选支持版本
    project_idStringemail名称,不能为空 + + + +
    +
    + + + +
    + +
    +
    + +
    +

    + 描述: + 增加接口集 +

    + +

    + 源码位置: + ./server/controllers/interfaceCol.js:33 +

    + + +

    + 参数: +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数名类型描述必选支持版本
    project_idNumber + + + +
    nameString + + + +
    descString + + + +
    +
    + + + +
    + +
    +
    + +
    +

    + 描述: + 获取一个接口集下的所有的接口用例 +

    + +

    + 源码位置: + ./server/controllers/interfaceCol.js:77 +

    + + +

    + 参数: +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数名类型描述必选支持版本
    col_idString接口集id + + + +
    +
    + + + +
    + +
    +
    + +
    +

    + 描述: + 增加一个接口用例 +

    + +

    + 源码位置: + ./server/controllers/interfaceCol.js:99 +

    + + +

    + 参数: +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数名类型描述必选支持版本
    casenameString + + + +
    col_idNumber + + + +
    project_idNumber + + + +
    envString + + + +
    domainString + + + +
    pathString + + + +
    methodString + + + +
    req_queryObject + + + +
    req_headersObject + + + +
    req_body_typeString + + + +
    req_body_formArray + + + +
    req_body_otherString + + + +
    +
    + + + +
    + +
    +
    + +
    +

    + 描述: + 获取一个接口用例详情 +

    + +

    + 源码位置: + ./server/controllers/interfaceCol.js:164 +

    + + +

    + 参数: +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数名类型描述必选支持版本
    caseidString + + + +
    +
    + + + +
    + +
    +
    + +
    +

    + 描述: + 更新一个接口集name或描述 +

    + +

    + 源码位置: + ./server/controllers/interfaceCol.js:185 +

    + + +

    + 参数: +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数名类型描述必选支持版本
    nameString + + + +
    descString + + + +
    +
    + + + +
    + +
    +
    + +
    +

    + 描述: + 更新多个接口case index +

    + +

    + 源码位置: + ./server/controllers/interfaceCol.js:211 +

    + + +

    + 参数: +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数名类型描述必选支持版本
    id, indexArray + +
    +
    + + + +
    + +
    +
    + +
    +

    + 描述: + 删除一个接口集 +

    + +

    + 源码位置: + ./server/controllers/interfaceCol.js:243 +

    + + +

    + 参数: +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数名类型描述必选支持版本
    String + + + +
    +
    + + +
    @@ -3147,7 +3912,7 @@

    源码位置: - ./server/controllers/log.js:12 + ./server/controllers/log.js:28

    diff --git a/static/doc/static/server/controllers/base.js.html b/static/doc/static/server/controllers/base.js.html index 1ebf0223..e52fd60b 100644 --- a/static/doc/static/server/controllers/base.js.html +++ b/static/doc/static/server/controllers/base.js.html @@ -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; } } diff --git a/static/doc/static/server/controllers/group.js.html b/static/doc/static/server/controllers/group.js.html index e283d75c..edda6bb4 100644 --- a/static/doc/static/server/controllers/group.js.html +++ b/static/doc/static/server/controllers/group.js.html @@ -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', diff --git a/static/doc/static/server/controllers/interfaceCol.js.html b/static/doc/static/server/controllers/interfaceCol.js.html new file mode 100644 index 00000000..87909f17 --- /dev/null +++ b/static/doc/static/server/controllers/interfaceCol.js.html @@ -0,0 +1,376 @@ + + + + + + YApi : ./server/controllers/interfaceCol.js + + + + + + +
    +
    +
    +
    +

    YApi : ./server/controllers/interfaceCol.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
    +                
    +
    +
    +
    +
    + + +
    +
    +

    +
    +
    +
    + + + diff --git a/static/doc/static/server/controllers/log.js.html b/static/doc/static/server/controllers/log.js.html index dcb5c7cb..e54994d8 100644 --- a/static/doc/static/server/controllers/log.js.html +++ b/static/doc/static/server/controllers/log.js.html @@ -35,6 +35,22 @@ 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); + } + + } /** @@ -49,19 +65,19 @@ class logController extends baseController { * @returns {Object} * @example ./api/project/list.json */ - + 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, '项目id不能为空'); } 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), diff --git a/static/doc/static/server/controllers/project.js.html b/static/doc/static/server/controllers/project.js.html index f5c06ccd..53cb54ae 100644 --- a/static/doc/static/server/controllers/project.js.html +++ b/static/doc/static/server/controllers/project.js.html @@ -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,50 @@ 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不存在'); + } + + console.log('cont',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 + } + })); + // console.log(arr); + + const fileName = 'mock.js'; + ctx.attachment(fileName); + await send(ctx, fileName, { root: __dirname + '/public' }); + + const res = ` + var data = ${arr}; + module.exports = data;` + .trim(); + return ctx.body = res; + } } module.exports = projectController; diff --git a/static/doc/static/server/controllers/user.js.html b/static/doc/static/server/controllers/user.js.html index 1ae4e7ea..33814228 100644 --- a/static/doc/static/server/controllers/user.js.html +++ b/static/doc/static/server/controllers/user.js.html @@ -548,6 +548,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; } From e2fa9c1c4e234d6a54f55505b411b03299d07e1e Mon Sep 17 00:00:00 2001 From: "wenbo.dong" Date: Thu, 10 Aug 2017 17:20:03 +0800 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20mock=E4=B8=8B=E8=BD=BD=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/project.js | 79 +- server/models/interface.js | 1 - server_dist/controllers/project.js | 1259 ------------------------- server_dist/controllers/user.js | 1263 -------------------------- server_dist/middleware/checkToken.js | 36 - server_dist/middleware/mockServer.js | 177 ---- server_dist/middleware/userauth.js | 44 - server_dist/models/avatar.js | 81 -- server_dist/models/base.js | 82 -- server_dist/models/follow.js | 109 --- server_dist/models/group.js | 153 ---- server_dist/models/interface.js | 144 --- server_dist/models/interfaceCase.js | 122 --- server_dist/models/interfaceCol.js | 104 --- server_dist/models/log.js | 117 --- server_dist/models/project.js | 200 ---- server_dist/models/user.js | 142 --- server_dist/utils/commons.js | 250 ----- server_dist/utils/db.js | 55 -- server_dist/utils/initConfig.js | 26 - 20 files changed, 50 insertions(+), 4394 deletions(-) delete mode 100644 server_dist/controllers/project.js delete mode 100644 server_dist/controllers/user.js delete mode 100644 server_dist/middleware/checkToken.js delete mode 100644 server_dist/middleware/mockServer.js delete mode 100644 server_dist/middleware/userauth.js delete mode 100644 server_dist/models/avatar.js delete mode 100644 server_dist/models/base.js delete mode 100644 server_dist/models/follow.js delete mode 100644 server_dist/models/group.js delete mode 100644 server_dist/models/interface.js delete mode 100644 server_dist/models/interfaceCase.js delete mode 100644 server_dist/models/interfaceCol.js delete mode 100644 server_dist/models/log.js delete mode 100644 server_dist/models/project.js delete mode 100644 server_dist/models/user.js delete mode 100644 server_dist/utils/commons.js delete mode 100644 server_dist/utils/db.js delete mode 100644 server_dist/utils/initConfig.js diff --git a/server/controllers/project.js b/server/controllers/project.js index b6d889b5..04d77279 100644 --- a/server/controllers/project.js +++ b/server/controllers/project.js @@ -525,39 +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); - // 根据 project_id 获取接口数据 - let count = await interfaceInst.list(project_id); + 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不存在'); - } + 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('cont',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 - } - })); - // console.log(arr); + 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 fileName = 'mock.js'; + ctx.attachment(fileName); + await send(ctx, fileName, { root: __dirname + '/public' }); - const res = ` - var data = ${arr}; - module.exports = data;` - .trim(); - return ctx.body = res; + 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; } } diff --git a/server/models/interface.js b/server/models/interface.js index dc65feae..a5a4f6e7 100644 --- a/server/models/interface.js +++ b/server/models/interface.js @@ -74,7 +74,6 @@ class interfaceModel extends baseModel { } list(project_id) { - console.log(project_id); return this.model.find({ project_id: project_id }) diff --git a/server_dist/controllers/project.js b/server_dist/controllers/project.js deleted file mode 100644 index dceb6e94..00000000 --- a/server_dist/controllers/project.js +++ /dev/null @@ -1,1259 +0,0 @@ -'use strict'; - -var _stringify = require('babel-runtime/core-js/json/stringify'); - -var _stringify2 = _interopRequireDefault(_stringify); - -var _regenerator = require('babel-runtime/regenerator'); - -var _regenerator2 = _interopRequireDefault(_regenerator); - -var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); - -var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); - -var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); - -var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); - -var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); - -var _inherits2 = require('babel-runtime/helpers/inherits'); - -var _inherits3 = _interopRequireDefault(_inherits2); - -var _project = require('../models/project.js'); - -var _project2 = _interopRequireDefault(_project); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _base = require('./base.js'); - -var _base2 = _interopRequireDefault(_base); - -var _interface = require('../models/interface.js'); - -var _interface2 = _interopRequireDefault(_interface); - -var _group = require('../models/group'); - -var _group2 = _interopRequireDefault(_group); - -var _commons = require('../utils/commons.js'); - -var _commons2 = _interopRequireDefault(_commons); - -var _user = require('../models/user.js'); - -var _user2 = _interopRequireDefault(_user); - -var _mockjs = require('mockjs'); - -var _mockjs2 = _interopRequireDefault(_mockjs); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var send = require('koa-send'); - -var projectController = function (_baseController) { - (0, _inherits3.default)(projectController, _baseController); - - function projectController(ctx) { - (0, _classCallCheck3.default)(this, projectController); - - var _this = (0, _possibleConstructorReturn3.default)(this, (projectController.__proto__ || (0, _getPrototypeOf2.default)(projectController)).call(this, ctx)); - - _this.Model = _yapi2.default.getInst(_project2.default); - _this.groupModel = _yapi2.default.getInst(_group2.default); - return _this; - } - - (0, _createClass3.default)(projectController, [{ - key: 'handleBasepath', - value: function handleBasepath(basepath) { - if (!basepath) return ""; - if (basepath === '/') return ""; - if (basepath[0] !== '/') basepath = '/' + basepath; - if (basepath[basepath.length - 1] === '/') basepath = basepath.substr(0, basepath.length - 1); - if (!_yapi2.default.commons.verifyPath(basepath)) { - return false; - } - return basepath; - } - }, { - key: 'verifyDomain', - value: function verifyDomain(domain) { - if (!domain) return false; - if (/^[a-zA-Z0-9\-_\.]+?\.[a-zA-Z0-9\-_\.]*?[a-zA-Z]{2,6}$/.test(domain)) { - return true; - } - return false; - } - - /** - * 添加项目分组 - * @interface /project/add - * @method POST - * @category project - * @foldnumber 10 - * @param {String} name 项目名称,不能为空 - * @param {String} basepath 项目基本路径,不能为空 - * @param {String} prd_host 项目线上域名,不能为空。可通过配置的域名访问到mock数据 - * @param {String} protocol 线上域名协议,不能为空 - * @param {Number} group_id 项目分组id,不能为空 - * @param {String} [desc] 项目描述 - * @returns {Object} - * @example ./api/project/add.json - */ - - }, { - key: 'add', - value: function () { - var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) { - var params, checkRepeat, checkRepeatDomain, data, result; - return _regenerator2.default.wrap(function _callee$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - params = ctx.request.body; - - params = _yapi2.default.commons.handleParams(params, { - name: 'string', - basepath: 'string', - prd_host: 'string', - protocol: 'string', - group_id: 'number', - desc: 'string' - }); - - _context.next = 4; - return this.checkAuth(params.group_id, 'group', 'edit'); - - case 4: - _context.t0 = _context.sent; - - if (!(_context.t0 !== true)) { - _context.next = 7; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); - - case 7: - if (params.group_id) { - _context.next = 9; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目分组id不能为空')); - - case 9: - if (params.name) { - _context.next = 11; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目名不能为空')); - - case 11: - _context.next = 13; - return this.Model.checkNameRepeat(params.name); - - case 13: - checkRepeat = _context.sent; - - if (!(checkRepeat > 0)) { - _context.next = 16; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的项目名')); - - case 16: - if (params.prd_host) { - _context.next = 18; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目domain不能为空')); - - case 18: - - params.basepath = params.basepath || ''; - - if (!((params.basepath = this.handleBasepath(params.basepath)) === false)) { - _context.next = 21; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, 'basepath格式有误')); - - case 21: - if (this.verifyDomain(params.prd_host)) { - _context.next = 23; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '线上域名格式有误')); - - case 23: - _context.next = 25; - return this.Model.checkDomainRepeat(params.prd_host, params.basepath); - - case 25: - checkRepeatDomain = _context.sent; - - if (!(checkRepeatDomain > 0)) { - _context.next = 28; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在domain和basepath')); - - case 28: - data = { - name: params.name, - desc: params.desc, - prd_host: params.prd_host, - basepath: params.basepath, - protocol: params.protocol || 'http', - members: [], - project_type: params.project_type || 'private', - uid: this.getUid(), - group_id: params.group_id, - add_time: _yapi2.default.commons.time(), - up_time: _yapi2.default.commons.time() - }; - _context.prev = 29; - _context.next = 32; - return this.Model.save(data); - - case 32: - result = _context.sent; - - ctx.body = _yapi2.default.commons.resReturn(result); - _context.next = 39; - break; - - case 36: - _context.prev = 36; - _context.t1 = _context['catch'](29); - - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context.t1.message); - - case 39: - case 'end': - return _context.stop(); - } - } - }, _callee, this, [[29, 36]]); - })); - - function add(_x) { - return _ref.apply(this, arguments); - } - - return add; - }() - /** - * 添加项目 - * @interface /project/add_member - * @method POST - * @category project - * @foldnumber 10 - * @param {Number} id 项目id,不能为空 - * @param {String} member_uid 项目成员uid,不能为空 - * @returns {Object} - * @example ./api/project/add_member.json - */ - - }, { - key: 'addMember', - value: function () { - var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(ctx) { - var params, check, userdata, result; - return _regenerator2.default.wrap(function _callee2$(_context2) { - while (1) { - switch (_context2.prev = _context2.next) { - case 0: - params = ctx.request.body; - - if (params.member_uid) { - _context2.next = 3; - break; - } - - return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目成员uid不能为空')); - - case 3: - if (params.id) { - _context2.next = 5; - break; - } - - return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); - - case 5: - _context2.next = 7; - return this.checkAuth(params.id, 'project', 'edit'); - - case 7: - _context2.t0 = _context2.sent; - - if (!(_context2.t0 !== true)) { - _context2.next = 10; - break; - } - - return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); - - case 10: - _context2.next = 12; - return this.Model.checkMemberRepeat(params.id, params.member_uid); - - case 12: - check = _context2.sent; - - if (!(check > 0)) { - _context2.next = 15; - break; - } - - return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目成员已存在')); - - case 15: - _context2.next = 17; - return this.getUserdata(params.member_uid); - - case 17: - userdata = _context2.sent; - - if (!(userdata === null)) { - _context2.next = 20; - break; - } - - return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '成员uid不存在')); - - case 20: - _context2.prev = 20; - _context2.next = 23; - return this.Model.addMember(params.id, userdata); - - case 23: - result = _context2.sent; - - ctx.body = _yapi2.default.commons.resReturn(result); - _context2.next = 30; - break; - - case 27: - _context2.prev = 27; - _context2.t1 = _context2['catch'](20); - - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context2.t1.message); - - case 30: - case 'end': - return _context2.stop(); - } - } - }, _callee2, this, [[20, 27]]); - })); - - function addMember(_x2) { - return _ref2.apply(this, arguments); - } - - return addMember; - }() - /** - * 添加项目 - * @interface /project/del_member - * @method POST - * @category project - * @foldnumber 10 - * @param {Number} id 项目id,不能为空 - * @param {member_uid} uid 项目成员uid,不能为空 - * @returns {Object} - * @example ./api/project/del_member.json - */ - - }, { - key: 'delMember', - value: function () { - var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) { - var params, check, result; - return _regenerator2.default.wrap(function _callee3$(_context3) { - while (1) { - switch (_context3.prev = _context3.next) { - case 0: - params = ctx.request.body; - - if (params.member_uid) { - _context3.next = 3; - break; - } - - return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目成员uid不能为空')); - - case 3: - if (params.id) { - _context3.next = 5; - break; - } - - return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); - - case 5: - _context3.next = 7; - return this.Model.checkMemberRepeat(params.id, params.member_uid); - - case 7: - check = _context3.sent; - - if (!(check === 0)) { - _context3.next = 10; - break; - } - - return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目成员不存在')); - - case 10: - _context3.next = 12; - return this.checkAuth(params.id, 'project', 'danger'); - - case 12: - _context3.t0 = _context3.sent; - - if (!(_context3.t0 !== true)) { - _context3.next = 15; - break; - } - - return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); - - case 15: - _context3.prev = 15; - _context3.next = 18; - return this.Model.delMember(params.id, params.member_uid); - - case 18: - result = _context3.sent; - - ctx.body = _yapi2.default.commons.resReturn(result); - _context3.next = 25; - break; - - case 22: - _context3.prev = 22; - _context3.t1 = _context3['catch'](15); - - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context3.t1.message); - - case 25: - case 'end': - return _context3.stop(); - } - } - }, _callee3, this, [[15, 22]]); - })); - - function delMember(_x3) { - return _ref3.apply(this, arguments); - } - - return delMember; - }() - }, { - key: 'getUserdata', - value: function () { - var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(uid, role) { - var userInst, userData; - return _regenerator2.default.wrap(function _callee4$(_context4) { - while (1) { - switch (_context4.prev = _context4.next) { - case 0: - role = role || 'dev'; - userInst = _yapi2.default.getInst(_user2.default); - _context4.next = 4; - return userInst.findById(uid); - - case 4: - userData = _context4.sent; - - if (userData) { - _context4.next = 7; - break; - } - - return _context4.abrupt('return', null); - - case 7: - return _context4.abrupt('return', { - role: role, - uid: userData._id, - username: userData.username, - email: userData.email - }); - - case 8: - case 'end': - return _context4.stop(); - } - } - }, _callee4, this); - })); - - function getUserdata(_x4, _x5) { - return _ref4.apply(this, arguments); - } - - return getUserdata; - }() - - /** - * 获取项目成员列表 - * @interface /project/get_member_list - * @method GET - * @category project - * @foldnumber 10 - * @param {Number} id 项目id,不能为空 - * @return {Object} - * @example ./api/project/get_member_list.json - */ - - }, { - key: 'getMemberList', - value: function () { - var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(ctx) { - var params, project; - return _regenerator2.default.wrap(function _callee5$(_context5) { - while (1) { - switch (_context5.prev = _context5.next) { - case 0: - params = ctx.request.query; - - if (params.id) { - _context5.next = 3; - break; - } - - return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); - - case 3: - _context5.prev = 3; - _context5.next = 6; - return this.Model.get(params.id); - - case 6: - project = _context5.sent; - - ctx.body = _yapi2.default.commons.resReturn(project.members); - _context5.next = 13; - break; - - case 10: - _context5.prev = 10; - _context5.t0 = _context5['catch'](3); - - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context5.t0.message); - - case 13: - case 'end': - return _context5.stop(); - } - } - }, _callee5, this, [[3, 10]]); - })); - - function getMemberList(_x6) { - return _ref5.apply(this, arguments); - } - - return getMemberList; - }() - - /** - * 添加项目 - * @interface /project/get - * @method GET - * @category project - * @foldnumber 10 - * @param {Number} id 项目id,不能为空 - * @returns {Object} - * @example ./api/project/get.json - */ - - }, { - key: 'get', - value: function () { - var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(ctx) { - var params, result; - return _regenerator2.default.wrap(function _callee6$(_context6) { - while (1) { - switch (_context6.prev = _context6.next) { - case 0: - params = ctx.request.query; - - if (params.id) { - _context6.next = 3; - break; - } - - return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); - - case 3: - _context6.prev = 3; - _context6.next = 6; - return this.Model.get(params.id); - - case 6: - result = _context6.sent; - - ctx.body = _yapi2.default.commons.resReturn(result); - _context6.next = 13; - break; - - case 10: - _context6.prev = 10; - _context6.t0 = _context6['catch'](3); - - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context6.t0.message); - - case 13: - case 'end': - return _context6.stop(); - } - } - }, _callee6, this, [[3, 10]]); - })); - - function get(_x7) { - return _ref6.apply(this, arguments); - } - - return get; - }() - - /** - * 获取项目列表 - * @interface /project/list - * @method GET - * @category project - * @foldnumber 10 - * @param {Number} group_id 项目group_id,不能为空 - * @returns {Object} - * @example ./api/project/list.json - */ - - }, { - key: 'list', - value: function () { - var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(ctx) { - var group_id, auth, result, uids, _users, users; - - return _regenerator2.default.wrap(function _callee7$(_context7) { - while (1) { - switch (_context7.prev = _context7.next) { - case 0: - group_id = ctx.request.query.group_id; - - if (group_id) { - _context7.next = 3; - break; - } - - return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目分组id不能为空')); - - case 3: - _context7.next = 5; - return this.checkAuth(group_id, 'group', 'edit'); - - case 5: - auth = _context7.sent; - _context7.prev = 6; - _context7.next = 9; - return this.Model.list(group_id, auth); - - case 9: - result = _context7.sent; - uids = []; - - result.forEach(function (item) { - if (uids.indexOf(item.uid) === -1) { - uids.push(item.uid); - } - }); - _users = {}; - _context7.next = 15; - return _yapi2.default.getInst(_user2.default).findByUids(uids); - - case 15: - users = _context7.sent; - - users.forEach(function (item) { - _users[item._id] = item; - }); - ctx.body = _yapi2.default.commons.resReturn({ - list: result, - userinfo: _users - }); - _context7.next = 23; - break; - - case 20: - _context7.prev = 20; - _context7.t0 = _context7['catch'](6); - - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context7.t0.message); - - case 23: - case 'end': - return _context7.stop(); - } - } - }, _callee7, this, [[6, 20]]); - })); - - function list(_x8) { - return _ref7.apply(this, arguments); - } - - return list; - }() - - /** - * 删除项目 - * @interface /project/del - * @method POST - * @category project - * @foldnumber 10 - * @param {Number} id 项目id,不能为空 - * @returns {Object} - * @example ./api/project/del.json - */ - - }, { - key: 'del', - value: function () { - var _ref8 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(ctx) { - var _id, interfaceInst, count, result; - - return _regenerator2.default.wrap(function _callee8$(_context8) { - while (1) { - switch (_context8.prev = _context8.next) { - case 0: - _context8.prev = 0; - _id = ctx.request.body.id; - - if (_id) { - _context8.next = 4; - break; - } - - return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); - - case 4: - interfaceInst = _yapi2.default.getInst(_interface2.default); - _context8.next = 7; - return interfaceInst.countByProjectId(_id); - - case 7: - count = _context8.sent; - - if (!(count > 0)) { - _context8.next = 10; - break; - } - - return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '请先删除该项目下所有接口')); - - case 10: - _context8.next = 12; - return this.checkAuth(_id, 'project', 'danger'); - - case 12: - _context8.t0 = _context8.sent; - - if (!(_context8.t0 !== true)) { - _context8.next = 15; - break; - } - - return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); - - case 15: - _context8.next = 17; - return this.Model.del(_id); - - case 17: - result = _context8.sent; - - ctx.body = _yapi2.default.commons.resReturn(result); - _context8.next = 24; - break; - - case 21: - _context8.prev = 21; - _context8.t1 = _context8['catch'](0); - - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context8.t1.message); - - case 24: - case 'end': - return _context8.stop(); - } - } - }, _callee8, this, [[0, 21]]); - })); - - function del(_x9) { - return _ref8.apply(this, arguments); - } - - return del; - }() - }, { - key: 'changeMemberRole', - value: function () { - var _ref9 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9(ctx) { - var params, groupInst, check, result; - return _regenerator2.default.wrap(function _callee9$(_context9) { - while (1) { - switch (_context9.prev = _context9.next) { - case 0: - params = ctx.request.body; - groupInst = _yapi2.default.getInst(_group2.default); - - if (params.member_uid) { - _context9.next = 4; - break; - } - - return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '分组成员uid不能为空')); - - case 4: - if (params.id) { - _context9.next = 6; - break; - } - - return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '分组id不能为空')); - - case 6: - _context9.next = 8; - return groupInst.checkMemberRepeat(params.id, params.member_uid); - - case 8: - check = _context9.sent; - - if (!(check === 0)) { - _context9.next = 11; - break; - } - - return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '分组成员不存在')); - - case 11: - _context9.next = 13; - return this.checkAuth(id, 'group', 'danger'); - - case 13: - _context9.t0 = _context9.sent; - - if (!(_context9.t0 !== true)) { - _context9.next = 16; - break; - } - - return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); - - case 16: - - params.role = params.role === 'owner' ? 'owner' : 'dev'; - - _context9.prev = 17; - _context9.next = 20; - return groupInst.changeMemberRole(params.id, params.member_uid, params.role); - - case 20: - result = _context9.sent; - - ctx.body = _yapi2.default.commons.resReturn(result); - _context9.next = 27; - break; - - case 24: - _context9.prev = 24; - _context9.t1 = _context9['catch'](17); - - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context9.t1.message); - - case 27: - case 'end': - return _context9.stop(); - } - } - }, _callee9, this, [[17, 24]]); - })); - - function changeMemberRole(_x10) { - return _ref9.apply(this, arguments); - } - - return changeMemberRole; - }() - - /** - * 编辑项目 - * @interface /project/up - * @method POST - * @category project - * @foldnumber 10 - * @param {Number} id 项目id,不能为空 - * @param {String} name 项目名称,不能为空 - * @param {String} basepath 项目基本路径,不能为空 - * @param {String} prd_host 项目线上域名,不能为空。可通过配置的域名访问到mock数据 - * @param {String} [desc] 项目描述 - * @param {Array} [env] 项目环境配置 - * @param {String} [env[].name] 环境名称 - * @param {String} [env[].domain] 环境域名 - * @returns {Object} - * @example ./api/project/up.json - */ - - }, { - key: 'up', - value: function () { - var _ref10 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(ctx) { - var _id2, params, projectData, checkRepeat, checkRepeatDomain, data, result; - - return _regenerator2.default.wrap(function _callee10$(_context10) { - while (1) { - switch (_context10.prev = _context10.next) { - case 0: - _context10.prev = 0; - _id2 = ctx.request.body.id; - params = ctx.request.body; - - params.basepath = params.basepath || ''; - params = _yapi2.default.commons.handleParams(params, { - name: 'string', - basepath: 'string', - prd_host: 'string', - protocol: 'string', - group_id: 'number', - desc: 'string' - }); - - if (_id2) { - _context10.next = 7; - break; - } - - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '项目id不能为空')); - - case 7: - _context10.next = 9; - return this.checkAuth(_id2, 'project', 'edit'); - - case 9: - _context10.t0 = _context10.sent; - - if (!(_context10.t0 !== true)) { - _context10.next = 12; - break; - } - - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); - - case 12: - _context10.next = 14; - return this.Model.get(_id2); - - case 14: - projectData = _context10.sent; - - if (!((params.basepath = this.handleBasepath(params.basepath)) === false)) { - _context10.next = 17; - break; - } - - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, 'basepath格式有误')); - - case 17: - if (this.verifyDomain(params.prd_host)) { - _context10.next = 19; - break; - } - - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '线上域名格式有误')); - - case 19: - - if (projectData.name === params.name) { - delete params.name; - } - if (projectData.basepath === params.basepath && projectData.prd_host === params.prd_host) { - delete params.basepath; - delete params.prd_host; - } - - if (!params.name) { - _context10.next = 27; - break; - } - - _context10.next = 24; - return this.Model.checkNameRepeat(params.name); - - case 24: - checkRepeat = _context10.sent; - - if (!(checkRepeat > 0)) { - _context10.next = 27; - break; - } - - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的项目名')); - - case 27: - if (!(params.basepath && params.prd_host)) { - _context10.next = 33; - break; - } - - _context10.next = 30; - return this.Model.checkDomainRepeat(params.prd_host, params.basepath); - - case 30: - checkRepeatDomain = _context10.sent; - - if (!(checkRepeatDomain > 0)) { - _context10.next = 33; - break; - } - - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在domain和basepath')); - - case 33: - data = { - up_time: _yapi2.default.commons.time() - }; - - - if (params.name) data.name = params.name; - if (params.desc) data.desc = params.desc; - if (params.prd_host) { - data.prd_host = params.prd_host; - data.basepath = params.basepath; - } - if (params.protocol) data.protocol = params.protocol; - if (params.env) data.env = params.env; - - _context10.next = 41; - return this.Model.up(_id2, data); - - case 41: - result = _context10.sent; - - ctx.body = _yapi2.default.commons.resReturn(result); - _context10.next = 48; - break; - - case 45: - _context10.prev = 45; - _context10.t1 = _context10['catch'](0); - - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context10.t1.message); - - case 48: - case 'end': - return _context10.stop(); - } - } - }, _callee10, this, [[0, 45]]); - })); - - function up(_x11) { - return _ref10.apply(this, arguments); - } - - return up; - }() - - /** - * 模糊搜索项目名称或者组名称 - * @interface /project/search - * @method GET - * @category project - * @foldnumber 10 - * @param {String} q - * @return {Object} - * @example ./api/project/search.json - */ - - }, { - key: 'search', - value: function () { - var _ref11 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11(ctx) { - var q, projectList, groupList, projectRules, groupRules, queryList; - return _regenerator2.default.wrap(function _callee11$(_context11) { - while (1) { - switch (_context11.prev = _context11.next) { - case 0: - q = ctx.request.query.q; - - if (q) { - _context11.next = 3; - break; - } - - return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'No keyword.')); - - case 3: - if (_yapi2.default.commons.validateSearchKeyword(q)) { - _context11.next = 5; - break; - } - - return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'Bad query.')); - - case 5: - _context11.next = 7; - return this.Model.search(q); - - case 7: - projectList = _context11.sent; - _context11.next = 10; - return this.groupModel.search(q); - - case 10: - groupList = _context11.sent; - projectRules = ['_id', 'name', 'basepath', 'uid', 'env', 'members', { key: 'group_id', alias: 'groupId' }, { key: 'up_time', alias: 'upTime' }, { key: 'prd_host', alias: 'prdHost' }, { key: 'add_time', alias: 'addTime' }]; - groupRules = ['_id', 'uid', { key: 'group_name', alias: 'groupName' }, { key: 'group_desc', alias: 'groupDesc' }, { key: 'add_time', alias: 'addTime' }, { key: 'up_time', alias: 'upTime' }]; - - - projectList = _commons2.default.filterRes(projectList, projectRules); - groupList = _commons2.default.filterRes(groupList, groupRules); - - queryList = { - project: projectList, - group: groupList - }; - return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(queryList, 0, 'ok')); - - case 17: - case 'end': - return _context11.stop(); - } - } - }, _callee11, this); - })); - - function search(_x12) { - return _ref11.apply(this, arguments); - } - - return search; - }() - - /** - * 下载项目的 Mock 数据 - * @interface /project/download - * @method GET - * @category project - * @foldnumber 10 - * @param {String} project_id - */ - - }, { - key: 'download', - value: function () { - var _ref12 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(ctx) { - var project_id, interfaceInst, count, arr, fileName, res; - return _regenerator2.default.wrap(function _callee12$(_context12) { - while (1) { - switch (_context12.prev = _context12.next) { - 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; - - 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: - - console.log('cont', count); - 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)); - return { - path: item.path, - mock: mockData - }; - })); - // console.log(arr); - - fileName = 'mock.js'; - - ctx.attachment(fileName); - _context12.next = 17; - return send(ctx, fileName, { root: __dirname + '/public' }); - - case 17: - res = ('\n var data = ' + arr + ';\n module.exports = data;').trim(); - return _context12.abrupt('return', ctx.body = res); - - case 19: - case 'end': - return _context12.stop(); - } - } - }, _callee12, this); - })); - - function download(_x13) { - return _ref12.apply(this, arguments); - } - - return download; - }() - }]); - return projectController; -}(_base2.default); - -module.exports = projectController; \ No newline at end of file diff --git a/server_dist/controllers/user.js b/server_dist/controllers/user.js deleted file mode 100644 index 34a5bb3a..00000000 --- a/server_dist/controllers/user.js +++ /dev/null @@ -1,1263 +0,0 @@ -'use strict'; - -var _promise = require('babel-runtime/core-js/promise'); - -var _promise2 = _interopRequireDefault(_promise); - -var _regenerator = require('babel-runtime/regenerator'); - -var _regenerator2 = _interopRequireDefault(_regenerator); - -var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); - -var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); - -var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); - -var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); - -var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); - -var _inherits2 = require('babel-runtime/helpers/inherits'); - -var _inherits3 = _interopRequireDefault(_inherits2); - -var _user = require('../models/user.js'); - -var _user2 = _interopRequireDefault(_user); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _base = require('./base.js'); - -var _base2 = _interopRequireDefault(_base); - -var _request2 = require('request'); - -var _request3 = _interopRequireDefault(_request2); - -var _commons = require('../utils/commons.js'); - -var _commons2 = _interopRequireDefault(_commons); - -var _interface = require('../models/interface.js'); - -var _interface2 = _interopRequireDefault(_interface); - -var _group = require('../models/group.js'); - -var _group2 = _interopRequireDefault(_group); - -var _project = require('../models/project.js'); - -var _project2 = _interopRequireDefault(_project); - -var _avatar = require('../models/avatar.js'); - -var _avatar2 = _interopRequireDefault(_avatar); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var jwt = require('jsonwebtoken'); - -var userController = function (_baseController) { - (0, _inherits3.default)(userController, _baseController); - - function userController(ctx) { - (0, _classCallCheck3.default)(this, userController); - - var _this = (0, _possibleConstructorReturn3.default)(this, (userController.__proto__ || (0, _getPrototypeOf2.default)(userController)).call(this, ctx)); - - _this.Model = _yapi2.default.getInst(_user2.default); - return _this; - } - /** - * 用户登录接口 - * @interface /user/login - * @method POST - * @category user - * @foldnumber 10 - * @param {String} email email名称,不能为空 - * @param {String} password 密码,不能为空 - * @returns {Object} - * @example ./api/user/login.json - */ - - - (0, _createClass3.default)(userController, [{ - key: 'login', - value: function () { - var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) { - var userInst, email, password, result; - return _regenerator2.default.wrap(function _callee$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - //登录 - userInst = _yapi2.default.getInst(_user2.default); //创建user实体 - - email = ctx.request.body.email; - password = ctx.request.body.password; - - if (email) { - _context.next = 5; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'email不能为空')); - - case 5: - if (password) { - _context.next = 7; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空')); - - case 7: - _context.next = 9; - return userInst.findByEmail(email); - - case 9: - result = _context.sent; - - if (result) { - _context.next = 14; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 404, '该用户不存在')); - - case 14: - if (!(_yapi2.default.commons.generatePassword(password, result.passsalt) === result.password)) { - _context.next = 19; - break; - } - - this.setLoginCookie(result._id, result.passsalt); - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn({ - username: result.username, - role: result.role, - uid: result._id, - email: result.email, - add_time: result.add_time, - up_time: result.up_time, - server_ip: _yapi2.default.WEBCONFIG.server_ip - - }, 0, 'logout success...')); - - case 19: - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '密码错误')); - - case 20: - case 'end': - return _context.stop(); - } - } - }, _callee, this); - })); - - function login(_x) { - return _ref.apply(this, arguments); - } - - return login; - }() - - /** - * 退出登录接口 - * @interface /user/logout - * @method GET - * @category user - * @foldnumber 10 - * @returns {Object} - * @example ./api/user/logout.json - */ - - }, { - key: 'logout', - value: function () { - var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(ctx) { - return _regenerator2.default.wrap(function _callee2$(_context2) { - while (1) { - switch (_context2.prev = _context2.next) { - case 0: - ctx.cookies.set('_yapi_token', null); - ctx.cookies.set('_yapi_uid', null); - ctx.body = _yapi2.default.commons.resReturn('ok'); - - case 3: - case 'end': - return _context2.stop(); - } - } - }, _callee2, this); - })); - - function logout(_x2) { - return _ref2.apply(this, arguments); - } - - return logout; - }() - - /** - * 第三方登录需要提供一个request方法和 token字段,暂时只支持qunar第三方 - * @return {email: String, username: String} - */ - - }, { - key: 'thirdQunarLogin', - value: function thirdQunarLogin() { - return { - request: function request(token) { - return new _promise2.default(function (resolve, reject) { - (0, _request3.default)('http://qsso.corp.qunar.com/api/verifytoken.php?token=' + token, function (error, response, body) { - if (!error && response.statusCode == 200) { - var result = JSON.parse(body); - if (result && result.ret === true) { - var ret = { - email: result.userId + '@qunar.com', - username: result.data.userInfo.name - }; - resolve(ret); - } else { - reject(result); - } - } - reject(error); - }); - }); - }, - tokenField: 'token' - }; - } - }, { - key: 'loginByToken', - value: function () { - var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) { - var config, token, ret, login; - return _regenerator2.default.wrap(function _callee3$(_context3) { - while (1) { - switch (_context3.prev = _context3.next) { - case 0: - config = this.thirdQunarLogin(); - token = ctx.request.body[config.tokenField] || ctx.request.query[config.tokenField]; - _context3.prev = 2; - _context3.next = 5; - return config.request(token); - - case 5: - ret = _context3.sent; - _context3.next = 8; - return this.handleThirdLogin(ret.email, ret.username); - - case 8: - login = _context3.sent; - - - if (login === true) { - _yapi2.default.commons.log('login success'); - ctx.redirect('/'); - } - _context3.next = 16; - break; - - case 12: - _context3.prev = 12; - _context3.t0 = _context3['catch'](2); - - _yapi2.default.commons.log(_context3.t0.message, 'error'); - ctx.redirect('/'); - - case 16: - case 'end': - return _context3.stop(); - } - } - }, _callee3, this, [[2, 12]]); - })); - - function loginByToken(_x3) { - return _ref3.apply(this, arguments); - } - - return loginByToken; - }() - }, { - key: 'handleThirdLogin', - value: function () { - var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(email, username) { - var user, data, passsalt, userInst; - return _regenerator2.default.wrap(function _callee4$(_context4) { - while (1) { - switch (_context4.prev = _context4.next) { - case 0: - user = void 0, data = void 0, passsalt = void 0; - userInst = _yapi2.default.getInst(_user2.default); - _context4.prev = 2; - _context4.next = 5; - return userInst.findByEmail(email); - - case 5: - user = _context4.sent; - - if (!(!user || !user._id)) { - _context4.next = 13; - break; - } - - passsalt = _yapi2.default.commons.randStr(); - data = { - username: username, - password: _yapi2.default.commons.generatePassword(passsalt, passsalt), - email: email, - passsalt: passsalt, - role: 'member', - add_time: _yapi2.default.commons.time(), - up_time: _yapi2.default.commons.time(), - type: 'third' - }; - _context4.next = 11; - return userInst.save(data); - - case 11: - user = _context4.sent; - - _yapi2.default.commons.sendMail({ - to: email, - contents: '

    \u4EB2\u7231\u7684\u7528\u6237\uFF1A

    \u60A8\u597D\uFF0C\u611F\u8C22\u4F7F\u7528YApi\u5E73\u53F0.

    ' - }); - - case 13: - - this.setLoginCookie(user._id, user.passsalt); - return _context4.abrupt('return', true); - - case 17: - _context4.prev = 17; - _context4.t0 = _context4['catch'](2); - - console.error('third_login:', _context4.t0.message); // eslint-disable-line - return _context4.abrupt('return', false); - - case 21: - case 'end': - return _context4.stop(); - } - } - }, _callee4, this, [[2, 17]]); - })); - - function handleThirdLogin(_x4, _x5) { - return _ref4.apply(this, arguments); - } - - return handleThirdLogin; - }() - - /** - * 修改用户密码 - * @interface /user/change_password - * @method POST - * @category user - * @param {Number} uid 用户ID - * @param {Number} [old_password] 旧密码, 非admin用户必须传 - * @param {Number} password 新密码 - * @return {Object} - * @example ./api/user/change_password.json - */ - - }, { - key: 'changePassword', - value: function () { - var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(ctx) { - var params, userInst, user, passsalt, data, result; - return _regenerator2.default.wrap(function _callee5$(_context5) { - while (1) { - switch (_context5.prev = _context5.next) { - case 0: - params = ctx.request.body; - userInst = _yapi2.default.getInst(_user2.default); - - if (params.uid) { - _context5.next = 4; - break; - } - - return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'uid不能为空')); - - case 4: - if (params.password) { - _context5.next = 6; - break; - } - - return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空')); - - case 6: - if (!(this.getRole() !== 'admin' && params.uid != this.getUid())) { - _context5.next = 8; - break; - } - - return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, '没有权限')); - - case 8: - if (!(this.getRole() !== 'admin')) { - _context5.next = 16; - break; - } - - if (params.old_password) { - _context5.next = 11; - break; - } - - return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '旧密码不能为空')); - - case 11: - _context5.next = 13; - return userInst.findById(params.uid); - - case 13: - user = _context5.sent; - - if (!(_yapi2.default.commons.generatePassword(params.old_password, user.passsalt) !== user.password)) { - _context5.next = 16; - break; - } - - return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, '旧密码错误')); - - case 16: - passsalt = _yapi2.default.commons.randStr(); - data = { - up_time: _yapi2.default.commons.time(), - password: _yapi2.default.commons.generatePassword(params.password, passsalt), - passsalt: passsalt - }; - _context5.prev = 18; - _context5.next = 21; - return userInst.update(params.uid, data); - - case 21: - result = _context5.sent; - - ctx.body = _yapi2.default.commons.resReturn(result); - _context5.next = 28; - break; - - case 25: - _context5.prev = 25; - _context5.t0 = _context5['catch'](18); - - ctx.body = _yapi2.default.commons.resReturn(null, 401, _context5.t0.message); - - case 28: - case 'end': - return _context5.stop(); - } - } - }, _callee5, this, [[18, 25]]); - })); - - function changePassword(_x6) { - return _ref5.apply(this, arguments); - } - - return changePassword; - }() - }, { - key: 'setLoginCookie', - value: function setLoginCookie(uid, passsalt) { - var token = jwt.sign({ uid: uid }, passsalt, { expiresIn: '7 days' }); - - this.ctx.cookies.set('_yapi_token', token, { - expires: _yapi2.default.commons.expireDate(7), - httpOnly: true - }); - this.ctx.cookies.set('_yapi_uid', uid, { - expires: _yapi2.default.commons.expireDate(7), - httpOnly: true - }); - } - - /** - * 用户注册接口 - * @interface /user/reg - * @method POST - * @category user - * @foldnumber 10 - * @param {String} email email名称,不能为空 - * @param {String} password 密码,不能为空 - * @param {String} [username] 用户名 - * @returns {Object} - * @example ./api/user/login.json - */ - - }, { - key: 'reg', - value: function () { - var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(ctx) { - var userInst, params, checkRepeat, passsalt, data, user; - return _regenerator2.default.wrap(function _callee6$(_context6) { - while (1) { - switch (_context6.prev = _context6.next) { - case 0: - //注册 - userInst = _yapi2.default.getInst(_user2.default); - params = ctx.request.body; //获取请求的参数,检查是否存在用户名和密码 - - params = _yapi2.default.commons.handleParams(params, { - username: 'string', - password: 'string', - email: 'string' - }); - - if (params.email) { - _context6.next = 5; - break; - } - - return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '邮箱不能为空')); - - case 5: - if (params.password) { - _context6.next = 7; - break; - } - - return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空')); - - case 7: - _context6.next = 9; - return userInst.checkRepeat(params.email); - - case 9: - checkRepeat = _context6.sent; - - if (!(checkRepeat > 0)) { - _context6.next = 12; - break; - } - - return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '该email已经注册')); - - case 12: - passsalt = _yapi2.default.commons.randStr(); - data = { - username: params.username, - password: _yapi2.default.commons.generatePassword(params.password, passsalt), //加密 - email: params.email, - passsalt: passsalt, - role: 'member', - add_time: _yapi2.default.commons.time(), - up_time: _yapi2.default.commons.time(), - type: "site" - }; - - - if (!data.username) { - data.username = data.email.substr(0, data.email.indexOf('@')); - } - - _context6.prev = 15; - _context6.next = 18; - return userInst.save(data); - - case 18: - user = _context6.sent; - - - this.setLoginCookie(user._id, user.passsalt); - ctx.body = _yapi2.default.commons.resReturn({ - uid: user._id, - email: user.email, - username: user.username, - add_time: user.add_time, - up_time: user.up_time, - role: 'member' - }); - _yapi2.default.commons.sendMail({ - to: user.email, - contents: '

    \u4EB2\u7231\u7684\u7528\u6237\uFF1A

    \u60A8\u597D\uFF0C\u611F\u8C22\u4F7F\u7528YApi,\u60A8\u7684\u8D26\u53F7 ' + params.email + ' \u5DF2\u7ECF\u6CE8\u518C\u6210\u529F

    ' - }); - _context6.next = 27; - break; - - case 24: - _context6.prev = 24; - _context6.t0 = _context6['catch'](15); - - ctx.body = _yapi2.default.commons.resReturn(null, 401, _context6.t0.message); - - case 27: - case 'end': - return _context6.stop(); - } - } - }, _callee6, this, [[15, 24]]); - })); - - function reg(_x7) { - return _ref6.apply(this, arguments); - } - - return reg; - }() - - /** - * 获取用户列表 - * @interface /user/list - * @method GET - * @category user - * @foldnumber 10 - * @param {Number} [page] 分页页码 - * @param {Number} [limit] 分页大小,默认为10条 - * @returns {Object} - * @example - */ - - }, { - key: 'list', - value: function () { - var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(ctx) { - var page, limit, userInst, user, count; - return _regenerator2.default.wrap(function _callee7$(_context7) { - while (1) { - switch (_context7.prev = _context7.next) { - case 0: - page = ctx.request.query.page || 1, limit = ctx.request.query.limit || 10; - userInst = _yapi2.default.getInst(_user2.default); - _context7.prev = 2; - _context7.next = 5; - return userInst.listWithPaging(page, limit); - - case 5: - user = _context7.sent; - _context7.next = 8; - return userInst.listCount(); - - case 8: - count = _context7.sent; - return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn({ - total: Math.ceil(count / limit), - list: user - })); - - case 12: - _context7.prev = 12; - _context7.t0 = _context7['catch'](2); - return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, _context7.t0.message)); - - case 15: - case 'end': - return _context7.stop(); - } - } - }, _callee7, this, [[2, 12]]); - })); - - function list(_x8) { - return _ref7.apply(this, arguments); - } - - return list; - }() - - /** - * 获取用户个人信息 - * @interface /user/find - * @method GET - * @param id 用户uid - * @category user - * @foldnumber 10 - * @returns {Object} - * @example - */ - - }, { - key: 'findById', - value: function () { - var _ref8 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(ctx) { - var userInst, id, result; - return _regenerator2.default.wrap(function _callee8$(_context8) { - while (1) { - switch (_context8.prev = _context8.next) { - case 0: - _context8.prev = 0; - userInst = _yapi2.default.getInst(_user2.default); - id = ctx.request.query.id; - - if (id) { - _context8.next = 5; - break; - } - - return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'uid不能为空')); - - case 5: - _context8.next = 7; - return userInst.findById(id); - - case 7: - result = _context8.sent; - - if (result) { - _context8.next = 10; - break; - } - - return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, '不存在的用户')); - - case 10: - return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn({ - uid: result._id, - username: result.username, - email: result.email, - role: result.role, - add_time: result.add_time, - up_time: result.up_time - })); - - case 13: - _context8.prev = 13; - _context8.t0 = _context8['catch'](0); - return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, _context8.t0.message)); - - case 16: - case 'end': - return _context8.stop(); - } - } - }, _callee8, this, [[0, 13]]); - })); - - function findById(_x9) { - return _ref8.apply(this, arguments); - } - - return findById; - }() - - /** - * 删除用户,只有admin用户才有此权限 - * @interface /user/del - * @method POST - * @param id 用户uid - * @category user - * @foldnumber 10 - * @returns {Object} - * @example - */ - - }, { - key: 'del', - value: function () { - var _ref9 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9(ctx) { - var userInst, id, result; - return _regenerator2.default.wrap(function _callee9$(_context9) { - while (1) { - switch (_context9.prev = _context9.next) { - case 0: - _context9.prev = 0; - - if (!(this.getRole() !== 'admin')) { - _context9.next = 3; - break; - } - - return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, 'Without permission.')); - - case 3: - userInst = _yapi2.default.getInst(_user2.default); - id = ctx.request.body.id; - - if (id) { - _context9.next = 7; - break; - } - - return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'uid不能为空')); - - case 7: - _context9.next = 9; - return userInst.del(id); - - case 9: - result = _context9.sent; - - - ctx.body = _yapi2.default.commons.resReturn(result); - _context9.next = 16; - break; - - case 13: - _context9.prev = 13; - _context9.t0 = _context9['catch'](0); - - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context9.t0.message); - - case 16: - case 'end': - return _context9.stop(); - } - } - }, _callee9, this, [[0, 13]]); - })); - - function del(_x10) { - return _ref9.apply(this, arguments); - } - - return del; - }() - - /** - * 更新用户个人信息 - * @interface /user/update - * @method POST - * @param uid 用户uid - * @param [role] 用户角色,只有管理员有权限修改 - * @param [username] String - * @param [email] String - * @category user - * @foldnumber 10 - * @returns {Object} - * @example - */ - - }, { - key: 'update', - value: function () { - var _ref10 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(ctx) { - var params, userInst, id, data, checkRepeat, result; - return _regenerator2.default.wrap(function _callee10$(_context10) { - while (1) { - switch (_context10.prev = _context10.next) { - case 0: - _context10.prev = 0; - params = ctx.request.body; - - - params = _yapi2.default.commons.handleParams(params, { - username: 'string', - email: 'string' - }); - - if (!(this.getRole() !== 'admin' && params.uid != this.getUid())) { - _context10.next = 5; - break; - } - - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '没有权限')); - - case 5: - userInst = _yapi2.default.getInst(_user2.default); - id = params.uid; - - if (id) { - _context10.next = 9; - break; - } - - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'uid不能为空')); - - case 9: - data = { - up_time: _yapi2.default.commons.time() - }; - - - params.username && (data.username = params.username); - params.email && (data.email = params.email); - - if (!data.email) { - _context10.next = 18; - break; - } - - _context10.next = 15; - return userInst.checkRepeat(data.email); - - case 15: - checkRepeat = _context10.sent; - - if (!(checkRepeat > 0)) { - _context10.next = 18; - break; - } - - return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '该email已经注册')); - - case 18: - _context10.next = 20; - return userInst.update(id, data); - - case 20: - result = _context10.sent; - - - ctx.body = _yapi2.default.commons.resReturn(result); - _context10.next = 27; - break; - - case 24: - _context10.prev = 24; - _context10.t0 = _context10['catch'](0); - - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context10.t0.message); - - case 27: - case 'end': - return _context10.stop(); - } - } - }, _callee10, this, [[0, 24]]); - })); - - function update(_x11) { - return _ref10.apply(this, arguments); - } - - return update; - }() - - /** - * - * @param {*} basecode base64编码,通过h5 api传给后端 - */ - - }, { - key: 'uploadAvatar', - value: function () { - var _ref11 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11(ctx) { - var basecode, pngPrefix, jpegPrefix, type, strLength, avatarInst, result; - return _regenerator2.default.wrap(function _callee11$(_context11) { - while (1) { - switch (_context11.prev = _context11.next) { - case 0: - _context11.prev = 0; - basecode = ctx.request.body.basecode; - - if (basecode) { - _context11.next = 4; - break; - } - - return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'basecode不能为空')); - - case 4: - pngPrefix = 'data:image/png;base64,'; - jpegPrefix = 'data:image/jpeg;base64,'; - type = void 0; - - if (!(basecode.substr(0, pngPrefix.length) === pngPrefix)) { - _context11.next = 12; - break; - } - - basecode = basecode.substr(pngPrefix.length); - type = 'image/png'; - _context11.next = 18; - break; - - case 12: - if (!(basecode.substr(0, jpegPrefix.length) === jpegPrefix)) { - _context11.next = 17; - break; - } - - basecode = basecode.substr(jpegPrefix.length); - type = 'image/jpeg'; - _context11.next = 18; - break; - - case 17: - return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '仅支持jpeg和png格式的图片')); - - case 18: - strLength = basecode.length; - - if (!(parseInt(strLength - strLength / 8 * 2) > 200000)) { - _context11.next = 21; - break; - } - - return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '图片大小不能超过200kb')); - - case 21: - avatarInst = _yapi2.default.getInst(_avatar2.default); - _context11.next = 24; - return avatarInst.up(this.getUid(), basecode, type); - - case 24: - result = _context11.sent; - - ctx.body = _yapi2.default.commons.resReturn(result); - - _context11.next = 31; - break; - - case 28: - _context11.prev = 28; - _context11.t0 = _context11['catch'](0); - - ctx.body = _yapi2.default.commons.resReturn(null, 401, _context11.t0.message); - - case 31: - case 'end': - return _context11.stop(); - } - } - }, _callee11, this, [[0, 28]]); - })); - - function uploadAvatar(_x12) { - return _ref11.apply(this, arguments); - } - - return uploadAvatar; - }() - }, { - key: 'avatar', - value: function () { - var _ref12 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(ctx) { - var avatarInst, data, dataBuffer, type; - return _regenerator2.default.wrap(function _callee12$(_context12) { - while (1) { - switch (_context12.prev = _context12.next) { - case 0: - _context12.prev = 0; - avatarInst = _yapi2.default.getInst(_avatar2.default); - _context12.next = 4; - return avatarInst.get(this.getUid()); - - case 4: - data = _context12.sent; - dataBuffer = void 0, type = void 0; - - if (!data || !data.basecode) { - dataBuffer = _yapi2.default.fs.readFileSync(_yapi2.default.path.join(_yapi2.default.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; - _context12.next = 14; - break; - - case 11: - _context12.prev = 11; - _context12.t0 = _context12['catch'](0); - - ctx.body = 'error:' + _context12.t0.message; - - case 14: - case 'end': - return _context12.stop(); - } - } - }, _callee12, this, [[0, 11]]); - })); - - function avatar(_x13) { - return _ref12.apply(this, arguments); - } - - return avatar; - }() - - /** - * 模糊搜索用户名或者email - * @interface /user/search - * @method GET - * @category user - * @foldnumber 10 - * @param {String} q - * @return {Object} - * @example ./api/user/search.json - */ - - }, { - key: 'search', - value: function () { - var _ref13 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee13(ctx) { - var q, queryList, rules, filteredRes; - return _regenerator2.default.wrap(function _callee13$(_context13) { - while (1) { - switch (_context13.prev = _context13.next) { - case 0: - q = ctx.request.query.q; - - if (q) { - _context13.next = 3; - break; - } - - return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'No keyword.')); - - case 3: - if (_yapi2.default.commons.validateSearchKeyword(q)) { - _context13.next = 5; - break; - } - - return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'Bad query.')); - - case 5: - _context13.next = 7; - return this.Model.search(q); - - case 7: - queryList = _context13.sent; - rules = [{ - key: '_id', - alias: 'uid' - }, 'username', 'email', 'role', { - key: 'add_time', - alias: 'addTime' - }, { - key: 'up_time', - alias: 'upTime' - }]; - filteredRes = _commons2.default.filterRes(queryList, rules); - return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(filteredRes, 0, 'ok')); - - case 11: - case 'end': - return _context13.stop(); - } - } - }, _callee13, this); - })); - - function search(_x14) { - return _ref13.apply(this, arguments); - } - - return search; - }() - - /** - * 根据路由id获取面包屑数据 - * @interface /user/nav - * @method GET - * @category user - * @foldnumber 10 - * @param {String} type 可选group|interface|project - * @param {Number} id - * @return {Object} - * @example ./api/user/nav.json - */ - - }, { - key: 'nav', - 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; - - return _regenerator2.default.wrap(function _callee14$(_context14) { - while (1) { - switch (_context14.prev = _context14.next) { - case 0: - _ctx$request$query = ctx.request.query, id = _ctx$request$query.id, type = _ctx$request$query.type; - result = {}; - _context14.prev = 2; - - if (!(type === 'interface')) { - _context14.next = 12; - break; - } - - interfaceInst = _yapi2.default.getInst(_interface2.default); - _context14.next = 7; - return interfaceInst.get(id); - - case 7: - interfaceData = _context14.sent; - - result["interface_id"] = interfaceData._id; - result["interface_name"] = interfaceData.path; - - type = 'project'; - id = interfaceData.project_id; - - case 12: - if (!(type === 'project')) { - _context14.next = 21; - break; - } - - projectInst = _yapi2.default.getInst(_project2.default); - _context14.next = 16; - return projectInst.get(id); - - case 16: - projectData = _context14.sent; - - result["project_id"] = projectData._id; - result["project_name"] = projectData.prd_host + projectData.basepath; - type = 'group'; - id = projectData.group_id; - - case 21: - if (!(type === 'group')) { - _context14.next = 28; - break; - } - - groupInst = _yapi2.default.getInst(_group2.default); - _context14.next = 25; - return groupInst.get(id); - - case 25: - groupData = _context14.sent; - - result["group_id"] = groupData._id; - result["group_name"] = groupData.group_name; - - case 28: - return _context14.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(result)); - - case 31: - _context14.prev = 31; - _context14.t0 = _context14['catch'](2); - return _context14.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(result, 422, _context14.t0.message)); - - case 34: - case 'end': - return _context14.stop(); - } - } - }, _callee14, this, [[2, 31]]); - })); - - function nav(_x15) { - return _ref14.apply(this, arguments); - } - - return nav; - }() - }]); - return userController; -}(_base2.default); - -module.exports = userController; \ No newline at end of file diff --git a/server_dist/middleware/checkToken.js b/server_dist/middleware/checkToken.js deleted file mode 100644 index 0eca1c2f..00000000 --- a/server_dist/middleware/checkToken.js +++ /dev/null @@ -1,36 +0,0 @@ -"use strict"; - -var _regenerator = require("babel-runtime/regenerator"); - -var _regenerator2 = _interopRequireDefault(_regenerator); - -var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator"); - -var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -// const jwt = require('jsonwebtoken'); -//检查token是否过期 - -module.exports = function () { - var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) { - return _regenerator2.default.wrap(function _callee$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - _context.next = 2; - return next(); - - case 2: - case "end": - return _context.stop(); - } - } - }, _callee, undefined); - })); - - return function (_x, _x2) { - return _ref.apply(this, arguments); - }; -}(); \ No newline at end of file diff --git a/server_dist/middleware/mockServer.js b/server_dist/middleware/mockServer.js deleted file mode 100644 index 2ee0428d..00000000 --- a/server_dist/middleware/mockServer.js +++ /dev/null @@ -1,177 +0,0 @@ -'use strict'; - -var _regenerator = require('babel-runtime/regenerator'); - -var _regenerator2 = _interopRequireDefault(_regenerator); - -var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); - -var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _project2 = require('../models/project.js'); - -var _project3 = _interopRequireDefault(_project2); - -var _interface = require('../models/interface.js'); - -var _interface2 = _interopRequireDefault(_interface); - -var _mockjs = require('mockjs'); - -var _mockjs2 = _interopRequireDefault(_mockjs); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -module.exports = function () { - var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) { - var hostname, config, projectInst, projects, matchProject, maxBasepath, i, l, _project, project, interfaceData, interfaceInst, res; - - return _regenerator2.default.wrap(function _callee$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - _yapi2.default.commons.log('Server Recevie Request...'); - - hostname = ctx.hostname; - config = _yapi2.default.WEBCONFIG; - - if (!(ctx.hostname === config.webhost)) { - _context.next = 8; - break; - } - - if (!next) { - _context.next = 7; - break; - } - - _context.next = 7; - return next(); - - case 7: - return _context.abrupt('return', true); - - case 8: - - _yapi2.default.commons.log('MockServer Running...'); - projectInst = _yapi2.default.getInst(_project3.default), projects = void 0; - _context.prev = 10; - _context.next = 13; - return projectInst.getByDomain(hostname); - - case 13: - projects = _context.sent; - _context.next = 19; - break; - - case 16: - _context.prev = 16; - _context.t0 = _context['catch'](10); - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 403, _context.t0.message)); - - case 19: - matchProject = [], maxBasepath = 0; - - - for (i = 0, l = projects.length; i < l; i++) { - _project = projects[i]; - - if (ctx.path && _project.basepath == "") { - matchProject = _project; - } else if (ctx.path && ctx.path.indexOf(_project.basepath) === 0) { - if (_project.basepath.length > maxBasepath) { - maxBasepath = _project.basepath.length; - matchProject = _project; - } - } - } - - if (!(matchProject === false)) { - _context.next = 23; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '不存在的domain')); - - case 23: - project = matchProject, interfaceData = void 0; - interfaceInst = _yapi2.default.getInst(_interface2.default); - _context.prev = 25; - _context.next = 28; - return interfaceInst.getByPath(project._id, ctx.path.substr(project.basepath.length), ctx.method); - - case 28: - interfaceData = _context.sent; - - if (!(!interfaceData || interfaceData.length === 0)) { - _context.next = 35; - break; - } - - if (!(ctx.method === 'OPTIONS')) { - _context.next = 34; - break; - } - - ctx.set("Access-Control-Allow-Origin", "*"); - ctx.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); - return _context.abrupt('return', ctx.body = 'ok'); - - case 34: - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 404, '不存在的api')); - - case 35: - if (!(interfaceData.length > 1)) { - _context.next = 37; - break; - } - - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '存在多个api,请检查数据库')); - - case 37: - - interfaceData = interfaceData[0]; - ctx.set("Access-Control-Allow-Origin", "*"); - - if (!(interfaceData.res_body_type === 'json')) { - _context.next = 48; - break; - } - - _context.prev = 40; - res = _mockjs2.default.mock(_yapi2.default.commons.json_parse(interfaceData.res_body)); - return _context.abrupt('return', ctx.body = res); - - case 45: - _context.prev = 45; - _context.t1 = _context['catch'](40); - return _context.abrupt('return', ctx.body = { - errcode: 400, - errmsg: 'mock json数据格式有误', - data: interfaceData.res_body - }); - - case 48: - return _context.abrupt('return', ctx.body = interfaceData.res_body); - - case 51: - _context.prev = 51; - _context.t2 = _context['catch'](25); - return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 409, _context.t2.message)); - - case 54: - case 'end': - return _context.stop(); - } - } - }, _callee, undefined, [[10, 16], [25, 51], [40, 45]]); - })); - - return function (_x, _x2) { - return _ref.apply(this, arguments); - }; -}(); \ No newline at end of file diff --git a/server_dist/middleware/userauth.js b/server_dist/middleware/userauth.js deleted file mode 100644 index 3cbed01f..00000000 --- a/server_dist/middleware/userauth.js +++ /dev/null @@ -1,44 +0,0 @@ -"use strict"; - -var _regenerator = require("babel-runtime/regenerator"); - -var _regenerator2 = _interopRequireDefault(_regenerator); - -var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator"); - -var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -module.exports = function () { - var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) { - var path; - return _regenerator2.default.wrap(function _callee$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - path = ctx.path; - - console.log(path); // eslint-disable-line - console.log(ctx.hostname); // eslint-disable-line - - if (!next) { - _context.next = 6; - break; - } - - _context.next = 6; - return next(); - - case 6: - case "end": - return _context.stop(); - } - } - }, _callee, undefined); - })); - - return function (_x, _x2) { - return _ref.apply(this, arguments); - }; -}(); \ No newline at end of file diff --git a/server_dist/models/avatar.js b/server_dist/models/avatar.js deleted file mode 100644 index 89a4d343..00000000 --- a/server_dist/models/avatar.js +++ /dev/null @@ -1,81 +0,0 @@ -'use strict'; - -var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); - -var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); - -var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); - -var _inherits2 = require('babel-runtime/helpers/inherits'); - -var _inherits3 = _interopRequireDefault(_inherits2); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _base = require('./base.js'); - -var _base2 = _interopRequireDefault(_base); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -// import userModel from '../models/user.js'; - -var avatarModel = function (_baseModel) { - (0, _inherits3.default)(avatarModel, _baseModel); - - function avatarModel() { - (0, _classCallCheck3.default)(this, avatarModel); - return (0, _possibleConstructorReturn3.default)(this, (avatarModel.__proto__ || (0, _getPrototypeOf2.default)(avatarModel)).apply(this, arguments)); - } - - (0, _createClass3.default)(avatarModel, [{ - key: 'getName', - value: function getName() { - return 'avatar'; - } - }, { - key: 'getSchema', - value: function getSchema() { - return { - uid: { type: Number, required: true }, - basecode: String, - type: String - }; - } - }, { - key: 'get', - value: function get(uid) { - - return this.model.findOne({ - uid: uid - }); - } - }, { - key: 'up', - value: function up(uid, basecode, type) { - return this.model.update({ - uid: uid - }, { - type: type, - basecode: basecode - }, { - upsert: true - }); - } - }]); - return avatarModel; -}(_base2.default); - -module.exports = avatarModel; \ No newline at end of file diff --git a/server_dist/models/base.js b/server_dist/models/base.js deleted file mode 100644 index 2680baef..00000000 --- a/server_dist/models/base.js +++ /dev/null @@ -1,82 +0,0 @@ -'use strict'; - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _mongoose = require('mongoose'); - -var _mongoose2 = _interopRequireDefault(_mongoose); - -var _mongooseAutoIncrement = require('mongoose-auto-increment'); - -var _mongooseAutoIncrement2 = _interopRequireDefault(_mongooseAutoIncrement); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -/** - * 所有的model都需要继承baseModel, 且需要 getSchema和getName方法,不然会报错 - */ - -var baseModel = function () { - function baseModel() { - (0, _classCallCheck3.default)(this, baseModel); - - this.schema = new _mongoose2.default.Schema(this.getSchema()); - this.name = this.getName(); - - if (this.isNeedAutoIncrement() === true) { - this.schema.plugin(_mongooseAutoIncrement2.default.plugin, { - model: this.name, - field: this.getPrimaryKey(), - startAt: 101, - incrementBy: _yapi2.default.commons.rand(1, 10) - }); - } - - this.model = _yapi2.default.db(this.name, this.schema); - } - - (0, _createClass3.default)(baseModel, [{ - key: 'isNeedAutoIncrement', - value: function isNeedAutoIncrement() { - return true; - } - - /** - * 可通过覆盖此方法生成其他自增字段 - */ - - }, { - key: 'getPrimaryKey', - value: function getPrimaryKey() { - return '_id'; - } - - /** - * 获取collection的schema结构 - */ - - }, { - key: 'getSchema', - value: function getSchema() { - _yapi2.default.commons.log('Model Class need getSchema function', 'error'); - } - }, { - key: 'getName', - value: function getName() { - _yapi2.default.commons.log('Model Class need name', 'error'); - } - }]); - return baseModel; -}(); - -module.exports = baseModel; \ No newline at end of file diff --git a/server_dist/models/follow.js b/server_dist/models/follow.js deleted file mode 100644 index 343250f4..00000000 --- a/server_dist/models/follow.js +++ /dev/null @@ -1,109 +0,0 @@ -'use strict'; - -var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); - -var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); - -var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); - -var _inherits2 = require('babel-runtime/helpers/inherits'); - -var _inherits3 = _interopRequireDefault(_inherits2); - -var _base = require('./base.js'); - -var _base2 = _interopRequireDefault(_base); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var followModel = function (_baseModel) { - (0, _inherits3.default)(followModel, _baseModel); - - function followModel() { - (0, _classCallCheck3.default)(this, followModel); - return (0, _possibleConstructorReturn3.default)(this, (followModel.__proto__ || (0, _getPrototypeOf2.default)(followModel)).apply(this, arguments)); - } - - (0, _createClass3.default)(followModel, [{ - key: 'getName', - value: function getName() { - return 'follow'; - } - }, { - key: 'getSchema', - value: function getSchema() { - return { - uid: { type: Number, required: true }, - projectid: { type: Number, required: true }, - projectname: { type: String, required: true }, - icon: String - }; - } - - /** - * @param {Number} uid 用户id - * @param {Number} projectid 项目id - * @param {String} projectname 项目名 - * @param {String} icon 项目图标 - */ - - }, { - key: 'save', - value: function save(data) { - //关注 - var saveData = { - uid: data.uid, - projectid: data.projectid, - projectname: data.projectname, - icon: data.icon - }; - var follow = new this.model(saveData); - return follow.save(); - } - }, { - key: 'del', - value: function del(id) { - return this.model.deleteOne({ - _id: id - }); - } - }, { - key: 'listWithPaging', - value: function listWithPaging(uid, page, limit) { - page = parseInt(page); - limit = parseInt(limit); - - return this.model.find({ - uid: uid - }).skip((page - 1) * limit).limit(limit).exec(); - } - }, { - key: 'listCount', - value: function listCount(uid) { - return this.model.count({ - uid: uid - }); - } - }, { - key: 'checkProjectRepeat', - value: function checkProjectRepeat(uid, projectid) { - return this.model.count({ - uid: uid, - projectid: projectid - }); - } - }]); - return followModel; -}(_base2.default); - -module.exports = followModel; \ No newline at end of file diff --git a/server_dist/models/group.js b/server_dist/models/group.js deleted file mode 100644 index 685e7d76..00000000 --- a/server_dist/models/group.js +++ /dev/null @@ -1,153 +0,0 @@ -'use strict'; - -var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); - -var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); - -var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); - -var _inherits2 = require('babel-runtime/helpers/inherits'); - -var _inherits3 = _interopRequireDefault(_inherits2); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _base = require('./base.js'); - -var _base2 = _interopRequireDefault(_base); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var groupModel = function (_baseModel) { - (0, _inherits3.default)(groupModel, _baseModel); - - function groupModel() { - (0, _classCallCheck3.default)(this, groupModel); - return (0, _possibleConstructorReturn3.default)(this, (groupModel.__proto__ || (0, _getPrototypeOf2.default)(groupModel)).apply(this, arguments)); - } - - (0, _createClass3.default)(groupModel, [{ - key: 'getName', - value: function getName() { - return 'group'; - } - }, { - key: 'getSchema', - value: function getSchema() { - return { - uid: Number, - group_name: String, - group_desc: String, - add_time: Number, - up_time: Number, - members: [{ - uid: Number, - role: { type: String, enum: ['owner', 'dev'] }, - username: String, - email: String - }] - }; - } - }, { - key: 'save', - value: function save(data) { - var m = new this.model(data); - return m.save(); - } - }, { - key: 'get', - value: function get(id) { - return this.model.findOne({ - _id: id - }).exec(); - } - }, { - key: 'checkRepeat', - value: function checkRepeat(name) { - return this.model.count({ - group_name: name - }); - } - }, { - key: 'addMember', - value: function addMember(id, data) { - return this.model.update({ - _id: id - }, { - $push: { members: data } - }); - } - }, { - key: 'delMember', - value: function delMember(id, uid) { - return this.model.update({ - _id: id - }, { - $pull: { members: { uid: uid } } - }); - } - }, { - key: 'changeMemberRole', - value: function changeMemberRole(id, uid, role) { - return this.model.update({ - _id: id, - "members.uid": uid - }, { - "$set": { "members.$.uid": role } - }); - } - }, { - key: 'checkMemberRepeat', - value: function checkMemberRepeat(id, uid) { - return this.model.count({ - _id: id, - "members.uid": uid - }); - } - }, { - key: 'list', - value: function list() { - return this.model.find().select('group_name _id group_desc add_time up_time').exec(); - } - }, { - key: 'del', - value: function del(id) { - return this.model.deleteOne({ - _id: id - }); - } - }, { - key: 'up', - value: function up(id, data) { - return this.model.update({ - _id: id - }, { - group_name: data.group_name, - group_desc: data.group_desc, - up_time: _yapi2.default.commons.time() - }); - } - }, { - key: 'search', - value: function search(keyword) { - return this.model.find({ - group_name: new RegExp(keyword, 'i') - }).limit(10); - } - }]); - return groupModel; -}(_base2.default); - -module.exports = groupModel; \ No newline at end of file diff --git a/server_dist/models/interface.js b/server_dist/models/interface.js deleted file mode 100644 index 7caef0e3..00000000 --- a/server_dist/models/interface.js +++ /dev/null @@ -1,144 +0,0 @@ -'use strict'; - -var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); - -var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); - -var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); - -var _inherits2 = require('babel-runtime/helpers/inherits'); - -var _inherits3 = _interopRequireDefault(_inherits2); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _base = require('./base.js'); - -var _base2 = _interopRequireDefault(_base); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var interfaceModel = function (_baseModel) { - (0, _inherits3.default)(interfaceModel, _baseModel); - - function interfaceModel() { - (0, _classCallCheck3.default)(this, interfaceModel); - return (0, _possibleConstructorReturn3.default)(this, (interfaceModel.__proto__ || (0, _getPrototypeOf2.default)(interfaceModel)).apply(this, arguments)); - } - - (0, _createClass3.default)(interfaceModel, [{ - key: 'getName', - value: function getName() { - return 'interface'; - } - }, { - key: 'getSchema', - value: function getSchema() { - return { - title: { type: String, required: true }, - uid: { type: Number, required: true }, - path: { type: String, required: true }, - method: { type: String, required: true }, - project_id: { type: Number, required: true }, - desc: String, - add_time: Number, - up_time: Number, - req_query: [{ - name: String, value: String, desc: String, required: Boolean - }], - req_headers: [{ - name: String, value: String, desc: String, required: Boolean - }], - req_params_type: { - type: String, - enum: ['form', 'json', 'text', 'xml'] - }, - req_params_form: [{ - name: String, value: String, value_type: { type: String, enum: ['text', 'file'] }, desc: String, required: Boolean - }], - req_params_other: String, - res_body_type: { - type: String, - enum: ['json', 'text', 'xml'] - }, - res_body: String - }; - } - }, { - key: 'save', - value: function save(data) { - var m = new this.model(data); - return m.save(); - } - }, { - key: 'get', - value: function get(id) { - return this.model.findOne({ - _id: id - }).exec(); - } - }, { - key: 'getByPath', - value: function getByPath(project_id, path, method) { - return this.model.find({ - project_id: project_id, - path: path, - method: method - }).exec(); - } - }, { - key: 'checkRepeat', - value: function checkRepeat(id, path, method) { - return this.model.count({ - project_id: id, - path: path, - method: method - }); - } - }, { - key: 'countByProjectId', - value: function countByProjectId(id) { - return this.model.count({ - project_id: id - }); - } - }, { - key: 'list', - value: function list(project_id) { - console.log(project_id); - return this.model.find({ - project_id: project_id - }).sort({ _id: -1 }).exec(); - } - }, { - key: 'del', - value: function del(id) { - return this.model.deleteOne({ - _id: id - }); - } - }, { - key: 'up', - value: function up(id, data) { - data.up_time = _yapi2.default.commons.time(); - return this.model.update({ - _id: id - }, data, { runValidators: true }); - } - }]); - return interfaceModel; -}(_base2.default); - -module.exports = interfaceModel; \ No newline at end of file diff --git a/server_dist/models/interfaceCase.js b/server_dist/models/interfaceCase.js deleted file mode 100644 index 5b8f5ba4..00000000 --- a/server_dist/models/interfaceCase.js +++ /dev/null @@ -1,122 +0,0 @@ -'use strict'; - -var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); - -var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); - -var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); - -var _inherits2 = require('babel-runtime/helpers/inherits'); - -var _inherits3 = _interopRequireDefault(_inherits2); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _base = require('./base.js'); - -var _base2 = _interopRequireDefault(_base); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var interfaceCase = function (_baseModel) { - (0, _inherits3.default)(interfaceCase, _baseModel); - - function interfaceCase() { - (0, _classCallCheck3.default)(this, interfaceCase); - return (0, _possibleConstructorReturn3.default)(this, (interfaceCase.__proto__ || (0, _getPrototypeOf2.default)(interfaceCase)).apply(this, arguments)); - } - - (0, _createClass3.default)(interfaceCase, [{ - key: 'getName', - value: function getName() { - return 'interface_case'; - } - }, { - key: 'getSchema', - value: function getSchema() { - return { - casename: { type: String, required: true }, - uid: { type: Number, required: true }, - col_id: { type: Number, required: true }, - index: { type: Number, default: 0 }, - project_id: { type: Number, required: true }, - add_time: Number, - up_time: Number, - env: { type: String, required: true }, - domain: { type: String }, - path: { type: String, required: true }, - method: { type: String, required: true }, - req_query: [{ - name: String, value: String - }], - req_headers: [{ - name: String, value: String - }], - req_body_type: { - type: String, - enum: ['form', 'json', 'text', 'xml'] - }, - res_body_form: [{ - name: String, value: String - }], - res_body_other: String - - }; - } - }, { - key: 'save', - value: function save(data) { - var m = new this.model(data); - return m.save(); - } - }, { - key: 'get', - value: function get(id) { - return this.model.findOne({ - _id: id - }).exec(); - } - }, { - key: 'list', - value: function list() { - return this.model.find().select("casename uid col_id _id index").exec(); - } - }, { - key: 'del', - value: function del(id) { - return this.model.deleteOne({ - _id: id - }); - } - }, { - key: 'up', - value: function up(id, data) { - data.up_time = _yapi2.default.commons.time(); - return this.model.update({ _id: id }, data); - } - }, { - key: 'upCaseIndex', - value: function upCaseIndex(id, index) { - return this.model.update({ - _id: id - }, { - index: index - }); - } - }]); - return interfaceCase; -}(_base2.default); - -module.exports = interfaceCase; \ No newline at end of file diff --git a/server_dist/models/interfaceCol.js b/server_dist/models/interfaceCol.js deleted file mode 100644 index efd06e40..00000000 --- a/server_dist/models/interfaceCol.js +++ /dev/null @@ -1,104 +0,0 @@ -'use strict'; - -var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); - -var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); - -var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); - -var _inherits2 = require('babel-runtime/helpers/inherits'); - -var _inherits3 = _interopRequireDefault(_inherits2); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _base = require('./base.js'); - -var _base2 = _interopRequireDefault(_base); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var interfaceCol = function (_baseModel) { - (0, _inherits3.default)(interfaceCol, _baseModel); - - function interfaceCol() { - (0, _classCallCheck3.default)(this, interfaceCol); - return (0, _possibleConstructorReturn3.default)(this, (interfaceCol.__proto__ || (0, _getPrototypeOf2.default)(interfaceCol)).apply(this, arguments)); - } - - (0, _createClass3.default)(interfaceCol, [{ - key: 'getName', - value: function getName() { - return 'interface_col'; - } - }, { - key: 'getSchema', - value: function getSchema() { - return { - name: { type: String, required: true }, - uid: { type: Number, required: true }, - project_id: { type: Number, required: true }, - desc: String, - add_time: Number, - up_time: Number - }; - } - }, { - key: 'save', - value: function save(data) { - var m = new this.model(data); - return m.save(); - } - }, { - key: 'get', - value: function get(id) { - return this.model.findOne({ - _id: id - }).exec(); - } - }, { - key: 'checkRepeat', - value: function checkRepeat(name) { - return this.model.count({ - name: name - }); - } - }, { - key: 'list', - value: function list(project_id) { - return this.model.find({ - project_id: project_id - }).exec(); - } - }, { - key: 'del', - value: function del(id) { - return this.model.deleteOne({ - _id: id - }); - } - }, { - key: 'up', - value: function up(id, data) { - data.up_time = _yapi2.default.commons.time(); - return this.model.update({ - _id: id - }, data); - } - }]); - return interfaceCol; -}(_base2.default); - -module.exports = interfaceCol; \ No newline at end of file diff --git a/server_dist/models/log.js b/server_dist/models/log.js deleted file mode 100644 index 401c918c..00000000 --- a/server_dist/models/log.js +++ /dev/null @@ -1,117 +0,0 @@ -'use strict'; - -var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); - -var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); - -var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); - -var _inherits2 = require('babel-runtime/helpers/inherits'); - -var _inherits3 = _interopRequireDefault(_inherits2); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _base = require('./base.js'); - -var _base2 = _interopRequireDefault(_base); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -// import userModel from '../models/user.js'; - -var logModel = function (_baseModel) { - (0, _inherits3.default)(logModel, _baseModel); - - function logModel() { - (0, _classCallCheck3.default)(this, logModel); - return (0, _possibleConstructorReturn3.default)(this, (logModel.__proto__ || (0, _getPrototypeOf2.default)(logModel)).apply(this, arguments)); - } - - (0, _createClass3.default)(logModel, [{ - key: 'getName', - value: function getName() { - return 'log'; - } - }, { - key: 'getSchema', - value: function getSchema() { - return { - uid: { type: Number, required: true }, - typeid: { type: Number, required: true }, - type: { type: String, enum: ['user', 'group', 'interface', 'project', 'other', 'interface_col'], required: true }, - content: { type: String, required: true }, - username: { type: String, required: true }, - add_time: Number - }; - } - - /** - * @param {String} content log内容 - * @param {Enum} type log类型, ['user', 'group', 'interface', 'project', 'other'] - * @param {Number} uid 用户id - */ - - }, { - key: 'save', - value: function save(data) { - var saveData = { - content: data.content, - type: data.type, - uid: data.uid, - username: data.username, - typeid: data.typeid, - add_time: _yapi2.default.commons.time() - }; - var log = new this.model(saveData); - - return log.save(); - } - }, { - key: 'del', - value: function del(id) { - return this.model.deleteOne({ - _id: id - }); - } - }, { - key: 'list', - value: function list(typeid) { - return this.model.find({ - typeid: typeid - }).exec(); - } - }, { - key: 'listWithPaging', - value: function listWithPaging(typeid, page, limit) { - page = parseInt(page); - limit = parseInt(limit); - - return this.model.find({ - typeid: typeid - }).skip((page - 1) * limit).limit(limit).exec(); - } - }, { - key: 'listCount', - value: function listCount(typeid) { - return this.model.count({ - typeid: typeid - }); - } - }]); - return logModel; -}(_base2.default); - -module.exports = logModel; \ No newline at end of file diff --git a/server_dist/models/project.js b/server_dist/models/project.js deleted file mode 100644 index b9b4988d..00000000 --- a/server_dist/models/project.js +++ /dev/null @@ -1,200 +0,0 @@ -'use strict'; - -var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); - -var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); - -var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); - -var _inherits2 = require('babel-runtime/helpers/inherits'); - -var _inherits3 = _interopRequireDefault(_inherits2); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _base = require('./base.js'); - -var _base2 = _interopRequireDefault(_base); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var projectModel = function (_baseModel) { - (0, _inherits3.default)(projectModel, _baseModel); - - function projectModel() { - (0, _classCallCheck3.default)(this, projectModel); - return (0, _possibleConstructorReturn3.default)(this, (projectModel.__proto__ || (0, _getPrototypeOf2.default)(projectModel)).apply(this, arguments)); - } - - (0, _createClass3.default)(projectModel, [{ - key: 'getName', - value: function getName() { - return 'project'; - } - }, { - key: 'getSchema', - value: function getSchema() { - return { - uid: { type: Number, required: true }, - name: { type: String, required: true }, - basepath: { type: String }, - desc: String, - group_id: { type: Number, required: true }, - project_type: { type: String, required: true, enum: ['public', 'private'] }, - members: [{ uid: Number, role: { type: String, enum: ['owner', 'dev'], username: String, email: String } }], - protocol: { type: String, required: true }, - prd_host: { type: String, required: true }, - env: [{ name: String, domain: String }], - add_time: Number, - up_time: Number - }; - } - }, { - key: 'save', - value: function save(data) { - var m = new this.model(data); - return m.save(); - } - }, { - key: 'get', - value: function get(id) { - return this.model.findOne({ - _id: id - }).exec(); - } - }, { - key: 'getByDomain', - value: function getByDomain(domain) { - return this.model.find({ - prd_host: domain - }).exec(); - } - }, { - key: 'checkNameRepeat', - value: function checkNameRepeat(name) { - return this.model.count({ - name: name - }); - } - }, { - key: 'checkDomainRepeat', - value: function checkDomainRepeat(domain, basepath) { - return this.model.count({ - prd_host: domain, - basepath: basepath - }); - } - }, { - key: 'list', - value: function list(group_id, auth) { - var params = { group_id: group_id }; - if (!auth) params.project_type = 'public'; - return this.model.find(params).sort({ _id: -1 }).exec(); - } - }, { - key: 'listWithPaging', - value: function listWithPaging(group_id, page, limit) { - page = parseInt(page); - limit = parseInt(limit); - return this.model.find({ - group_id: group_id - }).sort({ _id: -1 }).skip((page - 1) * limit).limit(limit).exec(); - } - }, { - key: 'listCount', - value: function listCount(group_id) { - return this.model.count({ - group_id: group_id - }); - } - }, { - key: 'countByGroupId', - value: function countByGroupId(group_id) { - return this.model.count({ - group_id: group_id - }); - } - }, { - key: 'del', - value: function del(id) { - return this.model.deleteOne({ - _id: id - }); - } - }, { - key: 'up', - value: function up(id, data) { - data.up_time = _yapi2.default.commons.time(); - return this.model.update({ - _id: id - }, data, { runValidators: true }); - } - }, { - key: 'addMember', - value: function addMember(id, data) { - return this.model.update({ - _id: id - }, { - $push: { members: data } - }); - } - }, { - key: 'delMember', - value: function delMember(id, uid) { - return this.model.update({ - _id: id - }, { - $pull: { members: uid } - }); - } - }, { - key: 'checkMemberRepeat', - value: function checkMemberRepeat(id, uid) { - return this.model.count({ - _id: id, - "members.uid": uid - }); - } - }, { - key: 'changeMemberRole', - value: function changeMemberRole(id, uid, role) { - return this.model.update({ - _id: id, - "members.uid": uid - }, { - "$set": { "members.$.uid": role } - }); - } - }, { - key: 'search', - value: function search(keyword) { - return this.model.find({ - name: new RegExp(keyword, 'ig') - }).limit(10); - } - }, { - key: 'download', - value: function download(id) { - console.log('models in download'); - // return this.model.find({ - // name: new RegExp(id, 'ig') - // }) - // .limit(10); - } - }]); - return projectModel; -}(_base2.default); - -module.exports = projectModel; \ No newline at end of file diff --git a/server_dist/models/user.js b/server_dist/models/user.js deleted file mode 100644 index c6a50f8d..00000000 --- a/server_dist/models/user.js +++ /dev/null @@ -1,142 +0,0 @@ -'use strict'; - -var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); - -var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); - -var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); - -var _inherits2 = require('babel-runtime/helpers/inherits'); - -var _inherits3 = _interopRequireDefault(_inherits2); - -var _base = require('./base.js'); - -var _base2 = _interopRequireDefault(_base); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var userModel = function (_baseModel) { - (0, _inherits3.default)(userModel, _baseModel); - - function userModel() { - (0, _classCallCheck3.default)(this, userModel); - return (0, _possibleConstructorReturn3.default)(this, (userModel.__proto__ || (0, _getPrototypeOf2.default)(userModel)).apply(this, arguments)); - } - - (0, _createClass3.default)(userModel, [{ - key: 'getName', - value: function getName() { - return 'user'; - } - }, { - key: 'getSchema', - value: function getSchema() { - return { - username: { - type: String, - required: true - }, - password: { - type: String, - required: true - }, - email: { - type: String, - required: true - }, - passsalt: String, - role: String, - add_time: Number, - up_time: Number, - type: { type: String, enum: ['site', 'third'], default: "site" //site用户是网站注册用户, third是第三方登录过来的用户 - } }; - } - }, { - key: 'save', - value: function save(data) { - var user = new this.model(data); - return user.save(); - } - }, { - key: 'checkRepeat', - value: function checkRepeat(email) { - return this.model.count({ - email: email - }); - } - }, { - key: 'list', - value: function list() { - return this.model.find().select('_id username email role add_time up_time').exec(); //显示id name email role - } - }, { - key: 'findByUids', - value: function findByUids(uids) { - return this.model.find({ - _id: { $in: uids } - }).select('_id username email role add_time up_time').exec(); - } - }, { - key: 'listWithPaging', - value: function listWithPaging(page, limit) { - page = parseInt(page); - limit = parseInt(limit); - return this.model.find().sort({ _id: -1 }).skip((page - 1) * limit).limit(limit).select('_id username email role add_time up_time').exec(); - } - }, { - key: 'listCount', - value: function listCount() { - return this.model.count(); - } - }, { - key: 'findByEmail', - value: function findByEmail(email) { - return this.model.findOne({ email: email }); - } - }, { - key: 'findById', - value: function findById(id) { - return this.model.findOne({ - _id: id - }); - } - }, { - key: 'del', - value: function del(id) { - return this.model.deleteOne({ - _id: id - }); - } - }, { - key: 'update', - value: function update(id, data) { - return this.model.update({ - _id: id - }, data); - } - }, { - key: 'search', - value: function search(keyword) { - return this.model.find({ - $or: [{ email: new RegExp(keyword, 'i') }, { username: new RegExp(keyword, 'i') }] - }, { - passsalt: 0, - password: 0 - }).limit(10); - } - }]); - return userModel; -}(_base2.default); - -module.exports = userModel; \ No newline at end of file diff --git a/server_dist/utils/commons.js b/server_dist/utils/commons.js deleted file mode 100644 index 9c5137f6..00000000 --- a/server_dist/utils/commons.js +++ /dev/null @@ -1,250 +0,0 @@ -'use strict'; - -var _stringify = require('babel-runtime/core-js/json/stringify'); - -var _stringify2 = _interopRequireDefault(_stringify); - -var _typeof2 = require('babel-runtime/helpers/typeof'); - -var _typeof3 = _interopRequireDefault(_typeof2); - -var _fsExtra = require('fs-extra'); - -var _fsExtra2 = _interopRequireDefault(_fsExtra); - -var _path = require('path'); - -var _path2 = _interopRequireDefault(_path); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _sha = require('sha1'); - -var _sha2 = _interopRequireDefault(_sha); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -exports.resReturn = function (data, num, errmsg) { - num = num || 0; - - return { - errcode: num, - errmsg: errmsg || 'success', - data: data - }; -}; - -exports.log = function (msg, type) { - if (!msg) { - return; - } - - type = type || 'log'; - - var f = void 0; - - switch (type) { - case 'log': - f = console.log; // eslint-disable-line - break; - case 'warn': - f = console.warn; // eslint-disable-line - break; - case 'error': - f = console.error; // eslint-disable-line - break; - default: - f = console.log; // eslint-disable-line - break; - } - - f(type + ':', msg); - - var date = new Date(); - var year = date.getFullYear(); - var month = date.getMonth(); - - var logfile = _path2.default.join(_yapi2.default.WEBROOT_LOG, year + '-' + month + '.log'); - - if ((typeof msg === 'undefined' ? 'undefined' : (0, _typeof3.default)(msg)) === 'object') { - if (msg instanceof Error) msg = msg.message;else msg = (0, _stringify2.default)(msg); - } - - var data = new Date().toLocaleTimeString() + '\t|\t' + type + '\t|\t' + msg; - - _fsExtra2.default.writeFileSync(logfile, data, { - flag: 'w+' - }); -}; - -exports.fileExist = function (filePath) { - try { - return _fsExtra2.default.statSync(filePath).isFile(); - } catch (err) { - return false; - } -}; - -exports.time = function () { - return Date.parse(new Date()) / 1000; -}; - -exports.fieldSelect = function (data, field) { - if (!data || !field || !Array.isArray(field)) { - return null; - } - - var arr = {}; - - field.forEach(function (f) { - data[f] && (arr[f] = data[f]); - }); - - return arr; -}; - -exports.rand = function (min, max) { - return Math.floor(Math.random() * (max - min) + min); -}; - -exports.json_parse = function (json) { - try { - return JSON.parse(json); - } catch (e) { - return json; - } -}; - -exports.randStr = function () { - return Math.random().toString(36).substr(2); -}; - -exports.generatePassword = function (password, passsalt) { - return (0, _sha2.default)(password + (0, _sha2.default)(passsalt)); -}; - -exports.expireDate = function (day) { - var date = new Date(); - date.setTime(date.getTime() + day * 86400000); - return date; -}; - -exports.sendMail = function (options, cb) { - if (!_yapi2.default.mail) return false; - options.subject = options.subject ? options.subject + '-yapi平台' : 'ypai平台'; - - cb = cb || function (err) { - if (err) { - _yapi2.default.commons.log('send mail ' + options.to + ' error,' + err.message, 'error'); - } else { - _yapi2.default.commons.log('send mail ' + options.to + ' success'); - } - }; - - try { - _yapi2.default.mail.sendMail({ - from: _yapi2.default.WEBCONFIG.mail.from, - to: options.to, - subject: 'yapi平台', - html: options.contents - }, cb); - } catch (e) { - console.error(e.message); // eslint-disable-line - } -}; - -exports.validateSearchKeyword = function (keyword) { - if (/^\*|\?|\+|\$|\^|\\|\.$/.test(keyword)) { - return false; - } - - return true; -}; - -exports.filterRes = function (list, rules) { - return list.map(function (item) { - var filteredRes = {}; - - rules.forEach(function (rule) { - if (typeof rule == 'string') { - filteredRes[rule] = item[rule]; - } else if ((typeof rule === 'undefined' ? 'undefined' : (0, _typeof3.default)(rule)) == 'object') { - filteredRes[rule.alias] = item[rule.key]; - } - }); - - return filteredRes; - }); -}; - -exports.verifyPath = function (path) { - if (/^\/[a-zA-Z0-9\-\/_:\.]+$/.test(path)) { - if (path[path.length - 1] === '/') { - return false; - } else { - return true; - } - } else { - return false; - } -}; - -function trim(str) { - if (!str) { - return str; - } - - str = str + ''; - - return str.replace(/(^\s*)|(\s*$)/g, ''); -} - -function ltrim(str) { - if (!str) { - return str; - } - - str = str + ''; - - return str.replace(/(^\s*)/g, ''); -} - -function rtrim(str) { - if (!str) { - return str; - } - - str = str + ''; - - return str.replace(/(\s*$)/g, ''); -} - -exports.trim = trim; -exports.ltrim = ltrim; -exports.rtrim = rtrim; - -exports.handleParams = function (params, keys) { - if (!params || (typeof params === 'undefined' ? 'undefined' : (0, _typeof3.default)(params)) !== 'object' || !keys || (typeof keys === 'undefined' ? 'undefined' : (0, _typeof3.default)(keys)) !== 'object') { - return false; - } - - for (var key in keys) { - var filter = keys[key]; - if (params[key]) { - switch (filter) { - case 'string': - params[key] = trim(params[key] + ''); - break; - case 'number': - params[key] = parseInt(params[key], 10); - break; - default: - params[key] = trim(params + ''); - } - } - } - - return params; -}; \ No newline at end of file diff --git a/server_dist/utils/db.js b/server_dist/utils/db.js deleted file mode 100644 index 21aae9bd..00000000 --- a/server_dist/utils/db.js +++ /dev/null @@ -1,55 +0,0 @@ -'use strict'; - -var _mongoose = require('mongoose'); - -var _mongoose2 = _interopRequireDefault(_mongoose); - -var _yapi = require('../yapi.js'); - -var _yapi2 = _interopRequireDefault(_yapi); - -var _mongooseAutoIncrement = require('mongoose-auto-increment'); - -var _mongooseAutoIncrement2 = _interopRequireDefault(_mongooseAutoIncrement); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -function model(model, schema) { - if (schema instanceof _mongoose2.default.Schema === false) { - schema = new _mongoose2.default.Schema(schema); - } - - schema.set('autoIndex', false); - - return _yapi2.default.connect.model(model, schema, model); -} - -function connect() { - _mongoose2.default.Promise = global.Promise; - - var config = _yapi2.default.WEBCONFIG; - var options = {}; - - if (config.user) { - options.user = config.db.user; - options.pass = config.db.pass; - } - - var db = _mongoose2.default.connect('mongodb://' + config.db.servername + ':' + config.db.port + '/' + config.db.DATABASE, options); - - db.then(function () { - _yapi2.default.commons.log('mongodb load success...'); - }, function (err) { - _yapi2.default.commons.log(err, 'Mongo connect error'); - }); - - _mongooseAutoIncrement2.default.initialize(db); - return db; -} - -_yapi2.default.db = model; - -module.exports = { - model: model, - connect: connect -}; \ No newline at end of file diff --git a/server_dist/utils/initConfig.js b/server_dist/utils/initConfig.js deleted file mode 100644 index c7ae500e..00000000 --- a/server_dist/utils/initConfig.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict'; - -var _stringify = require('babel-runtime/core-js/json/stringify'); - -var _stringify2 = _interopRequireDefault(_stringify); - -var _path = require('path'); - -var _path2 = _interopRequireDefault(_path); - -var _fsExtra = require('fs-extra'); - -var _fsExtra2 = _interopRequireDefault(_fsExtra); - -var _config = require('../config.js'); - -var _config2 = _interopRequireDefault(_config); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var runtimePath = _config2.default.runtime_path; -_fsExtra2.default.ensureDirSync(runtimePath); -_fsExtra2.default.ensureDirSync(_path2.default.join(runtimePath, 'log')); -var configPath = _path2.default.join(runtimePath, 'config.json'); - -_fsExtra2.default.writeFileSync(configPath, (0, _stringify2.default)(_config2.default, null, '\t'), { encoding: 'utf8' }); \ No newline at end of file From ad81bb3e1aabdce71a42e267430be95c9b338e8d Mon Sep 17 00:00:00 2001 From: "wenbo.dong" Date: Thu, 10 Aug 2017 17:20:47 +0800 Subject: [PATCH 3/6] =?UTF-8?q?docs:=20=E6=96=87=E6=A1=A3=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/doc/api.html | 474 +++++++++++------- .../static/server/controllers/follow.js.html | 213 ++++++++ .../doc/static/server/controllers/log.js.html | 10 +- .../static/server/controllers/project.js.html | 79 +-- .../static/server/controllers/user.js.html | 79 ++- 5 files changed, 628 insertions(+), 227 deletions(-) create mode 100644 static/doc/static/server/controllers/follow.js.html diff --git a/static/doc/api.html b/static/doc/api.html index 9255827d..fb2d2110 100644 --- a/static/doc/api.html +++ b/static/doc/api.html @@ -228,6 +228,28 @@ + +
  • + + follow +
  • + + +
  • @@ -273,13 +295,13 @@
  • - node + log
  • @@ -641,7 +663,7 @@

    源码位置: - ./server/controllers/user.js:17 + ./server/controllers/user.js:18

    @@ -728,7 +750,7 @@

    源码位置: - ./server/controllers/user.js:63 + ./server/controllers/user.js:64

    @@ -761,7 +783,7 @@

    源码位置: - ./server/controllers/user.js:160 + ./server/controllers/user.js:162

    @@ -857,7 +879,7 @@

    源码位置: - ./server/controllers/user.js:229 + ./server/controllers/user.js:227

    @@ -954,7 +976,7 @@

    源码位置: - ./server/controllers/user.js:301 + ./server/controllers/user.js:300

    @@ -1024,7 +1046,7 @@

    源码位置: - ./server/controllers/user.js:329 + ./server/controllers/user.js:328

    @@ -1086,7 +1108,7 @@

    源码位置: - ./server/controllers/user.js:367 + ./server/controllers/user.js:366

    @@ -1148,7 +1170,7 @@

    源码位置: - ./server/controllers/user.js:398 + ./server/controllers/user.js:397

    @@ -1240,7 +1262,7 @@

    源码位置: - ./server/controllers/user.js:457 + ./server/controllers/user.js:511

    @@ -1318,7 +1340,7 @@

    源码位置: - ./server/controllers/user.js:503 + ./server/controllers/user.js:556

    @@ -3227,6 +3249,260 @@ + + +
    +
    + +
    +

    + 描述: + 获取关注项目列表 +

    + +

    + 源码位置: + ./server/controllers/follow.js:24 +

    + + +

    + 参数: +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数名类型描述必选支持版本
    uidNumber用户id, 不能为空 + + + +
    pageNumber分页页码 + +
    limitNumber分页大小 + +
    +
    + + +
    示例:
    +
    /follow/list
    + + +
    + +
    +
    + +
    +

    + 描述: + 取消关注 +

    + +

    + 源码位置: + ./server/controllers/follow.js:60 +

    + + +

    + 参数: +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数名类型描述必选支持版本
    idNumber关注id + + + +
    +
    + + +
    示例:
    +
    /follow/del
    + + +
    + +
    +
    + +
    +

    + 描述: + 添加关注 +

    + +

    + 源码位置: + ./server/controllers/follow.js:86 +

    + + +

    + 参数: +

    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数名类型描述必选支持版本
    uidNumber用户id + + + +
    projectidNumber项目id + + + +
    projectnameString项目名 + + + +
    iconString项目icon + + + +
    +
    + + +
    示例:
    +
    /follow/add
    + + +
    + +
    @@ -3892,22 +4168,22 @@
    - +
    -

    描述: - 获取节点列表 + 获取动态列表

    @@ -3974,169 +4250,7 @@

    示例:
    -
    {
    -    "errcode": 0,
    -    "errmsg": "success",
    -    "data": {
    -        "total": 2,
    -        "list": [
    -            {
    -                "_id": 529,
    -                "name": "yapi",
    -                "desc": "aaa",
    -                "prd_host": "http://www.yapi.com",
    -                "basepath": "/a/",
    -                "uid": 107,
    -                "group_id": 181,
    -                "add_time": 1500465369,
    -                "up_time": 1500522419,
    -                "__v": 0,
    -                "env": [],
    -                "members": [
    -                    "107"
    -                ]
    -            },
    -            {
    -                "_id": 558,
    -                "name": "12",
    -                "desc": "21\n",
    -                "prd_host": "http://11/",
    -                "basepath": "12/",
    -                "uid": 107,
    -                "group_id": 181,
    -                "add_time": 1500466250,
    -                "up_time": 1500466250,
    -                "__v": 0,
    -                "env": [],
    -                "members": [
    -                    "107"
    -                ]
    -            },
    -            {
    -                "_id": 626,
    -                "name": "1233",
    -                "desc": "123",
    -                "prd_host": "http://1234/",
    -                "basepath": "1234/",
    -                "uid": 107,
    -                "group_id": 181,
    -                "add_time": 1500471230,
    -                "up_time": 1500471230,
    -                "__v": 0,
    -                "env": [],
    -                "members": [
    -                    "107"
    -                ]
    -            },
    -            {
    -                "_id": 665,
    -                "name": "222",
    -                "desc": "222",
    -                "prd_host": "http://222/",
    -                "basepath": "222/",
    -                "uid": 107,
    -                "group_id": 181,
    -                "add_time": 1500471668,
    -                "up_time": 1500471668,
    -                "__v": 0,
    -                "env": [],
    -                "members": [
    -                    "107"
    -                ]
    -            },
    -            {
    -                "_id": 704,
    -                "name": "333",
    -                "desc": "222333",
    -                "prd_host": "http://333/",
    -                "basepath": "333/",
    -                "uid": 107,
    -                "group_id": 181,
    -                "add_time": 1500471674,
    -                "up_time": 1500471674,
    -                "__v": 0,
    -                "env": [],
    -                "members": [
    -                    "107"
    -                ]
    -            },
    -            {
    -                "_id": 743,
    -                "name": "444",
    -                "desc": "444",
    -                "prd_host": "https://444/",
    -                "basepath": "444/",
    -                "uid": 107,
    -                "group_id": 181,
    -                "add_time": 1500471695,
    -                "up_time": 1500471695,
    -                "__v": 0,
    -                "env": [],
    -                "members": [
    -                    "107"
    -                ]
    -            },
    -            {
    -                "_id": 763,
    -                "name": "122333",
    -                "desc": "3/",
    -                "prd_host": "http://33/",
    -                "basepath": "33/",
    -                "uid": 107,
    -                "group_id": 181,
    -                "add_time": 1500481743,
    -                "up_time": 1500481743,
    -                "__v": 0,
    -                "env": [],
    -                "members": [
    -                    "107"
    -                ]
    -            },
    -            {
    -                "_id": 778,
    -                "name": "555",
    -                "desc": "555",
    -                "prd_host": "https://555/",
    -                "basepath": "555/",
    -                "uid": 107,
    -                "group_id": 181,
    -                "add_time": 1500519203,
    -                "up_time": 1500519203,
    -                "__v": 0,
    -                "env": [],
    -                "members": [
    -                    "107"
    -                ]
    -            },
    -            {
    -                "_id": 823,
    -                "name": "5",
    -                "desc": "5",
    -                "prd_host": "http://5/",
    -                "basepath": "5/",
    -                "uid": 107,
    -                "group_id": 181,
    -                "add_time": 1500519769,
    -                "up_time": 1500519769,
    -                "__v": 0,
    -                "env": [],
    -                "members": [
    -                    "107"
    -                ]
    -            }
    -        ],
    -        "userinfo": {
    -            "107": {
    -                "_id": 107,
    -                "username": "admin",
    -                "email": "admin@admin.com",
    -                "role": "admin",
    -                "add_time": 1500280333,
    -                "up_time": 1500373530
    -            }
    -        }
    -    }
    -}
    +
    /log/list
    diff --git a/static/doc/static/server/controllers/follow.js.html b/static/doc/static/server/controllers/follow.js.html new file mode 100644 index 00000000..c3b2df1f --- /dev/null +++ b/static/doc/static/server/controllers/follow.js.html @@ -0,0 +1,213 @@ + + + + + + YApi : ./server/controllers/follow.js + + + + + + +
    +
    +
    +
    +

    YApi : ./server/controllers/follow.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;
    +                
    +
    +
    +
    +
    + + +
    +
    +

    +
    +
    +
    + + + diff --git a/static/doc/static/server/controllers/log.js.html b/static/doc/static/server/controllers/log.js.html index e54994d8..688233b8 100644 --- a/static/doc/static/server/controllers/log.js.html +++ b/static/doc/static/server/controllers/log.js.html @@ -54,16 +54,16 @@ class logController extends baseController { } /** - * 获取节点列表 - * @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) { @@ -72,7 +72,7 @@ class logController extends baseController { limit = ctx.request.query.limit || 10; if (!typeid) { - return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空'); + return ctx.body = yapi.commons.resReturn(null, 400, 'typeid不能为空'); } try { diff --git a/static/doc/static/server/controllers/project.js.html b/static/doc/static/server/controllers/project.js.html index 53cb54ae..4f2559c2 100644 --- a/static/doc/static/server/controllers/project.js.html +++ b/static/doc/static/server/controllers/project.js.html @@ -552,39 +552,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); - // 根据 project_id 获取接口数据 - let count = await interfaceInst.list(project_id); + 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不存在'); - } + 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('cont',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 - } - })); - // console.log(arr); + 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 fileName = 'mock.js'; + ctx.attachment(fileName); + await send(ctx, fileName, { root: __dirname + '/public' }); - const res = ` - var data = ${arr}; - module.exports = data;` - .trim(); - return ctx.body = res; + 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; } } diff --git a/static/doc/static/server/controllers/user.js.html b/static/doc/static/server/controllers/user.js.html index 33814228..6c0fe69c 100644 --- a/static/doc/static/server/controllers/user.js.html +++ b/static/doc/static/server/controllers/user.js.html @@ -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: `

    亲爱的用户:

    您好,感谢使用YApi,系统检测您是第一次用Qsso账号登录YApi服务,您的Email是: ${email} ,初始化密码为:${passsalt}

    ` + contents: `

    亲爱的用户:

    您好,感谢使用YApi平台.

    ` }); } @@ -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,7 +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; } From 028c91cf55c9aa8ba70d8c6d99cb584d6557f0ad Mon Sep 17 00:00:00 2001 From: "wenbo.dong" Date: Thu, 10 Aug 2017 17:22:55 +0800 Subject: [PATCH 4/6] =?UTF-8?q?fix:=20=E4=B8=8B=E8=BD=BD=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E5=A4=9A=E4=BD=99=E5=88=86=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/project.js | 2 +- server_dist/controllers/project.js | 1255 +++++++++++++++++++++++++ server_dist/controllers/user.js | 1263 ++++++++++++++++++++++++++ server_dist/middleware/checkToken.js | 36 + server_dist/middleware/mockServer.js | 177 ++++ server_dist/middleware/userauth.js | 44 + server_dist/models/avatar.js | 81 ++ server_dist/models/base.js | 82 ++ server_dist/models/follow.js | 109 +++ server_dist/models/group.js | 153 ++++ server_dist/models/interface.js | 143 +++ server_dist/models/interfaceCase.js | 122 +++ server_dist/models/interfaceCol.js | 104 +++ server_dist/models/log.js | 117 +++ server_dist/models/project.js | 200 ++++ server_dist/models/user.js | 142 +++ server_dist/utils/commons.js | 250 +++++ server_dist/utils/db.js | 55 ++ server_dist/utils/initConfig.js | 26 + 19 files changed, 4360 insertions(+), 1 deletion(-) create mode 100644 server_dist/controllers/project.js create mode 100644 server_dist/controllers/user.js create mode 100644 server_dist/middleware/checkToken.js create mode 100644 server_dist/middleware/mockServer.js create mode 100644 server_dist/middleware/userauth.js create mode 100644 server_dist/models/avatar.js create mode 100644 server_dist/models/base.js create mode 100644 server_dist/models/follow.js create mode 100644 server_dist/models/group.js create mode 100644 server_dist/models/interface.js create mode 100644 server_dist/models/interfaceCase.js create mode 100644 server_dist/models/interfaceCol.js create mode 100644 server_dist/models/log.js create mode 100644 server_dist/models/project.js create mode 100644 server_dist/models/user.js create mode 100644 server_dist/utils/commons.js create mode 100644 server_dist/utils/db.js create mode 100644 server_dist/utils/initConfig.js diff --git a/server/controllers/project.js b/server/controllers/project.js index 04d77279..d50f664a 100644 --- a/server/controllers/project.js +++ b/server/controllers/project.js @@ -576,7 +576,7 @@ class projectController extends baseController { }, 500); }); } - module.exports = run;`; + module.exports = run;` .trim(); return ctx.body = res; } diff --git a/server_dist/controllers/project.js b/server_dist/controllers/project.js new file mode 100644 index 00000000..72adcd62 --- /dev/null +++ b/server_dist/controllers/project.js @@ -0,0 +1,1255 @@ +'use strict'; + +var _stringify = require('babel-runtime/core-js/json/stringify'); + +var _stringify2 = _interopRequireDefault(_stringify); + +var _regenerator = require('babel-runtime/regenerator'); + +var _regenerator2 = _interopRequireDefault(_regenerator); + +var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); + +var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); + +var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); + +var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); + +var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); + +var _inherits2 = require('babel-runtime/helpers/inherits'); + +var _inherits3 = _interopRequireDefault(_inherits2); + +var _project = require('../models/project.js'); + +var _project2 = _interopRequireDefault(_project); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _base = require('./base.js'); + +var _base2 = _interopRequireDefault(_base); + +var _interface = require('../models/interface.js'); + +var _interface2 = _interopRequireDefault(_interface); + +var _group = require('../models/group'); + +var _group2 = _interopRequireDefault(_group); + +var _commons = require('../utils/commons.js'); + +var _commons2 = _interopRequireDefault(_commons); + +var _user = require('../models/user.js'); + +var _user2 = _interopRequireDefault(_user); + +var _mockjs = require('mockjs'); + +var _mockjs2 = _interopRequireDefault(_mockjs); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var send = require('koa-send'); + +var projectController = function (_baseController) { + (0, _inherits3.default)(projectController, _baseController); + + function projectController(ctx) { + (0, _classCallCheck3.default)(this, projectController); + + var _this = (0, _possibleConstructorReturn3.default)(this, (projectController.__proto__ || (0, _getPrototypeOf2.default)(projectController)).call(this, ctx)); + + _this.Model = _yapi2.default.getInst(_project2.default); + _this.groupModel = _yapi2.default.getInst(_group2.default); + return _this; + } + + (0, _createClass3.default)(projectController, [{ + key: 'handleBasepath', + value: function handleBasepath(basepath) { + if (!basepath) return ""; + if (basepath === '/') return ""; + if (basepath[0] !== '/') basepath = '/' + basepath; + if (basepath[basepath.length - 1] === '/') basepath = basepath.substr(0, basepath.length - 1); + if (!_yapi2.default.commons.verifyPath(basepath)) { + return false; + } + return basepath; + } + }, { + key: 'verifyDomain', + value: function verifyDomain(domain) { + if (!domain) return false; + if (/^[a-zA-Z0-9\-_\.]+?\.[a-zA-Z0-9\-_\.]*?[a-zA-Z]{2,6}$/.test(domain)) { + return true; + } + return false; + } + + /** + * 添加项目分组 + * @interface /project/add + * @method POST + * @category project + * @foldnumber 10 + * @param {String} name 项目名称,不能为空 + * @param {String} basepath 项目基本路径,不能为空 + * @param {String} prd_host 项目线上域名,不能为空。可通过配置的域名访问到mock数据 + * @param {String} protocol 线上域名协议,不能为空 + * @param {Number} group_id 项目分组id,不能为空 + * @param {String} [desc] 项目描述 + * @returns {Object} + * @example ./api/project/add.json + */ + + }, { + key: 'add', + value: function () { + var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) { + var params, checkRepeat, checkRepeatDomain, data, result; + return _regenerator2.default.wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + params = ctx.request.body; + + params = _yapi2.default.commons.handleParams(params, { + name: 'string', + basepath: 'string', + prd_host: 'string', + protocol: 'string', + group_id: 'number', + desc: 'string' + }); + + _context.next = 4; + return this.checkAuth(params.group_id, 'group', 'edit'); + + case 4: + _context.t0 = _context.sent; + + if (!(_context.t0 !== true)) { + _context.next = 7; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); + + case 7: + if (params.group_id) { + _context.next = 9; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目分组id不能为空')); + + case 9: + if (params.name) { + _context.next = 11; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目名不能为空')); + + case 11: + _context.next = 13; + return this.Model.checkNameRepeat(params.name); + + case 13: + checkRepeat = _context.sent; + + if (!(checkRepeat > 0)) { + _context.next = 16; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的项目名')); + + case 16: + if (params.prd_host) { + _context.next = 18; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目domain不能为空')); + + case 18: + + params.basepath = params.basepath || ''; + + if (!((params.basepath = this.handleBasepath(params.basepath)) === false)) { + _context.next = 21; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, 'basepath格式有误')); + + case 21: + if (this.verifyDomain(params.prd_host)) { + _context.next = 23; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '线上域名格式有误')); + + case 23: + _context.next = 25; + return this.Model.checkDomainRepeat(params.prd_host, params.basepath); + + case 25: + checkRepeatDomain = _context.sent; + + if (!(checkRepeatDomain > 0)) { + _context.next = 28; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在domain和basepath')); + + case 28: + data = { + name: params.name, + desc: params.desc, + prd_host: params.prd_host, + basepath: params.basepath, + protocol: params.protocol || 'http', + members: [], + project_type: params.project_type || 'private', + uid: this.getUid(), + group_id: params.group_id, + add_time: _yapi2.default.commons.time(), + up_time: _yapi2.default.commons.time() + }; + _context.prev = 29; + _context.next = 32; + return this.Model.save(data); + + case 32: + result = _context.sent; + + ctx.body = _yapi2.default.commons.resReturn(result); + _context.next = 39; + break; + + case 36: + _context.prev = 36; + _context.t1 = _context['catch'](29); + + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context.t1.message); + + case 39: + case 'end': + return _context.stop(); + } + } + }, _callee, this, [[29, 36]]); + })); + + function add(_x) { + return _ref.apply(this, arguments); + } + + return add; + }() + /** + * 添加项目 + * @interface /project/add_member + * @method POST + * @category project + * @foldnumber 10 + * @param {Number} id 项目id,不能为空 + * @param {String} member_uid 项目成员uid,不能为空 + * @returns {Object} + * @example ./api/project/add_member.json + */ + + }, { + key: 'addMember', + value: function () { + var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(ctx) { + var params, check, userdata, result; + return _regenerator2.default.wrap(function _callee2$(_context2) { + while (1) { + switch (_context2.prev = _context2.next) { + case 0: + params = ctx.request.body; + + if (params.member_uid) { + _context2.next = 3; + break; + } + + return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目成员uid不能为空')); + + case 3: + if (params.id) { + _context2.next = 5; + break; + } + + return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); + + case 5: + _context2.next = 7; + return this.checkAuth(params.id, 'project', 'edit'); + + case 7: + _context2.t0 = _context2.sent; + + if (!(_context2.t0 !== true)) { + _context2.next = 10; + break; + } + + return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); + + case 10: + _context2.next = 12; + return this.Model.checkMemberRepeat(params.id, params.member_uid); + + case 12: + check = _context2.sent; + + if (!(check > 0)) { + _context2.next = 15; + break; + } + + return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目成员已存在')); + + case 15: + _context2.next = 17; + return this.getUserdata(params.member_uid); + + case 17: + userdata = _context2.sent; + + if (!(userdata === null)) { + _context2.next = 20; + break; + } + + return _context2.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '成员uid不存在')); + + case 20: + _context2.prev = 20; + _context2.next = 23; + return this.Model.addMember(params.id, userdata); + + case 23: + result = _context2.sent; + + ctx.body = _yapi2.default.commons.resReturn(result); + _context2.next = 30; + break; + + case 27: + _context2.prev = 27; + _context2.t1 = _context2['catch'](20); + + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context2.t1.message); + + case 30: + case 'end': + return _context2.stop(); + } + } + }, _callee2, this, [[20, 27]]); + })); + + function addMember(_x2) { + return _ref2.apply(this, arguments); + } + + return addMember; + }() + /** + * 添加项目 + * @interface /project/del_member + * @method POST + * @category project + * @foldnumber 10 + * @param {Number} id 项目id,不能为空 + * @param {member_uid} uid 项目成员uid,不能为空 + * @returns {Object} + * @example ./api/project/del_member.json + */ + + }, { + key: 'delMember', + value: function () { + var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) { + var params, check, result; + return _regenerator2.default.wrap(function _callee3$(_context3) { + while (1) { + switch (_context3.prev = _context3.next) { + case 0: + params = ctx.request.body; + + if (params.member_uid) { + _context3.next = 3; + break; + } + + return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目成员uid不能为空')); + + case 3: + if (params.id) { + _context3.next = 5; + break; + } + + return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); + + case 5: + _context3.next = 7; + return this.Model.checkMemberRepeat(params.id, params.member_uid); + + case 7: + check = _context3.sent; + + if (!(check === 0)) { + _context3.next = 10; + break; + } + + return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目成员不存在')); + + case 10: + _context3.next = 12; + return this.checkAuth(params.id, 'project', 'danger'); + + case 12: + _context3.t0 = _context3.sent; + + if (!(_context3.t0 !== true)) { + _context3.next = 15; + break; + } + + return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); + + case 15: + _context3.prev = 15; + _context3.next = 18; + return this.Model.delMember(params.id, params.member_uid); + + case 18: + result = _context3.sent; + + ctx.body = _yapi2.default.commons.resReturn(result); + _context3.next = 25; + break; + + case 22: + _context3.prev = 22; + _context3.t1 = _context3['catch'](15); + + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context3.t1.message); + + case 25: + case 'end': + return _context3.stop(); + } + } + }, _callee3, this, [[15, 22]]); + })); + + function delMember(_x3) { + return _ref3.apply(this, arguments); + } + + return delMember; + }() + }, { + key: 'getUserdata', + value: function () { + var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(uid, role) { + var userInst, userData; + return _regenerator2.default.wrap(function _callee4$(_context4) { + while (1) { + switch (_context4.prev = _context4.next) { + case 0: + role = role || 'dev'; + userInst = _yapi2.default.getInst(_user2.default); + _context4.next = 4; + return userInst.findById(uid); + + case 4: + userData = _context4.sent; + + if (userData) { + _context4.next = 7; + break; + } + + return _context4.abrupt('return', null); + + case 7: + return _context4.abrupt('return', { + role: role, + uid: userData._id, + username: userData.username, + email: userData.email + }); + + case 8: + case 'end': + return _context4.stop(); + } + } + }, _callee4, this); + })); + + function getUserdata(_x4, _x5) { + return _ref4.apply(this, arguments); + } + + return getUserdata; + }() + + /** + * 获取项目成员列表 + * @interface /project/get_member_list + * @method GET + * @category project + * @foldnumber 10 + * @param {Number} id 项目id,不能为空 + * @return {Object} + * @example ./api/project/get_member_list.json + */ + + }, { + key: 'getMemberList', + value: function () { + var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(ctx) { + var params, project; + return _regenerator2.default.wrap(function _callee5$(_context5) { + while (1) { + switch (_context5.prev = _context5.next) { + case 0: + params = ctx.request.query; + + if (params.id) { + _context5.next = 3; + break; + } + + return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); + + case 3: + _context5.prev = 3; + _context5.next = 6; + return this.Model.get(params.id); + + case 6: + project = _context5.sent; + + ctx.body = _yapi2.default.commons.resReturn(project.members); + _context5.next = 13; + break; + + case 10: + _context5.prev = 10; + _context5.t0 = _context5['catch'](3); + + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context5.t0.message); + + case 13: + case 'end': + return _context5.stop(); + } + } + }, _callee5, this, [[3, 10]]); + })); + + function getMemberList(_x6) { + return _ref5.apply(this, arguments); + } + + return getMemberList; + }() + + /** + * 添加项目 + * @interface /project/get + * @method GET + * @category project + * @foldnumber 10 + * @param {Number} id 项目id,不能为空 + * @returns {Object} + * @example ./api/project/get.json + */ + + }, { + key: 'get', + value: function () { + var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(ctx) { + var params, result; + return _regenerator2.default.wrap(function _callee6$(_context6) { + while (1) { + switch (_context6.prev = _context6.next) { + case 0: + params = ctx.request.query; + + if (params.id) { + _context6.next = 3; + break; + } + + return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); + + case 3: + _context6.prev = 3; + _context6.next = 6; + return this.Model.get(params.id); + + case 6: + result = _context6.sent; + + ctx.body = _yapi2.default.commons.resReturn(result); + _context6.next = 13; + break; + + case 10: + _context6.prev = 10; + _context6.t0 = _context6['catch'](3); + + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context6.t0.message); + + case 13: + case 'end': + return _context6.stop(); + } + } + }, _callee6, this, [[3, 10]]); + })); + + function get(_x7) { + return _ref6.apply(this, arguments); + } + + return get; + }() + + /** + * 获取项目列表 + * @interface /project/list + * @method GET + * @category project + * @foldnumber 10 + * @param {Number} group_id 项目group_id,不能为空 + * @returns {Object} + * @example ./api/project/list.json + */ + + }, { + key: 'list', + value: function () { + var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(ctx) { + var group_id, auth, result, uids, _users, users; + + return _regenerator2.default.wrap(function _callee7$(_context7) { + while (1) { + switch (_context7.prev = _context7.next) { + case 0: + group_id = ctx.request.query.group_id; + + if (group_id) { + _context7.next = 3; + break; + } + + return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目分组id不能为空')); + + case 3: + _context7.next = 5; + return this.checkAuth(group_id, 'group', 'edit'); + + case 5: + auth = _context7.sent; + _context7.prev = 6; + _context7.next = 9; + return this.Model.list(group_id, auth); + + case 9: + result = _context7.sent; + uids = []; + + result.forEach(function (item) { + if (uids.indexOf(item.uid) === -1) { + uids.push(item.uid); + } + }); + _users = {}; + _context7.next = 15; + return _yapi2.default.getInst(_user2.default).findByUids(uids); + + case 15: + users = _context7.sent; + + users.forEach(function (item) { + _users[item._id] = item; + }); + ctx.body = _yapi2.default.commons.resReturn({ + list: result, + userinfo: _users + }); + _context7.next = 23; + break; + + case 20: + _context7.prev = 20; + _context7.t0 = _context7['catch'](6); + + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context7.t0.message); + + case 23: + case 'end': + return _context7.stop(); + } + } + }, _callee7, this, [[6, 20]]); + })); + + function list(_x8) { + return _ref7.apply(this, arguments); + } + + return list; + }() + + /** + * 删除项目 + * @interface /project/del + * @method POST + * @category project + * @foldnumber 10 + * @param {Number} id 项目id,不能为空 + * @returns {Object} + * @example ./api/project/del.json + */ + + }, { + key: 'del', + value: function () { + var _ref8 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(ctx) { + var _id, interfaceInst, count, result; + + return _regenerator2.default.wrap(function _callee8$(_context8) { + while (1) { + switch (_context8.prev = _context8.next) { + case 0: + _context8.prev = 0; + _id = ctx.request.body.id; + + if (_id) { + _context8.next = 4; + break; + } + + return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); + + case 4: + interfaceInst = _yapi2.default.getInst(_interface2.default); + _context8.next = 7; + return interfaceInst.countByProjectId(_id); + + case 7: + count = _context8.sent; + + if (!(count > 0)) { + _context8.next = 10; + break; + } + + return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '请先删除该项目下所有接口')); + + case 10: + _context8.next = 12; + return this.checkAuth(_id, 'project', 'danger'); + + case 12: + _context8.t0 = _context8.sent; + + if (!(_context8.t0 !== true)) { + _context8.next = 15; + break; + } + + return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); + + case 15: + _context8.next = 17; + return this.Model.del(_id); + + case 17: + result = _context8.sent; + + ctx.body = _yapi2.default.commons.resReturn(result); + _context8.next = 24; + break; + + case 21: + _context8.prev = 21; + _context8.t1 = _context8['catch'](0); + + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context8.t1.message); + + case 24: + case 'end': + return _context8.stop(); + } + } + }, _callee8, this, [[0, 21]]); + })); + + function del(_x9) { + return _ref8.apply(this, arguments); + } + + return del; + }() + }, { + key: 'changeMemberRole', + value: function () { + var _ref9 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9(ctx) { + var params, groupInst, check, result; + return _regenerator2.default.wrap(function _callee9$(_context9) { + while (1) { + switch (_context9.prev = _context9.next) { + case 0: + params = ctx.request.body; + groupInst = _yapi2.default.getInst(_group2.default); + + if (params.member_uid) { + _context9.next = 4; + break; + } + + return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '分组成员uid不能为空')); + + case 4: + if (params.id) { + _context9.next = 6; + break; + } + + return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '分组id不能为空')); + + case 6: + _context9.next = 8; + return groupInst.checkMemberRepeat(params.id, params.member_uid); + + case 8: + check = _context9.sent; + + if (!(check === 0)) { + _context9.next = 11; + break; + } + + return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '分组成员不存在')); + + case 11: + _context9.next = 13; + return this.checkAuth(id, 'group', 'danger'); + + case 13: + _context9.t0 = _context9.sent; + + if (!(_context9.t0 !== true)) { + _context9.next = 16; + break; + } + + return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); + + case 16: + + params.role = params.role === 'owner' ? 'owner' : 'dev'; + + _context9.prev = 17; + _context9.next = 20; + return groupInst.changeMemberRole(params.id, params.member_uid, params.role); + + case 20: + result = _context9.sent; + + ctx.body = _yapi2.default.commons.resReturn(result); + _context9.next = 27; + break; + + case 24: + _context9.prev = 24; + _context9.t1 = _context9['catch'](17); + + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context9.t1.message); + + case 27: + case 'end': + return _context9.stop(); + } + } + }, _callee9, this, [[17, 24]]); + })); + + function changeMemberRole(_x10) { + return _ref9.apply(this, arguments); + } + + return changeMemberRole; + }() + + /** + * 编辑项目 + * @interface /project/up + * @method POST + * @category project + * @foldnumber 10 + * @param {Number} id 项目id,不能为空 + * @param {String} name 项目名称,不能为空 + * @param {String} basepath 项目基本路径,不能为空 + * @param {String} prd_host 项目线上域名,不能为空。可通过配置的域名访问到mock数据 + * @param {String} [desc] 项目描述 + * @param {Array} [env] 项目环境配置 + * @param {String} [env[].name] 环境名称 + * @param {String} [env[].domain] 环境域名 + * @returns {Object} + * @example ./api/project/up.json + */ + + }, { + key: 'up', + value: function () { + var _ref10 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(ctx) { + var _id2, params, projectData, checkRepeat, checkRepeatDomain, data, result; + + return _regenerator2.default.wrap(function _callee10$(_context10) { + while (1) { + switch (_context10.prev = _context10.next) { + case 0: + _context10.prev = 0; + _id2 = ctx.request.body.id; + params = ctx.request.body; + + params.basepath = params.basepath || ''; + params = _yapi2.default.commons.handleParams(params, { + name: 'string', + basepath: 'string', + prd_host: 'string', + protocol: 'string', + group_id: 'number', + desc: 'string' + }); + + if (_id2) { + _context10.next = 7; + break; + } + + return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '项目id不能为空')); + + case 7: + _context10.next = 9; + return this.checkAuth(_id2, 'project', 'edit'); + + case 9: + _context10.t0 = _context10.sent; + + if (!(_context10.t0 !== true)) { + _context10.next = 12; + break; + } + + return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); + + case 12: + _context10.next = 14; + return this.Model.get(_id2); + + case 14: + projectData = _context10.sent; + + if (!((params.basepath = this.handleBasepath(params.basepath)) === false)) { + _context10.next = 17; + break; + } + + return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, 'basepath格式有误')); + + case 17: + if (this.verifyDomain(params.prd_host)) { + _context10.next = 19; + break; + } + + return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '线上域名格式有误')); + + case 19: + + if (projectData.name === params.name) { + delete params.name; + } + if (projectData.basepath === params.basepath && projectData.prd_host === params.prd_host) { + delete params.basepath; + delete params.prd_host; + } + + if (!params.name) { + _context10.next = 27; + break; + } + + _context10.next = 24; + return this.Model.checkNameRepeat(params.name); + + case 24: + checkRepeat = _context10.sent; + + if (!(checkRepeat > 0)) { + _context10.next = 27; + break; + } + + return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的项目名')); + + case 27: + if (!(params.basepath && params.prd_host)) { + _context10.next = 33; + break; + } + + _context10.next = 30; + return this.Model.checkDomainRepeat(params.prd_host, params.basepath); + + case 30: + checkRepeatDomain = _context10.sent; + + if (!(checkRepeatDomain > 0)) { + _context10.next = 33; + break; + } + + return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在domain和basepath')); + + case 33: + data = { + up_time: _yapi2.default.commons.time() + }; + + + if (params.name) data.name = params.name; + if (params.desc) data.desc = params.desc; + if (params.prd_host) { + data.prd_host = params.prd_host; + data.basepath = params.basepath; + } + if (params.protocol) data.protocol = params.protocol; + if (params.env) data.env = params.env; + + _context10.next = 41; + return this.Model.up(_id2, data); + + case 41: + result = _context10.sent; + + ctx.body = _yapi2.default.commons.resReturn(result); + _context10.next = 48; + break; + + case 45: + _context10.prev = 45; + _context10.t1 = _context10['catch'](0); + + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context10.t1.message); + + case 48: + case 'end': + return _context10.stop(); + } + } + }, _callee10, this, [[0, 45]]); + })); + + function up(_x11) { + return _ref10.apply(this, arguments); + } + + return up; + }() + + /** + * 模糊搜索项目名称或者组名称 + * @interface /project/search + * @method GET + * @category project + * @foldnumber 10 + * @param {String} q + * @return {Object} + * @example ./api/project/search.json + */ + + }, { + key: 'search', + value: function () { + var _ref11 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11(ctx) { + var q, projectList, groupList, projectRules, groupRules, queryList; + return _regenerator2.default.wrap(function _callee11$(_context11) { + while (1) { + switch (_context11.prev = _context11.next) { + case 0: + q = ctx.request.query.q; + + if (q) { + _context11.next = 3; + break; + } + + return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'No keyword.')); + + case 3: + if (_yapi2.default.commons.validateSearchKeyword(q)) { + _context11.next = 5; + break; + } + + return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'Bad query.')); + + case 5: + _context11.next = 7; + return this.Model.search(q); + + case 7: + projectList = _context11.sent; + _context11.next = 10; + return this.groupModel.search(q); + + case 10: + groupList = _context11.sent; + projectRules = ['_id', 'name', 'basepath', 'uid', 'env', 'members', { key: 'group_id', alias: 'groupId' }, { key: 'up_time', alias: 'upTime' }, { key: 'prd_host', alias: 'prdHost' }, { key: 'add_time', alias: 'addTime' }]; + groupRules = ['_id', 'uid', { key: 'group_name', alias: 'groupName' }, { key: 'group_desc', alias: 'groupDesc' }, { key: 'add_time', alias: 'addTime' }, { key: 'up_time', alias: 'upTime' }]; + + + projectList = _commons2.default.filterRes(projectList, projectRules); + groupList = _commons2.default.filterRes(groupList, groupRules); + + queryList = { + project: projectList, + group: groupList + }; + return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(queryList, 0, 'ok')); + + case 17: + case 'end': + return _context11.stop(); + } + } + }, _callee11, this); + })); + + function search(_x12) { + return _ref11.apply(this, arguments); + } + + return search; + }() + + /** + * 下载项目的 Mock 数据 + * @interface /project/download + * @method GET + * @category project + * @foldnumber 10 + * @param {String} project_id + */ + + }, { + key: 'download', + value: function () { + var _ref12 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(ctx) { + var project_id, interfaceInst, count, arr, fileName, res; + return _regenerator2.default.wrap(function _callee12$(_context12) { + while (1) { + switch (_context12.prev = _context12.next) { + 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; + + 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)); + return { + path: item.path, + mock: mockData + }; + })); + fileName = 'mock.js'; + + ctx.attachment(fileName); + _context12.next = 16; + return send(ctx, fileName, { root: __dirname + '/public' }); + + 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 18: + case 'end': + return _context12.stop(); + } + } + }, _callee12, this); + })); + + function download(_x13) { + return _ref12.apply(this, arguments); + } + + return download; + }() + }]); + return projectController; +}(_base2.default); + +module.exports = projectController; \ No newline at end of file diff --git a/server_dist/controllers/user.js b/server_dist/controllers/user.js new file mode 100644 index 00000000..34a5bb3a --- /dev/null +++ b/server_dist/controllers/user.js @@ -0,0 +1,1263 @@ +'use strict'; + +var _promise = require('babel-runtime/core-js/promise'); + +var _promise2 = _interopRequireDefault(_promise); + +var _regenerator = require('babel-runtime/regenerator'); + +var _regenerator2 = _interopRequireDefault(_regenerator); + +var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); + +var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); + +var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); + +var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); + +var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); + +var _inherits2 = require('babel-runtime/helpers/inherits'); + +var _inherits3 = _interopRequireDefault(_inherits2); + +var _user = require('../models/user.js'); + +var _user2 = _interopRequireDefault(_user); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _base = require('./base.js'); + +var _base2 = _interopRequireDefault(_base); + +var _request2 = require('request'); + +var _request3 = _interopRequireDefault(_request2); + +var _commons = require('../utils/commons.js'); + +var _commons2 = _interopRequireDefault(_commons); + +var _interface = require('../models/interface.js'); + +var _interface2 = _interopRequireDefault(_interface); + +var _group = require('../models/group.js'); + +var _group2 = _interopRequireDefault(_group); + +var _project = require('../models/project.js'); + +var _project2 = _interopRequireDefault(_project); + +var _avatar = require('../models/avatar.js'); + +var _avatar2 = _interopRequireDefault(_avatar); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var jwt = require('jsonwebtoken'); + +var userController = function (_baseController) { + (0, _inherits3.default)(userController, _baseController); + + function userController(ctx) { + (0, _classCallCheck3.default)(this, userController); + + var _this = (0, _possibleConstructorReturn3.default)(this, (userController.__proto__ || (0, _getPrototypeOf2.default)(userController)).call(this, ctx)); + + _this.Model = _yapi2.default.getInst(_user2.default); + return _this; + } + /** + * 用户登录接口 + * @interface /user/login + * @method POST + * @category user + * @foldnumber 10 + * @param {String} email email名称,不能为空 + * @param {String} password 密码,不能为空 + * @returns {Object} + * @example ./api/user/login.json + */ + + + (0, _createClass3.default)(userController, [{ + key: 'login', + value: function () { + var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) { + var userInst, email, password, result; + return _regenerator2.default.wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + //登录 + userInst = _yapi2.default.getInst(_user2.default); //创建user实体 + + email = ctx.request.body.email; + password = ctx.request.body.password; + + if (email) { + _context.next = 5; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'email不能为空')); + + case 5: + if (password) { + _context.next = 7; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空')); + + case 7: + _context.next = 9; + return userInst.findByEmail(email); + + case 9: + result = _context.sent; + + if (result) { + _context.next = 14; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 404, '该用户不存在')); + + case 14: + if (!(_yapi2.default.commons.generatePassword(password, result.passsalt) === result.password)) { + _context.next = 19; + break; + } + + this.setLoginCookie(result._id, result.passsalt); + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn({ + username: result.username, + role: result.role, + uid: result._id, + email: result.email, + add_time: result.add_time, + up_time: result.up_time, + server_ip: _yapi2.default.WEBCONFIG.server_ip + + }, 0, 'logout success...')); + + case 19: + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '密码错误')); + + case 20: + case 'end': + return _context.stop(); + } + } + }, _callee, this); + })); + + function login(_x) { + return _ref.apply(this, arguments); + } + + return login; + }() + + /** + * 退出登录接口 + * @interface /user/logout + * @method GET + * @category user + * @foldnumber 10 + * @returns {Object} + * @example ./api/user/logout.json + */ + + }, { + key: 'logout', + value: function () { + var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(ctx) { + return _regenerator2.default.wrap(function _callee2$(_context2) { + while (1) { + switch (_context2.prev = _context2.next) { + case 0: + ctx.cookies.set('_yapi_token', null); + ctx.cookies.set('_yapi_uid', null); + ctx.body = _yapi2.default.commons.resReturn('ok'); + + case 3: + case 'end': + return _context2.stop(); + } + } + }, _callee2, this); + })); + + function logout(_x2) { + return _ref2.apply(this, arguments); + } + + return logout; + }() + + /** + * 第三方登录需要提供一个request方法和 token字段,暂时只支持qunar第三方 + * @return {email: String, username: String} + */ + + }, { + key: 'thirdQunarLogin', + value: function thirdQunarLogin() { + return { + request: function request(token) { + return new _promise2.default(function (resolve, reject) { + (0, _request3.default)('http://qsso.corp.qunar.com/api/verifytoken.php?token=' + token, function (error, response, body) { + if (!error && response.statusCode == 200) { + var result = JSON.parse(body); + if (result && result.ret === true) { + var ret = { + email: result.userId + '@qunar.com', + username: result.data.userInfo.name + }; + resolve(ret); + } else { + reject(result); + } + } + reject(error); + }); + }); + }, + tokenField: 'token' + }; + } + }, { + key: 'loginByToken', + value: function () { + var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) { + var config, token, ret, login; + return _regenerator2.default.wrap(function _callee3$(_context3) { + while (1) { + switch (_context3.prev = _context3.next) { + case 0: + config = this.thirdQunarLogin(); + token = ctx.request.body[config.tokenField] || ctx.request.query[config.tokenField]; + _context3.prev = 2; + _context3.next = 5; + return config.request(token); + + case 5: + ret = _context3.sent; + _context3.next = 8; + return this.handleThirdLogin(ret.email, ret.username); + + case 8: + login = _context3.sent; + + + if (login === true) { + _yapi2.default.commons.log('login success'); + ctx.redirect('/'); + } + _context3.next = 16; + break; + + case 12: + _context3.prev = 12; + _context3.t0 = _context3['catch'](2); + + _yapi2.default.commons.log(_context3.t0.message, 'error'); + ctx.redirect('/'); + + case 16: + case 'end': + return _context3.stop(); + } + } + }, _callee3, this, [[2, 12]]); + })); + + function loginByToken(_x3) { + return _ref3.apply(this, arguments); + } + + return loginByToken; + }() + }, { + key: 'handleThirdLogin', + value: function () { + var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(email, username) { + var user, data, passsalt, userInst; + return _regenerator2.default.wrap(function _callee4$(_context4) { + while (1) { + switch (_context4.prev = _context4.next) { + case 0: + user = void 0, data = void 0, passsalt = void 0; + userInst = _yapi2.default.getInst(_user2.default); + _context4.prev = 2; + _context4.next = 5; + return userInst.findByEmail(email); + + case 5: + user = _context4.sent; + + if (!(!user || !user._id)) { + _context4.next = 13; + break; + } + + passsalt = _yapi2.default.commons.randStr(); + data = { + username: username, + password: _yapi2.default.commons.generatePassword(passsalt, passsalt), + email: email, + passsalt: passsalt, + role: 'member', + add_time: _yapi2.default.commons.time(), + up_time: _yapi2.default.commons.time(), + type: 'third' + }; + _context4.next = 11; + return userInst.save(data); + + case 11: + user = _context4.sent; + + _yapi2.default.commons.sendMail({ + to: email, + contents: '

    \u4EB2\u7231\u7684\u7528\u6237\uFF1A

    \u60A8\u597D\uFF0C\u611F\u8C22\u4F7F\u7528YApi\u5E73\u53F0.

    ' + }); + + case 13: + + this.setLoginCookie(user._id, user.passsalt); + return _context4.abrupt('return', true); + + case 17: + _context4.prev = 17; + _context4.t0 = _context4['catch'](2); + + console.error('third_login:', _context4.t0.message); // eslint-disable-line + return _context4.abrupt('return', false); + + case 21: + case 'end': + return _context4.stop(); + } + } + }, _callee4, this, [[2, 17]]); + })); + + function handleThirdLogin(_x4, _x5) { + return _ref4.apply(this, arguments); + } + + return handleThirdLogin; + }() + + /** + * 修改用户密码 + * @interface /user/change_password + * @method POST + * @category user + * @param {Number} uid 用户ID + * @param {Number} [old_password] 旧密码, 非admin用户必须传 + * @param {Number} password 新密码 + * @return {Object} + * @example ./api/user/change_password.json + */ + + }, { + key: 'changePassword', + value: function () { + var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(ctx) { + var params, userInst, user, passsalt, data, result; + return _regenerator2.default.wrap(function _callee5$(_context5) { + while (1) { + switch (_context5.prev = _context5.next) { + case 0: + params = ctx.request.body; + userInst = _yapi2.default.getInst(_user2.default); + + if (params.uid) { + _context5.next = 4; + break; + } + + return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'uid不能为空')); + + case 4: + if (params.password) { + _context5.next = 6; + break; + } + + return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空')); + + case 6: + if (!(this.getRole() !== 'admin' && params.uid != this.getUid())) { + _context5.next = 8; + break; + } + + return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, '没有权限')); + + case 8: + if (!(this.getRole() !== 'admin')) { + _context5.next = 16; + break; + } + + if (params.old_password) { + _context5.next = 11; + break; + } + + return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '旧密码不能为空')); + + case 11: + _context5.next = 13; + return userInst.findById(params.uid); + + case 13: + user = _context5.sent; + + if (!(_yapi2.default.commons.generatePassword(params.old_password, user.passsalt) !== user.password)) { + _context5.next = 16; + break; + } + + return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, '旧密码错误')); + + case 16: + passsalt = _yapi2.default.commons.randStr(); + data = { + up_time: _yapi2.default.commons.time(), + password: _yapi2.default.commons.generatePassword(params.password, passsalt), + passsalt: passsalt + }; + _context5.prev = 18; + _context5.next = 21; + return userInst.update(params.uid, data); + + case 21: + result = _context5.sent; + + ctx.body = _yapi2.default.commons.resReturn(result); + _context5.next = 28; + break; + + case 25: + _context5.prev = 25; + _context5.t0 = _context5['catch'](18); + + ctx.body = _yapi2.default.commons.resReturn(null, 401, _context5.t0.message); + + case 28: + case 'end': + return _context5.stop(); + } + } + }, _callee5, this, [[18, 25]]); + })); + + function changePassword(_x6) { + return _ref5.apply(this, arguments); + } + + return changePassword; + }() + }, { + key: 'setLoginCookie', + value: function setLoginCookie(uid, passsalt) { + var token = jwt.sign({ uid: uid }, passsalt, { expiresIn: '7 days' }); + + this.ctx.cookies.set('_yapi_token', token, { + expires: _yapi2.default.commons.expireDate(7), + httpOnly: true + }); + this.ctx.cookies.set('_yapi_uid', uid, { + expires: _yapi2.default.commons.expireDate(7), + httpOnly: true + }); + } + + /** + * 用户注册接口 + * @interface /user/reg + * @method POST + * @category user + * @foldnumber 10 + * @param {String} email email名称,不能为空 + * @param {String} password 密码,不能为空 + * @param {String} [username] 用户名 + * @returns {Object} + * @example ./api/user/login.json + */ + + }, { + key: 'reg', + value: function () { + var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(ctx) { + var userInst, params, checkRepeat, passsalt, data, user; + return _regenerator2.default.wrap(function _callee6$(_context6) { + while (1) { + switch (_context6.prev = _context6.next) { + case 0: + //注册 + userInst = _yapi2.default.getInst(_user2.default); + params = ctx.request.body; //获取请求的参数,检查是否存在用户名和密码 + + params = _yapi2.default.commons.handleParams(params, { + username: 'string', + password: 'string', + email: 'string' + }); + + if (params.email) { + _context6.next = 5; + break; + } + + return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '邮箱不能为空')); + + case 5: + if (params.password) { + _context6.next = 7; + break; + } + + return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空')); + + case 7: + _context6.next = 9; + return userInst.checkRepeat(params.email); + + case 9: + checkRepeat = _context6.sent; + + if (!(checkRepeat > 0)) { + _context6.next = 12; + break; + } + + return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '该email已经注册')); + + case 12: + passsalt = _yapi2.default.commons.randStr(); + data = { + username: params.username, + password: _yapi2.default.commons.generatePassword(params.password, passsalt), //加密 + email: params.email, + passsalt: passsalt, + role: 'member', + add_time: _yapi2.default.commons.time(), + up_time: _yapi2.default.commons.time(), + type: "site" + }; + + + if (!data.username) { + data.username = data.email.substr(0, data.email.indexOf('@')); + } + + _context6.prev = 15; + _context6.next = 18; + return userInst.save(data); + + case 18: + user = _context6.sent; + + + this.setLoginCookie(user._id, user.passsalt); + ctx.body = _yapi2.default.commons.resReturn({ + uid: user._id, + email: user.email, + username: user.username, + add_time: user.add_time, + up_time: user.up_time, + role: 'member' + }); + _yapi2.default.commons.sendMail({ + to: user.email, + contents: '

    \u4EB2\u7231\u7684\u7528\u6237\uFF1A

    \u60A8\u597D\uFF0C\u611F\u8C22\u4F7F\u7528YApi,\u60A8\u7684\u8D26\u53F7 ' + params.email + ' \u5DF2\u7ECF\u6CE8\u518C\u6210\u529F

    ' + }); + _context6.next = 27; + break; + + case 24: + _context6.prev = 24; + _context6.t0 = _context6['catch'](15); + + ctx.body = _yapi2.default.commons.resReturn(null, 401, _context6.t0.message); + + case 27: + case 'end': + return _context6.stop(); + } + } + }, _callee6, this, [[15, 24]]); + })); + + function reg(_x7) { + return _ref6.apply(this, arguments); + } + + return reg; + }() + + /** + * 获取用户列表 + * @interface /user/list + * @method GET + * @category user + * @foldnumber 10 + * @param {Number} [page] 分页页码 + * @param {Number} [limit] 分页大小,默认为10条 + * @returns {Object} + * @example + */ + + }, { + key: 'list', + value: function () { + var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(ctx) { + var page, limit, userInst, user, count; + return _regenerator2.default.wrap(function _callee7$(_context7) { + while (1) { + switch (_context7.prev = _context7.next) { + case 0: + page = ctx.request.query.page || 1, limit = ctx.request.query.limit || 10; + userInst = _yapi2.default.getInst(_user2.default); + _context7.prev = 2; + _context7.next = 5; + return userInst.listWithPaging(page, limit); + + case 5: + user = _context7.sent; + _context7.next = 8; + return userInst.listCount(); + + case 8: + count = _context7.sent; + return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn({ + total: Math.ceil(count / limit), + list: user + })); + + case 12: + _context7.prev = 12; + _context7.t0 = _context7['catch'](2); + return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, _context7.t0.message)); + + case 15: + case 'end': + return _context7.stop(); + } + } + }, _callee7, this, [[2, 12]]); + })); + + function list(_x8) { + return _ref7.apply(this, arguments); + } + + return list; + }() + + /** + * 获取用户个人信息 + * @interface /user/find + * @method GET + * @param id 用户uid + * @category user + * @foldnumber 10 + * @returns {Object} + * @example + */ + + }, { + key: 'findById', + value: function () { + var _ref8 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(ctx) { + var userInst, id, result; + return _regenerator2.default.wrap(function _callee8$(_context8) { + while (1) { + switch (_context8.prev = _context8.next) { + case 0: + _context8.prev = 0; + userInst = _yapi2.default.getInst(_user2.default); + id = ctx.request.query.id; + + if (id) { + _context8.next = 5; + break; + } + + return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'uid不能为空')); + + case 5: + _context8.next = 7; + return userInst.findById(id); + + case 7: + result = _context8.sent; + + if (result) { + _context8.next = 10; + break; + } + + return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, '不存在的用户')); + + case 10: + return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn({ + uid: result._id, + username: result.username, + email: result.email, + role: result.role, + add_time: result.add_time, + up_time: result.up_time + })); + + case 13: + _context8.prev = 13; + _context8.t0 = _context8['catch'](0); + return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, _context8.t0.message)); + + case 16: + case 'end': + return _context8.stop(); + } + } + }, _callee8, this, [[0, 13]]); + })); + + function findById(_x9) { + return _ref8.apply(this, arguments); + } + + return findById; + }() + + /** + * 删除用户,只有admin用户才有此权限 + * @interface /user/del + * @method POST + * @param id 用户uid + * @category user + * @foldnumber 10 + * @returns {Object} + * @example + */ + + }, { + key: 'del', + value: function () { + var _ref9 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9(ctx) { + var userInst, id, result; + return _regenerator2.default.wrap(function _callee9$(_context9) { + while (1) { + switch (_context9.prev = _context9.next) { + case 0: + _context9.prev = 0; + + if (!(this.getRole() !== 'admin')) { + _context9.next = 3; + break; + } + + return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, 'Without permission.')); + + case 3: + userInst = _yapi2.default.getInst(_user2.default); + id = ctx.request.body.id; + + if (id) { + _context9.next = 7; + break; + } + + return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'uid不能为空')); + + case 7: + _context9.next = 9; + return userInst.del(id); + + case 9: + result = _context9.sent; + + + ctx.body = _yapi2.default.commons.resReturn(result); + _context9.next = 16; + break; + + case 13: + _context9.prev = 13; + _context9.t0 = _context9['catch'](0); + + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context9.t0.message); + + case 16: + case 'end': + return _context9.stop(); + } + } + }, _callee9, this, [[0, 13]]); + })); + + function del(_x10) { + return _ref9.apply(this, arguments); + } + + return del; + }() + + /** + * 更新用户个人信息 + * @interface /user/update + * @method POST + * @param uid 用户uid + * @param [role] 用户角色,只有管理员有权限修改 + * @param [username] String + * @param [email] String + * @category user + * @foldnumber 10 + * @returns {Object} + * @example + */ + + }, { + key: 'update', + value: function () { + var _ref10 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(ctx) { + var params, userInst, id, data, checkRepeat, result; + return _regenerator2.default.wrap(function _callee10$(_context10) { + while (1) { + switch (_context10.prev = _context10.next) { + case 0: + _context10.prev = 0; + params = ctx.request.body; + + + params = _yapi2.default.commons.handleParams(params, { + username: 'string', + email: 'string' + }); + + if (!(this.getRole() !== 'admin' && params.uid != this.getUid())) { + _context10.next = 5; + break; + } + + return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '没有权限')); + + case 5: + userInst = _yapi2.default.getInst(_user2.default); + id = params.uid; + + if (id) { + _context10.next = 9; + break; + } + + return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'uid不能为空')); + + case 9: + data = { + up_time: _yapi2.default.commons.time() + }; + + + params.username && (data.username = params.username); + params.email && (data.email = params.email); + + if (!data.email) { + _context10.next = 18; + break; + } + + _context10.next = 15; + return userInst.checkRepeat(data.email); + + case 15: + checkRepeat = _context10.sent; + + if (!(checkRepeat > 0)) { + _context10.next = 18; + break; + } + + return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '该email已经注册')); + + case 18: + _context10.next = 20; + return userInst.update(id, data); + + case 20: + result = _context10.sent; + + + ctx.body = _yapi2.default.commons.resReturn(result); + _context10.next = 27; + break; + + case 24: + _context10.prev = 24; + _context10.t0 = _context10['catch'](0); + + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context10.t0.message); + + case 27: + case 'end': + return _context10.stop(); + } + } + }, _callee10, this, [[0, 24]]); + })); + + function update(_x11) { + return _ref10.apply(this, arguments); + } + + return update; + }() + + /** + * + * @param {*} basecode base64编码,通过h5 api传给后端 + */ + + }, { + key: 'uploadAvatar', + value: function () { + var _ref11 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11(ctx) { + var basecode, pngPrefix, jpegPrefix, type, strLength, avatarInst, result; + return _regenerator2.default.wrap(function _callee11$(_context11) { + while (1) { + switch (_context11.prev = _context11.next) { + case 0: + _context11.prev = 0; + basecode = ctx.request.body.basecode; + + if (basecode) { + _context11.next = 4; + break; + } + + return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'basecode不能为空')); + + case 4: + pngPrefix = 'data:image/png;base64,'; + jpegPrefix = 'data:image/jpeg;base64,'; + type = void 0; + + if (!(basecode.substr(0, pngPrefix.length) === pngPrefix)) { + _context11.next = 12; + break; + } + + basecode = basecode.substr(pngPrefix.length); + type = 'image/png'; + _context11.next = 18; + break; + + case 12: + if (!(basecode.substr(0, jpegPrefix.length) === jpegPrefix)) { + _context11.next = 17; + break; + } + + basecode = basecode.substr(jpegPrefix.length); + type = 'image/jpeg'; + _context11.next = 18; + break; + + case 17: + return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '仅支持jpeg和png格式的图片')); + + case 18: + strLength = basecode.length; + + if (!(parseInt(strLength - strLength / 8 * 2) > 200000)) { + _context11.next = 21; + break; + } + + return _context11.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '图片大小不能超过200kb')); + + case 21: + avatarInst = _yapi2.default.getInst(_avatar2.default); + _context11.next = 24; + return avatarInst.up(this.getUid(), basecode, type); + + case 24: + result = _context11.sent; + + ctx.body = _yapi2.default.commons.resReturn(result); + + _context11.next = 31; + break; + + case 28: + _context11.prev = 28; + _context11.t0 = _context11['catch'](0); + + ctx.body = _yapi2.default.commons.resReturn(null, 401, _context11.t0.message); + + case 31: + case 'end': + return _context11.stop(); + } + } + }, _callee11, this, [[0, 28]]); + })); + + function uploadAvatar(_x12) { + return _ref11.apply(this, arguments); + } + + return uploadAvatar; + }() + }, { + key: 'avatar', + value: function () { + var _ref12 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(ctx) { + var avatarInst, data, dataBuffer, type; + return _regenerator2.default.wrap(function _callee12$(_context12) { + while (1) { + switch (_context12.prev = _context12.next) { + case 0: + _context12.prev = 0; + avatarInst = _yapi2.default.getInst(_avatar2.default); + _context12.next = 4; + return avatarInst.get(this.getUid()); + + case 4: + data = _context12.sent; + dataBuffer = void 0, type = void 0; + + if (!data || !data.basecode) { + dataBuffer = _yapi2.default.fs.readFileSync(_yapi2.default.path.join(_yapi2.default.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; + _context12.next = 14; + break; + + case 11: + _context12.prev = 11; + _context12.t0 = _context12['catch'](0); + + ctx.body = 'error:' + _context12.t0.message; + + case 14: + case 'end': + return _context12.stop(); + } + } + }, _callee12, this, [[0, 11]]); + })); + + function avatar(_x13) { + return _ref12.apply(this, arguments); + } + + return avatar; + }() + + /** + * 模糊搜索用户名或者email + * @interface /user/search + * @method GET + * @category user + * @foldnumber 10 + * @param {String} q + * @return {Object} + * @example ./api/user/search.json + */ + + }, { + key: 'search', + value: function () { + var _ref13 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee13(ctx) { + var q, queryList, rules, filteredRes; + return _regenerator2.default.wrap(function _callee13$(_context13) { + while (1) { + switch (_context13.prev = _context13.next) { + case 0: + q = ctx.request.query.q; + + if (q) { + _context13.next = 3; + break; + } + + return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'No keyword.')); + + case 3: + if (_yapi2.default.commons.validateSearchKeyword(q)) { + _context13.next = 5; + break; + } + + return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'Bad query.')); + + case 5: + _context13.next = 7; + return this.Model.search(q); + + case 7: + queryList = _context13.sent; + rules = [{ + key: '_id', + alias: 'uid' + }, 'username', 'email', 'role', { + key: 'add_time', + alias: 'addTime' + }, { + key: 'up_time', + alias: 'upTime' + }]; + filteredRes = _commons2.default.filterRes(queryList, rules); + return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(filteredRes, 0, 'ok')); + + case 11: + case 'end': + return _context13.stop(); + } + } + }, _callee13, this); + })); + + function search(_x14) { + return _ref13.apply(this, arguments); + } + + return search; + }() + + /** + * 根据路由id获取面包屑数据 + * @interface /user/nav + * @method GET + * @category user + * @foldnumber 10 + * @param {String} type 可选group|interface|project + * @param {Number} id + * @return {Object} + * @example ./api/user/nav.json + */ + + }, { + key: 'nav', + 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; + + return _regenerator2.default.wrap(function _callee14$(_context14) { + while (1) { + switch (_context14.prev = _context14.next) { + case 0: + _ctx$request$query = ctx.request.query, id = _ctx$request$query.id, type = _ctx$request$query.type; + result = {}; + _context14.prev = 2; + + if (!(type === 'interface')) { + _context14.next = 12; + break; + } + + interfaceInst = _yapi2.default.getInst(_interface2.default); + _context14.next = 7; + return interfaceInst.get(id); + + case 7: + interfaceData = _context14.sent; + + result["interface_id"] = interfaceData._id; + result["interface_name"] = interfaceData.path; + + type = 'project'; + id = interfaceData.project_id; + + case 12: + if (!(type === 'project')) { + _context14.next = 21; + break; + } + + projectInst = _yapi2.default.getInst(_project2.default); + _context14.next = 16; + return projectInst.get(id); + + case 16: + projectData = _context14.sent; + + result["project_id"] = projectData._id; + result["project_name"] = projectData.prd_host + projectData.basepath; + type = 'group'; + id = projectData.group_id; + + case 21: + if (!(type === 'group')) { + _context14.next = 28; + break; + } + + groupInst = _yapi2.default.getInst(_group2.default); + _context14.next = 25; + return groupInst.get(id); + + case 25: + groupData = _context14.sent; + + result["group_id"] = groupData._id; + result["group_name"] = groupData.group_name; + + case 28: + return _context14.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(result)); + + case 31: + _context14.prev = 31; + _context14.t0 = _context14['catch'](2); + return _context14.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(result, 422, _context14.t0.message)); + + case 34: + case 'end': + return _context14.stop(); + } + } + }, _callee14, this, [[2, 31]]); + })); + + function nav(_x15) { + return _ref14.apply(this, arguments); + } + + return nav; + }() + }]); + return userController; +}(_base2.default); + +module.exports = userController; \ No newline at end of file diff --git a/server_dist/middleware/checkToken.js b/server_dist/middleware/checkToken.js new file mode 100644 index 00000000..0eca1c2f --- /dev/null +++ b/server_dist/middleware/checkToken.js @@ -0,0 +1,36 @@ +"use strict"; + +var _regenerator = require("babel-runtime/regenerator"); + +var _regenerator2 = _interopRequireDefault(_regenerator); + +var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator"); + +var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +// const jwt = require('jsonwebtoken'); +//检查token是否过期 + +module.exports = function () { + var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) { + return _regenerator2.default.wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + _context.next = 2; + return next(); + + case 2: + case "end": + return _context.stop(); + } + } + }, _callee, undefined); + })); + + return function (_x, _x2) { + return _ref.apply(this, arguments); + }; +}(); \ No newline at end of file diff --git a/server_dist/middleware/mockServer.js b/server_dist/middleware/mockServer.js new file mode 100644 index 00000000..2ee0428d --- /dev/null +++ b/server_dist/middleware/mockServer.js @@ -0,0 +1,177 @@ +'use strict'; + +var _regenerator = require('babel-runtime/regenerator'); + +var _regenerator2 = _interopRequireDefault(_regenerator); + +var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); + +var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _project2 = require('../models/project.js'); + +var _project3 = _interopRequireDefault(_project2); + +var _interface = require('../models/interface.js'); + +var _interface2 = _interopRequireDefault(_interface); + +var _mockjs = require('mockjs'); + +var _mockjs2 = _interopRequireDefault(_mockjs); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +module.exports = function () { + var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) { + var hostname, config, projectInst, projects, matchProject, maxBasepath, i, l, _project, project, interfaceData, interfaceInst, res; + + return _regenerator2.default.wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + _yapi2.default.commons.log('Server Recevie Request...'); + + hostname = ctx.hostname; + config = _yapi2.default.WEBCONFIG; + + if (!(ctx.hostname === config.webhost)) { + _context.next = 8; + break; + } + + if (!next) { + _context.next = 7; + break; + } + + _context.next = 7; + return next(); + + case 7: + return _context.abrupt('return', true); + + case 8: + + _yapi2.default.commons.log('MockServer Running...'); + projectInst = _yapi2.default.getInst(_project3.default), projects = void 0; + _context.prev = 10; + _context.next = 13; + return projectInst.getByDomain(hostname); + + case 13: + projects = _context.sent; + _context.next = 19; + break; + + case 16: + _context.prev = 16; + _context.t0 = _context['catch'](10); + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 403, _context.t0.message)); + + case 19: + matchProject = [], maxBasepath = 0; + + + for (i = 0, l = projects.length; i < l; i++) { + _project = projects[i]; + + if (ctx.path && _project.basepath == "") { + matchProject = _project; + } else if (ctx.path && ctx.path.indexOf(_project.basepath) === 0) { + if (_project.basepath.length > maxBasepath) { + maxBasepath = _project.basepath.length; + matchProject = _project; + } + } + } + + if (!(matchProject === false)) { + _context.next = 23; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '不存在的domain')); + + case 23: + project = matchProject, interfaceData = void 0; + interfaceInst = _yapi2.default.getInst(_interface2.default); + _context.prev = 25; + _context.next = 28; + return interfaceInst.getByPath(project._id, ctx.path.substr(project.basepath.length), ctx.method); + + case 28: + interfaceData = _context.sent; + + if (!(!interfaceData || interfaceData.length === 0)) { + _context.next = 35; + break; + } + + if (!(ctx.method === 'OPTIONS')) { + _context.next = 34; + break; + } + + ctx.set("Access-Control-Allow-Origin", "*"); + ctx.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); + return _context.abrupt('return', ctx.body = 'ok'); + + case 34: + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 404, '不存在的api')); + + case 35: + if (!(interfaceData.length > 1)) { + _context.next = 37; + break; + } + + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '存在多个api,请检查数据库')); + + case 37: + + interfaceData = interfaceData[0]; + ctx.set("Access-Control-Allow-Origin", "*"); + + if (!(interfaceData.res_body_type === 'json')) { + _context.next = 48; + break; + } + + _context.prev = 40; + res = _mockjs2.default.mock(_yapi2.default.commons.json_parse(interfaceData.res_body)); + return _context.abrupt('return', ctx.body = res); + + case 45: + _context.prev = 45; + _context.t1 = _context['catch'](40); + return _context.abrupt('return', ctx.body = { + errcode: 400, + errmsg: 'mock json数据格式有误', + data: interfaceData.res_body + }); + + case 48: + return _context.abrupt('return', ctx.body = interfaceData.res_body); + + case 51: + _context.prev = 51; + _context.t2 = _context['catch'](25); + return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 409, _context.t2.message)); + + case 54: + case 'end': + return _context.stop(); + } + } + }, _callee, undefined, [[10, 16], [25, 51], [40, 45]]); + })); + + return function (_x, _x2) { + return _ref.apply(this, arguments); + }; +}(); \ No newline at end of file diff --git a/server_dist/middleware/userauth.js b/server_dist/middleware/userauth.js new file mode 100644 index 00000000..3cbed01f --- /dev/null +++ b/server_dist/middleware/userauth.js @@ -0,0 +1,44 @@ +"use strict"; + +var _regenerator = require("babel-runtime/regenerator"); + +var _regenerator2 = _interopRequireDefault(_regenerator); + +var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator"); + +var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +module.exports = function () { + var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) { + var path; + return _regenerator2.default.wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + path = ctx.path; + + console.log(path); // eslint-disable-line + console.log(ctx.hostname); // eslint-disable-line + + if (!next) { + _context.next = 6; + break; + } + + _context.next = 6; + return next(); + + case 6: + case "end": + return _context.stop(); + } + } + }, _callee, undefined); + })); + + return function (_x, _x2) { + return _ref.apply(this, arguments); + }; +}(); \ No newline at end of file diff --git a/server_dist/models/avatar.js b/server_dist/models/avatar.js new file mode 100644 index 00000000..89a4d343 --- /dev/null +++ b/server_dist/models/avatar.js @@ -0,0 +1,81 @@ +'use strict'; + +var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); + +var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); + +var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); + +var _inherits2 = require('babel-runtime/helpers/inherits'); + +var _inherits3 = _interopRequireDefault(_inherits2); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _base = require('./base.js'); + +var _base2 = _interopRequireDefault(_base); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +// import userModel from '../models/user.js'; + +var avatarModel = function (_baseModel) { + (0, _inherits3.default)(avatarModel, _baseModel); + + function avatarModel() { + (0, _classCallCheck3.default)(this, avatarModel); + return (0, _possibleConstructorReturn3.default)(this, (avatarModel.__proto__ || (0, _getPrototypeOf2.default)(avatarModel)).apply(this, arguments)); + } + + (0, _createClass3.default)(avatarModel, [{ + key: 'getName', + value: function getName() { + return 'avatar'; + } + }, { + key: 'getSchema', + value: function getSchema() { + return { + uid: { type: Number, required: true }, + basecode: String, + type: String + }; + } + }, { + key: 'get', + value: function get(uid) { + + return this.model.findOne({ + uid: uid + }); + } + }, { + key: 'up', + value: function up(uid, basecode, type) { + return this.model.update({ + uid: uid + }, { + type: type, + basecode: basecode + }, { + upsert: true + }); + } + }]); + return avatarModel; +}(_base2.default); + +module.exports = avatarModel; \ No newline at end of file diff --git a/server_dist/models/base.js b/server_dist/models/base.js new file mode 100644 index 00000000..2680baef --- /dev/null +++ b/server_dist/models/base.js @@ -0,0 +1,82 @@ +'use strict'; + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _mongoose = require('mongoose'); + +var _mongoose2 = _interopRequireDefault(_mongoose); + +var _mongooseAutoIncrement = require('mongoose-auto-increment'); + +var _mongooseAutoIncrement2 = _interopRequireDefault(_mongooseAutoIncrement); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 所有的model都需要继承baseModel, 且需要 getSchema和getName方法,不然会报错 + */ + +var baseModel = function () { + function baseModel() { + (0, _classCallCheck3.default)(this, baseModel); + + this.schema = new _mongoose2.default.Schema(this.getSchema()); + this.name = this.getName(); + + if (this.isNeedAutoIncrement() === true) { + this.schema.plugin(_mongooseAutoIncrement2.default.plugin, { + model: this.name, + field: this.getPrimaryKey(), + startAt: 101, + incrementBy: _yapi2.default.commons.rand(1, 10) + }); + } + + this.model = _yapi2.default.db(this.name, this.schema); + } + + (0, _createClass3.default)(baseModel, [{ + key: 'isNeedAutoIncrement', + value: function isNeedAutoIncrement() { + return true; + } + + /** + * 可通过覆盖此方法生成其他自增字段 + */ + + }, { + key: 'getPrimaryKey', + value: function getPrimaryKey() { + return '_id'; + } + + /** + * 获取collection的schema结构 + */ + + }, { + key: 'getSchema', + value: function getSchema() { + _yapi2.default.commons.log('Model Class need getSchema function', 'error'); + } + }, { + key: 'getName', + value: function getName() { + _yapi2.default.commons.log('Model Class need name', 'error'); + } + }]); + return baseModel; +}(); + +module.exports = baseModel; \ No newline at end of file diff --git a/server_dist/models/follow.js b/server_dist/models/follow.js new file mode 100644 index 00000000..343250f4 --- /dev/null +++ b/server_dist/models/follow.js @@ -0,0 +1,109 @@ +'use strict'; + +var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); + +var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); + +var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); + +var _inherits2 = require('babel-runtime/helpers/inherits'); + +var _inherits3 = _interopRequireDefault(_inherits2); + +var _base = require('./base.js'); + +var _base2 = _interopRequireDefault(_base); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var followModel = function (_baseModel) { + (0, _inherits3.default)(followModel, _baseModel); + + function followModel() { + (0, _classCallCheck3.default)(this, followModel); + return (0, _possibleConstructorReturn3.default)(this, (followModel.__proto__ || (0, _getPrototypeOf2.default)(followModel)).apply(this, arguments)); + } + + (0, _createClass3.default)(followModel, [{ + key: 'getName', + value: function getName() { + return 'follow'; + } + }, { + key: 'getSchema', + value: function getSchema() { + return { + uid: { type: Number, required: true }, + projectid: { type: Number, required: true }, + projectname: { type: String, required: true }, + icon: String + }; + } + + /** + * @param {Number} uid 用户id + * @param {Number} projectid 项目id + * @param {String} projectname 项目名 + * @param {String} icon 项目图标 + */ + + }, { + key: 'save', + value: function save(data) { + //关注 + var saveData = { + uid: data.uid, + projectid: data.projectid, + projectname: data.projectname, + icon: data.icon + }; + var follow = new this.model(saveData); + return follow.save(); + } + }, { + key: 'del', + value: function del(id) { + return this.model.deleteOne({ + _id: id + }); + } + }, { + key: 'listWithPaging', + value: function listWithPaging(uid, page, limit) { + page = parseInt(page); + limit = parseInt(limit); + + return this.model.find({ + uid: uid + }).skip((page - 1) * limit).limit(limit).exec(); + } + }, { + key: 'listCount', + value: function listCount(uid) { + return this.model.count({ + uid: uid + }); + } + }, { + key: 'checkProjectRepeat', + value: function checkProjectRepeat(uid, projectid) { + return this.model.count({ + uid: uid, + projectid: projectid + }); + } + }]); + return followModel; +}(_base2.default); + +module.exports = followModel; \ No newline at end of file diff --git a/server_dist/models/group.js b/server_dist/models/group.js new file mode 100644 index 00000000..685e7d76 --- /dev/null +++ b/server_dist/models/group.js @@ -0,0 +1,153 @@ +'use strict'; + +var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); + +var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); + +var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); + +var _inherits2 = require('babel-runtime/helpers/inherits'); + +var _inherits3 = _interopRequireDefault(_inherits2); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _base = require('./base.js'); + +var _base2 = _interopRequireDefault(_base); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var groupModel = function (_baseModel) { + (0, _inherits3.default)(groupModel, _baseModel); + + function groupModel() { + (0, _classCallCheck3.default)(this, groupModel); + return (0, _possibleConstructorReturn3.default)(this, (groupModel.__proto__ || (0, _getPrototypeOf2.default)(groupModel)).apply(this, arguments)); + } + + (0, _createClass3.default)(groupModel, [{ + key: 'getName', + value: function getName() { + return 'group'; + } + }, { + key: 'getSchema', + value: function getSchema() { + return { + uid: Number, + group_name: String, + group_desc: String, + add_time: Number, + up_time: Number, + members: [{ + uid: Number, + role: { type: String, enum: ['owner', 'dev'] }, + username: String, + email: String + }] + }; + } + }, { + key: 'save', + value: function save(data) { + var m = new this.model(data); + return m.save(); + } + }, { + key: 'get', + value: function get(id) { + return this.model.findOne({ + _id: id + }).exec(); + } + }, { + key: 'checkRepeat', + value: function checkRepeat(name) { + return this.model.count({ + group_name: name + }); + } + }, { + key: 'addMember', + value: function addMember(id, data) { + return this.model.update({ + _id: id + }, { + $push: { members: data } + }); + } + }, { + key: 'delMember', + value: function delMember(id, uid) { + return this.model.update({ + _id: id + }, { + $pull: { members: { uid: uid } } + }); + } + }, { + key: 'changeMemberRole', + value: function changeMemberRole(id, uid, role) { + return this.model.update({ + _id: id, + "members.uid": uid + }, { + "$set": { "members.$.uid": role } + }); + } + }, { + key: 'checkMemberRepeat', + value: function checkMemberRepeat(id, uid) { + return this.model.count({ + _id: id, + "members.uid": uid + }); + } + }, { + key: 'list', + value: function list() { + return this.model.find().select('group_name _id group_desc add_time up_time').exec(); + } + }, { + key: 'del', + value: function del(id) { + return this.model.deleteOne({ + _id: id + }); + } + }, { + key: 'up', + value: function up(id, data) { + return this.model.update({ + _id: id + }, { + group_name: data.group_name, + group_desc: data.group_desc, + up_time: _yapi2.default.commons.time() + }); + } + }, { + key: 'search', + value: function search(keyword) { + return this.model.find({ + group_name: new RegExp(keyword, 'i') + }).limit(10); + } + }]); + return groupModel; +}(_base2.default); + +module.exports = groupModel; \ No newline at end of file diff --git a/server_dist/models/interface.js b/server_dist/models/interface.js new file mode 100644 index 00000000..d51903dd --- /dev/null +++ b/server_dist/models/interface.js @@ -0,0 +1,143 @@ +'use strict'; + +var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); + +var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); + +var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); + +var _inherits2 = require('babel-runtime/helpers/inherits'); + +var _inherits3 = _interopRequireDefault(_inherits2); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _base = require('./base.js'); + +var _base2 = _interopRequireDefault(_base); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var interfaceModel = function (_baseModel) { + (0, _inherits3.default)(interfaceModel, _baseModel); + + function interfaceModel() { + (0, _classCallCheck3.default)(this, interfaceModel); + return (0, _possibleConstructorReturn3.default)(this, (interfaceModel.__proto__ || (0, _getPrototypeOf2.default)(interfaceModel)).apply(this, arguments)); + } + + (0, _createClass3.default)(interfaceModel, [{ + key: 'getName', + value: function getName() { + return 'interface'; + } + }, { + key: 'getSchema', + value: function getSchema() { + return { + title: { type: String, required: true }, + uid: { type: Number, required: true }, + path: { type: String, required: true }, + method: { type: String, required: true }, + project_id: { type: Number, required: true }, + desc: String, + add_time: Number, + up_time: Number, + req_query: [{ + name: String, value: String, desc: String, required: Boolean + }], + req_headers: [{ + name: String, value: String, desc: String, required: Boolean + }], + req_params_type: { + type: String, + enum: ['form', 'json', 'text', 'xml'] + }, + req_params_form: [{ + name: String, value: String, value_type: { type: String, enum: ['text', 'file'] }, desc: String, required: Boolean + }], + req_params_other: String, + res_body_type: { + type: String, + enum: ['json', 'text', 'xml'] + }, + res_body: String + }; + } + }, { + key: 'save', + value: function save(data) { + var m = new this.model(data); + return m.save(); + } + }, { + key: 'get', + value: function get(id) { + return this.model.findOne({ + _id: id + }).exec(); + } + }, { + key: 'getByPath', + value: function getByPath(project_id, path, method) { + return this.model.find({ + project_id: project_id, + path: path, + method: method + }).exec(); + } + }, { + key: 'checkRepeat', + value: function checkRepeat(id, path, method) { + return this.model.count({ + project_id: id, + path: path, + method: method + }); + } + }, { + key: 'countByProjectId', + value: function countByProjectId(id) { + return this.model.count({ + project_id: id + }); + } + }, { + key: 'list', + value: function list(project_id) { + return this.model.find({ + project_id: project_id + }).sort({ _id: -1 }).exec(); + } + }, { + key: 'del', + value: function del(id) { + return this.model.deleteOne({ + _id: id + }); + } + }, { + key: 'up', + value: function up(id, data) { + data.up_time = _yapi2.default.commons.time(); + return this.model.update({ + _id: id + }, data, { runValidators: true }); + } + }]); + return interfaceModel; +}(_base2.default); + +module.exports = interfaceModel; \ No newline at end of file diff --git a/server_dist/models/interfaceCase.js b/server_dist/models/interfaceCase.js new file mode 100644 index 00000000..5b8f5ba4 --- /dev/null +++ b/server_dist/models/interfaceCase.js @@ -0,0 +1,122 @@ +'use strict'; + +var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); + +var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); + +var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); + +var _inherits2 = require('babel-runtime/helpers/inherits'); + +var _inherits3 = _interopRequireDefault(_inherits2); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _base = require('./base.js'); + +var _base2 = _interopRequireDefault(_base); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var interfaceCase = function (_baseModel) { + (0, _inherits3.default)(interfaceCase, _baseModel); + + function interfaceCase() { + (0, _classCallCheck3.default)(this, interfaceCase); + return (0, _possibleConstructorReturn3.default)(this, (interfaceCase.__proto__ || (0, _getPrototypeOf2.default)(interfaceCase)).apply(this, arguments)); + } + + (0, _createClass3.default)(interfaceCase, [{ + key: 'getName', + value: function getName() { + return 'interface_case'; + } + }, { + key: 'getSchema', + value: function getSchema() { + return { + casename: { type: String, required: true }, + uid: { type: Number, required: true }, + col_id: { type: Number, required: true }, + index: { type: Number, default: 0 }, + project_id: { type: Number, required: true }, + add_time: Number, + up_time: Number, + env: { type: String, required: true }, + domain: { type: String }, + path: { type: String, required: true }, + method: { type: String, required: true }, + req_query: [{ + name: String, value: String + }], + req_headers: [{ + name: String, value: String + }], + req_body_type: { + type: String, + enum: ['form', 'json', 'text', 'xml'] + }, + res_body_form: [{ + name: String, value: String + }], + res_body_other: String + + }; + } + }, { + key: 'save', + value: function save(data) { + var m = new this.model(data); + return m.save(); + } + }, { + key: 'get', + value: function get(id) { + return this.model.findOne({ + _id: id + }).exec(); + } + }, { + key: 'list', + value: function list() { + return this.model.find().select("casename uid col_id _id index").exec(); + } + }, { + key: 'del', + value: function del(id) { + return this.model.deleteOne({ + _id: id + }); + } + }, { + key: 'up', + value: function up(id, data) { + data.up_time = _yapi2.default.commons.time(); + return this.model.update({ _id: id }, data); + } + }, { + key: 'upCaseIndex', + value: function upCaseIndex(id, index) { + return this.model.update({ + _id: id + }, { + index: index + }); + } + }]); + return interfaceCase; +}(_base2.default); + +module.exports = interfaceCase; \ No newline at end of file diff --git a/server_dist/models/interfaceCol.js b/server_dist/models/interfaceCol.js new file mode 100644 index 00000000..efd06e40 --- /dev/null +++ b/server_dist/models/interfaceCol.js @@ -0,0 +1,104 @@ +'use strict'; + +var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); + +var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); + +var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); + +var _inherits2 = require('babel-runtime/helpers/inherits'); + +var _inherits3 = _interopRequireDefault(_inherits2); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _base = require('./base.js'); + +var _base2 = _interopRequireDefault(_base); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var interfaceCol = function (_baseModel) { + (0, _inherits3.default)(interfaceCol, _baseModel); + + function interfaceCol() { + (0, _classCallCheck3.default)(this, interfaceCol); + return (0, _possibleConstructorReturn3.default)(this, (interfaceCol.__proto__ || (0, _getPrototypeOf2.default)(interfaceCol)).apply(this, arguments)); + } + + (0, _createClass3.default)(interfaceCol, [{ + key: 'getName', + value: function getName() { + return 'interface_col'; + } + }, { + key: 'getSchema', + value: function getSchema() { + return { + name: { type: String, required: true }, + uid: { type: Number, required: true }, + project_id: { type: Number, required: true }, + desc: String, + add_time: Number, + up_time: Number + }; + } + }, { + key: 'save', + value: function save(data) { + var m = new this.model(data); + return m.save(); + } + }, { + key: 'get', + value: function get(id) { + return this.model.findOne({ + _id: id + }).exec(); + } + }, { + key: 'checkRepeat', + value: function checkRepeat(name) { + return this.model.count({ + name: name + }); + } + }, { + key: 'list', + value: function list(project_id) { + return this.model.find({ + project_id: project_id + }).exec(); + } + }, { + key: 'del', + value: function del(id) { + return this.model.deleteOne({ + _id: id + }); + } + }, { + key: 'up', + value: function up(id, data) { + data.up_time = _yapi2.default.commons.time(); + return this.model.update({ + _id: id + }, data); + } + }]); + return interfaceCol; +}(_base2.default); + +module.exports = interfaceCol; \ No newline at end of file diff --git a/server_dist/models/log.js b/server_dist/models/log.js new file mode 100644 index 00000000..401c918c --- /dev/null +++ b/server_dist/models/log.js @@ -0,0 +1,117 @@ +'use strict'; + +var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); + +var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); + +var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); + +var _inherits2 = require('babel-runtime/helpers/inherits'); + +var _inherits3 = _interopRequireDefault(_inherits2); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _base = require('./base.js'); + +var _base2 = _interopRequireDefault(_base); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +// import userModel from '../models/user.js'; + +var logModel = function (_baseModel) { + (0, _inherits3.default)(logModel, _baseModel); + + function logModel() { + (0, _classCallCheck3.default)(this, logModel); + return (0, _possibleConstructorReturn3.default)(this, (logModel.__proto__ || (0, _getPrototypeOf2.default)(logModel)).apply(this, arguments)); + } + + (0, _createClass3.default)(logModel, [{ + key: 'getName', + value: function getName() { + return 'log'; + } + }, { + key: 'getSchema', + value: function getSchema() { + return { + uid: { type: Number, required: true }, + typeid: { type: Number, required: true }, + type: { type: String, enum: ['user', 'group', 'interface', 'project', 'other', 'interface_col'], required: true }, + content: { type: String, required: true }, + username: { type: String, required: true }, + add_time: Number + }; + } + + /** + * @param {String} content log内容 + * @param {Enum} type log类型, ['user', 'group', 'interface', 'project', 'other'] + * @param {Number} uid 用户id + */ + + }, { + key: 'save', + value: function save(data) { + var saveData = { + content: data.content, + type: data.type, + uid: data.uid, + username: data.username, + typeid: data.typeid, + add_time: _yapi2.default.commons.time() + }; + var log = new this.model(saveData); + + return log.save(); + } + }, { + key: 'del', + value: function del(id) { + return this.model.deleteOne({ + _id: id + }); + } + }, { + key: 'list', + value: function list(typeid) { + return this.model.find({ + typeid: typeid + }).exec(); + } + }, { + key: 'listWithPaging', + value: function listWithPaging(typeid, page, limit) { + page = parseInt(page); + limit = parseInt(limit); + + return this.model.find({ + typeid: typeid + }).skip((page - 1) * limit).limit(limit).exec(); + } + }, { + key: 'listCount', + value: function listCount(typeid) { + return this.model.count({ + typeid: typeid + }); + } + }]); + return logModel; +}(_base2.default); + +module.exports = logModel; \ No newline at end of file diff --git a/server_dist/models/project.js b/server_dist/models/project.js new file mode 100644 index 00000000..b9b4988d --- /dev/null +++ b/server_dist/models/project.js @@ -0,0 +1,200 @@ +'use strict'; + +var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); + +var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); + +var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); + +var _inherits2 = require('babel-runtime/helpers/inherits'); + +var _inherits3 = _interopRequireDefault(_inherits2); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _base = require('./base.js'); + +var _base2 = _interopRequireDefault(_base); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var projectModel = function (_baseModel) { + (0, _inherits3.default)(projectModel, _baseModel); + + function projectModel() { + (0, _classCallCheck3.default)(this, projectModel); + return (0, _possibleConstructorReturn3.default)(this, (projectModel.__proto__ || (0, _getPrototypeOf2.default)(projectModel)).apply(this, arguments)); + } + + (0, _createClass3.default)(projectModel, [{ + key: 'getName', + value: function getName() { + return 'project'; + } + }, { + key: 'getSchema', + value: function getSchema() { + return { + uid: { type: Number, required: true }, + name: { type: String, required: true }, + basepath: { type: String }, + desc: String, + group_id: { type: Number, required: true }, + project_type: { type: String, required: true, enum: ['public', 'private'] }, + members: [{ uid: Number, role: { type: String, enum: ['owner', 'dev'], username: String, email: String } }], + protocol: { type: String, required: true }, + prd_host: { type: String, required: true }, + env: [{ name: String, domain: String }], + add_time: Number, + up_time: Number + }; + } + }, { + key: 'save', + value: function save(data) { + var m = new this.model(data); + return m.save(); + } + }, { + key: 'get', + value: function get(id) { + return this.model.findOne({ + _id: id + }).exec(); + } + }, { + key: 'getByDomain', + value: function getByDomain(domain) { + return this.model.find({ + prd_host: domain + }).exec(); + } + }, { + key: 'checkNameRepeat', + value: function checkNameRepeat(name) { + return this.model.count({ + name: name + }); + } + }, { + key: 'checkDomainRepeat', + value: function checkDomainRepeat(domain, basepath) { + return this.model.count({ + prd_host: domain, + basepath: basepath + }); + } + }, { + key: 'list', + value: function list(group_id, auth) { + var params = { group_id: group_id }; + if (!auth) params.project_type = 'public'; + return this.model.find(params).sort({ _id: -1 }).exec(); + } + }, { + key: 'listWithPaging', + value: function listWithPaging(group_id, page, limit) { + page = parseInt(page); + limit = parseInt(limit); + return this.model.find({ + group_id: group_id + }).sort({ _id: -1 }).skip((page - 1) * limit).limit(limit).exec(); + } + }, { + key: 'listCount', + value: function listCount(group_id) { + return this.model.count({ + group_id: group_id + }); + } + }, { + key: 'countByGroupId', + value: function countByGroupId(group_id) { + return this.model.count({ + group_id: group_id + }); + } + }, { + key: 'del', + value: function del(id) { + return this.model.deleteOne({ + _id: id + }); + } + }, { + key: 'up', + value: function up(id, data) { + data.up_time = _yapi2.default.commons.time(); + return this.model.update({ + _id: id + }, data, { runValidators: true }); + } + }, { + key: 'addMember', + value: function addMember(id, data) { + return this.model.update({ + _id: id + }, { + $push: { members: data } + }); + } + }, { + key: 'delMember', + value: function delMember(id, uid) { + return this.model.update({ + _id: id + }, { + $pull: { members: uid } + }); + } + }, { + key: 'checkMemberRepeat', + value: function checkMemberRepeat(id, uid) { + return this.model.count({ + _id: id, + "members.uid": uid + }); + } + }, { + key: 'changeMemberRole', + value: function changeMemberRole(id, uid, role) { + return this.model.update({ + _id: id, + "members.uid": uid + }, { + "$set": { "members.$.uid": role } + }); + } + }, { + key: 'search', + value: function search(keyword) { + return this.model.find({ + name: new RegExp(keyword, 'ig') + }).limit(10); + } + }, { + key: 'download', + value: function download(id) { + console.log('models in download'); + // return this.model.find({ + // name: new RegExp(id, 'ig') + // }) + // .limit(10); + } + }]); + return projectModel; +}(_base2.default); + +module.exports = projectModel; \ No newline at end of file diff --git a/server_dist/models/user.js b/server_dist/models/user.js new file mode 100644 index 00000000..c6a50f8d --- /dev/null +++ b/server_dist/models/user.js @@ -0,0 +1,142 @@ +'use strict'; + +var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); + +var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); + +var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); + +var _inherits2 = require('babel-runtime/helpers/inherits'); + +var _inherits3 = _interopRequireDefault(_inherits2); + +var _base = require('./base.js'); + +var _base2 = _interopRequireDefault(_base); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var userModel = function (_baseModel) { + (0, _inherits3.default)(userModel, _baseModel); + + function userModel() { + (0, _classCallCheck3.default)(this, userModel); + return (0, _possibleConstructorReturn3.default)(this, (userModel.__proto__ || (0, _getPrototypeOf2.default)(userModel)).apply(this, arguments)); + } + + (0, _createClass3.default)(userModel, [{ + key: 'getName', + value: function getName() { + return 'user'; + } + }, { + key: 'getSchema', + value: function getSchema() { + return { + username: { + type: String, + required: true + }, + password: { + type: String, + required: true + }, + email: { + type: String, + required: true + }, + passsalt: String, + role: String, + add_time: Number, + up_time: Number, + type: { type: String, enum: ['site', 'third'], default: "site" //site用户是网站注册用户, third是第三方登录过来的用户 + } }; + } + }, { + key: 'save', + value: function save(data) { + var user = new this.model(data); + return user.save(); + } + }, { + key: 'checkRepeat', + value: function checkRepeat(email) { + return this.model.count({ + email: email + }); + } + }, { + key: 'list', + value: function list() { + return this.model.find().select('_id username email role add_time up_time').exec(); //显示id name email role + } + }, { + key: 'findByUids', + value: function findByUids(uids) { + return this.model.find({ + _id: { $in: uids } + }).select('_id username email role add_time up_time').exec(); + } + }, { + key: 'listWithPaging', + value: function listWithPaging(page, limit) { + page = parseInt(page); + limit = parseInt(limit); + return this.model.find().sort({ _id: -1 }).skip((page - 1) * limit).limit(limit).select('_id username email role add_time up_time').exec(); + } + }, { + key: 'listCount', + value: function listCount() { + return this.model.count(); + } + }, { + key: 'findByEmail', + value: function findByEmail(email) { + return this.model.findOne({ email: email }); + } + }, { + key: 'findById', + value: function findById(id) { + return this.model.findOne({ + _id: id + }); + } + }, { + key: 'del', + value: function del(id) { + return this.model.deleteOne({ + _id: id + }); + } + }, { + key: 'update', + value: function update(id, data) { + return this.model.update({ + _id: id + }, data); + } + }, { + key: 'search', + value: function search(keyword) { + return this.model.find({ + $or: [{ email: new RegExp(keyword, 'i') }, { username: new RegExp(keyword, 'i') }] + }, { + passsalt: 0, + password: 0 + }).limit(10); + } + }]); + return userModel; +}(_base2.default); + +module.exports = userModel; \ No newline at end of file diff --git a/server_dist/utils/commons.js b/server_dist/utils/commons.js new file mode 100644 index 00000000..9c5137f6 --- /dev/null +++ b/server_dist/utils/commons.js @@ -0,0 +1,250 @@ +'use strict'; + +var _stringify = require('babel-runtime/core-js/json/stringify'); + +var _stringify2 = _interopRequireDefault(_stringify); + +var _typeof2 = require('babel-runtime/helpers/typeof'); + +var _typeof3 = _interopRequireDefault(_typeof2); + +var _fsExtra = require('fs-extra'); + +var _fsExtra2 = _interopRequireDefault(_fsExtra); + +var _path = require('path'); + +var _path2 = _interopRequireDefault(_path); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _sha = require('sha1'); + +var _sha2 = _interopRequireDefault(_sha); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +exports.resReturn = function (data, num, errmsg) { + num = num || 0; + + return { + errcode: num, + errmsg: errmsg || 'success', + data: data + }; +}; + +exports.log = function (msg, type) { + if (!msg) { + return; + } + + type = type || 'log'; + + var f = void 0; + + switch (type) { + case 'log': + f = console.log; // eslint-disable-line + break; + case 'warn': + f = console.warn; // eslint-disable-line + break; + case 'error': + f = console.error; // eslint-disable-line + break; + default: + f = console.log; // eslint-disable-line + break; + } + + f(type + ':', msg); + + var date = new Date(); + var year = date.getFullYear(); + var month = date.getMonth(); + + var logfile = _path2.default.join(_yapi2.default.WEBROOT_LOG, year + '-' + month + '.log'); + + if ((typeof msg === 'undefined' ? 'undefined' : (0, _typeof3.default)(msg)) === 'object') { + if (msg instanceof Error) msg = msg.message;else msg = (0, _stringify2.default)(msg); + } + + var data = new Date().toLocaleTimeString() + '\t|\t' + type + '\t|\t' + msg; + + _fsExtra2.default.writeFileSync(logfile, data, { + flag: 'w+' + }); +}; + +exports.fileExist = function (filePath) { + try { + return _fsExtra2.default.statSync(filePath).isFile(); + } catch (err) { + return false; + } +}; + +exports.time = function () { + return Date.parse(new Date()) / 1000; +}; + +exports.fieldSelect = function (data, field) { + if (!data || !field || !Array.isArray(field)) { + return null; + } + + var arr = {}; + + field.forEach(function (f) { + data[f] && (arr[f] = data[f]); + }); + + return arr; +}; + +exports.rand = function (min, max) { + return Math.floor(Math.random() * (max - min) + min); +}; + +exports.json_parse = function (json) { + try { + return JSON.parse(json); + } catch (e) { + return json; + } +}; + +exports.randStr = function () { + return Math.random().toString(36).substr(2); +}; + +exports.generatePassword = function (password, passsalt) { + return (0, _sha2.default)(password + (0, _sha2.default)(passsalt)); +}; + +exports.expireDate = function (day) { + var date = new Date(); + date.setTime(date.getTime() + day * 86400000); + return date; +}; + +exports.sendMail = function (options, cb) { + if (!_yapi2.default.mail) return false; + options.subject = options.subject ? options.subject + '-yapi平台' : 'ypai平台'; + + cb = cb || function (err) { + if (err) { + _yapi2.default.commons.log('send mail ' + options.to + ' error,' + err.message, 'error'); + } else { + _yapi2.default.commons.log('send mail ' + options.to + ' success'); + } + }; + + try { + _yapi2.default.mail.sendMail({ + from: _yapi2.default.WEBCONFIG.mail.from, + to: options.to, + subject: 'yapi平台', + html: options.contents + }, cb); + } catch (e) { + console.error(e.message); // eslint-disable-line + } +}; + +exports.validateSearchKeyword = function (keyword) { + if (/^\*|\?|\+|\$|\^|\\|\.$/.test(keyword)) { + return false; + } + + return true; +}; + +exports.filterRes = function (list, rules) { + return list.map(function (item) { + var filteredRes = {}; + + rules.forEach(function (rule) { + if (typeof rule == 'string') { + filteredRes[rule] = item[rule]; + } else if ((typeof rule === 'undefined' ? 'undefined' : (0, _typeof3.default)(rule)) == 'object') { + filteredRes[rule.alias] = item[rule.key]; + } + }); + + return filteredRes; + }); +}; + +exports.verifyPath = function (path) { + if (/^\/[a-zA-Z0-9\-\/_:\.]+$/.test(path)) { + if (path[path.length - 1] === '/') { + return false; + } else { + return true; + } + } else { + return false; + } +}; + +function trim(str) { + if (!str) { + return str; + } + + str = str + ''; + + return str.replace(/(^\s*)|(\s*$)/g, ''); +} + +function ltrim(str) { + if (!str) { + return str; + } + + str = str + ''; + + return str.replace(/(^\s*)/g, ''); +} + +function rtrim(str) { + if (!str) { + return str; + } + + str = str + ''; + + return str.replace(/(\s*$)/g, ''); +} + +exports.trim = trim; +exports.ltrim = ltrim; +exports.rtrim = rtrim; + +exports.handleParams = function (params, keys) { + if (!params || (typeof params === 'undefined' ? 'undefined' : (0, _typeof3.default)(params)) !== 'object' || !keys || (typeof keys === 'undefined' ? 'undefined' : (0, _typeof3.default)(keys)) !== 'object') { + return false; + } + + for (var key in keys) { + var filter = keys[key]; + if (params[key]) { + switch (filter) { + case 'string': + params[key] = trim(params[key] + ''); + break; + case 'number': + params[key] = parseInt(params[key], 10); + break; + default: + params[key] = trim(params + ''); + } + } + } + + return params; +}; \ No newline at end of file diff --git a/server_dist/utils/db.js b/server_dist/utils/db.js new file mode 100644 index 00000000..21aae9bd --- /dev/null +++ b/server_dist/utils/db.js @@ -0,0 +1,55 @@ +'use strict'; + +var _mongoose = require('mongoose'); + +var _mongoose2 = _interopRequireDefault(_mongoose); + +var _yapi = require('../yapi.js'); + +var _yapi2 = _interopRequireDefault(_yapi); + +var _mongooseAutoIncrement = require('mongoose-auto-increment'); + +var _mongooseAutoIncrement2 = _interopRequireDefault(_mongooseAutoIncrement); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function model(model, schema) { + if (schema instanceof _mongoose2.default.Schema === false) { + schema = new _mongoose2.default.Schema(schema); + } + + schema.set('autoIndex', false); + + return _yapi2.default.connect.model(model, schema, model); +} + +function connect() { + _mongoose2.default.Promise = global.Promise; + + var config = _yapi2.default.WEBCONFIG; + var options = {}; + + if (config.user) { + options.user = config.db.user; + options.pass = config.db.pass; + } + + var db = _mongoose2.default.connect('mongodb://' + config.db.servername + ':' + config.db.port + '/' + config.db.DATABASE, options); + + db.then(function () { + _yapi2.default.commons.log('mongodb load success...'); + }, function (err) { + _yapi2.default.commons.log(err, 'Mongo connect error'); + }); + + _mongooseAutoIncrement2.default.initialize(db); + return db; +} + +_yapi2.default.db = model; + +module.exports = { + model: model, + connect: connect +}; \ No newline at end of file diff --git a/server_dist/utils/initConfig.js b/server_dist/utils/initConfig.js new file mode 100644 index 00000000..c7ae500e --- /dev/null +++ b/server_dist/utils/initConfig.js @@ -0,0 +1,26 @@ +'use strict'; + +var _stringify = require('babel-runtime/core-js/json/stringify'); + +var _stringify2 = _interopRequireDefault(_stringify); + +var _path = require('path'); + +var _path2 = _interopRequireDefault(_path); + +var _fsExtra = require('fs-extra'); + +var _fsExtra2 = _interopRequireDefault(_fsExtra); + +var _config = require('../config.js'); + +var _config2 = _interopRequireDefault(_config); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var runtimePath = _config2.default.runtime_path; +_fsExtra2.default.ensureDirSync(runtimePath); +_fsExtra2.default.ensureDirSync(_path2.default.join(runtimePath, 'log')); +var configPath = _path2.default.join(runtimePath, 'config.json'); + +_fsExtra2.default.writeFileSync(configPath, (0, _stringify2.default)(_config2.default, null, '\t'), { encoding: 'utf8' }); \ No newline at end of file From f52a576e257aa1037aa9dd6b0cee227df5d43443 Mon Sep 17 00:00:00 2001 From: "wenbo.dong" Date: Thu, 10 Aug 2017 18:26:35 +0800 Subject: [PATCH 5/6] =?UTF-8?q?feat:=20=E6=96=B0=E5=BB=BA=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E9=A1=B5=E9=9D=A2=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/Application.js | 3 +- client/components/Header/Header.js | 2 +- .../Project/AddProject/AddProject.js | 274 ++++++++++++++++++ .../Project/AddProject/Addproject.scss | 6 + client/containers/index.js | 4 +- client/reducer/modules/project.js | 3 - client/styles/common.scss | 5 +- 7 files changed, 289 insertions(+), 8 deletions(-) create mode 100644 client/containers/Project/AddProject/AddProject.js create mode 100644 client/containers/Project/AddProject/Addproject.scss diff --git a/client/Application.js b/client/Application.js index f903c880..7d4a5a30 100644 --- a/client/Application.js +++ b/client/Application.js @@ -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'; @@ -59,6 +59,7 @@ export default class App extends Component { +