Skip to content

Commit

Permalink
20201212
Browse files Browse the repository at this point in the history
添加账号管理接口
  • Loading branch information
kekodmc committed Dec 12, 2020
1 parent d3e7da9 commit cf1c76e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 2 deletions.
23 changes: 23 additions & 0 deletions app/controller/Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Created by PhpStorm.
* User: kekodmc
* Date: 2020/12/11
* Time: 11:25
*/

namespace app\controller;


use app\BaseController;
use app\service\AccountService;

class Account extends BaseController
{
public function index(){
return invoke(AccountService::class)->index($this->params);
}
public function update(){
return invoke(AccountService::class)->update($this->params);
}
}
6 changes: 5 additions & 1 deletion app/middleware/LoginAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)){
Expand Down
41 changes: 41 additions & 0 deletions app/service/AccountService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Created by PhpStorm.
* User: kekodmc
* Date: 2020/12/11
* Time: 18:08
*/

namespace app\service;


use app\model\AdminModel;
use app\validate\AccountValidate;
use think\exception\ValidateException;

class AccountService extends BaseService
{
public function index($params){
$data=AdminModel::find($params['uid']);
return successResponse('查询成功',[
'username'=>$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('保存成功');
}
}
1 change: 1 addition & 0 deletions app/service/LoginService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
40 changes: 40 additions & 0 deletions app/validate/AccountValidate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Created by PhpStorm.
* User: kekodmc
* Date: 2020/11/26
* Time: 10:41
*/

namespace app\validate;



class AccountValidate extends BaseValidate
{
/**
* 定义验证规则
* 格式:'字段名' => ['规则1','规则2'...]
*
* @var array
*/
protected $rule = [
'avatar'=>'require',
'password'=>'require',
];

/**
* 定义错误信息
* 格式:'字段名.规则名' => '错误信息'
*
* @var array
*/
protected $message = [
'avatar.require'=>'请上传头像',
'password.require'=>'请输入密码',
];

protected $scene = [
'update'=>['avatar'],
];
}
2 changes: 1 addition & 1 deletion route/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit cf1c76e

Please sign in to comment.