织梦园模板网提供云优cms模板,pbootcms模板,Wordpress主题模板等各类企业新闻资讯网站模板下载服务。织梦园网站模板交流群
用户名:
密码:
注册
注册后享受折扣价

织梦DEDECMS gbk站点mip改造方案

织梦园模板 / 2019-05-06 / 收藏
***部分:模板修改
1、js部分:删除或使用现有组件替换

2、调用百度mip文件:
head里加<link rel="stylesheet" type="text/css"href="https://mipcache.bdstatic.com/static/mipmain-v1.1.1.css">
body里加<script src="https://mipcache.bdstatic.com/static/mipmain-v1.1.2.js"></script>

3、head里加<link rel="canonical"href="{dede:global.cfg_basehost/}{dede:field name='arcurl'/}" >,通过dedecms标签直接调用当前页url。

4、外部通用css文件:建议将css文件中的样式代码嵌入<style mip-custom>…</style>中,另存为模板文件(如css.htm),用{dede:includefilename="css.htm"/}替换相关模板中的<link rel="stylesheet" type="text/css"href="…" />。
模板中的内联css可人工进行查找替换,合并至<stylemip-custom>中。(虽在下面代码中可以自动进行处理,但从静态文件生成性能角度考虑,还是建议人工先将模板中的内联样式一次性整改好。)

注:以上操作大多可通过批量查找替换来完成,看似需要修改很多,但实际工作量并不大。


第二部分:程序文件修改
 
· 静态生成移动站:

找到 /include/dedetag.class.php文件中解析模板输出为文件的函数:
    function SaveTo($filename)
    {
        $fp = @fopen($filename,"w")or die("DedeTag Engine Create File False");
        fwrite($fp,$this->GetResult());
        fclose($fp);
}

替换为(部分代码可根据实际情况进行改动):

     //路径转换函数文件。$content:代码源,$feed_url:首页,$f_url:相对路径的目录部分
     function relative_to_absolute($content,$protocol, $domain, $f_url) {  
                   //根目录相对路径(如href="/a/b.html")转换
                   $new_content =preg_replace('/href\s*\=\s*([\'"])\s*\//','href=\\1'.$protocol.$domain.'/', $content);
                   $new_content =preg_replace('/src\s*\=\s*([\'"])\s*\//', 'src=\\1'.$protocol.$domain.'/',$new_content);
                   //当前页相对路径(如href="a/b.html")转换
                   $new_content =preg_replace('/href\s*\=\s*([\'"])(?!(http|https):\/\/)/','href=\\1'.$protocol.$domain.$f_url,$new_content);
                   $new_content =preg_replace('/src\s*\=\s*([\'"])(?!(http|https):\/\/)/','src=\\1'.$protocol.$domain.$f_url, $new_content);
                   return $new_content;
     }

    function SaveTo($filename)
    {                 
        $fp=@fopen($filename,"w") ordie("DedeTag Engine Create File False");
                   if(substr($_SERVER['PHP_SELF'],-6)=='_m.php'||substr($filename,-13)=='/m/index.html'){ //跳转适配站识别是否为移动端生成,不影响pc端的gbk编码。移动端为独立站点需去掉此判断条件。
                            $f_url=explode('www.域名.com/m',dirname($filename));//分割路径,获取当前页相对路径的目录部分
                            //如dirname($filename)得到的本地***路径为D:/wwwroot/www.域名.com/m/yygk/xwzx,用网站目录“www.域名.com/m”作为标识分割路径,得到目录部分“/yygk/xwzx”。
                            $html=$this->GetResult();
                            $html=$this->relative_to_absolute($html,'http://','m.域名.com',$f_url[1].'/');//相对路径转换***路径
                            $html=str_replace('<metacharset="gb2312">','<metacharset="utf-8">',iconv('gbk','utf-8//ignore',$html)); //转换为utf-8编码声明,fwrite会以此生成对应编码的静态页面
                            $html=str_replace('<a','<a target="_blank" ',$html); //<a>标签加target
                            $html=str_replace('<img','<mip-img ',$html);  //替换<img>标签

                            /* 主要针对编辑器生成的内联样式,将内联样式转换到head的style标签中 */
                            if(preg_match_all('/\sstyle\s*\=\s*[\'"](.*?)[\'"]/',$html,$css)){
                                     $css0=array_unique($css[0]);//过滤重复style                                 
                                     foreach($css0as $k => $v){
                                               $html=str_replace($v,'class="mip_add_css_'.$k.'"',$html); //mip_add_css_为自定义样式名前缀,可自行修改,但需避免与原有样式名重复
                                               $temp_name='mip_add_css_'.$k;
                                               $$temp_name=$css[1][$k];
                                               $add_css.='.'.$temp_name.'{'.$css[1][$k]."}\n";
                                     }
                                     $html=str_replace('<stylemip-custom>',"<style mip-custom>\n".$add_css,$html);
                            }
                            fwrite($fp, $html);
                   }else{ //pc端执行
                            fwrite($fp,$this->GetResult());
                   }
        fclose($fp);
    }

注:该方案初步测试成功,因生成静态文件时处理程序增加,理论上来说会对生成效率有所影响。另外,不排除存在问题的可能性,如有问题或其他想法可回帖共同研究探讨。
 
· 默认动态移动站:
1、修改/m目录下index.php、list.php、view.php三个php文件的编码,改为utf-8。
 
2、找到 /include/dedetag.class.php文件中解析模板直接输出的函数:
    function Display()
    {
        echo $this->GetResult();
    }
替换为:
    function Display()
    {
        $html=str_replace('<meta charset="gb2312">','<meta charset="utf-8">',$this->GetResult()); //转换为utf-8编码声明,此处源内容$this->GetResult()不需要转编码
        echo $html;
    }
 
如此即可以utf-8编码输出动态页面,其他处理可参照上面静态生成站点方案中的代码。

相关安装使用

收缩