From cf1c76e35e101b4bcbeab878bdd541fc784bc7c1 Mon Sep 17 00:00:00 2001 From: keko Date: Sat, 12 Dec 2020 14:20:30 +0800 Subject: [PATCH] 20201212 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加账号管理接口 --- app/controller/Account.php | 23 ++++++++++++++++++ app/middleware/LoginAuth.php | 6 ++++- app/service/AccountService.php | 41 ++++++++++++++++++++++++++++++++ app/service/LoginService.php | 1 + app/validate/AccountValidate.php | 40 +++++++++++++++++++++++++++++++ route/app.php | 2 +- 6 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 app/controller/Account.php create mode 100644 app/service/AccountService.php create mode 100644 app/validate/AccountValidate.php diff --git a/app/controller/Account.php b/app/controller/Account.php new file mode 100644 index 0000000..ee01517 --- /dev/null +++ b/app/controller/Account.php @@ -0,0 +1,23 @@ +index($this->params); + } + public function update(){ + return invoke(AccountService::class)->update($this->params); + } +} \ No newline at end of file diff --git a/app/middleware/LoginAuth.php b/app/middleware/LoginAuth.php index b007ae3..b98697c 100644 --- a/app/middleware/LoginAuth.php +++ b/app/middleware/LoginAuth.php @@ -39,11 +39,15 @@ protected function logout(){ return errorResponse('请重新登录',401); } //检查权限 + protected $safeList=[ + 'login-logout', + 'account-update', + ];//白名单 protected function checkPower($power,Request $request){ $c=$request->controller(true); $a=$request->action(); $url="$c-$a"; - if($url=='login-logout'){ + if(in_array($url,$this->safeList)){ return true; } if(in_array($url,$power)){ diff --git a/app/service/AccountService.php b/app/service/AccountService.php new file mode 100644 index 0000000..523ea9a --- /dev/null +++ b/app/service/AccountService.php @@ -0,0 +1,41 @@ +$data->username, + 'avatar'=>$data->avatar, + ]); + } + public function update($params){ + try { + validate(AccountValidate::class)->scene('update')->check($params); + } catch (ValidateException $e) { + // 验证失败 输出错误信息 + return errorResponse($e->getError()); + } + $password=$params['password']??''; + $data=AdminModel::find($params['uid']); + $data->avatar=$params['avatar']; + if($password){ + $data->password=password_hash($password,PASSWORD_BCRYPT,['code'=>9527]); + } + $data->save(); + return successResponse('保存成功'); + } +} \ No newline at end of file diff --git a/app/service/LoginService.php b/app/service/LoginService.php index b3b86d2..61705cb 100644 --- a/app/service/LoginService.php +++ b/app/service/LoginService.php @@ -54,6 +54,7 @@ public function login($params){ $data->save(); $result['token']=$token; $result['avatar']=$data->avatar; + $result['role_name']=$data->role->name; return successResponse('登录成功',$result); } public function logout($params){ diff --git a/app/validate/AccountValidate.php b/app/validate/AccountValidate.php new file mode 100644 index 0000000..2a39f62 --- /dev/null +++ b/app/validate/AccountValidate.php @@ -0,0 +1,40 @@ + ['规则1','规则2'...] + * + * @var array + */ + protected $rule = [ + 'avatar'=>'require', + 'password'=>'require', + ]; + + /** + * 定义错误信息 + * 格式:'字段名.规则名' => '错误信息' + * + * @var array + */ + protected $message = [ + 'avatar.require'=>'请上传头像', + 'password.require'=>'请输入密码', + ]; + + protected $scene = [ + 'update'=>['avatar'], + ]; +} \ No newline at end of file diff --git a/route/app.php b/route/app.php index 8138a21..bf9a48e 100644 --- a/route/app.php +++ b/route/app.php @@ -11,13 +11,13 @@ use think\facade\Route; Route::group('api',function (){ - Route::get('getLotteryPksInfo','Index/api'); Route::post('login','Login/login'); Route::group(function (){ Route::post('logout','Login/logout'); Route::get('admin/role','Admin/role'); Route::post('admin/disable','Admin/disable'); Route::resource('admin','Admin'); + Route::resource('account','Account'); Route::get('power/mod','Power/mod'); Route::resource('power','Power'); Route::get('role/power','Role/power');