From 7566d61944948237058c58177c72079576e07b94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Thu, 4 Feb 2021 18:27:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=8C=89=E6=9C=88=E5=88=86=E8=A1=A8?= =?UTF-8?q?=E6=88=96=E6=8C=89=E5=B9=B4=E5=88=86=E8=A1=A8=EF=BC=8C=E7=9A=86?= =?UTF-8?q?=E4=B8=8D=E5=8F=AF=E4=BD=BF=E7=94=A8=20with=20=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=85=B3=E8=81=94=E6=A8=A1=E5=9E=8B=EF=BC=8C=E5=BF=85?= =?UTF-8?q?=E9=A1=BB=E4=BD=BF=E7=94=A8load=EF=BC=9A=20public=20static=20fu?= =?UTF-8?q?nction=20with($relations)=20=20=20=20=20{=20=20=20=20=20=20=20?= =?UTF-8?q?=20=20return=20static::query()->with(=20=20=20=20=20=20=20=20?= =?UTF-8?q?=20=20=20=20=20is=5Fstring($relations)=20=3F=20func=5Fget=5Farg?= =?UTF-8?q?s()=20:=20$relations=20=20=20=20=20=20=20=20=20);=20=20=20=20?= =?UTF-8?q?=20}=20=E4=BC=9A=E9=87=8D=E6=96=B0static=20=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=EF=BC=8C=E6=89=8B=E5=8A=A8=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E7=9A=84=E8=A1=A8=E5=90=8D=E5=B0=86=E5=85=A8=E9=83=A8=E5=A4=B1?= =?UTF-8?q?=E6=95=88=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/MonthModel.php | 9 +++--- app/Models/YearModel.php | 7 ++--- .../Admin/Http/Middleware/CheckAuth.php | 28 +++++++++---------- .../Admin/Http/Middleware/CheckRabc.php | 12 ++++---- .../Admin/Services/AdminLogService.php | 2 +- .../Admin/Services/AdminLoginLogService.php | 2 +- 6 files changed, 29 insertions(+), 31 deletions(-) diff --git a/app/Models/MonthModel.php b/app/Models/MonthModel.php index 34b9eb9..43946e9 100644 --- a/app/Models/MonthModel.php +++ b/app/Models/MonthModel.php @@ -39,11 +39,10 @@ class MonthModel extends Model // 替换为日期格式,否则将无法转化为时间戳(有效的日期格式 - 拼接才可以) $month = str_replace('_', '-', $month); - // 当表名大于最小表名时,设置表名。 - if ( $month >= str_replace('_', '-', self::MIN_TABLE) ) { - $this->month = date(self::MONTH_FORMAT, strtotime($month)); - $this->table = $this->getOldTableName() . '_' . $this->month; - } + // 当表名大于最小表名时,依旧设置表名。 + // if ( $month >= str_replace('_', '-', self::MIN_TABLE) ) {} + $this->month = date(self::MONTH_FORMAT, strtotime($month)); + $this->table = $this->getOldTableName() . '_' . $this->month; return $this; } diff --git a/app/Models/YearModel.php b/app/Models/YearModel.php index 9db7a50..ce3f389 100644 --- a/app/Models/YearModel.php +++ b/app/Models/YearModel.php @@ -41,10 +41,9 @@ class YearModel extends MonthModel $month = str_replace('_', '-', $month); // 当表名大于最小表名时,设置表名。 - if ( $month >= self::MIN_TABLE ) { - $this->month = $month; - $this->table = $this->getOldTableName() . '_' . $this->month; - } + // if ( $month >= self::MIN_TABLE ) {} + $this->month = $month; + $this->table = $this->getOldTableName() . '_' . $this->month; return $this; } diff --git a/app/Modules/Admin/Http/Middleware/CheckAuth.php b/app/Modules/Admin/Http/Middleware/CheckAuth.php index 5e297bd..eaf612d 100644 --- a/app/Modules/Admin/Http/Middleware/CheckAuth.php +++ b/app/Modules/Admin/Http/Middleware/CheckAuth.php @@ -24,20 +24,20 @@ class CheckAuth */ public function handle(Request $request, Closure $next) { -// $this->guard = 'admin'; -// // Auth认证 -// $auth = Auth()->guard($this->guard); -// try { -// if ( !$auth->check() ) { //未登录踢回,给予错误返回提示 -// return $this->errorJson('认证失败,请重新登录!'); -// } -// } catch (TokenExpiredException $e) { -// return $this->errorJson($e->getMessage()); -// } catch (TokenInvalidException $e) { -// return $this->errorJson($e->getMessage()); -// } catch (JWTException $e) { -// return $this->errorJson($e->getMessage()); -// } + $this->guard = 'admin'; + // Auth认证 + $auth = Auth()->guard($this->guard); + try { + if ( !$auth->check() ) { //未登录踢回,给予错误返回提示 + return $this->errorJson('认证失败,请重新登录!'); + } + } catch (TokenExpiredException $e) { + return $this->errorJson($e->getMessage()); + } catch (TokenInvalidException $e) { + return $this->errorJson($e->getMessage()); + } catch (JWTException $e) { + return $this->errorJson($e->getMessage()); + } return $next($request); } diff --git a/app/Modules/Admin/Http/Middleware/CheckRabc.php b/app/Modules/Admin/Http/Middleware/CheckRabc.php index 89dab88..d29421d 100644 --- a/app/Modules/Admin/Http/Middleware/CheckRabc.php +++ b/app/Modules/Admin/Http/Middleware/CheckRabc.php @@ -25,12 +25,12 @@ class CheckRabc */ public function handle(Request $request, Closure $next) { -// // 权限认证 -// $this->guard = 'admin'; -// // 开始验证路由权限 -// if (!$this->checkRabc($request, Auth()->guard($this->guard)->user()->getAuthIdentifier(), $load_error)){ -// return $this->errorJson('无权限' . (empty($load_error) ? '!' : ',' . $load_error), -2); -// } + // 权限认证 + $this->guard = 'admin'; + // 开始验证路由权限 + if (!$this->checkRabc($request, Auth()->guard($this->guard)->user()->getAuthIdentifier(), $load_error)){ + return $this->errorJson('无权限' . (empty($load_error) ? '!' : ',' . $load_error), -2); + } return $next($request); } diff --git a/app/Modules/Admin/Services/AdminLogService.php b/app/Modules/Admin/Services/AdminLogService.php index 0b6549b..082df24 100644 --- a/app/Modules/Admin/Services/AdminLogService.php +++ b/app/Modules/Admin/Services/AdminLogService.php @@ -14,7 +14,7 @@ class AdminLogService extends BaseService public function lists(array $params) : array { $model = $this->model->setMonthTable($this->getSearchMonth()) - ->with([ + ->load([ 'admin' => function($query) { $query->select('admin_id', 'admin_name'); }, diff --git a/app/Modules/Admin/Services/AdminLoginLogService.php b/app/Modules/Admin/Services/AdminLoginLogService.php index a4ff296..8242a50 100644 --- a/app/Modules/Admin/Services/AdminLoginLogService.php +++ b/app/Modules/Admin/Services/AdminLoginLogService.php @@ -14,7 +14,7 @@ class AdminLoginLogService extends BaseService public function lists(array $params) : array { $model = $this->model->setMonthTable($this->getSearchMonth()) - ->with([ + ->load([ 'admin' => function($query) { $query->select('admin_id', 'admin_name'); }, -- Gitee From 9c62cd3143b8087a60ffb30a1e4db40a24e9e950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Thu, 4 Feb 2021 18:34:57 +0800 Subject: [PATCH 2/2] update readme --- README.en.md | 20 +++++++++++++++++--- README.md | 20 +++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/README.en.md b/README.en.md index 3fd2a1e..5ec5f1e 100644 --- a/README.en.md +++ b/README.en.md @@ -26,9 +26,23 @@ Software architecture description #### Instructions -1. xxxx -2. xxxx -3. xxxx +1. 按月、按年分表的模型,皆不可使用 `with`,可使用 `load` 代替,`static::query` 会重新 实例化当前模型,之前设置的分表名称将被替换。 + + 具体原因看代码: +```php + /** + * Begin querying a model with eager loading. + * + * @param array|string $relations + * @return \Illuminate\Database\Eloquent\Builder + */ + public static function with($relations) + { + return static::query()->with( + is_string($relations) ? func_get_args() : $relations + ); + } +``` #### Contribution diff --git a/README.md b/README.md index 68963b6..cabd128 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,23 @@ #### 使用说明 -1. xxxx -2. xxxx -3. xxxx +1. 按月、按年分表的模型,皆不可使用 `with`,可使用 `load` 代替,`static::query` 会重新 实例化当前模型,之前设置的分表名称将被替换。 + + 具体原因看代码: +```php + /** + * Begin querying a model with eager loading. + * + * @param array|string $relations + * @return \Illuminate\Database\Eloquent\Builder + */ + public static function with($relations) + { + return static::query()->with( + is_string($relations) ? func_get_args() : $relations + ); + } +``` #### 参与贡献 -- Gitee