Commit b42df8e9a2519ba9d044e15c2eeb0aba4a857fd6

Authored by Aaron.Liu
1 parent ce1abbf8
Exists in master

增加用户修改信息和密码接口

app/controller/user.js
... ... @@ -61,6 +61,21 @@ class UserController extends Controller {
61 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 79 async remove() {
65 80 const ctx = this.ctx;
66 81 try {
... ...
app/router.js
... ... @@ -9,6 +9,7 @@ module.exports = app => {
9 9 // 首页统计
10 10 router.get('/statistic/count', controller.statistic.count);
11 11 // 用户管理
  12 + router.post('/user/password', controller.user.password);
12 13 router.post('/user/validate', controller.user.validate);
13 14 router.post('/user/login', controller.user.login);
14 15 router.get('/user/list', controller.user.list);
... ...
app/service/user.js
... ... @@ -34,7 +34,7 @@ class UserService extends Service {
34 34 }
35 35 async login(data) {
36 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 38 return result;
39 39 }
40 40 async validate(data) {
... ... @@ -42,6 +42,11 @@ class UserService extends Service {
42 42 const result = await this.app.mysql.query('select id from user where username = ? and password = ?', [ username, password ]);
43 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 52 module.exports = UserService;
... ...