yapi/server_dist/controllers/user.js

531 lines
20 KiB
JavaScript
Raw Normal View History

2017-07-05 17:59:53 +08:00
'use strict';
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-10 11:11:35 +08:00
var _mongoose = require('mongoose');
var _mongoose2 = _interopRequireDefault(_mongoose);
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);
2017-07-11 16:50:17 +08:00
return (0, _possibleConstructorReturn3.default)(this, (userController.__proto__ || (0, _getPrototypeOf2.default)(userController)).call(this, ctx));
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-11 12:12:43 +08:00
var userInst, email, password, result, token;
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)) {
_context.next = 21;
2017-07-06 20:55:02 +08:00
break;
}
2017-07-11 12:12:43 +08:00
token = jwt.sign({ uid: result._id }, result.passsalt, { expiresIn: '7 days' });
2017-07-10 20:51:04 +08:00
2017-07-11 12:12:43 +08:00
ctx.cookies.set('_yapi_token', token, {
2017-07-11 16:50:17 +08:00
expires: _yapi2.default.commons.expireDate(7),
httpOnly: true
2017-07-11 12:12:43 +08:00
});
ctx.cookies.set('_yapi_uid', result._id, {
2017-07-11 16:50:17 +08:00
expires: _yapi2.default.commons.expireDate(7),
httpOnly: true
2017-07-11 12:12:43 +08:00
});
2017-07-11 18:22:20 +08:00
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn({
uid: result._id,
email: result.email,
add_time: result.add_time,
up_time: result.up_time
}, 0, 'logout success...'));
2017-07-06 20:55:02 +08:00
2017-07-11 12:12:43 +08:00
case 21:
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '密码错误'));
2017-07-06 20:55:02 +08:00
2017-07-11 12:12:43 +08:00
case 22:
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
/**
* 用户注册接口
* @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 () {
var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) {
var userInst, params, checkRepeat, passsalt, data, user;
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
2017-07-06 20:55:02 +08:00
case 0:
//注册
userInst = _yapi2.default.getInst(_user2.default);
params = ctx.request.body; //获取请求的参数,检查是否存在用户名和密码
2017-07-10 20:51:04 +08:00
if (params.email) {
2017-07-11 16:50:17 +08:00
_context3.next = 4;
2017-07-10 20:51:04 +08:00
break;
}
2017-07-11 16:50:17 +08:00
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '邮箱不能为空'));
2017-07-10 20:51:04 +08:00
2017-07-11 12:12:43 +08:00
case 4:
if (params.password) {
2017-07-11 16:50:17 +08:00
_context3.next = 6;
2017-07-10 20:51:04 +08:00
break;
}
2017-07-11 16:50:17 +08:00
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空'));
2017-07-10 20:51:04 +08:00
2017-07-11 12:12:43 +08:00
case 6:
2017-07-11 16:50:17 +08:00
_context3.next = 8;
2017-07-10 20:51:04 +08:00
return userInst.checkRepeat(params.email);
2017-07-11 12:12:43 +08:00
case 8:
2017-07-11 16:50:17 +08:00
checkRepeat = _context3.sent;
2017-07-10 20:51:04 +08:00
if (!(checkRepeat > 0)) {
2017-07-11 16:50:17 +08:00
_context3.next = 11;
2017-07-10 20:51:04 +08:00
break;
}
2017-07-11 16:50:17 +08:00
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '该email已经注册'));
2017-07-10 20:51:04 +08:00
2017-07-11 12:12:43 +08:00
case 11:
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(),
up_time: _yapi2.default.commons.time()
};
2017-07-11 16:50:17 +08:00
_context3.prev = 13;
_context3.next = 16;
2017-07-06 20:55:02 +08:00
return userInst.save(data);
2017-07-11 12:12:43 +08:00
case 16:
2017-07-11 16:50:17 +08:00
user = _context3.sent;
2017-07-06 20:55:02 +08:00
2017-07-11 18:22:20 +08:00
ctx.body = _yapi2.default.commons.resReturn({
uid: user._id,
email: user.email,
add_time: user.add_time,
up_time: user.up_time,
role: 'member'
});
2017-07-11 16:50:17 +08:00
_yapi2.default.commons.sendMail({
to: params.email,
contents: '\u6B22\u8FCE\u6CE8\u518C\uFF0C\u60A8\u7684\u8D26\u53F7 ' + params.email + ' \u5DF2\u7ECF\u6CE8\u518C\u6210\u529F'
});
2017-07-11 18:22:20 +08:00
_context3.next = 24;
2017-07-05 17:59:53 +08:00
break;
2017-07-11 18:22:20 +08:00
case 21:
_context3.prev = 21;
2017-07-11 16:50:17 +08:00
_context3.t0 = _context3['catch'](13);
2017-07-05 17:59:53 +08:00
2017-07-11 16:50:17 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 401, _context3.t0.message);
2017-07-05 17:59:53 +08:00
2017-07-11 18:22:20 +08:00
case 24:
2017-07-06 20:55:02 +08:00
case 'end':
2017-07-11 16:50:17 +08:00
return _context3.stop();
2017-07-06 20:55:02 +08:00
}
}
2017-07-11 18:22:20 +08:00
}, _callee3, this, [[13, 21]]);
2017-07-06 20:55:02 +08:00
}));
2017-07-11 16:50:17 +08:00
function reg(_x3) {
return _ref3.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
* @returns {Object}
* @example
*/
2017-07-06 20:55:02 +08:00
}, {
key: 'list',
value: function () {
2017-07-11 16:50:17 +08:00
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(ctx) {
2017-07-06 20:55:02 +08:00
var userInst, user;
2017-07-11 16:50:17 +08:00
return _regenerator2.default.wrap(function _callee4$(_context4) {
2017-07-06 20:55:02 +08:00
while (1) {
2017-07-11 16:50:17 +08:00
switch (_context4.prev = _context4.next) {
2017-07-06 20:55:02 +08:00
case 0:
2017-07-11 18:22:20 +08:00
if (!(this.getRole() !== 'admin')) {
_context4.next = 2;
break;
}
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, 'Without permission.'));
case 2:
2017-07-06 20:55:02 +08:00
userInst = _yapi2.default.getInst(_user2.default);
2017-07-11 18:22:20 +08:00
_context4.prev = 3;
_context4.next = 6;
2017-07-06 20:55:02 +08:00
return userInst.list();
2017-07-11 18:22:20 +08:00
case 6:
2017-07-11 16:50:17 +08:00
user = _context4.sent;
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(user));
2017-07-06 20:55:02 +08:00
2017-07-11 18:22:20 +08:00
case 10:
_context4.prev = 10;
_context4.t0 = _context4['catch'](3);
2017-07-11 16:50:17 +08:00
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, _context4.t0.message));
2017-07-06 20:55:02 +08:00
2017-07-11 18:22:20 +08:00
case 13:
2017-07-06 20:55:02 +08:00
case 'end':
2017-07-11 16:50:17 +08:00
return _context4.stop();
2017-07-06 20:55:02 +08:00
}
}
2017-07-11 18:22:20 +08:00
}, _callee4, this, [[3, 10]]);
2017-07-06 20:55:02 +08:00
}));
2017-07-11 16:50:17 +08:00
function list(_x4) {
return _ref4.apply(this, arguments);
2017-07-06 20:55:02 +08:00
}
return list;
}()
2017-07-11 18:22:20 +08:00
/**
* 获取用户列表
* @interface /user/list
* @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-07-11 16:50:17 +08:00
var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(ctx) {
2017-07-06 20:55:02 +08:00
var userInst, id, result;
2017-07-11 16:50:17 +08:00
return _regenerator2.default.wrap(function _callee5$(_context5) {
2017-07-06 20:55:02 +08:00
while (1) {
2017-07-11 16:50:17 +08:00
switch (_context5.prev = _context5.next) {
2017-07-06 20:55:02 +08:00
case 0:
2017-07-11 16:50:17 +08:00
_context5.prev = 0;
2017-07-06 20:55:02 +08:00
userInst = _yapi2.default.getInst(_user2.default);
id = ctx.request.body.id;
2017-07-11 16:50:17 +08:00
if (!(this.getUid() != id)) {
_context5.next = 5;
break;
}
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, 'Without permission.'));
2017-07-06 20:55:02 +08:00
case 5:
2017-07-11 16:50:17 +08:00
_context5.next = 7;
return userInst.findById(id);
2017-07-06 20:55:02 +08:00
2017-07-11 16:50:17 +08:00
case 7:
result = _context5.sent;
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(result));
2017-07-06 20:55:02 +08:00
2017-07-11 16:50:17 +08:00
case 11:
_context5.prev = 11;
_context5.t0 = _context5['catch'](0);
return _context5.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, _context5.t0.message));
case 14:
2017-07-06 20:55:02 +08:00
case 'end':
2017-07-11 16:50:17 +08:00
return _context5.stop();
2017-07-06 20:55:02 +08:00
}
}
2017-07-11 16:50:17 +08:00
}, _callee5, this, [[0, 11]]);
2017-07-06 20:55:02 +08:00
}));
2017-07-11 16:50:17 +08:00
function findById(_x5) {
return _ref5.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
/**
* 获取用户列表,只有admin用户才有此权限
* @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-07-11 16:50:17 +08:00
var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(ctx) {
2017-07-06 20:55:02 +08:00
var userInst, id, result;
2017-07-11 16:50:17 +08:00
return _regenerator2.default.wrap(function _callee6$(_context6) {
2017-07-06 20:55:02 +08:00
while (1) {
2017-07-11 16:50:17 +08:00
switch (_context6.prev = _context6.next) {
2017-07-06 20:55:02 +08:00
case 0:
2017-07-11 16:50:17 +08:00
_context6.prev = 0;
if (!(this.getRole() !== 'admin')) {
_context6.next = 3;
break;
}
return _context6.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, 'Without permission.'));
case 3:
2017-07-06 20:55:02 +08:00
userInst = _yapi2.default.getInst(_user2.default);
id = ctx.request.body.id;
2017-07-11 16:50:17 +08:00
_context6.next = 7;
2017-07-06 20:55:02 +08:00
return userInst.del(id);
2017-07-11 16:50:17 +08:00
case 7:
result = _context6.sent;
2017-07-06 20:55:02 +08:00
ctx.body = _yapi2.default.commons.resReturn(result);
2017-07-11 16:50:17 +08:00
_context6.next = 14;
2017-07-06 20:55:02 +08:00
break;
2017-07-05 17:59:53 +08:00
2017-07-11 16:50:17 +08:00
case 11:
_context6.prev = 11;
_context6.t0 = _context6['catch'](0);
2017-07-05 17:59:53 +08:00
2017-07-11 16:50:17 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context6.t0.message);
2017-07-06 20:55:02 +08:00
2017-07-11 16:50:17 +08:00
case 14:
2017-07-06 20:55:02 +08:00
case 'end':
2017-07-11 16:50:17 +08:00
return _context6.stop();
2017-07-06 20:55:02 +08:00
}
2017-07-05 17:59:53 +08:00
}
2017-07-11 16:50:17 +08:00
}, _callee6, this, [[0, 11]]);
2017-07-06 20:55:02 +08:00
}));
2017-07-11 16:50:17 +08:00
function del(_x6) {
return _ref6.apply(this, arguments);
2017-07-06 20:55:02 +08:00
}
return del;
}()
}, {
key: 'update',
value: function () {
2017-07-11 16:50:17 +08:00
var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(ctx) {
2017-07-06 20:55:02 +08:00
var userInst, id, data, result;
2017-07-11 16:50:17 +08:00
return _regenerator2.default.wrap(function _callee7$(_context7) {
2017-07-06 20:55:02 +08:00
while (1) {
2017-07-11 16:50:17 +08:00
switch (_context7.prev = _context7.next) {
2017-07-06 20:55:02 +08:00
case 0:
2017-07-11 16:50:17 +08:00
_context7.prev = 0;
2017-07-06 20:55:02 +08:00
userInst = _yapi2.default.getInst(_user2.default);
2017-07-11 16:50:17 +08:00
id = this.getUid();
2017-07-06 20:55:02 +08:00
data = {};
ctx.request.body.username && (data.username = ctx.request.body.username);
ctx.request.body.email && (data.email = ctx.request.body.email);
2017-07-11 16:50:17 +08:00
_context7.next = 8;
2017-07-06 20:55:02 +08:00
return userInst.update(id, data);
2017-07-11 16:50:17 +08:00
case 8:
result = _context7.sent;
2017-07-06 20:55:02 +08:00
ctx.body = _yapi2.default.commons.resReturn(result);
2017-07-11 16:50:17 +08:00
_context7.next = 15;
2017-07-05 17:59:53 +08:00
break;
2017-07-11 16:50:17 +08:00
case 12:
_context7.prev = 12;
_context7.t0 = _context7['catch'](0);
2017-07-05 17:59:53 +08:00
2017-07-11 16:50:17 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context7.t0.message);
2017-07-05 17:59:53 +08:00
2017-07-11 16:50:17 +08:00
case 15:
2017-07-06 20:55:02 +08:00
case 'end':
2017-07-11 16:50:17 +08:00
return _context7.stop();
2017-07-06 20:55:02 +08:00
}
2017-07-05 17:59:53 +08:00
}
2017-07-11 16:50:17 +08:00
}, _callee7, this, [[0, 12]]);
2017-07-06 20:55:02 +08:00
}));
2017-07-11 16:50:17 +08:00
function update(_x7) {
return _ref7.apply(this, arguments);
2017-07-06 20:55:02 +08:00
}
return update;
}()
}]);
return userController;
}(_base2.default);
module.exports = userController;