2017-07-05 17:59:53 +08:00
|
|
|
import userModel from '../models/user.js'
|
|
|
|
import yapi from '../yapi.js'
|
2017-07-06 18:25:53 +08:00
|
|
|
import baseController from './base.js'
|
2017-07-05 17:59:53 +08:00
|
|
|
|
2017-07-06 18:25:53 +08:00
|
|
|
class userController extends baseController{
|
2017-07-06 20:55:02 +08:00
|
|
|
constructor(ctx){
|
|
|
|
super(ctx)
|
2017-07-06 18:25:53 +08:00
|
|
|
console.log('constructor...')
|
|
|
|
}
|
|
|
|
async login(ctx){ //登录
|
|
|
|
var userInst = yapi.getInst(userModel); //创建user实体
|
|
|
|
let username = ctx.request.body.username;
|
|
|
|
let password = sha1(ctx.request.body.password);
|
|
|
|
let id = ctx.request.body.id;
|
|
|
|
let result = await userInst.getUser(id); //获取登录用户的id
|
|
|
|
if(!username){
|
2017-07-05 17:59:53 +08:00
|
|
|
return ctx.body = yapi.commons.resReturn(null,400,'用户名不能为空');
|
|
|
|
}
|
2017-07-06 18:25:53 +08:00
|
|
|
if(!password){
|
2017-07-05 17:59:53 +08:00
|
|
|
return ctx.body = yapi.commons.resReturn(null,400,'密码不能为空');
|
|
|
|
}
|
|
|
|
//输入一个不存在的用户名
|
2017-07-06 18:25:53 +08:00
|
|
|
var checkRepeat = await userInst.checkRepeat(username);//然后检查是否已经存在该用户
|
|
|
|
if(checkRepeat==0){
|
|
|
|
return ctx.body = yapi.commons.resReturn(null,404,'该用户不存在'); //返回的错误码对吗????
|
|
|
|
}else if(result.password===password){ //用户名存在,判断密码是否正确,正确则可以登录
|
|
|
|
console.log('密码一致'); //是不是还需要把用户名密码一些东西写到session
|
2017-07-06 20:55:02 +08:00
|
|
|
setCookie('token', sha1(username+password));
|
|
|
|
userInst.update({_id, result._id}, {token: sha1(username+password)})
|
|
|
|
return ctx.body = {username: ''}
|
2017-07-06 18:25:53 +08:00
|
|
|
}else{
|
|
|
|
return ctx.body = yapi.commons.resReturn(null,400,'密码错误');
|
2017-07-05 17:59:53 +08:00
|
|
|
}
|
2017-07-06 18:25:53 +08:00
|
|
|
}
|
2017-07-06 20:55:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-06 18:25:53 +08:00
|
|
|
async reg(ctx){ //注册
|
|
|
|
var userInst = yapi.getInst(userModel);
|
|
|
|
let params = ctx.request.body; //获取请求的参数,检查是否存在用户名和密码
|
|
|
|
if(!params.username){
|
2017-07-05 17:59:53 +08:00
|
|
|
return ctx.body = yapi.commons.resReturn(null,400,'用户名不能为空');
|
|
|
|
}
|
2017-07-06 18:25:53 +08:00
|
|
|
if(!params.password){
|
|
|
|
return ctx.body = yapi.commons.resReturn(null,400,'密码不能为空');
|
|
|
|
}
|
|
|
|
if(!params.email){
|
|
|
|
return ctx.body = yapi.commons.resReturn(null,400,'邮箱不能为空');
|
|
|
|
}
|
|
|
|
|
|
|
|
var checkRepeat = await userInst.checkRepeat(params.username);//然后检查是否已经存在该用户
|
|
|
|
if(checkRepeat>0){
|
|
|
|
return ctx.body = yapi.commons.resReturn(null,401,'该用户名已经注册');
|
|
|
|
}
|
|
|
|
var checkRepeat = await userInst.checkRepeat(params.email);//然后检查是否已经存在该用户
|
2017-07-05 17:59:53 +08:00
|
|
|
if(checkRepeat>0){
|
2017-07-06 18:25:53 +08:00
|
|
|
return ctx.body = yapi.commons.resReturn(null,401,'该邮箱已经注册');
|
2017-07-05 17:59:53 +08:00
|
|
|
}
|
2017-07-06 20:55:02 +08:00
|
|
|
|
2017-07-05 17:59:53 +08:00
|
|
|
let data = {
|
2017-07-06 18:25:53 +08:00
|
|
|
username: params.username,
|
|
|
|
password: sha1(params.password),//加密
|
|
|
|
email: params.email,
|
|
|
|
role: params.role,
|
2017-07-05 17:59:53 +08:00
|
|
|
add_time: yapi.commons.time(),
|
|
|
|
up_time: yapi.commons.time()
|
|
|
|
}
|
|
|
|
try{
|
2017-07-06 18:25:53 +08:00
|
|
|
let user = await userInst.save(data);
|
|
|
|
user = yapi.commons.fieldSelect(user,['id','username','password','email','role'])
|
2017-07-05 17:59:53 +08:00
|
|
|
ctx.body = yapi.commons.resReturn(user);
|
|
|
|
}catch(e){
|
2017-07-06 20:55:02 +08:00
|
|
|
ctx.body = yapi.commons.resReturn(null, 401, e.message);
|
2017-07-05 17:59:53 +08:00
|
|
|
}
|
2017-07-06 18:25:53 +08:00
|
|
|
}
|
|
|
|
async list(ctx){ //获取用户列表并分页
|
|
|
|
var userInst = yapi.getInst(userModel);
|
2017-07-05 17:59:53 +08:00
|
|
|
try{
|
2017-07-06 18:25:53 +08:00
|
|
|
let user = await userInst.list();
|
2017-07-05 17:59:53 +08:00
|
|
|
return ctx.body = yapi.commons.resReturn(user);
|
|
|
|
}catch(e){
|
|
|
|
return ctx.body = yapi.commons.resReturn(null,402,e.message);
|
|
|
|
}
|
2017-07-06 18:25:53 +08:00
|
|
|
}
|
|
|
|
async getUser(ctx){ //根据id获取用户信息
|
|
|
|
try{
|
|
|
|
var userInst = yapi.getInst(userModel);
|
|
|
|
let id = ctx.request.body.id;
|
|
|
|
let result = await userInst.getUser(id);
|
|
|
|
return ctx.body = yapi.commons.resReturn(result);
|
|
|
|
}catch(e){
|
|
|
|
return ctx.body = yapi.commons.resReturn(null,402,e.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async del(ctx){ //根据id删除一个用户
|
2017-07-05 17:59:53 +08:00
|
|
|
try{
|
2017-07-06 18:25:53 +08:00
|
|
|
var userInst = yapi.getInst(userModel);
|
|
|
|
let id = ctx.request.body.id;
|
|
|
|
let result = await userInst.del(id);
|
2017-07-05 17:59:53 +08:00
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
|
|
|
}catch(e){
|
|
|
|
ctx.body = yapi.commons.resReturn(null,402,e.message);
|
|
|
|
}
|
2017-07-06 18:25:53 +08:00
|
|
|
}
|
|
|
|
async update(ctx){ //更新用户信息
|
2017-07-05 17:59:53 +08:00
|
|
|
try{
|
2017-07-06 18:25:53 +08:00
|
|
|
var userInst = yapi.getInst(userModel);
|
2017-07-05 17:59:53 +08:00
|
|
|
let id = ctx.request.body.id;
|
|
|
|
let data ={};
|
2017-07-06 18:25:53 +08:00
|
|
|
ctx.request.body.username && (data.username = ctx.request.body.username)
|
|
|
|
ctx.request.body.password && (data.password = ctx.request.body.password)
|
|
|
|
ctx.request.body.email && (data.email = ctx.request.body.email)
|
|
|
|
ctx.request.body.role && (data.role = ctx.request.body.role)
|
2017-07-05 17:59:53 +08:00
|
|
|
if (Object.keys(data).length===0){
|
2017-07-06 18:25:53 +08:00
|
|
|
ctx.body = yapi.commons.resReturn(null,404,'用户名、密码、Email、role都为空');
|
2017-07-05 17:59:53 +08:00
|
|
|
}
|
2017-07-06 18:25:53 +08:00
|
|
|
let result = await userInst.update(id,data);
|
2017-07-05 17:59:53 +08:00
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
|
|
|
}catch(e){
|
|
|
|
ctx.body = yapi.commons.resReturn(null,402,e.message);
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 18:25:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = userController
|