UNPKG

horse-generator

Version:
1 lines 4.86 kB
"use strict";var _deleteProperty=require("babel-runtime/core-js/reflect/delete-property"),_deleteProperty2=_interopRequireDefault(_deleteProperty);function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}var captcha=require("trek-captcha"),config=require("../../../config/env"),_require=require("../../model"),User=_require.User,_require2=require("../../proxy"),LogsProxy=_require2.LogsProxy,EMAIL_REGEXP=/^([A-Za-z0-9_\-\.])+\@(o-in.me|qq.com)$/,USERNAME_REGEXP=/^[(\u4e00-\u9fa5)0-9a-zA-Z\_\s@]+$/,PWD_REGEXP=/^(\w){6,20}$/;exports.add=async function(e,r,s){var t=e.body,a=t.email?t.email.replace(/(^\s+)|(\s+$)/g,""):"",o=t.username?t.username.replace(/(^\s+)|(\s+$)/g,""):"",i=t.password?t.password.replace(/(^\s+)|(\s+$)/g,""):"",d=void 0;if(""===a?d="邮箱地址不能为空":""===o?d="用户名不能为空":""===i?d="密码不能为空":a.length<=4||a.length>30||!EMAIL_REGEXP.test(a)?d="邮箱地址不合法,只能输入5-30个字母、数字、下划线":o.length<=4||o.length>30||!USERNAME_REGEXP.test(o)?d="用户名不合法,只能输入5-30个包含中文中文英文(包含大小写)空格以及中文名字带":(i.length<=5||i.length>20||!PWD_REGEXP.test(i))&&(d="密码不合法,只能输入6-20个字母、数字、下划线"),d)return r.resMid.resBody(r,422,d);try{t.role&&(0,_deleteProperty2.default)(t,"role"),t.status&&(0,_deleteProperty2.default)(t,"status");var n=new User(t);n.role="user",n=await n.save(),await LogsProxy.createLogs(e.user&&e.user._id||n._id,n._id,"User","添加用户"),r.resMid.resBody(r,"","",{_id:n._id})}catch(e){r.throw(e)}},exports.getList=async function(e,r,s){try{var t={},a=e.query;a.where&&(t.$and=[],t.$and.push({$or:[{email:{$regex:new RegExp(a.where,"ig")}},{username:{$regex:new RegExp(a.where,"ig")}}]})),a.select="-salt -hashedPassword";var o=await User.paginate(t,a);r.resMid.resBody(r,"","",o)}catch(e){r.throw(e)}},exports.getOne=async function(e,r,s){var t=e.params.id;try{var a=await User.findById(t).select("-salt -hashedPassword");r.resMid.resBody(r,"","",a.userInfo)}catch(e){r.throw(e)}},exports.getMe=async function(e,r,s){var t=e.user._id;try{var a=await User.findOne({_id:t},"-salt -hashedPassword");r.resMid.resBody(r,"","",a),LogsProxy.createLogs(e.user._id,a._id,"User","用户登录")}catch(e){r.throw(e)}},exports.modOne=async function(e,r,s){var t=e.params.id,a=e.body,o=a.role||"user",i=void 0;if(config.userRoles.includes(o)||(i="角色不合法"),o!==config.userRoles[1]||a.partner||(i="请选择经销商类型"),o!==config.userRoles[1]&&(0,_deleteProperty2.default)(a,"partner"),i)return r.resMid.resBody(r,422,i);try{var d=await User.findById(t);o&&(d.role=o),a.partner&&(d.partner=a.partner),d.role!==config.userRoles[1]&&(d.partner=null),a.status&&(d.status=a.status),a.password&&(d.password=a.password),d=await d.save(),await LogsProxy.createLogs(e.user._id,d._id,"User","修改用户"),r.resMid.resBody(r,"","",{_id:d._id})}catch(e){r.throw(e)}},exports.modMe=async function(e,r,s){var t=e.body,a=t.username?t.username.replace(/(^\s+)|(\s+$)/g,""):"",o=void 0;if(""===a?o="用户名不能为空":(a.length<=2||a.length>15||!USERNAME_REGEXP.test(a))&&(o="用户名不合法"),o)return r.resMid.resBody(r,422,o);var i=e.user;i.username=a;try{i=await i.save(),await LogsProxy.createLogs(e.user._id,i._id,"User","修改用户自己"),r.resMid.resBody(r,"","",i.userInfo)}catch(e){r.throw(e)}},exports.delOne=async function(e,r,s){var t=e.user._id;if(String(t)===String(e.params.id))return r.resMid.resBody(r,403,"不能删除自己已经登录的账号");try{var a=await User.findByIdAndRemove(e.params.id);await LogsProxy.createLogs(e.user._id,a._id,"User","删除用户"),r.resMid.resBody(r,"","")}catch(e){r.throw(e)}},exports.password=async function(e,r,s){var t=e.body,a=t.oldPassword?t.oldPassword.replace(/(^\s+)|(\s+$)/g,""):"",o=t.newPassword?t.newPassword.replace(/(^\s+)|(\s+$)/g,""):"",i=void 0;if((a.length<=5||a.length>20||!PWD_REGEXP.test(a))&&(i="旧密码不合法,只能输入6-20个字母、数字、下划线"),(o.length<=5||o.length>20||!PWD_REGEXP.test(o))&&(i="新密码不合法,只能输入6-20个字母、数字、下划线"),i)return r.resMid.resBody(r,422,i);var d=e.user;try{(d=await User.findById(d._id)).authenticate(a)?(d.password=o,await d.save()):r.throw("新旧密码一致"),await LogsProxy.createLogs(e.user._id,d._id,"User","修改用户密码"),r.resMid.resBody(r,"","",d.userInfo)}catch(e){r.throw(e)}},exports.getUserProvider=async function(e,r,s){try{var t=await User.findById(e.user._id);r.resMid.resBody(r,"","",t.providerInfo)}catch(e){r.throw(e)}},exports.getSnsLogins=async function(e,r,s){config.snsLogins?r.resMid.resBody(r,"","",config.snsLogins):r.throw(404)},exports.getCaptcha=async function(e,r,s){var t=await captcha({size:6}),a=t.token,o=t.buffer;e.session.captcha=a,r.resMid.resBody(r,"","",o)};