Commit ce1abbf863588151ee4c9f36d6773f3f4c1d6812
1 parent
5b2253e6
Exists in
master
增加登录与锁定验证
Showing
3 changed files
with
41 additions
and
0 deletions
Show diff stats
app/controller/user.js
| ... | ... | @@ -70,6 +70,35 @@ class UserController extends Controller { |
| 70 | 70 | this.ctx.body = { success: false, error }; |
| 71 | 71 | } |
| 72 | 72 | } |
| 73 | + async login() { | |
| 74 | + const ctx = this.ctx; | |
| 75 | + const data = ctx.request.body; | |
| 76 | + try { | |
| 77 | + const results = await ctx.service.user.login(data); | |
| 78 | + if (results.length > 0) { | |
| 79 | + const userInfo = results[0]; | |
| 80 | + this.ctx.body = { result: userInfo, success: true }; | |
| 81 | + } else { | |
| 82 | + this.ctx.body = { success: false, error: '用户名或密码不正确' }; | |
| 83 | + } | |
| 84 | + } catch (error) { | |
| 85 | + this.ctx.body = { success: false, error }; | |
| 86 | + } | |
| 87 | + } | |
| 88 | + async validate() { | |
| 89 | + const ctx = this.ctx; | |
| 90 | + const data = ctx.request.body; | |
| 91 | + try { | |
| 92 | + const results = await ctx.service.user.validate(data); | |
| 93 | + if (results.length > 0) { | |
| 94 | + this.ctx.body = { success: true }; | |
| 95 | + } else { | |
| 96 | + this.ctx.body = { success: false, error: '用户密码不正确' }; | |
| 97 | + } | |
| 98 | + } catch (error) { | |
| 99 | + this.ctx.body = { success: false, error }; | |
| 100 | + } | |
| 101 | + } | |
| 73 | 102 | } |
| 74 | 103 | |
| 75 | 104 | module.exports = UserController; | ... | ... |
app/router.js
| ... | ... | @@ -9,6 +9,8 @@ module.exports = app => { |
| 9 | 9 | // 首页统计 |
| 10 | 10 | router.get('/statistic/count', controller.statistic.count); |
| 11 | 11 | // 用户管理 |
| 12 | + router.post('/user/validate', controller.user.validate); | |
| 13 | + router.post('/user/login', controller.user.login); | |
| 12 | 14 | router.get('/user/list', controller.user.list); |
| 13 | 15 | router.get('/user/:id', controller.user.info); |
| 14 | 16 | router.post('/user', controller.user.add); | ... | ... |
app/service/user.js
| ... | ... | @@ -32,6 +32,16 @@ class UserService extends Service { |
| 32 | 32 | const result = await this.app.mysql.delete('user', data); |
| 33 | 33 | return result; |
| 34 | 34 | } |
| 35 | + async login(data) { | |
| 36 | + const { username, password } = data; | |
| 37 | + const result = await this.app.mysql.query('select name, username, role from user where username = ? and password = ?', [ username, password ]); | |
| 38 | + return result; | |
| 39 | + } | |
| 40 | + async validate(data) { | |
| 41 | + const { username, password } = data; | |
| 42 | + const result = await this.app.mysql.query('select id from user where username = ? and password = ?', [ username, password ]); | |
| 43 | + return result; | |
| 44 | + } | |
| 35 | 45 | } |
| 36 | 46 | |
| 37 | 47 | module.exports = UserService; | ... | ... |