From 018cec6338e9af88c6b5b1a01b315e2e426692f4 Mon Sep 17 00:00:00 2001 From: lwg Date: Fri, 7 Jul 2017 12:04:14 +0800 Subject: [PATCH] user_up --- package.json | 3 +- server/controllers/base.js | 9 +++--- server/controllers/user.js | 17 +++++----- server/models/user.js | 18 +++-------- server/router.js | 2 +- server_dist/controllers/base.js | 6 ++++ server_dist/controllers/user.js | 56 +++++++++++++++++++-------------- server_dist/models/user.js | 22 +++++-------- server_dist/router.js | 2 +- ydoc.json | 2 +- 10 files changed, 72 insertions(+), 65 deletions(-) diff --git a/package.json b/package.json index 717cf7ea..7531e6d4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server/controllers/base.js b/server/controllers/base.js index ebd9b2e5..fe6610d8 100644 --- a/server/controllers/base.js +++ b/server/controllers/base.js @@ -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 } diff --git a/server/controllers/user.js b/server/controllers/user.js index 7d373a74..52824600 100644 --- a/server/controllers/user.js +++ b/server/controllers/user.js @@ -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); diff --git a/server/models/user.js b/server/models/user.js index 68b49d95..f5b32fed 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -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 }) diff --git a/server/router.js b/server/router.js index decc334d..9ee4eba8 100644 --- a/server/router.js +++ b/server/router.js @@ -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') diff --git a/server_dist/controllers/base.js b/server_dist/controllers/base.js index 8b1b740e..62de3e71 100644 --- a/server_dist/controllers/base.js +++ b/server_dist/controllers/base.js @@ -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; } }, { diff --git a/server_dist/controllers/user.js b/server_dist/controllers/user.js index a97f30bc..497069c8 100644 --- a/server_dist/controllers/user.js +++ b/server_dist/controllers/user.js @@ -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', diff --git a/server_dist/models/user.js b/server_dist/models/user.js index 290a4be0..c810aa4d 100644 --- a/server_dist/models/user.js +++ b/server_dist/models/user.js @@ -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 }); diff --git a/server_dist/router.js b/server_dist/router.js index 01a0fb73..b8e9ccad 100644 --- a/server_dist/router.js +++ b/server_dist/router.js @@ -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'); diff --git a/ydoc.json b/ydoc.json index 29eba25b..85997652 100644 --- a/ydoc.json +++ b/ydoc.json @@ -33,7 +33,7 @@ "options": { "type": "interface", // 类型,可选 component 和 lib,默认 component "source": true, // 是否生成源文件预览,默认 false - "categories":["group"] + "categories":["group", "user"] } }] }