From 1d04ec2d2863f39a5990a77bfa11d7d0c9253524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9D=E9=91=AB=E5=A5=94?= Date: Tue, 18 Jul 2017 15:31:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E8=BF=94=E5=9B=9E=E5=AF=86=E7=A0=81=E5=92=8C?= =?UTF-8?q?=E7=9B=90=E5=8F=8A=E8=BF=87=E6=BB=A4=E9=A1=B9=E7=9B=AE=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exampleCode/api/project/search.json | 59 +++-- exampleCode/api/user/search.json | 9 +- server/controllers/project.js | 73 +++--- server/controllers/user.js | 203 ++++++++------- server/models/user.js | 3 + server/utils/commons.js | 82 +++--- server_dist/controllers/project.js | 370 +++++++++------------------- server_dist/controllers/user.js | 24 +- server_dist/models/user.js | 3 + server_dist/utils/commons.js | 14 ++ 10 files changed, 387 insertions(+), 453 deletions(-) diff --git a/exampleCode/api/project/search.json b/exampleCode/api/project/search.json index 2b7fea72..20590765 100644 --- a/exampleCode/api/project/search.json +++ b/exampleCode/api/project/search.json @@ -1,34 +1,31 @@ { - "errcode": 200, - "errmsg": "ok", - "data": { - "project": [ - { - "_id": 101, - "name": "project yapi", - "prd_host": "yapi.qunar.com", - "basepath": "/yapi/", - "uid": 101, - "group_id": 193, - "add_time": 1500013365, - "up_time": 1500013365, - "__v": 0, - "env": [], - "members": [ - "101" + "errcode": 200, + "errmsg": "ok", + "data": { + "project": [ + { + "_id": 101, + "name": "qav_project", + "prdHost": "xxx.qunar.com:8080", + "basepath": "/qav/", + "addTime": 1500013365, + "uid": 101, + "upTime": 1500013365, + "env": [], + "members": [ + "101" + ] + } + ], + "group": [ + { + "_id": 193, + "groupName": "qav", + "groupDesc": "project qav", + "uid": 0, + "addTime": 1500013066, + "upTime": 1500013066 + } ] - } - ], - "group": [ - { - "_id": 193, - "group_name": "yapi", - "group_desc": "group yapi", - "uid": 0, - "add_time": 1500013066, - "up_time": 1500013066, - "__v": 0 - } - ] - } + } } \ No newline at end of file diff --git a/exampleCode/api/user/search.json b/exampleCode/api/user/search.json index 8f257ab3..c6587783 100644 --- a/exampleCode/api/user/search.json +++ b/exampleCode/api/user/search.json @@ -3,14 +3,11 @@ "errmsg": "ok", "data": [ { - "_id": 101, + "uid": 101, "email": "admin@admin.com", - "password": "3d078af947521bb4a99c93f5e089fc2ac601fa09", - "passsalt": "qc8lnjpnbs9z1vodz4ynfjemi", "role": "admin", - "add_time": 1499936103, - "up_time": 1499936103, - "__v": 0 + "addTime": 1499936103, + "upTime": 1499936103 } ] } \ No newline at end of file diff --git a/server/controllers/project.js b/server/controllers/project.js index 3a20a7c6..cf4bb669 100644 --- a/server/controllers/project.js +++ b/server/controllers/project.js @@ -2,8 +2,8 @@ import projectModel from '../models/project.js' import yapi from '../yapi.js' import baseController from './base.js' import interfaceModel from '../models/interface.js' -import userModel from '../models/user.js' import groupModel from '../models/group' +import commons from '../utils/commons.js' class projectController extends baseController { @@ -76,7 +76,7 @@ class projectController extends baseController { } /** - * 添加项目成员 + * 添加项目 * @interface /project/add_member * @method POST * @category project @@ -108,7 +108,7 @@ class projectController extends baseController { } /** - * 删除项目成员 + * 添加项目 * @interface /project/del_member * @method POST * @category project @@ -139,41 +139,6 @@ class projectController extends baseController { ctx.body = yapi.commons.resReturn(null, 402, e.message) } } - - /** - * 获取项目成员列表 - * @interface /project/get_member_list.json - * @method GET - * @category project - * @foldnumber 10 - * @param {Number} id 项目id,不能为空 - * @return {Object} - * @example ./api/project/get_member_list.json - */ - - async getMemberList(ctx) { - let params = ctx.request.query; - if(!params.id) { - return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空'); - } - - try { - let project = await this.Model.get(params.id); - let userInst = yapi.getInst(userModel); - let result = []; - - for(let i of project.members) { - let user = await userInst.findById(i); - result.push(user); - } - - ctx.body = yapi.commons.resReturn(result); - } catch(e) { - ctx.body = yapi.commons.resReturn(null, 402, e.message); - } - } - - /** * 添加项目 * @interface /project/get @@ -337,10 +302,36 @@ class projectController extends baseController { return ctx.body = yapi.commons.resReturn(void 0, 400, 'Bad query.') } + let projectList = await this.Model.search(q); + let groupList = await this.groupModel.search(q); + let 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' } + ]; + let groupRules = [ + '_id', + 'uid', + { key: 'group_name', alias: 'groupName'}, + { key: 'group_desc', alias: 'groupDesc' }, + { key: 'add_time', alias: 'addTime' }, + { key: 'up_time', alias: 'upTime' } + ]; + + projectList = commons.filterRes(projectList, projectRules); + groupList = commons.filterRes(groupList, groupRules); + let queryList = { - project: await this.Model.search(q), - group: await this.groupModel.search(q) - } + project: projectList, + group: groupList + }; return ctx.body = yapi.commons.resReturn(queryList, 200, 'ok') } diff --git a/server/controllers/user.js b/server/controllers/user.js index ec46e45c..865b6660 100644 --- a/server/controllers/user.js +++ b/server/controllers/user.js @@ -3,11 +3,12 @@ import yapi from '../yapi.js' import baseController from './base.js' import mongoose from 'mongoose' import request from 'request' +import common from '../utils/commons.js' const jwt = require('jsonwebtoken'); -class userController extends baseController{ - constructor(ctx){ +class userController extends baseController { + constructor(ctx) { super(ctx) this.Model = yapi.getInst(userModel); } @@ -22,26 +23,26 @@ class userController extends baseController{ * @returns {Object} * @example ./api/user/login.json */ - async login(ctx){ //登录 + async login(ctx) { //登录 let userInst = yapi.getInst(userModel); //创建user实体 let email = ctx.request.body.email; let password = ctx.request.body.password; - - if(!email){ - return ctx.body = yapi.commons.resReturn(null,400,'email不能为空'); + + if (!email) { + return ctx.body = yapi.commons.resReturn(null, 400, 'email不能为空'); } - if(!password){ - return ctx.body = yapi.commons.resReturn(null,400,'密码不能为空'); + if (!password) { + return ctx.body = yapi.commons.resReturn(null, 400, '密码不能为空'); } let result = await userInst.findByEmail(email); - - - if(!result){ - return ctx.body = yapi.commons.resReturn(null,404,'该用户不存在'); - }else if(yapi.commons.generatePassword(password, result.passsalt) === result.password){ + + + if (!result) { + return ctx.body = yapi.commons.resReturn(null, 404, '该用户不存在'); + } else if (yapi.commons.generatePassword(password, result.passsalt) === result.password) { this.setLoginCookie(result._id, result.passsalt) - + return ctx.body = yapi.commons.resReturn({ username: result.username, uid: result._id, @@ -49,8 +50,8 @@ class userController extends baseController{ add_time: result.add_time, up_time: result.up_time - }, 0, 'logout success...'); - }else{ + }, 0, 'logout success...'); + } else { return ctx.body = yapi.commons.resReturn(null, 405, '密码错误'); } } @@ -65,7 +66,7 @@ class userController extends baseController{ * @example ./api/user/logout.json */ - async logout(ctx){ + async logout(ctx) { ctx.cookies.set('_yapi_token', null); ctx.cookies.set('_yapi_uid', null); ctx.body = yapi.commons.resReturn('ok'); @@ -76,22 +77,22 @@ class userController extends baseController{ * 第三方登录需要提供一个request方法和 token字段,暂时只支持qunar第三方 * @return {email: String, username: String} */ - thirdQunarLogin(){ + thirdQunarLogin() { return { request: (token) => { - return new Promise((resolve, reject) =>{ - request('http://qsso.corp.qunar.com/api/verifytoken.php?token=' + token ,function (error, response, body) { + return new Promise((resolve, reject) => { + request('http://qsso.corp.qunar.com/api/verifytoken.php?token=' + token, function (error, response, body) { if (!error && response.statusCode == 200) { let result = JSON.parse(body); - if(result && result.ret === true){ + if (result && result.ret === true) { let ret = { email: result.userId + '@qunar.com', username: result.data.userInfo.name } - resolve(ret) - }else{ + resolve(ret) + } else { reject(result) - } + } } reject(error) }) @@ -103,30 +104,30 @@ class userController extends baseController{ - async loginByToken(ctx){ + async loginByToken(ctx) { let config = this.thirdQunarLogin(); let token = ctx.request.body[config.tokenField] || ctx.request.query[config.tokenField]; - - try{ + + try { let ret = await config.request(token); let login = await this.handleThirdLogin(ret.email, ret.username); - if(login === true){ + if (login === true) { yapi.commons.log('login success'); ctx.redirect('/') } - }catch(e){ + } catch (e) { yapi.commons.log(e.message, 'error') ctx.redirect('/') } } - async handleThirdLogin(email, username){ - let user, data, passsalt; - var userInst = yapi.getInst(userModel); - try{ + async handleThirdLogin(email, username) { + let user, data, passsalt; + var userInst = yapi.getInst(userModel); + try { user = await userInst.findByEmail(email); - if(!user || !user._id){ + if (!user || !user._id) { passsalt = yapi.commons.randStr(); data = { username: username, @@ -139,10 +140,10 @@ class userController extends baseController{ } user = await userInst.save(data); } - + this.setLoginCookie(user._id, user.passsalt) return true; - }catch(e){ + } catch (e) { console.error(e.message) return false; } @@ -159,20 +160,20 @@ class userController extends baseController{ * @return {Object} * @example ./api/user/change_password */ - async changePassword(ctx){ + async changePassword(ctx) { let params = ctx.request.body; let userInst = yapi.getInst(userModel); - if(this.getRole() !== 'admin' && params.uid != this.getUid()){ + if (this.getRole() !== 'admin' && params.uid != this.getUid()) { console.log(this.getRole(), this.getUid()); return ctx.body = yapi.commons.resReturn(null, 402, '没有权限'); } - if(this.getRole() !== 'admin') { - if(!params.old_password) { + if (this.getRole() !== 'admin') { + if (!params.old_password) { return ctx.body = yapi.commons.resReturn(null, 400, '旧密码不能为空'); } let user = await userInst.findById(params.uid); - if(yapi.commons.generatePassword(params.old_password, user.passsalt) !== user.password) { + if (yapi.commons.generatePassword(params.old_password, user.passsalt) !== user.password) { return ctx.body = yapi.commons.resReturn(null, 402, '旧密码错误'); } } @@ -183,24 +184,24 @@ class userController extends baseController{ password: yapi.commons.generatePassword(params.password, passsalt), passsalt: passsalt }; - try{ + try { let result = await userInst.update(params.uid, data); ctx.body = yapi.commons.resReturn(result); - }catch(e){ + } catch (e) { ctx.body = yapi.commons.resReturn(null, 401, e.message); } } - async forgetPassword(ctx){ - - } - - async resetPassword(ctx){ + async forgetPassword(ctx) { } - setLoginCookie(uid, passsalt){ - let token = jwt.sign({uid: uid},passsalt,{expiresIn: '7 days'}); + async resetPassword(ctx) { + + } + + setLoginCookie(uid, passsalt) { + let token = jwt.sign({ uid: uid }, passsalt, { expiresIn: '7 days' }); this.ctx.cookies.set('_yapi_token', token, { expires: yapi.commons.expireDate(7), httpOnly: true @@ -224,19 +225,19 @@ class userController extends baseController{ * @returns {Object} * @example ./api/user/login.json */ - async reg(ctx){ //注册 - var userInst = yapi.getInst(userModel); + async reg(ctx) { //注册 + var userInst = yapi.getInst(userModel); let params = ctx.request.body; //获取请求的参数,检查是否存在用户名和密码 - if(!params.email){ - return ctx.body = yapi.commons.resReturn(null,400,'邮箱不能为空'); + if (!params.email) { + return ctx.body = yapi.commons.resReturn(null, 400, '邮箱不能为空'); } - if(!params.password){ - return ctx.body = yapi.commons.resReturn(null,400,'密码不能为空'); + if (!params.password) { + return ctx.body = yapi.commons.resReturn(null, 400, '密码不能为空'); } - + var checkRepeat = await userInst.checkRepeat(params.email);//然后检查是否已经存在该用户 - if(checkRepeat>0){ - return ctx.body = yapi.commons.resReturn(null,401,'该email已经注册'); + if (checkRepeat > 0) { + return ctx.body = yapi.commons.resReturn(null, 401, '该email已经注册'); } let passsalt = yapi.commons.randStr(); @@ -249,13 +250,13 @@ class userController extends baseController{ add_time: yapi.commons.time(), up_time: yapi.commons.time() } - if(!data.username){ + if (!data.username) { data.username = data.email.substr(0, data.email.indexOf('@')); } - try{ + try { let user = await userInst.save(data); this.setLoginCookie(user._id, user.passsalt) - + ctx.body = yapi.commons.resReturn({ uid: user._id, email: user.email, @@ -268,7 +269,7 @@ class userController extends baseController{ to: params.email, contents: `欢迎注册,您的账号 ${params.email} 已经注册成功` }) - }catch(e){ + } catch (e) { ctx.body = yapi.commons.resReturn(null, 401, e.message); } } @@ -284,16 +285,16 @@ class userController extends baseController{ * @example */ - async list(ctx){ - if(this.getRole() !== 'admin'){ + async list(ctx) { + if (this.getRole() !== 'admin') { return ctx.body = yapi.commons.resReturn(null, 402, '没有权限'); } var userInst = yapi.getInst(userModel); - try{ - let user = await userInst.list(); + try { + let user = await userInst.list(); return ctx.body = yapi.commons.resReturn(user); - }catch(e){ - return ctx.body = yapi.commons.resReturn(null,402,e.message); + } catch (e) { + return ctx.body = yapi.commons.resReturn(null, 402, e.message); } } @@ -308,14 +309,14 @@ class userController extends baseController{ * @example */ - async findById(ctx){ //根据id获取用户信息 - try{ + async findById(ctx) { //根据id获取用户信息 + try { var userInst = yapi.getInst(userModel); let id = ctx.request.body.id; let result = await userInst.findById(id); return ctx.body = yapi.commons.resReturn(result); - }catch(e){ - return ctx.body = yapi.commons.resReturn(null,402,e.message); + } catch (e) { + return ctx.body = yapi.commons.resReturn(null, 402, e.message); } } @@ -329,17 +330,17 @@ class userController extends baseController{ * @returns {Object} * @example */ - async del(ctx){ //根据id删除一个用户 - try{ - if(this.getRole() !== 'admin'){ + async del(ctx) { //根据id删除一个用户 + try { + if (this.getRole() !== 'admin') { return ctx.body = yapi.commons.resReturn(null, 402, 'Without permission.'); } var userInst = yapi.getInst(userModel); let id = ctx.request.body.id; let result = await userInst.del(id); ctx.body = yapi.commons.resReturn(result); - }catch(e){ - ctx.body = yapi.commons.resReturn(null,402,e.message); + } catch (e) { + ctx.body = yapi.commons.resReturn(null, 402, e.message); } } @@ -354,28 +355,28 @@ class userController extends baseController{ * @returns {Object} * @example */ - async update(ctx){ //更新用户信息 - try{ + async update(ctx) { //更新用户信息 + try { var userInst = yapi.getInst(userModel); let id = this.getUid(); - let data ={ + let data = { up_time: yapi.commons.time() }; ctx.request.body.username && (data.username = ctx.request.body.username) ctx.request.body.email && (data.email = ctx.request.body.email) - if(data.email){ + if (data.email) { var checkRepeat = await userInst.checkRepeat(data.email);//然后检查是否已经存在该用户 - if(checkRepeat>0){ - return ctx.body = yapi.commons.resReturn(null,401,'该email已经注册'); + if (checkRepeat > 0) { + return ctx.body = yapi.commons.resReturn(null, 401, '该email已经注册'); } } let result = await userInst.update(id, data); ctx.body = yapi.commons.resReturn(result); - }catch(e){ - ctx.body = yapi.commons.resReturn(null,402,e.message); + } catch (e) { + ctx.body = yapi.commons.resReturn(null, 402, e.message); } } @@ -393,16 +394,36 @@ class userController extends baseController{ const { q } = ctx.request.query; if (!q) { - return ctx.body = yapi.commons.resReturn(void 0, 400, 'No keyword.') + return ctx.body = yapi.commons.resReturn(void 0, 400, 'No keyword.'); } if (!yapi.commons.validateSearchKeyword(q)) { - return ctx.body = yapi.commons.resReturn(void 0, 400, 'Bad query.') + return ctx.body = yapi.commons.resReturn(void 0, 400, 'Bad query.'); } - + let queryList = await this.Model.search(q); - return ctx.body = yapi.commons.resReturn(queryList, 200, 'ok') + let rules = [ + { + key: '_id', + alias: 'uid' + }, + 'email', + 'role', + { + key: 'add_time', + alias: 'addTime' + }, + { + key: 'up_time', + alias: 'upTime' + } + ]; + + let filteredRes = common.filterRes(queryList, rules); + console.log(queryList) + + return ctx.body = yapi.commons.resReturn(filteredRes, 200, 'ok'); } } -module.exports = userController \ No newline at end of file +module.exports = userController; \ No newline at end of file diff --git a/server/models/user.js b/server/models/user.js index 69eca4e7..7ceb8acd 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -60,6 +60,9 @@ class userModel extends baseModel{ { email: new RegExp(keyword, 'i') }, { username: new RegExp(keyword, 'i')} ] + }, { + passsalt: 0, + password: 0 }).limit(10) } diff --git a/server/utils/commons.js b/server/utils/commons.js index 9db55a0b..1e2a95ac 100644 --- a/server/utils/commons.js +++ b/server/utils/commons.js @@ -4,7 +4,7 @@ import path from 'path' import yapi from '../yapi.js' import sha1 from 'sha1' -exports.resReturn = (data, num, errmsg)=> { +exports.resReturn = (data, num, errmsg) => { num = num || 0; return { errcode: num, @@ -14,42 +14,42 @@ exports.resReturn = (data, num, errmsg)=> { } const MSGTYPE = { - 'log' : 'Log', - 'warn' : 'warning', + 'log': 'Log', + 'warn': 'warning', 'error': 'Error' } -exports.log = (msg, type) => { - if(!msg) return; +exports.log = (msg, type) => { + if (!msg) return; type = type || 'log'; let f; - switch(type){ + switch (type) { case 'log': f = console.log; break; case 'warn': f = console.warn; break; - case 'error': f= console.error; break; - default : f = console.log; break; + case 'error': f = console.error; break; + default: f = console.log; break; } f(type + ':', msg); let date = new Date(); let year = date.getFullYear(); let month = date.getMonth(); - + let logfile = path.join(yapi.WEBROOT_LOG, year + '-' + month + '.log'); - if(typeof msg === 'object'){ - if(msg instanceof Error) msg = msg.message; + if (typeof msg === 'object') { + if (msg instanceof Error) msg = msg.message; else msg = JSON.stringify(msg); } - let data= (new Date).toLocaleTimeString() + "\t|\t" + type + "\t|\t" + msg; + let data = (new Date).toLocaleTimeString() + "\t|\t" + type + "\t|\t" + msg; fs.writeFileSync(logfile, data, { flag: 'w+' }); - + } -exports.fileExist = (filePath) =>{ +exports.fileExist = (filePath) => { try { return fs.statSync(filePath).isFile(); } catch (err) { @@ -58,31 +58,31 @@ exports.fileExist = (filePath) =>{ } exports.time = () => { - return Date.parse(new Date())/1000; + return Date.parse(new Date()) / 1000; } -exports.fieldSelect = (data, field)=>{ - if(!data || !field || !Array.isArray(field)) return null; +exports.fieldSelect = (data, field) => { + if (!data || !field || !Array.isArray(field)) return null; var arr = {}; - field.forEach( (f) => { + field.forEach((f) => { data[f] && (arr[f] = data[f]); - } ) + }) return arr; } -exports.rand =(min, max)=>{ +exports.rand = (min, max) => { return Math.floor(Math.random() * (max - min) + min); } -exports.json_parse = (json)=>{ - try{ +exports.json_parse = (json) => { + try { return JSON.parse(json); - }catch(e){ + } catch (e) { return json } } -exports.randStr = ()=> { +exports.randStr = () => { return Math.random().toString(36).substr(2) } @@ -96,20 +96,20 @@ exports.expireDate = (day) => { return date; } -exports.sendMail = (options,cb) => { - if(!yapi.mail) return false; - options.subject = options.subject? options.subject + '-yapi平台' : 'ypai平台'; - cb = cb || function(err, info){ - if(err){ - yapi.commons.log('send mail ' + options.to +' error,'+ err.message, 'error'); - }else{ - yapi.commons.log('send mail ' + options.to +' success'); +exports.sendMail = (options, cb) => { + if (!yapi.mail) return false; + options.subject = options.subject ? options.subject + '-yapi平台' : 'ypai平台'; + cb = cb || function (err, info) { + if (err) { + yapi.commons.log('send mail ' + options.to + ' error,' + err.message, 'error'); + } else { + yapi.commons.log('send mail ' + options.to + ' success'); } - + } yapi.mail.sendMail({ from: yapi.WEBCONFIG.mail.auth.user, - to : options.to, + to: options.to, subject: 'yapi平台', html: options.contents }, cb) @@ -120,4 +120,18 @@ exports.validateSearchKeyword = keyword => { return false; } return true; +} + +exports.filterRes = (list, rules) => { + return list.map(item => { + let filteredRes = {}; + rules.forEach(rule => { + if (typeof rule == 'string') { + filteredRes[rule] = item[rule]; + } else if (typeof rule == 'object') { + filteredRes[rule.alias] = item[rule.key]; + } + }); + return filteredRes; + }) } \ No newline at end of file diff --git a/server_dist/controllers/project.js b/server_dist/controllers/project.js index f1956733..66d18e4c 100644 --- a/server_dist/controllers/project.js +++ b/server_dist/controllers/project.js @@ -1,9 +1,5 @@ 'use strict'; -var _getIterator2 = require('babel-runtime/core-js/get-iterator'); - -var _getIterator3 = _interopRequireDefault(_getIterator2); - var _regenerator = require('babel-runtime/regenerator'); var _regenerator2 = _interopRequireDefault(_regenerator); @@ -48,14 +44,14 @@ var _interface = require('../models/interface.js'); var _interface2 = _interopRequireDefault(_interface); -var _user = require('../models/user.js'); - -var _user2 = _interopRequireDefault(_user); - var _group = require('../models/group'); var _group2 = _interopRequireDefault(_group); +var _commons = require('../utils/commons.js'); + +var _commons2 = _interopRequireDefault(_commons); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var projectController = function (_baseController) { @@ -201,7 +197,7 @@ var projectController = function (_baseController) { return add; }() /** - * 添加项目成员 + * 添加项目 * @interface /project/add_member * @method POST * @category project @@ -285,7 +281,7 @@ var projectController = function (_baseController) { return addMember; }() /** - * 删除项目成员 + * 添加项目 * @interface /project/del_member * @method POST * @category project @@ -368,24 +364,22 @@ var projectController = function (_baseController) { return delMember; }() - /** - * 获取项目成员列表 - * @interface /project/get_member_list.json - * @method GET - * @category project - * @foldnumber 10 - * @param {Number} id 项目id,不能为空 - * @return {Object} - * @example ./api/project/get_member_list.json - */ + * 添加项目 + * @interface /project/get + * @method GET + * @category project + * @foldnumber 10 + * @param {Number} id 项目id,不能为空 + * @returns {Object} + * @example ./api/project/get.json + */ }, { - key: 'getMemberList', + key: 'get', value: function () { var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(ctx) { - var params, project, userInst, result, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, i, user; - + var params, result; return _regenerator2.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { @@ -405,155 +399,30 @@ var projectController = function (_baseController) { return this.Model.get(params.id); case 6: - project = _context4.sent; - userInst = _yapi2.default.getInst(_user2.default); - result = []; - _iteratorNormalCompletion = true; - _didIteratorError = false; - _iteratorError = undefined; - _context4.prev = 12; - _iterator = (0, _getIterator3.default)(project.members); - - case 14: - if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { - _context4.next = 23; - break; - } - - i = _step.value; - _context4.next = 18; - return userInst.findById(i); - - case 18: - user = _context4.sent; - - result.push(user); - - case 20: - _iteratorNormalCompletion = true; - _context4.next = 14; - break; - - case 23: - _context4.next = 29; - break; - - case 25: - _context4.prev = 25; - _context4.t0 = _context4['catch'](12); - _didIteratorError = true; - _iteratorError = _context4.t0; - - case 29: - _context4.prev = 29; - _context4.prev = 30; - - if (!_iteratorNormalCompletion && _iterator.return) { - _iterator.return(); - } - - case 32: - _context4.prev = 32; - - if (!_didIteratorError) { - _context4.next = 35; - break; - } - - throw _iteratorError; - - case 35: - return _context4.finish(32); - - case 36: - return _context4.finish(29); - - case 37: + result = _context4.sent; ctx.body = _yapi2.default.commons.resReturn(result); - _context4.next = 43; + _context4.next = 13; break; - case 40: - _context4.prev = 40; - _context4.t1 = _context4['catch'](3); + case 10: + _context4.prev = 10; + _context4.t0 = _context4['catch'](3); - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context4.t1.message); + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context4.t0.message); - case 43: + case 13: case 'end': return _context4.stop(); } } - }, _callee4, this, [[3, 40], [12, 25, 29, 37], [30,, 32, 36]]); + }, _callee4, this, [[3, 10]]); })); - function getMemberList(_x4) { + function get(_x4) { return _ref4.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 _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(ctx) { - var params, result; - 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: - result = _context5.sent; - - ctx.body = _yapi2.default.commons.resReturn(result); - _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 get(_x5) { - return _ref5.apply(this, arguments); - } - return get; }() @@ -571,49 +440,49 @@ var projectController = function (_baseController) { }, { key: 'list', value: function () { - var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(ctx) { + var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(ctx) { var group_id, result; - return _regenerator2.default.wrap(function _callee6$(_context6) { + return _regenerator2.default.wrap(function _callee5$(_context5) { while (1) { - switch (_context6.prev = _context6.next) { + switch (_context5.prev = _context5.next) { case 0: group_id = ctx.request.query.group_id; if (group_id) { - _context6.next = 3; + _context5.next = 3; break; } - return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目分组id不能为空')); + return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目分组id不能为空')); case 3: - _context6.prev = 3; - _context6.next = 6; + _context5.prev = 3; + _context5.next = 6; return this.Model.list(group_id); case 6: - result = _context6.sent; + result = _context5.sent; ctx.body = _yapi2.default.commons.resReturn(result); - _context6.next = 13; + _context5.next = 13; break; case 10: - _context6.prev = 10; - _context6.t0 = _context6['catch'](3); + _context5.prev = 10; + _context5.t0 = _context5['catch'](3); ctx.body = _yapi2.default.commons.resReturn(null, 402, e.message); case 13: case 'end': - return _context6.stop(); + return _context5.stop(); } } - }, _callee6, this, [[3, 10]]); + }, _callee5, this, [[3, 10]]); })); - function list(_x6) { - return _ref6.apply(this, arguments); + function list(_x5) { + return _ref5.apply(this, arguments); } return list; @@ -633,78 +502,78 @@ var projectController = function (_baseController) { }, { key: 'del', value: function () { - var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(ctx) { + var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(ctx) { var id, interfaceInst, count, result; - return _regenerator2.default.wrap(function _callee7$(_context7) { + return _regenerator2.default.wrap(function _callee6$(_context6) { while (1) { - switch (_context7.prev = _context7.next) { + switch (_context6.prev = _context6.next) { case 0: - _context7.prev = 0; + _context6.prev = 0; id = ctx.request.body.id; if (id) { - _context7.next = 4; + _context6.next = 4; break; } - return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); + return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空')); case 4: interfaceInst = _yapi2.default.getInst(_interface2.default); - _context7.next = 7; + _context6.next = 7; return interfaceInst.countByProjectId(id); case 7: - count = _context7.sent; + count = _context6.sent; if (!(count > 0)) { - _context7.next = 10; + _context6.next = 10; break; } - return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '请先删除该项目下所有接口')); + return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '请先删除该项目下所有接口')); case 10: - _context7.next = 12; + _context6.next = 12; return this.jungeProjectAuth(id); case 12: - _context7.t0 = _context7.sent; + _context6.t0 = _context6.sent; - if (!(_context7.t0 !== true)) { - _context7.next = 15; + if (!(_context6.t0 !== true)) { + _context6.next = 15; break; } - return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); + return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); case 15: - _context7.next = 17; + _context6.next = 17; return this.Model.del(id); case 17: - result = _context7.sent; + result = _context6.sent; ctx.body = _yapi2.default.commons.resReturn(result); - _context7.next = 24; + _context6.next = 24; break; case 21: - _context7.prev = 21; - _context7.t1 = _context7['catch'](0); + _context6.prev = 21; + _context6.t1 = _context6['catch'](0); ctx.body = _yapi2.default.commons.resReturn(null, 402, e.message); case 24: case 'end': - return _context7.stop(); + return _context6.stop(); } } - }, _callee7, this, [[0, 21]]); + }, _callee6, this, [[0, 21]]); })); - function del(_x7) { - return _ref7.apply(this, arguments); + function del(_x6) { + return _ref6.apply(this, arguments); } return del; @@ -731,65 +600,65 @@ var projectController = function (_baseController) { }, { key: 'up', value: function () { - var _ref8 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(ctx) { + var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(ctx) { var id, params, checkRepeat, checkRepeatDomain, data, result; - return _regenerator2.default.wrap(function _callee8$(_context8) { + return _regenerator2.default.wrap(function _callee7$(_context7) { while (1) { - switch (_context8.prev = _context8.next) { + switch (_context7.prev = _context7.next) { case 0: - _context8.prev = 0; + _context7.prev = 0; id = ctx.request.body.id; params = ctx.request.body; - _context8.next = 5; + _context7.next = 5; return this.jungeMemberAuth(id, this.getUid()); case 5: - _context8.t0 = _context8.sent; + _context7.t0 = _context7.sent; - if (!(_context8.t0 !== true)) { - _context8.next = 8; + if (!(_context7.t0 !== true)) { + _context7.next = 8; break; } - return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); + return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '没有权限')); case 8: if (!params.name) { - _context8.next = 14; + _context7.next = 14; break; } - _context8.next = 11; + _context7.next = 11; return this.Model.checkNameRepeat(params.name); case 11: - checkRepeat = _context8.sent; + checkRepeat = _context7.sent; if (!(checkRepeat > 0)) { - _context8.next = 14; + _context7.next = 14; break; } - return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的项目名')); + return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的项目名')); case 14: if (!(params.basepath && params.prd_host)) { - _context8.next = 20; + _context7.next = 20; break; } - _context8.next = 17; + _context7.next = 17; return this.Model.checkDomainRepeat(params.prd_host, params.basepath); case 17: - checkRepeatDomain = _context8.sent; + checkRepeatDomain = _context7.sent; if (!(checkRepeatDomain > 0)) { - _context8.next = 20; + _context7.next = 20; break; } - return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在domain和basepath')); + return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在domain和basepath')); case 20: data = { @@ -806,32 +675,32 @@ var projectController = function (_baseController) { } if (params.env) data.env = params.env; - _context8.next = 27; + _context7.next = 27; return this.Model.up(id, data); case 27: - result = _context8.sent; + result = _context7.sent; ctx.body = _yapi2.default.commons.resReturn(result); - _context8.next = 34; + _context7.next = 34; break; case 31: - _context8.prev = 31; - _context8.t1 = _context8['catch'](0); + _context7.prev = 31; + _context7.t1 = _context7['catch'](0); - ctx.body = _yapi2.default.commons.resReturn(null, 402, _context8.t1.message); + ctx.body = _yapi2.default.commons.resReturn(null, 402, _context7.t1.message); case 34: case 'end': - return _context8.stop(); + return _context7.stop(); } } - }, _callee8, this, [[0, 31]]); + }, _callee7, this, [[0, 31]]); })); - function up(_x8) { - return _ref8.apply(this, arguments); + function up(_x7) { + return _ref7.apply(this, arguments); } return up; @@ -851,56 +720,63 @@ var projectController = function (_baseController) { }, { key: 'search', value: function () { - var _ref9 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9(ctx) { - var q, queryList; - return _regenerator2.default.wrap(function _callee9$(_context9) { + var _ref8 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(ctx) { + var q, projectList, groupList, projectRules, groupRules, queryList; + return _regenerator2.default.wrap(function _callee8$(_context8) { while (1) { - switch (_context9.prev = _context9.next) { + switch (_context8.prev = _context8.next) { case 0: q = ctx.request.query.q; if (q) { - _context9.next = 3; + _context8.next = 3; break; } - return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'No keyword.')); + return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'No keyword.')); case 3: if (_yapi2.default.commons.validateSearchKeyword(q)) { - _context9.next = 5; + _context8.next = 5; break; } - return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'Bad query.')); + return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'Bad query.')); case 5: - _context9.next = 7; + _context8.next = 7; return this.Model.search(q); case 7: - _context9.t0 = _context9.sent; - _context9.next = 10; + projectList = _context8.sent; + _context8.next = 10; return this.groupModel.search(q); case 10: - _context9.t1 = _context9.sent; - queryList = { - project: _context9.t0, - group: _context9.t1 - }; - return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(queryList, 200, 'ok')); + groupList = _context8.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' }]; - case 13: + + projectList = _commons2.default.filterRes(projectList, projectRules); + groupList = _commons2.default.filterRes(groupList, groupRules); + + queryList = { + project: projectList, + group: groupList + }; + return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(queryList, 200, 'ok')); + + case 17: case 'end': - return _context9.stop(); + return _context8.stop(); } } - }, _callee9, this); + }, _callee8, this); })); - function search(_x9) { - return _ref9.apply(this, arguments); + function search(_x8) { + return _ref8.apply(this, arguments); } return search; diff --git a/server_dist/controllers/user.js b/server_dist/controllers/user.js index b4e86a46..c2354199 100644 --- a/server_dist/controllers/user.js +++ b/server_dist/controllers/user.js @@ -52,6 +52,10 @@ var _request2 = require('request'); var _request3 = _interopRequireDefault(_request2); +var _commons = require('../utils/commons.js'); + +var _commons2 = _interopRequireDefault(_commons); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var jwt = require('jsonwebtoken'); @@ -877,7 +881,7 @@ var userController = function (_baseController) { key: 'search', value: function () { var _ref13 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee13(ctx) { - var q, queryList; + var q, queryList, rules, filteredRes; return _regenerator2.default.wrap(function _callee13$(_context13) { while (1) { switch (_context13.prev = _context13.next) { @@ -905,9 +909,23 @@ var userController = function (_baseController) { case 7: queryList = _context13.sent; - return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(queryList, 200, 'ok')); + rules = [{ + key: '_id', + alias: 'uid' + }, 'email', 'role', { + key: 'add_time', + alias: 'addTime' + }, { + key: 'up_time', + alias: 'upTime' + }]; + filteredRes = _commons2.default.filterRes(queryList, rules); - case 9: + console.log(queryList); + + return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(filteredRes, 200, 'ok')); + + case 12: case 'end': return _context13.stop(); } diff --git a/server_dist/models/user.js b/server_dist/models/user.js index fb91e9e5..1055c659 100644 --- a/server_dist/models/user.js +++ b/server_dist/models/user.js @@ -115,6 +115,9 @@ var userModel = function (_baseModel) { 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); } }]); diff --git a/server_dist/utils/commons.js b/server_dist/utils/commons.js index 39a6dabf..4c0320df 100644 --- a/server_dist/utils/commons.js +++ b/server_dist/utils/commons.js @@ -141,4 +141,18 @@ exports.validateSearchKeyword = function (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; + }); }; \ No newline at end of file