yapi/server_dist/controllers/user.js

1264 lines
50 KiB
JavaScript
Raw Normal View History

2017-07-05 17:59:53 +08:00
'use strict';
2017-07-12 17:01:27 +08:00
var _promise = require('babel-runtime/core-js/promise');
var _promise2 = _interopRequireDefault(_promise);
2017-07-05 17:59:53 +08:00
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
2017-07-06 20:55:02 +08:00
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);
2017-07-05 17:59:53 +08:00
var _user = require('../models/user.js');
var _user2 = _interopRequireDefault(_user);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
2017-07-06 20:55:02 +08:00
var _base = require('./base.js');
var _base2 = _interopRequireDefault(_base);
2017-07-12 17:01:27 +08:00
var _request2 = require('request');
var _request3 = _interopRequireDefault(_request2);
2017-07-18 16:46:19 +08:00
var _commons = require('../utils/commons.js');
var _commons2 = _interopRequireDefault(_commons);
2017-07-28 17:29:17 +08:00
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);
2017-08-10 15:22:42 +08:00
var _avatar = require('../models/avatar.js');
var _avatar2 = _interopRequireDefault(_avatar);
2017-07-05 17:59:53 +08:00
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2017-07-10 20:51:04 +08:00
var jwt = require('jsonwebtoken');
2017-07-07 12:04:14 +08:00
2017-07-06 20:55:02 +08:00
var userController = function (_baseController) {
(0, _inherits3.default)(userController, _baseController);
2017-07-05 17:59:53 +08:00
2017-07-06 20:55:02 +08:00
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;
2017-07-06 20:55:02 +08:00
}
2017-07-10 11:11:35 +08:00
/**
2017-07-11 18:22:20 +08:00
* 用户登录接口
2017-07-10 11:11:35 +08:00
* @interface /user/login
* @method POST
* @category user
* @foldnumber 10
2017-07-11 18:22:20 +08:00
* @param {String} email email名称不能为空
2017-07-10 11:11:35 +08:00
* @param {String} password 密码不能为空
* @returns {Object}
* @example ./api/user/login.json
*/
2017-07-05 17:59:53 +08:00
2017-07-06 20:55:02 +08:00
(0, _createClass3.default)(userController, [{
key: 'login',
value: function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
2017-07-12 17:01:27 +08:00
var userInst, email, password, result;
2017-07-06 20:55:02 +08:00
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
//登录
userInst = _yapi2.default.getInst(_user2.default); //创建user实体
2017-07-11 12:12:43 +08:00
email = ctx.request.body.email;
2017-07-10 11:11:35 +08:00
password = ctx.request.body.password;
2017-07-06 20:55:02 +08:00
2017-07-11 12:12:43 +08:00
if (email) {
_context.next = 5;
2017-07-06 20:55:02 +08:00
break;
}
2017-07-11 16:50:17 +08:00
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'email不能为空'));
2017-07-06 20:55:02 +08:00
2017-07-11 12:12:43 +08:00
case 5:
2017-07-10 20:51:04 +08:00
if (password) {
2017-07-11 12:12:43 +08:00
_context.next = 7;
2017-07-10 20:51:04 +08:00
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空'));
2017-07-06 20:55:02 +08:00
2017-07-11 12:12:43 +08:00
case 7:
_context.next = 9;
return userInst.findByEmail(email);
2017-07-10 20:51:04 +08:00
2017-07-11 12:12:43 +08:00
case 9:
result = _context.sent;
2017-07-06 20:55:02 +08:00
2017-07-11 12:12:43 +08:00
if (result) {
_context.next = 14;
2017-07-06 20:55:02 +08:00
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 404, '该用户不存在'));
2017-07-11 12:12:43 +08:00
case 14:
if (!(_yapi2.default.commons.generatePassword(password, result.passsalt) === result.password)) {
2017-07-12 17:01:27 +08:00
_context.next = 19;
2017-07-06 20:55:02 +08:00
break;
}
2017-07-12 17:01:27 +08:00
this.setLoginCookie(result._id, result.passsalt);
2017-07-11 18:22:20 +08:00
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn({
2017-07-13 16:13:47 +08:00
username: result.username,
2017-08-01 15:03:34 +08:00
role: result.role,
2017-07-11 18:22:20 +08:00
uid: result._id,
email: result.email,
add_time: result.add_time,
2017-07-27 18:00:43 +08:00
up_time: result.up_time,
server_ip: _yapi2.default.WEBCONFIG.server_ip
2017-07-11 18:22:20 +08:00
}, 0, 'logout success...'));
2017-07-06 20:55:02 +08:00
2017-07-12 17:01:27 +08:00
case 19:
2017-07-11 12:12:43 +08:00
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '密码错误'));
2017-07-06 20:55:02 +08:00
2017-07-12 17:01:27 +08:00
case 20:
2017-07-06 20:55:02 +08:00
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function login(_x) {
return _ref.apply(this, arguments);
}
return login;
}()
2017-07-11 18:22:20 +08:00
/**
* 退出登录接口
* @interface /user/logout
* @method GET
* @category user
* @foldnumber 10
* @returns {Object}
* @example ./api/user/logout.json
*/
2017-07-06 20:55:02 +08:00
}, {
2017-07-11 16:50:17 +08:00
key: 'logout',
2017-07-06 20:55:02 +08:00
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) {
2017-07-11 16:50:17 +08:00
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;
}()
2017-07-11 18:22:20 +08:00
2017-07-12 17:01:27 +08:00
/**
* 第三方登录需要提供一个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;
2017-07-27 19:49:26 +08:00
2017-07-12 17:01:27 +08:00
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)) {
2017-07-24 11:24:08 +08:00
_context4.next = 13;
2017-07-12 17:01:27 +08:00
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(),
2017-08-10 15:22:42 +08:00
up_time: _yapi2.default.commons.time(),
type: 'third'
2017-07-12 17:01:27 +08:00
};
_context4.next = 11;
return userInst.save(data);
case 11:
user = _context4.sent;
2017-07-24 11:24:08 +08:00
_yapi2.default.commons.sendMail({
2017-07-25 15:42:05 +08:00
to: email,
2017-08-10 15:22:42 +08:00
contents: '<h3>\u4EB2\u7231\u7684\u7528\u6237\uFF1A</h3><p>\u60A8\u597D\uFF0C\u611F\u8C22\u4F7F\u7528YApi\u5E73\u53F0.</p>'
2017-07-24 11:24:08 +08:00
});
case 13:
2017-07-12 17:01:27 +08:00
this.setLoginCookie(user._id, user.passsalt);
return _context4.abrupt('return', true);
2017-07-24 11:24:08 +08:00
case 17:
_context4.prev = 17;
2017-07-12 17:01:27 +08:00
_context4.t0 = _context4['catch'](2);
2017-07-27 19:49:26 +08:00
console.error('third_login:', _context4.t0.message); // eslint-disable-line
2017-07-12 17:01:27 +08:00
return _context4.abrupt('return', false);
2017-07-24 11:24:08 +08:00
case 21:
2017-07-12 17:01:27 +08:00
case 'end':
return _context4.stop();
}
}
2017-07-24 11:24:08 +08:00
}, _callee4, this, [[2, 17]]);
2017-07-12 17:01:27 +08:00
}));
function handleThirdLogin(_x4, _x5) {
return _ref4.apply(this, arguments);
}
return handleThirdLogin;
}()
2017-07-13 16:13:47 +08:00
/**
* 修改用户密码
* @interface /user/change_password
* @method POST
* @category user
* @param {Number} uid 用户ID
* @param {Number} [old_password] 旧密码, 非admin用户必须传
* @param {Number} password 新密码
* @return {Object}
2017-07-18 16:46:19 +08:00
* @example ./api/user/change_password.json
2017-07-13 16:13:47 +08:00
*/
2017-07-12 17:01:27 +08:00
}, {
key: 'changePassword',
value: function () {
var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(ctx) {
var params, userInst, user, passsalt, data, result;
2017-07-12 17:01:27 +08:00
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);
2017-07-18 19:47:38 +08:00
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:
2017-07-12 17:01:27 +08:00
if (!(this.getRole() !== 'admin' && params.uid != this.getUid())) {
2017-07-18 19:47:38 +08:00
_context5.next = 8;
2017-07-12 17:01:27 +08:00
break;
}
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, '没有权限'));
2017-07-18 19:47:38 +08:00
case 8:
if (!(this.getRole() !== 'admin')) {
2017-07-18 19:47:38 +08:00
_context5.next = 16;
break;
}
if (params.old_password) {
2017-07-18 19:47:38 +08:00
_context5.next = 11;
break;
}
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '旧密码不能为空'));
2017-07-18 19:47:38 +08:00
case 11:
_context5.next = 13;
return userInst.findById(params.uid);
2017-07-18 19:47:38 +08:00
case 13:
user = _context5.sent;
if (!(_yapi2.default.commons.generatePassword(params.old_password, user.passsalt) !== user.password)) {
2017-07-18 19:47:38 +08:00
_context5.next = 16;
break;
}
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, '旧密码错误'));
2017-07-18 19:47:38 +08:00
case 16:
2017-07-12 17:01:27 +08:00
passsalt = _yapi2.default.commons.randStr();
data = {
up_time: _yapi2.default.commons.time(),
password: _yapi2.default.commons.generatePassword(params.password, passsalt),
2017-07-12 17:01:27 +08:00
passsalt: passsalt
};
2017-07-18 19:47:38 +08:00
_context5.prev = 18;
_context5.next = 21;
return userInst.update(params.uid, data);
2017-07-12 17:01:27 +08:00
2017-07-18 19:47:38 +08:00
case 21:
2017-07-12 17:01:27 +08:00
result = _context5.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
2017-07-18 19:47:38 +08:00
_context5.next = 28;
2017-07-12 17:01:27 +08:00
break;
2017-07-18 19:47:38 +08:00
case 25:
_context5.prev = 25;
_context5.t0 = _context5['catch'](18);
2017-07-12 17:01:27 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 401, _context5.t0.message);
2017-07-18 19:47:38 +08:00
case 28:
2017-07-12 17:01:27 +08:00
case 'end':
return _context5.stop();
}
}
2017-07-18 19:47:38 +08:00
}, _callee5, this, [[18, 25]]);
2017-07-12 17:01:27 +08:00
}));
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' });
2017-07-27 19:49:26 +08:00
2017-07-12 17:01:27 +08:00
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
});
}
2017-07-11 18:22:20 +08:00
/**
* 用户注册接口
* @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
*/
2017-07-11 16:50:17 +08:00
}, {
key: 'reg',
value: function () {
2017-08-10 15:22:42 +08:00
var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(ctx) {
2017-07-11 16:50:17 +08:00
var userInst, params, checkRepeat, passsalt, data, user;
2017-08-10 15:22:42 +08:00
return _regenerator2.default.wrap(function _callee6$(_context6) {
2017-07-11 16:50:17 +08:00
while (1) {
2017-08-10 15:22:42 +08:00
switch (_context6.prev = _context6.next) {
2017-07-06 20:55:02 +08:00
case 0:
//注册
userInst = _yapi2.default.getInst(_user2.default);
params = ctx.request.body; //获取请求的参数,检查是否存在用户名和密码
2017-07-26 14:22:59 +08:00
params = _yapi2.default.commons.handleParams(params, {
username: 'string',
password: 'string',
email: 'string'
});
2017-07-10 20:51:04 +08:00
if (params.email) {
2017-08-10 15:22:42 +08:00
_context6.next = 5;
2017-07-10 20:51:04 +08:00
break;
}
2017-08-10 15:22:42 +08:00
return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '邮箱不能为空'));
2017-07-10 20:51:04 +08:00
2017-07-26 14:22:59 +08:00
case 5:
2017-07-11 12:12:43 +08:00
if (params.password) {
2017-08-10 15:22:42 +08:00
_context6.next = 7;
2017-07-10 20:51:04 +08:00
break;
}
2017-08-10 15:22:42 +08:00
return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空'));
2017-07-10 20:51:04 +08:00
2017-07-26 14:22:59 +08:00
case 7:
2017-08-10 15:22:42 +08:00
_context6.next = 9;
2017-07-10 20:51:04 +08:00
return userInst.checkRepeat(params.email);
2017-07-26 14:22:59 +08:00
case 9:
2017-08-10 15:22:42 +08:00
checkRepeat = _context6.sent;
2017-07-10 20:51:04 +08:00
if (!(checkRepeat > 0)) {
2017-08-10 15:22:42 +08:00
_context6.next = 12;
2017-07-10 20:51:04 +08:00
break;
}
2017-08-10 15:22:42 +08:00
return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '该email已经注册'));
2017-07-10 20:51:04 +08:00
2017-07-26 14:22:59 +08:00
case 12:
2017-07-11 12:12:43 +08:00
passsalt = _yapi2.default.commons.randStr();
2017-07-06 20:55:02 +08:00
data = {
username: params.username,
2017-07-11 12:12:43 +08:00
password: _yapi2.default.commons.generatePassword(params.password, passsalt), //加密
2017-07-06 20:55:02 +08:00
email: params.email,
2017-07-11 12:12:43 +08:00
passsalt: passsalt,
2017-07-11 16:50:17 +08:00
role: 'member',
2017-07-06 20:55:02 +08:00
add_time: _yapi2.default.commons.time(),
2017-08-10 15:22:42 +08:00
up_time: _yapi2.default.commons.time(),
type: "site"
2017-07-06 20:55:02 +08:00
};
2017-07-12 20:47:20 +08:00
2017-07-27 19:49:26 +08:00
2017-07-12 20:47:20 +08:00
if (!data.username) {
data.username = data.email.substr(0, data.email.indexOf('@'));
}
2017-07-27 19:49:26 +08:00
2017-08-10 15:22:42 +08:00
_context6.prev = 15;
_context6.next = 18;
2017-07-06 20:55:02 +08:00
return userInst.save(data);
2017-07-26 14:22:59 +08:00
case 18:
2017-08-10 15:22:42 +08:00
user = _context6.sent;
2017-07-06 20:55:02 +08:00
2017-07-11 18:22:20 +08:00
2017-07-27 19:49:26 +08:00
this.setLoginCookie(user._id, user.passsalt);
2017-07-11 18:22:20 +08:00
ctx.body = _yapi2.default.commons.resReturn({
uid: user._id,
email: user.email,
2017-07-12 20:47:20 +08:00
username: user.username,
2017-07-11 18:22:20 +08:00
add_time: user.add_time,
up_time: user.up_time,
role: 'member'
});
2017-07-11 16:50:17 +08:00
_yapi2.default.commons.sendMail({
2017-07-25 15:42:05 +08:00
to: user.email,
2017-07-24 11:24:08 +08:00
contents: '<h3>\u4EB2\u7231\u7684\u7528\u6237\uFF1A</h3><p>\u60A8\u597D\uFF0C\u611F\u8C22\u4F7F\u7528YApi,\u60A8\u7684\u8D26\u53F7 ' + params.email + ' \u5DF2\u7ECF\u6CE8\u518C\u6210\u529F</p>'
2017-07-11 16:50:17 +08:00
});
2017-08-10 15:22:42 +08:00
_context6.next = 27;
2017-07-05 17:59:53 +08:00
break;
2017-07-26 14:22:59 +08:00
case 24:
2017-08-10 15:22:42 +08:00
_context6.prev = 24;
_context6.t0 = _context6['catch'](15);
2017-07-05 17:59:53 +08:00
2017-08-10 15:22:42 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 401, _context6.t0.message);
2017-07-05 17:59:53 +08:00
2017-07-26 14:22:59 +08:00
case 27:
2017-07-06 20:55:02 +08:00
case 'end':
2017-08-10 15:22:42 +08:00
return _context6.stop();
2017-07-06 20:55:02 +08:00
}
}
2017-08-10 15:22:42 +08:00
}, _callee6, this, [[15, 24]]);
2017-07-06 20:55:02 +08:00
}));
2017-07-27 19:49:26 +08:00
function reg(_x7) {
2017-08-10 15:22:42 +08:00
return _ref6.apply(this, arguments);
2017-07-06 20:55:02 +08:00
}
return reg;
}()
2017-07-11 18:22:20 +08:00
/**
* 获取用户列表
* @interface /user/list
* @method GET
* @category user
* @foldnumber 10
2017-07-18 16:37:28 +08:00
* @param {Number} [page] 分页页码
* @param {Number} [limit] 分页大小,默认为10条
2017-07-11 18:22:20 +08:00
* @returns {Object}
* @example
*/
2017-07-06 20:55:02 +08:00
}, {
key: 'list',
value: function () {
2017-08-10 15:22:42 +08:00
var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(ctx) {
2017-07-18 16:37:28 +08:00
var page, limit, userInst, user, count;
2017-08-10 15:22:42 +08:00
return _regenerator2.default.wrap(function _callee7$(_context7) {
2017-07-06 20:55:02 +08:00
while (1) {
2017-08-10 15:22:42 +08:00
switch (_context7.prev = _context7.next) {
2017-07-06 20:55:02 +08:00
case 0:
2017-07-18 16:37:28 +08:00
page = ctx.request.query.page || 1, limit = ctx.request.query.limit || 10;
2017-07-06 20:55:02 +08:00
userInst = _yapi2.default.getInst(_user2.default);
2017-08-10 15:22:42 +08:00
_context7.prev = 2;
_context7.next = 5;
2017-07-18 16:37:28 +08:00
return userInst.listWithPaging(page, limit);
2017-07-06 20:55:02 +08:00
2017-07-18 17:18:16 +08:00
case 5:
2017-08-10 15:22:42 +08:00
user = _context7.sent;
_context7.next = 8;
2017-07-18 16:37:28 +08:00
return userInst.listCount();
2017-07-18 15:13:47 +08:00
2017-07-18 17:18:16 +08:00
case 8:
2017-08-10 15:22:42 +08:00
count = _context7.sent;
return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn({
total: Math.ceil(count / limit),
2017-07-18 16:37:28 +08:00
list: user
2017-07-18 15:13:47 +08:00
}));
2017-07-18 17:18:16 +08:00
case 12:
2017-08-10 15:22:42 +08:00
_context7.prev = 12;
_context7.t0 = _context7['catch'](2);
return _context7.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, _context7.t0.message));
2017-07-18 15:13:47 +08:00
2017-07-18 17:18:16 +08:00
case 15:
2017-07-06 20:55:02 +08:00
case 'end':
2017-08-10 15:22:42 +08:00
return _context7.stop();
2017-07-06 20:55:02 +08:00
}
}
2017-08-10 15:22:42 +08:00
}, _callee7, this, [[2, 12]]);
2017-07-06 20:55:02 +08:00
}));
2017-07-27 19:49:26 +08:00
function list(_x8) {
2017-08-10 15:22:42 +08:00
return _ref7.apply(this, arguments);
2017-07-06 20:55:02 +08:00
}
return list;
}()
2017-07-11 18:22:20 +08:00
/**
2017-07-12 17:01:27 +08:00
* 获取用户个人信息
* @interface /user/find
2017-07-11 18:22:20 +08:00
* @method GET
* @param id 用户uid
* @category user
* @foldnumber 10
* @returns {Object}
* @example
*/
2017-07-06 20:55:02 +08:00
}, {
2017-07-07 12:04:14 +08:00
key: 'findById',
2017-07-06 20:55:02 +08:00
value: function () {
2017-08-10 15:22:42 +08:00
var _ref8 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(ctx) {
var userInst, id, result;
2017-08-10 15:22:42 +08:00
return _regenerator2.default.wrap(function _callee8$(_context8) {
2017-07-06 20:55:02 +08:00
while (1) {
2017-08-10 15:22:42 +08:00
switch (_context8.prev = _context8.next) {
2017-07-06 20:55:02 +08:00
case 0:
2017-08-10 15:22:42 +08:00
_context8.prev = 0;
2017-07-06 20:55:02 +08:00
userInst = _yapi2.default.getInst(_user2.default);
2017-07-18 16:46:19 +08:00
id = ctx.request.query.id;
2017-07-18 19:47:38 +08:00
if (id) {
2017-08-10 15:22:42 +08:00
_context8.next = 5;
2017-07-18 19:47:38 +08:00
break;
}
2017-08-10 15:22:42 +08:00
return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'uid不能为空'));
2017-07-06 20:55:02 +08:00
case 5:
2017-08-10 15:22:42 +08:00
_context8.next = 7;
2017-07-18 19:47:38 +08:00
return userInst.findById(id);
case 7:
2017-08-10 15:22:42 +08:00
result = _context8.sent;
2017-07-18 17:18:16 +08:00
if (result) {
2017-08-10 15:22:42 +08:00
_context8.next = 10;
2017-07-18 17:18:16 +08:00
break;
}
2017-08-10 15:22:42 +08:00
return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, '不存在的用户'));
2017-07-18 17:18:16 +08:00
2017-07-18 19:47:38 +08:00
case 10:
2017-08-10 15:22:42 +08:00
return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn({
2017-07-18 16:46:19 +08:00
uid: result._id,
username: result.username,
email: result.email,
role: result.role,
add_time: result.add_time,
up_time: result.up_time
}));
2017-07-06 20:55:02 +08:00
2017-07-18 19:47:38 +08:00
case 13:
2017-08-10 15:22:42 +08:00
_context8.prev = 13;
_context8.t0 = _context8['catch'](0);
return _context8.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, _context8.t0.message));
2017-07-11 16:50:17 +08:00
2017-07-18 19:47:38 +08:00
case 16:
2017-07-06 20:55:02 +08:00
case 'end':
2017-08-10 15:22:42 +08:00
return _context8.stop();
2017-07-06 20:55:02 +08:00
}
}
2017-08-10 15:22:42 +08:00
}, _callee8, this, [[0, 13]]);
2017-07-06 20:55:02 +08:00
}));
2017-07-27 19:49:26 +08:00
function findById(_x9) {
2017-08-10 15:22:42 +08:00
return _ref8.apply(this, arguments);
2017-07-06 20:55:02 +08:00
}
2017-07-07 12:04:14 +08:00
return findById;
2017-07-06 20:55:02 +08:00
}()
2017-07-11 18:22:20 +08:00
/**
2017-07-12 17:01:27 +08:00
* 删除用户,只有admin用户才有此权限
2017-07-11 18:22:20 +08:00
* @interface /user/del
* @method POST
* @param id 用户uid
* @category user
* @foldnumber 10
* @returns {Object}
* @example
*/
2017-07-06 20:55:02 +08:00
}, {
key: 'del',
value: function () {
2017-08-10 15:22:42 +08:00
var _ref9 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9(ctx) {
var userInst, id, result;
2017-08-10 15:22:42 +08:00
return _regenerator2.default.wrap(function _callee9$(_context9) {
2017-07-06 20:55:02 +08:00
while (1) {
2017-08-10 15:22:42 +08:00
switch (_context9.prev = _context9.next) {
2017-07-06 20:55:02 +08:00
case 0:
2017-08-10 15:22:42 +08:00
_context9.prev = 0;
2017-07-11 16:50:17 +08:00
if (!(this.getRole() !== 'admin')) {
2017-08-10 15:22:42 +08:00
_context9.next = 3;
2017-07-11 16:50:17 +08:00
break;
}
2017-08-10 15:22:42 +08:00
return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, 'Without permission.'));
2017-07-11 16:50:17 +08:00
case 3:
2017-07-06 20:55:02 +08:00
userInst = _yapi2.default.getInst(_user2.default);
id = ctx.request.body.id;
2017-07-18 19:47:38 +08:00
if (id) {
2017-08-10 15:22:42 +08:00
_context9.next = 7;
2017-07-18 19:47:38 +08:00
break;
}
2017-08-10 15:22:42 +08:00
return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'uid不能为空'));
2017-07-06 20:55:02 +08:00
2017-07-11 16:50:17 +08:00
case 7:
2017-08-10 15:22:42 +08:00
_context9.next = 9;
2017-07-18 19:47:38 +08:00
return userInst.del(id);
case 9:
2017-08-10 15:22:42 +08:00
result = _context9.sent;
2017-07-06 20:55:02 +08:00
2017-07-27 19:49:26 +08:00
2017-07-06 20:55:02 +08:00
ctx.body = _yapi2.default.commons.resReturn(result);
2017-08-10 15:22:42 +08:00
_context9.next = 16;
2017-07-06 20:55:02 +08:00
break;
2017-07-05 17:59:53 +08:00
2017-07-18 19:47:38 +08:00
case 13:
2017-08-10 15:22:42 +08:00
_context9.prev = 13;
_context9.t0 = _context9['catch'](0);
2017-07-05 17:59:53 +08:00
2017-08-10 15:22:42 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context9.t0.message);
2017-07-06 20:55:02 +08:00
2017-07-18 19:47:38 +08:00
case 16:
2017-07-06 20:55:02 +08:00
case 'end':
2017-08-10 15:22:42 +08:00
return _context9.stop();
2017-07-06 20:55:02 +08:00
}
2017-07-05 17:59:53 +08:00
}
2017-08-10 15:22:42 +08:00
}, _callee9, this, [[0, 13]]);
2017-07-06 20:55:02 +08:00
}));
2017-07-27 19:49:26 +08:00
function del(_x10) {
2017-08-10 15:22:42 +08:00
return _ref9.apply(this, arguments);
2017-07-06 20:55:02 +08:00
}
return del;
}()
2017-07-12 17:01:27 +08:00
/**
* 更新用户个人信息
* @interface /user/update
* @method POST
2017-07-18 16:46:19 +08:00
* @param uid 用户uid
* @param [role] 用户角色,只有管理员有权限修改
* @param [username] String
* @param [email] String
2017-07-12 17:01:27 +08:00
* @category user
* @foldnumber 10
* @returns {Object}
* @example
*/
2017-07-06 20:55:02 +08:00
}, {
key: 'update',
value: function () {
2017-08-10 15:22:42 +08:00
var _ref10 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(ctx) {
2017-07-25 15:42:05 +08:00
var params, userInst, id, data, checkRepeat, result;
2017-08-10 15:22:42 +08:00
return _regenerator2.default.wrap(function _callee10$(_context10) {
2017-07-06 20:55:02 +08:00
while (1) {
2017-08-10 15:22:42 +08:00
switch (_context10.prev = _context10.next) {
2017-07-06 20:55:02 +08:00
case 0:
2017-08-10 15:22:42 +08:00
_context10.prev = 0;
2017-07-25 15:42:05 +08:00
params = ctx.request.body;
2017-07-18 16:46:19 +08:00
2017-07-27 19:49:26 +08:00
2017-07-26 14:22:59 +08:00
params = _yapi2.default.commons.handleParams(params, {
username: 'string',
email: 'string'
});
2017-07-25 15:42:05 +08:00
if (!(this.getRole() !== 'admin' && params.uid != this.getUid())) {
2017-08-10 15:22:42 +08:00
_context10.next = 5;
2017-07-18 16:46:19 +08:00
break;
}
2017-08-10 15:22:42 +08:00
return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '没有权限'));
2017-07-18 16:46:19 +08:00
2017-07-26 14:22:59 +08:00
case 5:
2017-07-06 20:55:02 +08:00
userInst = _yapi2.default.getInst(_user2.default);
2017-07-25 15:42:05 +08:00
id = params.uid;
2017-07-18 19:47:38 +08:00
if (id) {
2017-08-10 15:22:42 +08:00
_context10.next = 9;
2017-07-18 19:47:38 +08:00
break;
}
2017-08-10 15:22:42 +08:00
return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'uid不能为空'));
2017-07-18 19:47:38 +08:00
2017-07-26 14:22:59 +08:00
case 9:
2017-07-12 17:01:27 +08:00
data = {
up_time: _yapi2.default.commons.time()
};
2017-07-06 20:55:02 +08:00
2017-07-27 19:49:26 +08:00
2017-07-25 15:42:05 +08:00
params.username && (data.username = params.username);
params.email && (data.email = params.email);
2017-07-06 20:55:02 +08:00
2017-07-12 17:01:27 +08:00
if (!data.email) {
2017-08-10 15:22:42 +08:00
_context10.next = 18;
2017-07-12 17:01:27 +08:00
break;
}
2017-08-10 15:22:42 +08:00
_context10.next = 15;
2017-07-12 17:01:27 +08:00
return userInst.checkRepeat(data.email);
2017-08-10 15:22:42 +08:00
case 15:
checkRepeat = _context10.sent;
2017-07-12 17:01:27 +08:00
if (!(checkRepeat > 0)) {
2017-08-10 15:22:42 +08:00
_context10.next = 18;
2017-07-12 17:01:27 +08:00
break;
}
2017-08-10 15:22:42 +08:00
return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '该email已经注册'));
2017-07-12 17:01:27 +08:00
2017-08-10 15:22:42 +08:00
case 18:
_context10.next = 20;
return userInst.update(id, data);
2017-07-12 17:01:27 +08:00
2017-08-10 15:22:42 +08:00
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'));
2017-07-26 14:22:59 +08:00
case 21:
2017-08-10 15:22:42 +08:00
avatarInst = _yapi2.default.getInst(_avatar2.default);
_context11.next = 24;
return avatarInst.up(this.getUid(), basecode, type);
2017-07-06 20:55:02 +08:00
2017-08-10 15:22:42 +08:00
case 24:
result = _context11.sent;
2017-07-27 19:49:26 +08:00
2017-07-06 20:55:02 +08:00
ctx.body = _yapi2.default.commons.resReturn(result);
2017-08-10 15:22:42 +08:00
_context11.next = 31;
2017-07-05 17:59:53 +08:00
break;
2017-08-10 15:22:42 +08:00
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, defaultAvatar, 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;
defaultAvatar = require('../utils/defaultAvatar.js');
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 = 15;
break;
case 12:
_context12.prev = 12;
2017-07-12 17:01:27 +08:00
_context12.t0 = _context12['catch'](0);
2017-07-05 17:59:53 +08:00
2017-08-10 15:22:42 +08:00
ctx.body = 'error:' + _context12.t0.message;
2017-07-05 17:59:53 +08:00
2017-08-10 15:22:42 +08:00
case 15:
2017-07-06 20:55:02 +08:00
case 'end':
2017-07-12 17:01:27 +08:00
return _context12.stop();
2017-07-06 20:55:02 +08:00
}
2017-07-05 17:59:53 +08:00
}
2017-08-10 15:22:42 +08:00
}, _callee12, this, [[0, 12]]);
2017-07-06 20:55:02 +08:00
}));
2017-08-10 15:22:42 +08:00
function avatar(_x13) {
2017-07-12 17:01:27 +08:00
return _ref12.apply(this, arguments);
2017-07-06 20:55:02 +08:00
}
2017-08-10 15:22:42 +08:00
return avatar;
2017-07-06 20:55:02 +08:00
}()
/**
* 模糊搜索用户名或者email
* @interface /user/search
* @method GET
* @category user
* @foldnumber 10
* @param {String} q
* @return {Object}
2017-07-14 17:00:30 +08:00
* @example ./api/user/search.json
*/
}, {
key: 'search',
value: function () {
var _ref13 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee13(ctx) {
2017-07-18 16:46:19 +08:00
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;
2017-07-18 16:46:19 +08:00
rules = [{
key: '_id',
alias: 'uid'
2017-07-19 13:58:12 +08:00
}, 'username', 'email', 'role', {
2017-07-18 16:46:19 +08:00
key: 'add_time',
alias: 'addTime'
}, {
key: 'up_time',
alias: 'upTime'
}];
filteredRes = _commons2.default.filterRes(queryList, rules);
2017-07-19 13:58:12 +08:00
return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(filteredRes, 0, 'ok'));
2017-08-10 15:22:42 +08:00
case 11:
case 'end':
return _context13.stop();
}
}
}, _callee13, this);
}));
2017-08-10 15:22:42 +08:00
function search(_x14) {
return _ref13.apply(this, arguments);
}
return search;
}()
2017-07-28 17:29:17 +08:00
/**
* 根据路由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;
2017-08-08 14:45:19 +08:00
2017-07-28 17:29:17 +08:00
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]]);
}));
2017-08-10 15:22:42 +08:00
function nav(_x15) {
2017-07-28 17:29:17 +08:00
return _ref14.apply(this, arguments);
}
return nav;
}()
2017-07-06 20:55:02 +08:00
}]);
return userController;
}(_base2.default);
module.exports = userController;