Commit b42df8e9a2519ba9d044e15c2eeb0aba4a857fd6
1 parent
ce1abbf8
Exists in
master
增加用户修改信息和密码接口
Showing
3 changed files
with
22 additions
and
1 deletions
Show diff stats
app/controller/user.js
| @@ -61,6 +61,21 @@ class UserController extends Controller { | @@ -61,6 +61,21 @@ class UserController extends Controller { | ||
| 61 | this.ctx.body = { success: false, error }; | 61 | this.ctx.body = { success: false, error }; |
| 62 | } | 62 | } |
| 63 | } | 63 | } |
| 64 | + async password() { | ||
| 65 | + const ctx = this.ctx; | ||
| 66 | + const data = ctx.request.body; | ||
| 67 | + try { | ||
| 68 | + const result = await ctx.service.user.password(data); | ||
| 69 | + const { affectedRows = 0 } = !result ? {} : result; | ||
| 70 | + if (affectedRows > 0) { | ||
| 71 | + this.ctx.body = { success: true }; | ||
| 72 | + } else { | ||
| 73 | + this.ctx.body = { success: false, error: '原密码错误' }; | ||
| 74 | + } | ||
| 75 | + } catch (error) { | ||
| 76 | + this.ctx.body = { success: false, error }; | ||
| 77 | + } | ||
| 78 | + } | ||
| 64 | async remove() { | 79 | async remove() { |
| 65 | const ctx = this.ctx; | 80 | const ctx = this.ctx; |
| 66 | try { | 81 | try { |
app/router.js
| @@ -9,6 +9,7 @@ module.exports = app => { | @@ -9,6 +9,7 @@ module.exports = app => { | ||
| 9 | // 首页统计 | 9 | // 首页统计 |
| 10 | router.get('/statistic/count', controller.statistic.count); | 10 | router.get('/statistic/count', controller.statistic.count); |
| 11 | // 用户管理 | 11 | // 用户管理 |
| 12 | + router.post('/user/password', controller.user.password); | ||
| 12 | router.post('/user/validate', controller.user.validate); | 13 | router.post('/user/validate', controller.user.validate); |
| 13 | router.post('/user/login', controller.user.login); | 14 | router.post('/user/login', controller.user.login); |
| 14 | router.get('/user/list', controller.user.list); | 15 | router.get('/user/list', controller.user.list); |
app/service/user.js
| @@ -34,7 +34,7 @@ class UserService extends Service { | @@ -34,7 +34,7 @@ class UserService extends Service { | ||
| 34 | } | 34 | } |
| 35 | async login(data) { | 35 | async login(data) { |
| 36 | const { username, password } = 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 ]); | 37 | + const result = await this.app.mysql.query('select id, name, username, role from user where username = ? and password = ?', [ username, password ]); |
| 38 | return result; | 38 | return result; |
| 39 | } | 39 | } |
| 40 | async validate(data) { | 40 | async validate(data) { |
| @@ -42,6 +42,11 @@ class UserService extends Service { | @@ -42,6 +42,11 @@ class UserService extends Service { | ||
| 42 | const result = await this.app.mysql.query('select id from user where username = ? and password = ?', [ username, password ]); | 42 | const result = await this.app.mysql.query('select id from user where username = ? and password = ?', [ username, password ]); |
| 43 | return result; | 43 | return result; |
| 44 | } | 44 | } |
| 45 | + async password(data) { | ||
| 46 | + const { id, oldPassword, newPassword } = data; | ||
| 47 | + const result = await this.app.mysql.query('update user set password = ? where id = ? and password = ?', [ newPassword, id, oldPassword ]); | ||
| 48 | + return result; | ||
| 49 | + } | ||
| 45 | } | 50 | } |
| 46 | 51 | ||
| 47 | module.exports = UserService; | 52 | module.exports = UserService; |