From ce1abbf863588151ee4c9f36d6773f3f4c1d6812 Mon Sep 17 00:00:00 2001 From: Aaron.Liu <427787340@qq.com> Date: Wed, 25 Apr 2018 11:48:48 +0800 Subject: [PATCH] 增加登录与锁定验证 --- app/controller/user.js | 29 +++++++++++++++++++++++++++++ app/router.js | 2 ++ app/service/user.js | 10 ++++++++++ 3 files changed, 41 insertions(+), 0 deletions(-) diff --git a/app/controller/user.js b/app/controller/user.js index 22f11ce..230b5f6 100644 --- a/app/controller/user.js +++ b/app/controller/user.js @@ -70,6 +70,35 @@ class UserController extends Controller { this.ctx.body = { success: false, error }; } } + async login() { + const ctx = this.ctx; + const data = ctx.request.body; + try { + const results = await ctx.service.user.login(data); + if (results.length > 0) { + const userInfo = results[0]; + this.ctx.body = { result: userInfo, success: true }; + } else { + this.ctx.body = { success: false, error: '用户名或密码不正确' }; + } + } catch (error) { + this.ctx.body = { success: false, error }; + } + } + async validate() { + const ctx = this.ctx; + const data = ctx.request.body; + try { + const results = await ctx.service.user.validate(data); + if (results.length > 0) { + this.ctx.body = { success: true }; + } else { + this.ctx.body = { success: false, error: '用户密码不正确' }; + } + } catch (error) { + this.ctx.body = { success: false, error }; + } + } } module.exports = UserController; diff --git a/app/router.js b/app/router.js index 76d246e..5779f69 100644 --- a/app/router.js +++ b/app/router.js @@ -9,6 +9,8 @@ module.exports = app => { // 首页统计 router.get('/statistic/count', controller.statistic.count); // 用户管理 + router.post('/user/validate', controller.user.validate); + router.post('/user/login', controller.user.login); router.get('/user/list', controller.user.list); router.get('/user/:id', controller.user.info); router.post('/user', controller.user.add); diff --git a/app/service/user.js b/app/service/user.js index e2e89bd..791cf88 100644 --- a/app/service/user.js +++ b/app/service/user.js @@ -32,6 +32,16 @@ class UserService extends Service { const result = await this.app.mysql.delete('user', data); return result; } + async login(data) { + const { username, password } = data; + const result = await this.app.mysql.query('select name, username, role from user where username = ? and password = ?', [ username, password ]); + return result; + } + async validate(data) { + const { username, password } = data; + const result = await this.app.mysql.query('select id from user where username = ? and password = ?', [ username, password ]); + return result; + } } module.exports = UserService; -- libgit2 0.21.0