作者 Your Name

gx

@@ -5,12 +5,10 @@ namespace App\Exceptions; @@ -5,12 +5,10 @@ namespace App\Exceptions;
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Enums\Common\Common; 6 use App\Enums\Common\Common;
7 use App\Services\DingService; 7 use App\Services\DingService;
8 -use App\Traits\RedisTrait;  
9 use App\Utils\EncryptUtils; 8 use App\Utils\EncryptUtils;
10 use App\Utils\LogUtils; 9 use App\Utils\LogUtils;
11 use Illuminate\Database\Eloquent\ModelNotFoundException; 10 use Illuminate\Database\Eloquent\ModelNotFoundException;
12 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; 11 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
13 -use Illuminate\Support\Arr;  
14 use Illuminate\Support\Facades\Route; 12 use Illuminate\Support\Facades\Route;
15 use Illuminate\Validation\ValidationException; 13 use Illuminate\Validation\ValidationException;
16 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; 14 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
@@ -60,5 +58,94 @@ class Handler extends ExceptionHandler @@ -60,5 +58,94 @@ class Handler extends ExceptionHandler
60 */ 58 */
61 public function report(Throwable $exception) 59 public function report(Throwable $exception)
62 { 60 {
  61 +
  62 + //日志记录
  63 + $exceptionMessage = "错误CODE:" . $exception->getCode() .
  64 + "-----错误message:" . $exception->getMessage() .
  65 + '------错误文件:' . $exception->getFile() .
  66 + '-------错误行数:' . $exception->getLine();
  67 + //A端错误
  68 + if ($exception instanceof AsideGlobalException) {
  69 + LogUtils::error("AsideGlobalException", [], $exceptionMessage);
  70 + }
  71 + //B端错误
  72 + elseif($exception instanceof BsideGlobalException) {
  73 + LogUtils::error("BsideGlobalException", [], $exceptionMessage);
  74 + }
  75 + //验证错误(非手动抛出)
  76 + elseif ($exception instanceof ValidationException) {
  77 + LogUtils::error("参数验证失败", [], $exceptionMessage);
  78 + }
  79 + //Redis错误(非手动抛出)
  80 + elseif ($exception instanceof \RedisException) {
  81 + LogUtils::error("Redis服务异常", [], $exceptionMessage);
  82 + }
  83 + //Mysql错误(非手动抛出)
  84 + elseif ($exception instanceof \PDOException) {
  85 + LogUtils::error("数据库服务异常", [], $exceptionMessage);
  86 + }
  87 + //模型未找到错误(非手动抛出)
  88 + elseif ($exception instanceof ModelNotFoundException) {
  89 + LogUtils::error("模型资源未找到", [], $exceptionMessage);
  90 + }
  91 + //其他
  92 + else {
  93 + LogUtils::error("全局系统异常", [], $exceptionMessage);
  94 + }
  95 + }
  96 + /**
  97 + * Render an exception into an HTTP response.
  98 + *
  99 + * @param \Illuminate\Http\Request $request
  100 + * @param \Throwable $exception
  101 + * @return \Symfony\Component\HttpFoundation\Response
  102 + *
  103 + * @throws \Throwable
  104 + */
  105 + public function render($request, Throwable $exception)
  106 + {
  107 + $message = $exception->getMessage();
  108 +
  109 + if ($exception instanceof AsideGlobalException) {
  110 + $code = $exception->getCode();
  111 + }elseif ($exception instanceof BsideGlobalException) {
  112 + $code = $exception->getCode();
  113 + } elseif ($exception instanceof ValidationException) {
  114 + $code = Code::USER_PARAMS_ERROE();
  115 + } elseif ($exception instanceof \RedisException) {
  116 + $code = Code::SERVER_REDIS_ERROR();
  117 + } elseif ($exception instanceof \PDOException) {
  118 + $code = Code::SERVER_MYSQL_ERROR();
  119 + } elseif ($exception instanceof ModelNotFoundException) {
  120 + $code = Code::USER_MODEL_NOTFOUND_ERROE();
  121 + } elseif ($exception instanceof NotFoundHttpException || $exception instanceof MethodNotAllowedHttpException) {
  122 + return response('404 Not Found', 404);
  123 + } else {
  124 + $code = Code::SYSTEM_ERROR();
  125 + }
  126 + //钉钉通知错误信息
  127 + if (in_array(config('app.env'), ['test', 'production']) && (in_array(substr($code, 0, 1), ['B', 'C']))) {
  128 + $exceptionMessage = "路由:" . Route::current()->uri .
  129 + "-----错误CODE:" . $exception->getCode() .
  130 + "-----错误message:" . $exception->getMessage() .
  131 + '------错误文件:' . $exception->getFile() . '-------错误行数:' .
  132 + $exception->getLine();
  133 + (new DingService())->handle(['keyword' => "ERROR", 'msg' => config('app.env') . '环境报错:' . $exceptionMessage, 'isAtAll' => false]);
  134 + }
  135 + //开启debug 错误原样输出
  136 + $debub = config('app.debug');
  137 + $message = $debub ? $message : ($code->description ?? $message);
  138 + $response = [
  139 + 'code' => $code,
  140 + 'message' => $message
  141 + ];
  142 + //加密返回
  143 + if (config('app.params_encrypt')) {
  144 + $k = config('app.params_encrypt_key');
  145 + $i = config('app.params_encrypt_iv');
  146 + $response = [
  147 + 'p' => (new EncryptUtils())->openssl_en($response, $k, $i)];
  148 + }
  149 + return response($response);
63 } 150 }
64 } 151 }
@@ -42,7 +42,6 @@ class BaseController extends Controller @@ -42,7 +42,6 @@ class BaseController extends Controller
42 * @method 42 * @method
43 */ 43 */
44 public function auth_token(){ 44 public function auth_token(){
45 - $this->token = '810f28c1d80d0ccf27509816bf44b329';  
46 $info = Cache::get($this->token); 45 $info = Cache::get($this->token);
47 if(!empty($info)){ 46 if(!empty($info)){
48 $this->user = $info; 47 $this->user = $info;
@@ -60,7 +60,7 @@ class ComController extends BaseController @@ -60,7 +60,7 @@ class ComController extends BaseController
60 $info = $projectRoleModel->read(['id'=>$this->user['role_id']]); 60 $info = $projectRoleModel->read(['id'=>$this->user['role_id']]);
61 $projectMenuModel = new ProjectMenuModel(); 61 $projectMenuModel = new ProjectMenuModel();
62 $info['role_menu'] = trim($info['role_menu'],','); 62 $info['role_menu'] = trim($info['role_menu'],',');
63 - $lists = $this->where(['status'=>0,'is_role'=>0])->whereIn('id',explode(',',$info['role_menu']))->get(); 63 + $lists = $projectMenuModel->where(['status'=>0,'is_role'=>0])->whereIn('id',explode(',',$info['role_menu']))->get();
64 $lists = $lists->toArray(); 64 $lists = $lists->toArray();
65 $menu = array(); 65 $menu = array();
66 foreach ($lists as $k => $v){ 66 foreach ($lists as $k => $v){
@@ -22,8 +22,6 @@ class LoginAuthMiddleware @@ -22,8 +22,6 @@ class LoginAuthMiddleware
22 public function handle(Request $request, Closure $next) 22 public function handle(Request $request, Closure $next)
23 { 23 {
24 $token = $request->header('token'); 24 $token = $request->header('token');
25 - var_dump(Cache::get($token));  
26 - die();  
27 if(!isset($token) || empty($token)){ 25 if(!isset($token) || empty($token)){
28 $this->response('当前用户未登录',Code::USER_ERROR); 26 $this->response('当前用户未登录',Code::USER_ERROR);
29 } 27 }
@@ -39,9 +37,11 @@ class LoginAuthMiddleware @@ -39,9 +37,11 @@ class LoginAuthMiddleware
39 //查询当前用户是否拥有权限操作 37 //查询当前用户是否拥有权限操作
40 $projectMenuModel = new ProjectMenu(); 38 $projectMenuModel = new ProjectMenu();
41 $menu_id = $projectMenuModel->read(['action'=>$action['as']],['id']); 39 $menu_id = $projectMenuModel->read(['action'=>$action['as']],['id']);
  40 + if($menu_id !== false){
42 if(strpos($role_info['role_menu'], $menu_id['id']) < 0){ 41 if(strpos($role_info['role_menu'], $menu_id['id']) < 0){
43 $this->response('当前用户没有权限',Code::USER_ERROR); 42 $this->response('当前用户没有权限',Code::USER_ERROR);
44 } 43 }
  44 + }
45 return $next($request); 45 return $next($request);
46 } 46 }
47 /** 47 /**
@@ -83,6 +83,7 @@ class User extends Base @@ -83,6 +83,7 @@ class User extends Base
83 //生成新token 83 //生成新token
84 $token = md5(uniqid().$info['id']); 84 $token = md5(uniqid().$info['id']);
85 //存储缓存 85 //存储缓存
  86 + $info['token'] = $token;
86 Cache::add($token,$info); 87 Cache::add($token,$info);
87 $rs = $this->edit(['token'=>$token],['id'=>$info['id']]); 88 $rs = $this->edit(['token'=>$token],['id'=>$info['id']]);
88 if($rs === false){ 89 if($rs === false){