dedecms 只需要几个步骤快速实现独立的手机版功能

在dedecms系统中主要常用到的有index.php view.php list.php。

如下以dedecms5.7为例子:

1.需要修改的路径有

/index.php

/include/arc.archives.class.php

/include/arc.listview.class.php

2.我们需要在模板目录下新建mobile目录

/templets/mobile/

将/templets/default/整个文件夹丢到/templets/mobile/

就变成如下/templets/mobile/default

判断是否为手机版的方法 三个地方都会用到

3-1判断方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 判断是否为手机版 true是手机版
* @return boolean
*/
function  is_mobile() {
if  ( empty ( $_SERVER [ 'HTTP_USER_AGENT' ])) {
return  false;
}
$user_agent  $_SERVER [ 'HTTP_USER_AGENT' ];
$mobile_agents  = Array( "240x320" "acer" "acoon" "acs-" "abacho" "ahong" "airness" "alcatel" "amoi" "android" "anywhereyougo.com" "applewebkit/525" "applewebkit/532" "asus" "audio" "au-mic" "avantogo" "becker" "benq" "bilbo" "bird" "blackberry" "blazer" "bleu" "cdm-" "compal" "coolpad" "danger" "dbtel" "dopod" "elaine" "eric" "etouch" "fly " "fly_" "fly-" "go.web" "goodaccess" "gradiente" "grundig" "haier" "hedy" "hitachi" "htc" "huawei" "hutchison" "inno" "ipad" "ipaq" "ipod" "jbrowser" "kddi" "kgt" "kwc" "lenovo" "lg " "lg2" "lg3" "lg4" "lg5" "lg7" "lg8" "lg9" "lg-" "lge-" "lge9" "longcos" "maemo" "mercator" "meridian" "micromax" "midp" "mini" "mitsu" "mmm" "mmp" "mobi" "mot-" "moto" "nec-" "netfront" "newgen" "nexian" "nf-browser" "nintendo" "nitro" "nokia" "nook" "novarra" "obigo" "palm" "panasonic" "pantech" "philips" "phone" "pg-" "playstation" "pocket" "pt-" "qc-" "qtek" "rover" "sagem" "sama" "samu" "sanyo" "samsung" "sch-" "scooter" "sec-" "sendo" "sgh-" "sharp" "siemens" "sie-" "softbank" "sony" "spice" "sprint" "spv" "symbian" "tablet" "talkabout" "tcl-" "teleca" "telit" "tianyu" "tim-" "toshiba" "tsm" "up.browser" "utec" "utstar" "verykool" "virgin" "vk-" "voda" "voxtel" "vx" "wap" "wellco" "wig browser" "wii" "windows ce" "wireless" "xda" "xde" "zte" );
$is_mobile  = false;
foreach  ( $mobile_agents  as  $device ) {
if  ( stristr ( $user_agent $device )) {
$is_mobile  = true;
break ;
}
}
return  $is_mobile ;
}

3.修改/index.php

找到里面的$pv->SetTemplet($cfg_basedir . $cfg_templets_dir . "/" . $row['templet']);

将3-1方法放到index.php的后面即可


替换成

1
2
3
4
5
if  (is_mobile()) {
$pv ->SetTemplet( $cfg_basedir  $cfg_templets_dir  "/mobile/"  $row [ 'templet' ]);
else  {
$pv ->SetTemplet( $cfg_basedir  $cfg_templets_dir  "/"  $row [ 'templet' ]);
}


这样首页就折腾完了

修改/templets/mobile/default/index.htm即可

然后在模板里面引入其他的模板地址也需要改变

{dede:include filename="/mobile/default/head.htm"/} 所有的模板前面多了/mobile/default/

4.修改/include/arc.archives.class.php

还是一样将方法放入类中

在if($this->TypeLink->TypeInfos['corank'] > 0 && $this->Fields['arcrank']==0)上面添加如下代码:

1
2
3
4
5
6
7
if ( $this ->is_mobile())
{
$this ->TypeLink->modDir= $this ->TypeLink->modDir. "/mobile" ;
$this ->TypeLink->TypeInfos[ 'tempindex' ]= "mobile/" . $this ->TypeLink->TypeInfos[ 'tempindex' ];
$this ->TypeLink->TypeInfos[ 'templist' ]= "mobile/" . $this ->TypeLink->TypeInfos[ 'templist' ];
$this ->TypeLink->TypeInfos[ 'temparticle' ]= "mobile/" . $this ->TypeLink->TypeInfos[ 'temparticle' ];
}

5.修改/include/arc.listview.class.php

还是一样将方法放入类中

在 if(!$this->IsError)上面添加

1
2
3
4
5
6
7
if ( $this ->is_mobile())
{
$this ->TypeLink->modDir= $this ->TypeLink->modDir. "/mobile" ;
$this ->TypeLink->TypeInfos[ 'tempindex' ]= "mobile/" . $this ->TypeLink->TypeInfos[ 'tempindex' ];
$this ->TypeLink->TypeInfos[ 'templist' ]= "mobile/" . $this ->TypeLink->TypeInfos[ 'templist' ];
$this ->TypeLink->TypeInfos[ 'temparticle' ]= "mobile/" . $this ->TypeLink->TypeInfos[ 'temparticle' ];
}

这样就完成了 整个手机版的修改 这样在mobile/default新增和修改对应的模板就能访问到独立的手机版HTML


原文地址:https://www.apizl.com/archives/view-133830-1.html

猜你喜欢

转载自blog.csdn.net/moxun2011/article/details/80733437