refactor: ipd平台访问重定向为mobile view改为可配置

This commit is contained in:
2025-06-27 09:18:01 +08:00
parent c2b93bc290
commit fbeab9b7a0
5 changed files with 91 additions and 12 deletions

View File

@@ -35,3 +35,21 @@ MAX_ATTACHMENT_SIZE = 100mb; # 附件上传最大限制
[OPENAPI]
RESOURCE_IMAGES_DOMAIN = http://local.orico.com; # 图片资源服务器地址
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

View File

@@ -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;
}
}

View File

@@ -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

View File

@@ -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);
}

View File

@@ -28,5 +28,31 @@
{include file="public/footer"/}
{/block}
{block name="script"}{/block}
<script>
$(window).ready(function () {
// 为所有站内链接,添加标识
// 使用mtpl=1参数标识解决ipad访问站点时从pc重定向到mobile每次页面都会pc - mobile闪现问题
var LURL = new URL(window.location.href);
if (LURL.searchParams.get('mtpl') == 1) {
$('a').each(function () {
var href = $(this).attr('href');
if (href) {
var origin = LURL.origin;
if (href.indexOf('http') == -1) {
href = new URL(href, origin);
href.searchParams.set('mtpl', '1');
$(this).attr('href', href);
} else {
href = new URL(href);
if (href.origin == origin) {
href.searchParams.set('mtpl', '1');
$(this).attr('href', href);
}
}
}
})
}
})
</script>
</body>
</html>