作者 Your Name

gx

... ... @@ -5,12 +5,10 @@ namespace App\Exceptions;
use App\Enums\Common\Code;
use App\Enums\Common\Common;
use App\Services\DingService;
use App\Traits\RedisTrait;
use App\Utils\EncryptUtils;
use App\Utils\LogUtils;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
... ... @@ -60,5 +58,94 @@ class Handler extends ExceptionHandler
*/
public function report(Throwable $exception)
{
//日志记录
$exceptionMessage = "错误CODE:" . $exception->getCode() .
"-----错误message:" . $exception->getMessage() .
'------错误文件:' . $exception->getFile() .
'-------错误行数:' . $exception->getLine();
//A端错误
if ($exception instanceof AsideGlobalException) {
LogUtils::error("AsideGlobalException", [], $exceptionMessage);
}
//B端错误
elseif($exception instanceof BsideGlobalException) {
LogUtils::error("BsideGlobalException", [], $exceptionMessage);
}
//验证错误(非手动抛出)
elseif ($exception instanceof ValidationException) {
LogUtils::error("参数验证失败", [], $exceptionMessage);
}
//Redis错误(非手动抛出)
elseif ($exception instanceof \RedisException) {
LogUtils::error("Redis服务异常", [], $exceptionMessage);
}
//Mysql错误(非手动抛出)
elseif ($exception instanceof \PDOException) {
LogUtils::error("数据库服务异常", [], $exceptionMessage);
}
//模型未找到错误(非手动抛出)
elseif ($exception instanceof ModelNotFoundException) {
LogUtils::error("模型资源未找到", [], $exceptionMessage);
}
//其他
else {
LogUtils::error("全局系统异常", [], $exceptionMessage);
}
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
$message = $exception->getMessage();
if ($exception instanceof AsideGlobalException) {
$code = $exception->getCode();
}elseif ($exception instanceof BsideGlobalException) {
$code = $exception->getCode();
} elseif ($exception instanceof ValidationException) {
$code = Code::USER_PARAMS_ERROE();
} elseif ($exception instanceof \RedisException) {
$code = Code::SERVER_REDIS_ERROR();
} elseif ($exception instanceof \PDOException) {
$code = Code::SERVER_MYSQL_ERROR();
} elseif ($exception instanceof ModelNotFoundException) {
$code = Code::USER_MODEL_NOTFOUND_ERROE();
} elseif ($exception instanceof NotFoundHttpException || $exception instanceof MethodNotAllowedHttpException) {
return response('404 Not Found', 404);
} else {
$code = Code::SYSTEM_ERROR();
}
//钉钉通知错误信息
if (in_array(config('app.env'), ['test', 'production']) && (in_array(substr($code, 0, 1), ['B', 'C']))) {
$exceptionMessage = "路由:" . Route::current()->uri .
"-----错误CODE:" . $exception->getCode() .
"-----错误message:" . $exception->getMessage() .
'------错误文件:' . $exception->getFile() . '-------错误行数:' .
$exception->getLine();
(new DingService())->handle(['keyword' => "ERROR", 'msg' => config('app.env') . '环境报错:' . $exceptionMessage, 'isAtAll' => false]);
}
//开启debug 错误原样输出
$debub = config('app.debug');
$message = $debub ? $message : ($code->description ?? $message);
$response = [
'code' => $code,
'message' => $message
];
//加密返回
if (config('app.params_encrypt')) {
$k = config('app.params_encrypt_key');
$i = config('app.params_encrypt_iv');
$response = [
'p' => (new EncryptUtils())->openssl_en($response, $k, $i)];
}
return response($response);
}
}
... ...
... ... @@ -42,7 +42,6 @@ class BaseController extends Controller
* @method
*/
public function auth_token(){
$this->token = '810f28c1d80d0ccf27509816bf44b329';
$info = Cache::get($this->token);
if(!empty($info)){
$this->user = $info;
... ...
... ... @@ -60,7 +60,7 @@ class ComController extends BaseController
$info = $projectRoleModel->read(['id'=>$this->user['role_id']]);
$projectMenuModel = new ProjectMenuModel();
$info['role_menu'] = trim($info['role_menu'],',');
$lists = $this->where(['status'=>0,'is_role'=>0])->whereIn('id',explode(',',$info['role_menu']))->get();
$lists = $projectMenuModel->where(['status'=>0,'is_role'=>0])->whereIn('id',explode(',',$info['role_menu']))->get();
$lists = $lists->toArray();
$menu = array();
foreach ($lists as $k => $v){
... ...
... ... @@ -22,8 +22,6 @@ class LoginAuthMiddleware
public function handle(Request $request, Closure $next)
{
$token = $request->header('token');
var_dump(Cache::get($token));
die();
if(!isset($token) || empty($token)){
$this->response('当前用户未登录',Code::USER_ERROR);
}
... ... @@ -39,8 +37,10 @@ class LoginAuthMiddleware
//查询当前用户是否拥有权限操作
$projectMenuModel = new ProjectMenu();
$menu_id = $projectMenuModel->read(['action'=>$action['as']],['id']);
if(strpos($role_info['role_menu'], $menu_id['id']) < 0){
$this->response('当前用户没有权限',Code::USER_ERROR);
if($menu_id !== false){
if(strpos($role_info['role_menu'], $menu_id['id']) < 0){
$this->response('当前用户没有权限',Code::USER_ERROR);
}
}
return $next($request);
}
... ...
... ... @@ -83,6 +83,7 @@ class User extends Base
//生成新token
$token = md5(uniqid().$info['id']);
//存储缓存
$info['token'] = $token;
Cache::add($token,$info);
$rs = $this->edit(['token'=>$token],['id'=>$info['id']]);
if($rs === false){
... ...