Merge branch 'feature/mzl_dev' into develop
# Conflicts: # app/common/controller/AuthController.php # app/common/logic/AuthLogic.php # app/common/model/User.php # app/controller/IndexController.php # app/functions.php # app/middleware/AuthCheck.php # app/view/index/qrcode.html # composer.json # composer.lock # config/middleware.php # config/route.php # config/whitelist.php
This commit is contained in:
@@ -1,83 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use League\OAuth2\Server\AuthorizationServer;
|
||||
use League\OAuth2\Server\ResourceServer;
|
||||
use GuzzleHttp\Psr7\Response as Psr7Response;
|
||||
use app\service\AuthService;
|
||||
use app\model\User;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
|
||||
class AuthController_bak
|
||||
{
|
||||
|
||||
protected AuthorizationServer $authServer;
|
||||
protected ResourceServer $resourceServer;
|
||||
|
||||
public function __construct(AuthorizationServer $auth, ResourceServer $res)
|
||||
{
|
||||
$this->authServer = $auth;
|
||||
$this->resourceServer = $res;
|
||||
}
|
||||
|
||||
// 浏览器授权页
|
||||
public function authorize(ServerRequestInterface $request)
|
||||
{
|
||||
// 如果未登录,跳转登录页(自行实现)
|
||||
// 登录后,确认授权 -> issue auth code -> 重定向回 client
|
||||
|
||||
// …未登录重定向到登录页,登录后调用:
|
||||
// $authRequest = $this->authServer->validateAuthorizationRequest($request);
|
||||
// …用户同意授权…
|
||||
// return $this->authServer->completeAuthorizationRequest($authRequest, new Psr7Response());
|
||||
}
|
||||
|
||||
// 授权码换 Token
|
||||
public function token(ServerRequestInterface $request)
|
||||
{
|
||||
return $this->authServer->respondToAccessTokenRequest($request, new Psr7Response());
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
public function userinfo(ServerRequestInterface $request)
|
||||
{
|
||||
$psr = $this->resourceServer->validateAuthenticatedRequest($request);
|
||||
$userId = $psr->getAttribute('oauth_user_id');
|
||||
// 根据 $userId 查询用户表返回
|
||||
// return json(['id' => $userId, 'username' => 'demo']);
|
||||
|
||||
// 用 Webman 的 support\Response 返回 JSON 更方便
|
||||
return json([
|
||||
'id' => $userId,
|
||||
'username' => User::find($userId)->username,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function info(Request $request)
|
||||
{
|
||||
$userId = $request->session()->get('user_id');
|
||||
if (!$userId) {
|
||||
return json(['code'=>401,'msg'=>'未登录']);
|
||||
}
|
||||
|
||||
$user = User::with('roles')->find($userId);
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'data' => [
|
||||
'user' => [
|
||||
'id' => $user->id,
|
||||
'username' => $user->username,
|
||||
'roles' => $user->roles->pluck('name')->all(),
|
||||
],
|
||||
'menus' => AuthService::getMenuTree($userId),
|
||||
'permissions' => AuthService::getPermissionCodes($userId),
|
||||
],
|
||||
'msg' => 'ok',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\validate\RoleValidate;
|
||||
use app\common\service\RoleService;
|
||||
use support\Request;
|
||||
|
||||
class RoleController extends \app\common\controller\BaseController
|
||||
{
|
||||
/**
|
||||
* 获取角色列表
|
||||
*/
|
||||
public function list(Request $request)
|
||||
{
|
||||
try {
|
||||
$params = $request->all();
|
||||
$result = (new RoleService())->getRoleList($params);
|
||||
// 定义后处理器
|
||||
$postProcessor = function($formattedData) {
|
||||
if (isset($formattedData['created_at']))
|
||||
$formattedData['created_at'] = date('Y-m-d H:i:s', $formattedData['created_at']);
|
||||
if (isset($formattedData['updated_at']))
|
||||
$formattedData['updated_at'] = date('Y-m-d H:i:s', $formattedData['updated_at']);
|
||||
return $formattedData;
|
||||
};
|
||||
return $this->success($this->transformData($result, $postProcessor), '角色列表获取成功');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色详情
|
||||
*/
|
||||
public function detail(Request $request)
|
||||
{
|
||||
try {
|
||||
$roleId = $request->get('role_id');
|
||||
if (!$roleId) {
|
||||
return $this->error('缺少角色ID', 422);
|
||||
}
|
||||
$result = (new RoleService())->getRoleDetail($roleId);
|
||||
return $this->success($result, '角色详情获取成功');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加角色
|
||||
*/
|
||||
public function create(Request $request)
|
||||
{
|
||||
try {
|
||||
$data = $this->validate(new RoleValidate(), $request->post(), 'role_create');
|
||||
$result = (new RoleService())->createRole($data);
|
||||
return $this->success($result, '角色创建成功');
|
||||
} catch (\think\exception\ValidateException $e) {
|
||||
return $this->error($e->getError(), 422);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改角色
|
||||
*/
|
||||
public function update(Request $request)
|
||||
{
|
||||
try {
|
||||
$data = $this->validate(new RoleValidate(), $request->post(), 'role_update');
|
||||
$result = (new RoleService())->updateRole($data);
|
||||
return $this->success($result, '角色更新成功');
|
||||
} catch (\think\exception\ValidateException $e) {
|
||||
return $this->error($e->getError(), 422);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*/
|
||||
public function delete(Request $request)
|
||||
{
|
||||
try {
|
||||
$roleId = $request->get('role_id');
|
||||
if (!$roleId) {
|
||||
return $this->error('缺少角色ID', 422);
|
||||
}
|
||||
(new RoleService())->deleteRole($roleId);
|
||||
return $this->success([], '角色删除成功');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user