mirror of
https://gitee.com/technical-laohu/mpay_v2_webman.git
synced 2026-04-22 01:54:25 +08:00
1. 调整异常处理类
2. 统一职责分工 3. 清除多余代码
This commit is contained in:
@@ -27,12 +27,8 @@ class AuthController extends BaseController
|
||||
*/
|
||||
public function captcha(Request $request)
|
||||
{
|
||||
try {
|
||||
$data = $this->captchaService->generate();
|
||||
return $this->success($data);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->fail('验证码生成失败:' . $e->getMessage(), 500);
|
||||
}
|
||||
$data = $this->captchaService->generate();
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,14 +48,8 @@ class AuthController extends BaseController
|
||||
return $this->fail('请填写完整登录信息', 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$data = $this->authService->login($username, $password, $verifyCode, $captchaId);
|
||||
return $this->success($data);
|
||||
} catch (\RuntimeException $e) {
|
||||
return $this->fail($e->getMessage(), $e->getCode() ?: 500);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->fail('登录失败:' . $e->getMessage(), 500);
|
||||
}
|
||||
$data = $this->authService->login($username, $password, $verifyCode, $captchaId);
|
||||
return $this->success($data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace app\http\admin\controller;
|
||||
|
||||
use app\common\base\BaseController;
|
||||
use app\services\MenuService;
|
||||
use support\Request;
|
||||
|
||||
/**
|
||||
@@ -10,44 +11,15 @@ use support\Request;
|
||||
*/
|
||||
class MenuController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
protected MenuService $menuService
|
||||
) {
|
||||
}
|
||||
|
||||
public function getRouters()
|
||||
{
|
||||
// 获取菜单数据并转换为树形结构
|
||||
$routers = $this->buildMenuTree($this->getSystemMenu());
|
||||
|
||||
$routers = $this->menuService->getRouters();
|
||||
return $this->success($routers);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统菜单数据
|
||||
* 从配置文件读取
|
||||
*/
|
||||
private function getSystemMenu(): array
|
||||
{
|
||||
return config('menu', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建菜单树形结构
|
||||
*/
|
||||
private function buildMenuTree(array $menus, string $parentId = '0'): array
|
||||
{
|
||||
$tree = [];
|
||||
|
||||
foreach ($menus as $menu) {
|
||||
if (($menu['parentId'] ?? '0') === $parentId) {
|
||||
$children = $this->buildMenuTree($menus, $menu['id']);
|
||||
$menu['children'] = !empty($children) ? $children : null;
|
||||
$tree[] = $menu;
|
||||
}
|
||||
}
|
||||
|
||||
// 按 sort 排序
|
||||
usort($tree, function ($a, $b) {
|
||||
return ($a['meta']['sort'] ?? 0) <=> ($b['meta']['sort'] ?? 0);
|
||||
});
|
||||
|
||||
return $tree;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace app\http\admin\controller;
|
||||
|
||||
use app\common\base\BaseController;
|
||||
use app\services\SystemSettingService;
|
||||
use support\Request;
|
||||
|
||||
/**
|
||||
@@ -10,6 +11,10 @@ use support\Request;
|
||||
*/
|
||||
class SystemController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
protected SystemSettingService $settingService
|
||||
) {
|
||||
}
|
||||
/**
|
||||
* GET /system/getDict
|
||||
* GET /system/getDict/{code}
|
||||
@@ -24,23 +29,50 @@ class SystemController extends BaseController
|
||||
*/
|
||||
public function getDict(Request $request, string $code = '')
|
||||
{
|
||||
// 获取所有字典数据
|
||||
$allDicts = config('dict', []);
|
||||
|
||||
// 如果指定了 code,则只返回对应的字典
|
||||
if (!empty($code)) {
|
||||
// 将数组转换为以 code 为键的关联数组,便于快速查找
|
||||
$dictsByCode = array_column($allDicts, null, 'code');
|
||||
$dict = $dictsByCode[$code] ?? null;
|
||||
|
||||
if ($dict === null) {
|
||||
return $this->fail('未找到指定的字典:' . $code, 404);
|
||||
}
|
||||
return $this->success($dict);
|
||||
$data = $this->settingService->getDict($code);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /system/base-config/tabs
|
||||
*
|
||||
* 获取所有Tab配置
|
||||
* 由 SystemSettingService 负责读取配置和缓存
|
||||
*/
|
||||
public function getTabsConfig()
|
||||
{
|
||||
$tabs = $this->settingService->getTabs();
|
||||
return $this->success($tabs);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /system/base-config/form/{tabKey}
|
||||
*
|
||||
* 获取指定Tab的表单配置
|
||||
* 从 SystemSettingService 获取合并后的配置
|
||||
*/
|
||||
public function getFormConfig(Request $request, string $tabKey)
|
||||
{
|
||||
$formConfig = $this->settingService->getFormConfig($tabKey);
|
||||
return $this->success($formConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /system/base-config/submit/{tabKey}
|
||||
*
|
||||
* 提交表单数据
|
||||
* 接收表单数据,直接使用字段名(fieldName)作为 config_key 保存到数据库
|
||||
*/
|
||||
public function submitConfig(Request $request, string $tabKey)
|
||||
{
|
||||
$formData = $request->post();
|
||||
|
||||
if (empty($formData)) {
|
||||
return $this->fail('提交数据不能为空', 400);
|
||||
}
|
||||
|
||||
// 返回所有字典
|
||||
return $this->success($allDicts);
|
||||
|
||||
$this->settingService->saveFormConfig($tabKey, $formData);
|
||||
return $this->success(null, '保存成功');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,12 +33,8 @@ class UserController extends BaseController
|
||||
return $this->fail('未获取到用户信息,请先登录', 401);
|
||||
}
|
||||
|
||||
try {
|
||||
$data = $this->userService->getUserInfoById($userId);
|
||||
return $this->success($data);
|
||||
} catch (\RuntimeException $e) {
|
||||
return $this->fail($e->getMessage(), $e->getCode() ?: 500);
|
||||
}
|
||||
$data = $this->userService->getUserInfoById($userId);
|
||||
return $this->success($data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
namespace app\http\admin\middleware;
|
||||
|
||||
use Webman\MiddlewareInterface;
|
||||
use Webman\Http\Response;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
use app\common\utils\JwtUtil;
|
||||
use app\exceptions\UnauthorizedException;
|
||||
|
||||
/**
|
||||
* JWT 认证中间件
|
||||
@@ -25,7 +26,7 @@ class AuthMiddleware implements MiddlewareInterface
|
||||
// 从请求头中获取 token
|
||||
$auth = $request->header('Authorization', '');
|
||||
if (!$auth) {
|
||||
return $this->unauthorized('缺少认证令牌');
|
||||
throw new UnauthorizedException('缺少认证令牌');
|
||||
}
|
||||
|
||||
// 兼容 "Bearer xxx" 或直接 "xxx"
|
||||
@@ -36,7 +37,7 @@ class AuthMiddleware implements MiddlewareInterface
|
||||
}
|
||||
|
||||
if (!$token) {
|
||||
return $this->unauthorized('认证令牌格式错误');
|
||||
throw new UnauthorizedException('认证令牌格式错误');
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -44,7 +45,7 @@ class AuthMiddleware implements MiddlewareInterface
|
||||
$payload = JwtUtil::parseToken($token);
|
||||
|
||||
if (empty($payload) || !isset($payload['user_id'])) {
|
||||
return $this->unauthorized('认证令牌无效');
|
||||
throw new UnauthorizedException('认证令牌无效');
|
||||
}
|
||||
|
||||
// 将用户信息存储到请求对象中,供控制器使用
|
||||
@@ -53,29 +54,20 @@ class AuthMiddleware implements MiddlewareInterface
|
||||
|
||||
// 继续处理请求
|
||||
return $handler($request);
|
||||
} catch (UnauthorizedException $e) {
|
||||
// 重新抛出业务异常,让框架处理
|
||||
throw $e;
|
||||
} catch (\Throwable $e) {
|
||||
// 根据异常类型返回不同的错误信息
|
||||
$message = $e->getMessage();
|
||||
if (str_contains($message, 'expired') || str_contains($message, 'Expired')) {
|
||||
return $this->unauthorized('认证令牌已过期', 401);
|
||||
throw new UnauthorizedException('认证令牌已过期');
|
||||
} elseif (str_contains($message, 'signature') || str_contains($message, 'Signature')) {
|
||||
return $this->unauthorized('认证令牌签名无效', 401);
|
||||
throw new UnauthorizedException('认证令牌签名无效');
|
||||
} else {
|
||||
return $this->unauthorized('认证令牌验证失败:' . $message, 401);
|
||||
throw new UnauthorizedException('认证令牌验证失败:' . $message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回未授权响应
|
||||
*/
|
||||
private function unauthorized(string $message, int $code = 401): Response
|
||||
{
|
||||
return json([
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
'data' => null,
|
||||
], $code);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user