wip
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app;
|
||||
@@ -6,12 +7,15 @@ namespace app;
|
||||
use think\App;
|
||||
use think\exception\ValidateException;
|
||||
use think\Validate;
|
||||
use app\common\Trait\ApiResponse;
|
||||
|
||||
/**
|
||||
* 控制器基础类
|
||||
*/
|
||||
abstract class BaseController
|
||||
{
|
||||
use ApiResponse;
|
||||
|
||||
/**
|
||||
* Request实例
|
||||
* @var \think\Request
|
||||
@@ -51,8 +55,7 @@ abstract class BaseController
|
||||
}
|
||||
|
||||
// 初始化
|
||||
protected function initialize()
|
||||
{}
|
||||
protected function initialize() {}
|
||||
|
||||
/**
|
||||
* 验证数据
|
||||
@@ -90,5 +93,4 @@ abstract class BaseController
|
||||
|
||||
return $v->failException(true)->check($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace app;
|
||||
|
||||
use think\db\exception\DataNotFoundException;
|
||||
@@ -9,12 +10,15 @@ use think\exception\HttpResponseException;
|
||||
use think\exception\ValidateException;
|
||||
use think\Response;
|
||||
use Throwable;
|
||||
use app\common\Trait\ApiResponse;
|
||||
|
||||
/**
|
||||
* 应用异常处理类
|
||||
*/
|
||||
class ExceptionHandle extends Handle
|
||||
{
|
||||
use ApiResponse;
|
||||
|
||||
/**
|
||||
* 不需要记录信息(日志)的异常类列表
|
||||
* @var array
|
||||
@@ -50,7 +54,20 @@ class ExceptionHandle extends Handle
|
||||
*/
|
||||
public function render($request, Throwable $e): Response
|
||||
{
|
||||
// 添加自定义异常处理机制
|
||||
// 验证器异常
|
||||
if ($e instanceof ValidateException) {
|
||||
return $this->error($e->getError(), 422); // 验证器没通过
|
||||
};
|
||||
|
||||
// 请求异常
|
||||
if ($e instanceof HttpException) {
|
||||
return $this->error($e->getMessage(), $e->getStatusCode());
|
||||
};
|
||||
|
||||
// 处理【自定义业务异常】(在 Service 层主动 throw 的错误)
|
||||
if ($e instanceof \think\Exception) {
|
||||
return $this->error($e->getMessage(), $e->getCode() ?: 400);
|
||||
}
|
||||
|
||||
// 其他错误交给系统处理
|
||||
return parent::render($request, $e);
|
||||
|
||||
@@ -13,9 +13,7 @@ class CompanyAndOperators extends BaseController
|
||||
*/
|
||||
public function getAllOperators(TicketService $ticketService)
|
||||
{
|
||||
|
||||
$result = $ticketService->getAllOperators();
|
||||
|
||||
return json(['code' => 200, 'msg' => '查询成功', 'data' => $result]);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,28 @@ use think\facade\Session;
|
||||
|
||||
class Auth
|
||||
{
|
||||
/**
|
||||
* 无需 admin 角色即可访问的路由名称(方法名小写)
|
||||
*/
|
||||
protected $publicActions = [];
|
||||
|
||||
/**
|
||||
* 需要 admin 角色才能访问的路由名称(方法名小写)
|
||||
*/
|
||||
protected $adminActions = [
|
||||
'deleteoperator',
|
||||
'deletecompany',
|
||||
'addcompany',
|
||||
'addoperator',
|
||||
'getallusers',
|
||||
'addaccount',
|
||||
'updateaccountstatus',
|
||||
'deleteaccount',
|
||||
'getexportdata',
|
||||
'deleteticket',
|
||||
'batchupdate',
|
||||
];
|
||||
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
if (!Session::has('user_info')) {
|
||||
@@ -13,9 +35,18 @@ class Auth
|
||||
}
|
||||
|
||||
$user = Session::get('user_info');
|
||||
$role = $user['role'] ?? 'user';
|
||||
|
||||
$request->login_user_id = $user['id'] ?? null;
|
||||
$request->login_role = $user['role'] ?? null;
|
||||
$request->login_role = $role;
|
||||
|
||||
// 获取当前请求的路由方法名
|
||||
$action = strtolower($request->action());
|
||||
|
||||
// admin 专用接口
|
||||
if (in_array($action, $this->adminActions) && $role !== 'admin') {
|
||||
return json(['code' => 403, 'msg' => '权限不足,仅管理员可执行此操作']);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use think\facade\Route;
|
||||
use app\admin\middleware\Auth;
|
||||
use app\middleware\CheckLogin;
|
||||
|
||||
Route::group(function () {
|
||||
Route::get('ticketsfind', 'Ticket/ticketsFind');
|
||||
@@ -11,7 +12,6 @@ Route::group(function () {
|
||||
Route::put('tickets/batch-update', 'Ticket/batchUpdate');
|
||||
Route::get('getticketdashboardstats', 'Ticket/getTicketDashboardStats');
|
||||
|
||||
|
||||
Route::get('getUserStaffList', 'CompanyAndOperators/getStaffList');
|
||||
Route::get('operators', 'CompanyAndOperators/getAllOperators');
|
||||
Route::get('getOperatorList', 'CompanyAndOperators/TestgetAllOperators');
|
||||
@@ -26,4 +26,4 @@ Route::group(function () {
|
||||
Route::delete('deleteAccount/:id', 'AccountManagement/deleteAccount');
|
||||
|
||||
Route::post('getExportData', 'Utill/getExportData');
|
||||
})->middleware(Auth::class);
|
||||
})->middleware([CheckLogin::class, Auth::class]);
|
||||
|
||||
@@ -1,66 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\Trait;
|
||||
|
||||
use think\Response;
|
||||
use think\response\Json;
|
||||
|
||||
/**
|
||||
* API 响应 Trait
|
||||
* 提供统一的 JSON 响应格式
|
||||
*/
|
||||
trait ApiResponse
|
||||
{
|
||||
/**
|
||||
* 成功响应
|
||||
*
|
||||
* @param mixed $data 响应数据
|
||||
* @param string $msg 响应消息
|
||||
* @param int $code 响应码
|
||||
* @param int $httpCode HTTP状态码
|
||||
* @return Response
|
||||
* @param string $message 提示信息
|
||||
* @param mixed $data 返回数据
|
||||
* @param int $code 自定义业务状态码
|
||||
* @return Json
|
||||
*/
|
||||
protected function successResponse($data = [], $msg = '成功', $code = 200, $httpCode = 200)
|
||||
protected function success(int $code = 200, string $message = '操作成功', mixed $data = []): Json
|
||||
{
|
||||
return json([
|
||||
'code' => $code,
|
||||
'msg' => $msg,
|
||||
'data' => $data
|
||||
], $httpCode);
|
||||
return $this->jsonResponse($code, $message, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 错误响应
|
||||
*
|
||||
* @param string $msg 错误消息
|
||||
* @param mixed $data 错误数据
|
||||
* @param int $code 错误码
|
||||
* @param int $httpCode HTTP状态码
|
||||
* @return Response
|
||||
* 失败响应
|
||||
* @param string $message 错误提示信息
|
||||
* @param int $code 自定义业务状态码
|
||||
* @param mixed $data 额外的错误数据(如验证未通过的具体字段)
|
||||
* @return Json
|
||||
*/
|
||||
protected function errorResponse($msg = '失败', $data = null, $code = 400, $httpCode = 400)
|
||||
protected function error(int $code = 400, string $message = '操作失败', mixed $data = []): Json
|
||||
{
|
||||
return json([
|
||||
'code' => $code,
|
||||
'msg' => $msg,
|
||||
'data' => $data
|
||||
], $httpCode);
|
||||
return $this->jsonResponse($code, $message, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义响应
|
||||
*
|
||||
* @param int $code 响应码
|
||||
* @param string $msg 响应消息
|
||||
* @param mixed $data 响应数据
|
||||
* @param int $httpCode HTTP状态码
|
||||
* @return Response
|
||||
* 统一 JSON 返回格式
|
||||
*/
|
||||
protected function customResponse($code, $msg, $data = [], $httpCode = 200)
|
||||
private function jsonResponse(int $code, string $message, mixed $data): Json
|
||||
{
|
||||
return json([
|
||||
$result = [
|
||||
'code' => $code,
|
||||
'msg' => $msg,
|
||||
'data' => $data
|
||||
], $httpCode);
|
||||
'msg' => $message,
|
||||
'data' => $data,
|
||||
];
|
||||
|
||||
return json($result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ use think\facade\Session;
|
||||
// 已完成
|
||||
class Login extends BaseController
|
||||
{
|
||||
|
||||
/*
|
||||
处理登录请求(已完成)
|
||||
*/
|
||||
@@ -20,14 +19,14 @@ class Login extends BaseController
|
||||
|
||||
// 验证数据
|
||||
if (empty($params['username']) || empty($params['password'])) {
|
||||
return json(['code' => 400, 'message' => '用户名和密码不能为空']);
|
||||
return $this->error(400, '用户名和密码不能为空');
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $authService->login($params['username'], $params['password']);
|
||||
return json(['code' => 200, 'message' => '登录成功', 'data' => $result]);
|
||||
return $this->success(200, '登录成功', $result);
|
||||
} catch (\Exception $e) {
|
||||
return json(['code' => 400, 'message' => $e->getMessage(), 'data' => []]);
|
||||
return $this->error(400, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +36,6 @@ class Login extends BaseController
|
||||
public function logout()
|
||||
{
|
||||
Session::clear();
|
||||
return json(['code' => 200, 'message' => '退出成功']);
|
||||
return $this->success(200, '退出成功');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ class CheckLogin
|
||||
{
|
||||
/**
|
||||
* 处理请求
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @param \Closure $next
|
||||
* @return Response
|
||||
@@ -17,7 +16,6 @@ class CheckLogin
|
||||
if (!Session::has('user_info')) {
|
||||
return json(['code' => 401, 'message' => '未登录']);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,23 +58,14 @@ return [
|
||||
'fields_cache' => false,
|
||||
],
|
||||
'remote' => [
|
||||
// 数据库类型
|
||||
'type' => env('REMOTE_DB_TYPE', 'mysql'),
|
||||
// 服务器地址
|
||||
'hostname' => env('REMOTE_DB_HOST', '127.0.0.1'),
|
||||
// 数据库名
|
||||
'database' => env('REMOTE_DB_NAME', ''),
|
||||
// 用户名
|
||||
'username' => env('REMOTE_DB_USER', 'root'),
|
||||
// 密码
|
||||
'password' => env('REMOTE_DB_PASS', ''),
|
||||
// 端口
|
||||
'hostport' => env('REMOTE_DB_PORT', '3306'),
|
||||
// 数据库编码
|
||||
'charset' => env('REMOTE_DB_CHARSET', 'utf8mb4'),
|
||||
// 数据库表前缀
|
||||
'type' => 'mysql',
|
||||
'hostname' => '112.5.15.136',
|
||||
'database' => '112_5_15_136_255',
|
||||
'username' => '112_5_15_136_255',
|
||||
'password' => 'xz1YX1jswSWzkE4K',
|
||||
'hostport' => '3306',
|
||||
'charset' => 'utf8mb4',
|
||||
'prefix' => '',
|
||||
// 远程连接建议开启断线重连
|
||||
'break_reconnect' => true,
|
||||
],
|
||||
|
||||
|
||||
Reference in New Issue
Block a user