feat: 增加用户名/email/组/项目模糊搜索

This commit is contained in:
祝鑫奔 2017-07-14 16:41:14 +08:00
parent 9a0a238f4d
commit b6656a10d2
14 changed files with 258 additions and 3 deletions

View File

@ -2,12 +2,14 @@ import projectModel from '../models/project.js'
import yapi from '../yapi.js'
import baseController from './base.js'
import interfaceModel from '../models/interface.js'
import groupModel from '../models/group'
class projectController extends baseController {
constructor(ctx){
super(ctx)
this.Model = yapi.getInst(projectModel);
this.groupModel = yapi.getInst(groupModel);
}
/**
@ -79,7 +81,7 @@ class projectController extends baseController {
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @param {member_uid} uid 项目成员uid,不能为空
* @param {String} member_uid 项目成员uid,不能为空
* @returns {Object}
* @example ./api/project/add_member.json
*/
@ -277,6 +279,35 @@ class projectController extends baseController {
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
/**
* 模糊搜索项目名称或者组名称
* @interface /project/search
* @method GET
* @category project
* @foldnumber 10
* @param {String} q
* @return {Object}
* @example
*/
async search(ctx) {
const { q } = ctx.request.query;
if (!q) {
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.')
}
let queryList = {
project: await this.Model.search(q),
group: await this.groupModel.search(q)
}
return ctx.body = yapi.commons.resReturn(queryList, 200, 'ok')
}
}
module.exports = projectController;

View File

@ -9,6 +9,7 @@ const jwt = require('jsonwebtoken');
class userController extends baseController{
constructor(ctx){
super(ctx)
this.Model = yapi.getInst(userModel);
}
/**
* 用户登录接口
@ -358,6 +359,31 @@ class userController extends baseController{
ctx.body = yapi.commons.resReturn(null,402,e.message);
}
}
/**
* 模糊搜索用户名或者email
* @interface /user/search
* @method GET
* @category user
* @foldnumber 10
* @param {String} q
* @return {Object}
* @example
*/
async search(ctx) {
const { q } = ctx.request.query;
if (!q) {
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.')
}
let queryList = await this.Model.search(q);
return ctx.body = yapi.commons.resReturn(queryList, 200, 'ok')
}
}
module.exports = userController

View File

@ -44,6 +44,12 @@ class groupModel extends baseModel{
})
}
search(keyword) {
return this.model.find({
name: new RegExp(keyword, 'ig')
})
.limit(10)
}
}

View File

@ -107,6 +107,13 @@ class projectModel extends baseModel{
})
}
search(keyword) {
return this.model.find({
name: new RegExp(keyword, 'ig')
})
.limit(10)
}
}
module.exports = projectModel;

View File

@ -59,6 +59,14 @@ class userModel extends baseModel{
up_time: yapi.commons.time()
})
}
search(keyword) {
return this.model.find({
$or: [
{ email: new RegExp(keyword, 'i') },
{ username: new RegExp(keyword, 'i')}
]
}).limit(10)
}
}

View File

@ -44,6 +44,7 @@ createAction('user', 'del', 'post', 'del')
createAction('user', 'status', 'get', 'getLoginStatus')
createAction('user', 'logout', 'get', 'logout')
createAction('user', 'login_by_token', 'post', 'loginByToken')
createAction('user', 'search', 'get', 'search')
//project
@ -54,6 +55,7 @@ createAction('project', 'up', 'post', 'up')
createAction('project', 'del', 'post', 'del')
createAction('project', 'add_member', 'post', 'addMember')
createAction('project', 'del_member', 'post', 'delMember')
createAction('project', 'search', 'get', 'search')
//interface
createAction('interface', 'add', 'post', 'add')

View File

@ -113,4 +113,11 @@ exports.sendMail = (options,cb) => {
subject: 'yapi平台',
html: options.contents
}, cb)
}
exports.validateSearchKeyword = keyword => {
if (/^\*|\?|\+|\$|\^|\\|\.$/.test(keyword)) {
return false;
}
return true;
}

View File

@ -44,6 +44,10 @@ var _interface = require('../models/interface.js');
var _interface2 = _interopRequireDefault(_interface);
var _group = require('../models/group');
var _group2 = _interopRequireDefault(_group);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var projectController = function (_baseController) {
@ -55,6 +59,7 @@ var projectController = function (_baseController) {
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;
}
@ -194,7 +199,7 @@ var projectController = function (_baseController) {
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @param {member_uid} uid 项目成员uid,不能为空
* @param {String} member_uid 项目成员uid,不能为空
* @returns {Object}
* @example ./api/project/add_member.json
*/
@ -696,6 +701,75 @@ var projectController = function (_baseController) {
return up;
}()
/**
* 模糊搜索项目名称或者组名称
* @interface /project/search
* @method GET
* @category project
* @foldnumber 10
* @param {String} q
* @return {Object}
* @example
*/
}, {
key: 'search',
value: function () {
var _ref8 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(ctx) {
var q, queryList;
return _regenerator2.default.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
q = ctx.request.query.q;
if (q) {
_context8.next = 3;
break;
}
return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'No keyword.'));
case 3:
if (_yapi2.default.commons.validateSearchKeyword(q)) {
_context8.next = 5;
break;
}
return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(void 0, 400, 'Bad query.'));
case 5:
_context8.next = 7;
return this.Model.search(q);
case 7:
_context8.t0 = _context8.sent;
_context8.next = 10;
return this.groupModel.search(q);
case 10:
_context8.t1 = _context8.sent;
queryList = {
project: _context8.t0,
group: _context8.t1
};
return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(queryList, 200, 'ok'));
case 13:
case 'end':
return _context8.stop();
}
}
}, _callee8, this);
}));
function search(_x8) {
return _ref8.apply(this, arguments);
}
return search;
}()
}]);
return projectController;
}(_base2.default);

View File

@ -61,7 +61,11 @@ var userController = function (_baseController) {
function userController(ctx) {
(0, _classCallCheck3.default)(this, userController);
return (0, _possibleConstructorReturn3.default)(this, (userController.__proto__ || (0, _getPrototypeOf2.default)(userController)).call(this, ctx));
var _this = (0, _possibleConstructorReturn3.default)(this, (userController.__proto__ || (0, _getPrototypeOf2.default)(userController)).call(this, ctx));
_this.Model = _yapi2.default.getInst(_user2.default);
return _this;
}
/**
* 用户登录接口
@ -825,6 +829,66 @@ var userController = function (_baseController) {
return update;
}()
/**
* 模糊搜索用户名或者email
* @interface /user/search
* @method GET
* @category user
* @foldnumber 10
* @param {String} q
* @return {Object}
* @example
*/
}, {
key: 'search',
value: function () {
var _ref13 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee13(ctx) {
var q, queryList;
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;
return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(queryList, 200, 'ok'));
case 9:
case 'end':
return _context13.stop();
}
}
}, _callee13, this);
}));
function search(_x14) {
return _ref13.apply(this, arguments);
}
return search;
}()
}]);
return userController;
}(_base2.default);

View File

@ -94,6 +94,13 @@ var groupModel = function (_baseModel) {
up_time: _yapi2.default.commons.time()
});
}
}, {
key: 'search',
value: function search(keyword) {
return this.model.find({
name: new RegExp(keyword, 'ig')
}).limit(10);
}
}]);
return groupModel;
}(_base2.default);

View File

@ -154,6 +154,13 @@ var projectModel = function (_baseModel) {
members: [uid]
});
}
}, {
key: 'search',
value: function search(keyword) {
return this.model.find({
name: new RegExp(keyword, 'ig')
}).limit(10);
}
}]);
return projectModel;
}(_base2.default);

View File

@ -115,6 +115,13 @@ var userModel = function (_baseModel) {
up_time: _yapi2.default.commons.time()
});
}
}, {
key: 'search',
value: function search(keyword) {
return this.model.find({
$or: [{ email: new RegExp(keyword, 'i') }, { username: new RegExp(keyword, 'i') }]
}).limit(10);
}
}]);
return userModel;
}(_base2.default);

View File

@ -71,6 +71,7 @@ createAction('user', 'del', 'post', 'del');
createAction('user', 'status', 'get', 'getLoginStatus');
createAction('user', 'logout', 'get', 'logout');
createAction('user', 'login_by_token', 'post', 'loginByToken');
createAction('user', 'search', 'get', 'search');
//project
createAction('project', 'add', 'post', 'add');
@ -80,6 +81,7 @@ createAction('project', 'up', 'post', 'up');
createAction('project', 'del', 'post', 'del');
createAction('project', 'add_member', 'post', 'addMember');
createAction('project', 'del_member', 'post', 'delMember');
createAction('project', 'search', 'get', 'search');
//interface
createAction('interface', 'add', 'post', 'add');

View File

@ -134,4 +134,11 @@ exports.sendMail = function (options, cb) {
subject: 'yapi平台',
html: options.contents
}, cb);
};
exports.validateSearchKeyword = function (keyword) {
if (/^\*|\?|\+|\$|\^|\\|\.$/.test(keyword)) {
return false;
}
return true;
};