From 9fd3954b4fda5531f0623dc3ebb32f5570c03514 Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Tue, 27 May 2025 14:02:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=BF=9B=E4=B8=80=E6=AD=A5?= =?UTF-8?q?=E5=8C=BA=E5=88=86banner=E7=9A=84pc=E4=B8=8Emobile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/index/config/view.php | 12 +++++----- app/index/controller/Index.php | 7 +++--- app/index/middleware.php | 2 ++ app/index/middleware/ConfirmRequestFrom.php | 25 +++++++++++++++++++++ app/index/model/SysBannerModel.php | 10 +++++++++ 5 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 app/index/middleware/ConfirmRequestFrom.php diff --git a/app/index/config/view.php b/app/index/config/view.php index ea481a58..43f0cc8a 100644 --- a/app/index/config/view.php +++ b/app/index/config/view.php @@ -2,9 +2,9 @@ // +---------------------------------------------------------------------- // | 模板设置 // +---------------------------------------------------------------------- -$view_deivce_name = 'pc'; +$view_device_name = 'pc'; if (request()->isMobile()) { - $view_deivce_name = 'mobile'; + $view_device_name = 'mobile'; } return [ @@ -13,7 +13,7 @@ return [ // 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写 3 保持操作方法 'auto_rule' => 1, // 模板目录名 - 'view_dir_name' => "view/{$view_deivce_name}", + 'view_dir_name' => "view/{$view_device_name}", // 模板后缀 'view_suffix' => 'html', // 模板文件名分隔符 @@ -28,8 +28,8 @@ return [ 'taglib_end' => '}', // 模板输出替换 'tpl_replace_string' => [ - '__CSS__' => "/static/index/{$view_deivce_name}/css", - '__JS__' => "/static/index/{$view_deivce_name}/js", - '__IMAGES__' => "/static/index/{$view_deivce_name}/images", + '__CSS__' => "/static/index/{$view_device_name}/css", + '__JS__' => "/static/index/{$view_device_name}/js", + '__IMAGES__' => "/static/index/{$view_device_name}/images", ] ]; diff --git a/app/index/controller/Index.php b/app/index/controller/Index.php index efb15f72..b21a03a5 100644 --- a/app/index/controller/Index.php +++ b/app/index/controller/Index.php @@ -54,11 +54,12 @@ class Index extends Common $banners = SysBannerModel::with(['items' => function($query) { $query->where('type', 'IN', ['image', 'video'])->where('status', '=', 1)->order(['sort' => 'asc', 'id' => 'desc']); }]) + ->atPlatform(request()->from) ->uniqueLabel([ - 'BANNER_67f61cd70e8e1', + 'BANNER_67f61cd70e8e1', 'BANNER_67f633023a5b3', - 'BANNER_67f63f8ab5029', - 'BANNER_67f724ed81b1e', + 'BANNER_67f63f8ab5029', + 'BANNER_67f724ed81b1e', 'BANNER_67f7392b4d83a', 'BANNER_67f7410e244fb', 'BANNER_67f76a96545f9', diff --git a/app/index/middleware.php b/app/index/middleware.php index 90a6ad68..bf7bb315 100644 --- a/app/index/middleware.php +++ b/app/index/middleware.php @@ -3,4 +3,6 @@ return [ // 启用多语言支持 think\middleware\LoadLangPack::class, + // 确认请求来源 + app\index\middleware\ConfirmRequestFrom::class, ]; diff --git a/app/index/middleware/ConfirmRequestFrom.php b/app/index/middleware/ConfirmRequestFrom.php new file mode 100644 index 00000000..b8cc9588 --- /dev/null +++ b/app/index/middleware/ConfirmRequestFrom.php @@ -0,0 +1,25 @@ +from = 'pc'; + if ($request->isMobile()) { + $request->from = 'mobile'; + } + + return $next($request); + } +} diff --git a/app/index/model/SysBannerModel.php b/app/index/model/SysBannerModel.php index 0b46819d..525f69f3 100644 --- a/app/index/model/SysBannerModel.php +++ b/app/index/model/SysBannerModel.php @@ -23,6 +23,16 @@ class SysBannerModel extends SysBannerBaseModel return $this->hasMany(SysBannerItemModel::class, 'banner_id', 'id'); } + // 所属平台范围查询 + public function scopeAtPlatform($query, string|array $platform) + { + if (is_array($platform)) { + $query->whereIn('at_platform', $platform); + return; + } + $query->where('at_platform', '=', $platform); + } + // 唯一标识范围查询 public function scopeUniqueLabel($query, string|array $unique_label) {