This commit is contained in:
lwg 2017-07-07 12:04:14 +08:00
parent 4a33e7d7c6
commit 018cec6338
10 changed files with 72 additions and 65 deletions

View File

@ -26,7 +26,8 @@
"koa-static": "^3.0.0",
"koa-views": "^5.2.0",
"mongoose": "4.10.8",
"mongoose-auto-increment": "^5.0.1"
"mongoose-auto-increment": "^5.0.1",
"sha1": "^1.1.1"
},
"devDependencies": {
"babel-cli": "^6.24.1",

View File

@ -9,11 +9,12 @@ class baseController{
}
getLoginStatus(){
let token = getCookie('_yapi_token');
let uid = getCookie('_yapi_uid');
let usermodel
// let token = getCookie('_yapi_token');
// let uid = getCookie('_yapi_uid');
// let usermodel
usermode.token === token
// usermode.token === token
// return true
return true
}

View File

@ -2,6 +2,8 @@ import userModel from '../models/user.js'
import yapi from '../yapi.js'
import baseController from './base.js'
const sha1 = require('sha1');
class userController extends baseController{
constructor(ctx){
super(ctx)
@ -11,8 +13,9 @@ class userController extends baseController{
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
let user = await userInst.findByName(username);
let id = user.id;
let result = await userInst.findById(id); //获取登录用户的id
if(!username){
return ctx.body = yapi.commons.resReturn(null,400,'用户名不能为空');
}
@ -25,9 +28,9 @@ class userController extends baseController{
return ctx.body = yapi.commons.resReturn(null,404,'该用户不存在'); //返回的错误码对吗????
}else if(result.password===password){ //用户名存在,判断密码是否正确,正确则可以登录
console.log('密码一致'); //是不是还需要把用户名密码一些东西写到session
setCookie('token', sha1(username+password));
userInst.update({_id, result._id}, {token: sha1(username+password)})
return ctx.body = {username: ''}
// setCookie('token', sha1(username+password));
// userInst.update({_id, result._id}, {token: sha1(username+password)})
// return ctx.body = {username: ''}
}else{
return ctx.body = yapi.commons.resReturn(null,400,'密码错误');
}
@ -82,11 +85,11 @@ class userController extends baseController{
return ctx.body = yapi.commons.resReturn(null,402,e.message);
}
}
async getUser(ctx){ //根据id获取用户信息
async findById(ctx){ //根据id获取用户信息
try{
var userInst = yapi.getInst(userModel);
let id = ctx.request.body.id;
let result = await userInst.getUser(id);
let result = await userInst.findById(id);
return ctx.body = yapi.commons.resReturn(result);
}catch(e){
return ctx.body = yapi.commons.resReturn(null,402,e.message);

View File

@ -10,18 +10,7 @@ class userModel extends baseModel{
getSchema(){
return{
username: String,
password: {
type: String,
required: true,
validate: {
validator: function(v) {
if(typeof v !== 'string') return false;
if(v.length < 6 || v.length > 64) return false;
return true;
},
message: '{VALUE} is not a valid password!'
},
},
password: String,
passsalt: String,
email: String,
role: String,
@ -41,7 +30,10 @@ class userModel extends baseModel{
list(){
return this.model.find().select("username_id username email role add_time up_time").exec() //显示id name email role
}
getUser(id){
findByName(name){
return this.model.find({"username":name})
}
findById(id){
return this.model.findById({
_id: id
})

View File

@ -38,7 +38,7 @@ createAction('group', 'del', 'post', 'del')
createAction('user', 'login', 'post', 'login')
createAction('user', 'reg', 'post', 'reg')
createAction('user', 'list', 'get', 'list')
createAction('user', 'getUser', 'get', 'getUser')
createAction('user', 'findById', 'post', 'findById')
createAction('user', 'update', 'post', 'update')
createAction('user', 'del', 'post', 'del')

View File

@ -29,6 +29,12 @@ var baseController = function () {
}, {
key: 'getLoginStatus',
value: function getLoginStatus() {
// let token = getCookie('_yapi_token');
// let uid = getCookie('_yapi_uid');
// let usermodel
// usermode.token === token
// return true
return true;
}
}, {

View File

@ -46,6 +46,8 @@ var _base2 = _interopRequireDefault(_base);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var sha1 = require('sha1');
var userController = function (_baseController) {
(0, _inherits3.default)(userController, _baseController);
@ -62,7 +64,7 @@ var userController = function (_baseController) {
key: 'login',
value: function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
var userInst, username, password, id, result, checkRepeat;
var userInst, username, password, user, id, result, checkRepeat;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
@ -72,57 +74,65 @@ var userController = function (_baseController) {
username = ctx.request.body.username;
password = sha1(ctx.request.body.password);
id = ctx.request.body.id;
_context.next = 6;
return userInst.getUser(id);
_context.next = 5;
return userInst.findByName(username);
case 6:
case 5:
user = _context.sent;
id = user.id;
_context.next = 9;
return userInst.findById(id);
case 9:
result = _context.sent;
if (username) {
_context.next = 9;
_context.next = 12;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '用户名不能为空'));
case 9:
case 12:
if (password) {
_context.next = 11;
_context.next = 14;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码不能为空'));
case 11:
_context.next = 13;
case 14:
_context.next = 16;
return userInst.checkRepeat(username);
case 13:
case 16:
checkRepeat = _context.sent;
if (!(checkRepeat == 0)) {
_context.next = 18;
_context.next = 21;
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 404, '该用户不存在'));
case 18:
case 21:
if (!(result.password === password)) {
_context.next = 24;
_context.next = 25;
break;
}
//用户名存在,判断密码是否正确,正确则可以登录
console.log('密码一致'); //是不是还需要把用户名密码一些东西写到session
setCookie('token', sha1(username + password));
return _context.abrupt('return', ctx.body = { username: '' });
case 24:
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码错误'));
// setCookie('token', sha1(username+password));
// userInst.update({_id, result._id}, {token: sha1(username+password)})
// return ctx.body = {username: ''}
_context.next = 26;
break;
case 25:
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '密码错误'));
case 26:
case 'end':
return _context.stop();
}
@ -280,7 +290,7 @@ var userController = function (_baseController) {
return list;
}()
}, {
key: 'getUser',
key: 'findById',
value: function () {
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(ctx) {
var userInst, id, result;
@ -292,7 +302,7 @@ var userController = function (_baseController) {
userInst = _yapi2.default.getInst(_user2.default);
id = ctx.request.body.id;
_context4.next = 5;
return userInst.getUser(id);
return userInst.findById(id);
case 5:
result = _context4.sent;
@ -311,11 +321,11 @@ var userController = function (_baseController) {
}, _callee4, this, [[0, 9]]);
}));
function getUser(_x4) {
function findById(_x4) {
return _ref4.apply(this, arguments);
}
return getUser;
return findById;
}()
}, {
key: 'del',

View File

@ -52,18 +52,7 @@ var userModel = function (_baseModel) {
value: function getSchema() {
return {
username: String,
password: {
type: String,
required: true,
validate: {
validator: function validator(v) {
if (typeof v !== 'string') return false;
if (v.length < 6 || v.length > 64) return false;
return true;
},
message: '{VALUE} is not a valid password!'
}
},
password: String,
passsalt: String,
email: String,
role: String,
@ -90,8 +79,13 @@ var userModel = function (_baseModel) {
return this.model.find().select("username_id username email role add_time up_time").exec(); //显示id name email role
}
}, {
key: 'getUser',
value: function getUser(id) {
key: 'findByName',
value: function findByName(name) {
return this.model.find({ "username": name });
}
}, {
key: 'findById',
value: function findById(id) {
return this.model.findById({
_id: id
});

View File

@ -65,7 +65,7 @@ createAction('group', 'del', 'post', 'del');
createAction('user', 'login', 'post', 'login');
createAction('user', 'reg', 'post', 'reg');
createAction('user', 'list', 'get', 'list');
createAction('user', 'getUser', 'get', 'getUser');
createAction('user', 'findById', 'post', 'findById');
createAction('user', 'update', 'post', 'update');
createAction('user', 'del', 'post', 'del');

View File

@ -33,7 +33,7 @@
"options": {
"type": "interface", // component lib component
"source": true, // false
"categories":["group"]
"categories":["group", "user"]
}
}]
}