From b42df8e9a2519ba9d044e15c2eeb0aba4a857fd6 Mon Sep 17 00:00:00 2001 From: Aaron.Liu <427787340@qq.com> Date: Wed, 25 Apr 2018 16:12:35 +0800 Subject: [PATCH] 增加用户修改信息和密码接口 --- app/controller/user.js | 15 +++++++++++++++ app/router.js | 1 + app/service/user.js | 7 ++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/controller/user.js b/app/controller/user.js index 230b5f6..c3317dc 100644 --- a/app/controller/user.js +++ b/app/controller/user.js @@ -61,6 +61,21 @@ class UserController extends Controller { this.ctx.body = { success: false, error }; } } + async password() { + const ctx = this.ctx; + const data = ctx.request.body; + try { + const result = await ctx.service.user.password(data); + const { affectedRows = 0 } = !result ? {} : result; + if (affectedRows > 0) { + this.ctx.body = { success: true }; + } else { + this.ctx.body = { success: false, error: '原密码错误' }; + } + } catch (error) { + this.ctx.body = { success: false, error }; + } + } async remove() { const ctx = this.ctx; try { diff --git a/app/router.js b/app/router.js index 5779f69..cb6fa48 100644 --- a/app/router.js +++ b/app/router.js @@ -9,6 +9,7 @@ module.exports = app => { // 首页统计 router.get('/statistic/count', controller.statistic.count); // 用户管理 + router.post('/user/password', controller.user.password); router.post('/user/validate', controller.user.validate); router.post('/user/login', controller.user.login); router.get('/user/list', controller.user.list); diff --git a/app/service/user.js b/app/service/user.js index 791cf88..092e15d 100644 --- a/app/service/user.js +++ b/app/service/user.js @@ -34,7 +34,7 @@ class UserService extends Service { } 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 ]); + const result = await this.app.mysql.query('select id, name, username, role from user where username = ? and password = ?', [ username, password ]); return result; } async validate(data) { @@ -42,6 +42,11 @@ class UserService extends Service { const result = await this.app.mysql.query('select id from user where username = ? and password = ?', [ username, password ]); return result; } + async password(data) { + const { id, oldPassword, newPassword } = data; + const result = await this.app.mysql.query('update user set password = ? where id = ? and password = ?', [ newPassword, id, oldPassword ]); + return result; + } } module.exports = UserService; -- libgit2 0.21.0