diff --git a/.env.local b/.env.local index dab1f91e..e9c25f0d 100644 --- a/.env.local +++ b/.env.local @@ -34,4 +34,22 @@ MAX_ATTACHMENT_SIZE = 100mb; # 附件上传最大限制 # 开放API [OPENAPI] RESOURCE_IMAGES_DOMAIN = http://local.orico.com; # 图片资源服务器地址 -RESOURCE_VIDEOS_DOMAIN = http://local.orico.com; # 视频资源服务器地址 \ No newline at end of file +RESOURCE_VIDEOS_DOMAIN = http://local.orico.com; # 视频资源服务器地址 + +# 视图模板规则配置 +[VIEW_TPL] +# 视图目录 +# query 规则:URL参数 mtpl=1 表示移动端访问 +# 例如:http://xxxx.com?mtpl=1 +# domain 规则:根据特定域名,判断是否移动端访问 +# 例如:http://mobile.orico.cn +RULE = query +# query 规则参数名 +RULE_QUERY_NAME = mtpl +# query 规则参数值 +RULE_QUERY_VALUE = 1 +# domain 规则协议 +RULE_DOMAIN_SCHEME[] = http +RULE_DOMAIN_SCHEME[] = https +# domain 规则域名 +RULE_DOMAIN_HOST = mobile.orico.cn \ No newline at end of file diff --git a/app/index/common.php b/app/index/common.php index 6bc89d59..de61e023 100644 --- a/app/index/common.php +++ b/app/index/common.php @@ -178,3 +178,47 @@ if (!function_exists('date_format_i18n')) { return date($fmt, $timestamp); } } + +if (!function_exists('get_platform')) { + /** + * 获取平台 + * @return string + */ + function get_platform(): string + { + $detect = new \Detection\MobileDetect(); + $platform = 'pc'; + + if ($detect->isMobile() || $detect->isTablet()) { + $platform = 'mobile'; + } else { + // 在非移动端环境,根据配置规则判断是否要显示移动端 + $view_cfg = $view_cfg = [ + 'rule' => env('VIEW_TPL.RULE', 'query'), + 'query' => [ + 'name' => env('VIEW_TPL.RULE_QUERY_NAME', 'mtpl'), + 'value' => env('VIEW_TPL.RULE_QUERY_VALUE', '1'), + ], + 'domain' => [ + 'scheme' => env('VIEW_TPL.RULE_DOMAIN_SCHEME', ['http']), + 'host' => env('VIEW_TPL.RULE_DOMAIN_HOST'), + ], + ]; + if ($view_cfg['rule'] == 'query') { + $name = $view_cfg['query']['name']; + $value = $view_cfg['query']['value']; + if (request()->get($name) == $value) { + $platform = 'mobile'; + } + } elseif ($view_cfg['rule'] == 'domain') { + $scheme = $view_cfg['domain']['scheme']; + $host = $view_cfg['domain']['host']; + if (in_array(request()->scheme(), $scheme) && $host == request()->host()) { + $platform = 'mobile'; + } + } + } + + return $platform; + } +} diff --git a/app/index/config/view.php b/app/index/config/view.php index 0fefd60b..2af91b5a 100644 --- a/app/index/config/view.php +++ b/app/index/config/view.php @@ -3,13 +3,7 @@ // | 模板设置 // +---------------------------------------------------------------------- -use Detection\MobileDetect; - -$detect = new MobileDetect(); -$view_device_name = 'pc'; -if ($detect->isMobile() || $detect->isTablet() || request()->get('mtpl') == 1) { - $view_device_name = 'mobile'; -} +$view_device_name = get_platform(); return [ // 模板引擎类型使用Think diff --git a/app/index/middleware/ConfirmRequestFrom.php b/app/index/middleware/ConfirmRequestFrom.php index 14a5e707..a469fe79 100644 --- a/app/index/middleware/ConfirmRequestFrom.php +++ b/app/index/middleware/ConfirmRequestFrom.php @@ -15,10 +15,7 @@ class ConfirmRequestFrom public function handle($request, \Closure $next) { // 确认请求来源 - $request->from = 'pc'; - if ($request->isMobile() || $request->get('mtpl') == 1) { - $request->from = 'mobile'; - } + $request->from = get_platform(); return $next($request); } diff --git a/app/index/view/mobile/public/base.html b/app/index/view/mobile/public/base.html index 3c7fc1d4..369f255f 100644 --- a/app/index/view/mobile/public/base.html +++ b/app/index/view/mobile/public/base.html @@ -28,5 +28,31 @@ {include file="public/footer"/} {/block} {block name="script"}{/block} + \ No newline at end of file