优化静态发布页面的速度

hotfix/paypal-note
尹诚诚 4 years ago
parent 1eb41f2dbf
commit c83bd0cace

@ -26,7 +26,7 @@ class Information extends CI_Controller
$this->load->model('Feedback_model');
$this->load->library('Amplib'); //加载AMP处理类
$this->load->library('html_optimize_lib'); //加载HTML优化类
}
public function index()
@ -410,8 +410,12 @@ class Information extends CI_Controller
} else if (strcasecmp($site_code, "ch") == 0 && !empty($auto_update_cache)) {
//读取模板生成PC和移动优先文件
$information_new = $this->Information_model->Detail($this->input->post('is_id'));
$this->make_www_cache('mobile', $information_new);
$this->make_www_cache('pc', $information_new);
$recommand_information = $this->recommand_information($information);
//$this->benchmark->mark('111');
$this->make_www_cache('mobile', $information_new, $recommand_information);
//$this->benchmark->mark('222');
$this->make_www_cache('pc', $information_new, $recommand_information);
//echo $this->benchmark->elapsed_time('111', '222');
} else if (strcasecmp($site_code, "ah") == 0 && !empty($auto_update_cache)) {
//读取模板生成PC和移动优先文件
$information_new = $this->Information_model->Detail($this->input->post('is_id'));
@ -420,7 +424,7 @@ class Information extends CI_Controller
} else if (strcasecmp($site_code, "gh") == 0 && !empty($auto_update_cache)) {
$information_new = $this->Information_model->Detail($this->input->post('is_id'));
$this->make_www_cache_gh($information_new);
}else if (strcasecmp($site_code, "chinatravel") == 0 && !empty($auto_update_cache)) {
} else if (strcasecmp($site_code, "chinatravel") == 0 && !empty($auto_update_cache)) {
//chinatravel读取模板生成PC和移动优先文件
$information_new = $this->Information_model->Detail($this->input->post('is_id'));
$this->make_www_cache_ct($information_new);
@ -513,7 +517,7 @@ class Information extends CI_Controller
$this->make_www_cache_ah('mobile', $information);
} else if ($this->config->item('site_code') == 'chinatravel') {
$this->make_www_cache_ct($information);
// $this->make_www_cache_ct('mobile', $information);
// $this->make_www_cache_ct('mobile', $information);
}
echo json_encode(array('name' => 'yes', 'data' => '更新成功!'));
@ -565,7 +569,7 @@ class Information extends CI_Controller
}
//生成静态文件 $device='mobile' or 'pc'
function make_www_cache($device, $information)
function make_www_cache($device, $information, $recommand_information)
{
$data = array();
switch ($device) {
@ -591,7 +595,6 @@ class Information extends CI_Controller
break;
default:
}
$breadcrumb_data = $this->Information_model->get_path_exclude_self($information->is_id, $information->is_path);
$breadcrumb_structured_data_content = $this->load->view(
@ -603,7 +606,6 @@ class Information extends CI_Controller
$breadcrumb_structured_data_content,
$template);
//替换模板中的标签
$template = str_replace('<!--@TITLE@-->', $information->ic_seo_title, $template);
$template = str_replace('<!--@DESCRIPTION@-->', $information->ic_seo_description, $template);
@ -630,6 +632,7 @@ class Information extends CI_Controller
}
}
//火车票搜索框添加 -- zp
if ($information->is_parent_id == "278008234") {
//本地测试的火车父类ID为278035939 。网前为278008234
@ -637,7 +640,6 @@ class Information extends CI_Controller
$template = str_replace('<!--@TRAIN-SEARCH@-->', $template_TrainSearch, $template);
}
//非产品页面
$meta_product_code = get_meta($information->ic_id, 'meta_product_code');
if (empty($meta_product_code)) {
@ -672,30 +674,28 @@ class Information extends CI_Controller
$information->ic_content = str_replace('<!--@BREADCRUMB-BLOCK@-->', $breadcrumb_content, $information->ic_content);
}
//---day tours--- 20210922 zp
//本地 278035929
//网前 278008224
if ($information->is_parent_id == "278008224") {
if (!empty($meta_product_code)){
if (!empty($meta_product_code)) {
//准备需要的数据
$dataTrippest = [];
$dataTrippest["ic_title"] = $information->ic_title;
$dataTrippest["ic_title"] = $information->ic_title;
//附加选项
$t_Type = get_meta($information->ic_id, 'meta_trippest_type');
if (isset($t_Type)){
if (isset($t_Type)) {
$dataTrippest["t_Type"] = $t_Type;
}else{
} else {
$dataTrippest["t_Type"] = "general";
}
$t_package = get_meta($information->ic_id, 'meta_trippest_package');
$dataTrippest["t_package"] = explode("|",$t_package);
$dataTrippest["t_package"] = explode("|", $t_package);
$t_excludedate = get_meta($information->ic_id, 'meta_trippest_excludedate');
$dataTrippest["t_excludedate"] = $t_excludedate;
$t_includedate = get_meta($information->ic_id, 'meta_trippest_includedate');
$t_includedate = get_meta($information->ic_id, 'meta_trippest_includedate');
$dataTrippest["t_includedate"] = $t_includedate;
$t_excludemonth = get_meta($information->ic_id, 'meta_trippest_excludemonth');
$dataTrippest["t_excludemonth"] = $t_excludemonth;
@ -710,7 +710,7 @@ class Information extends CI_Controller
// 132004 桂林目的地
// 132005 成都目的地
$PAG_SourceType = $dataTrippest['PagDetail']->PAG_SourceType;
switch ($PAG_SourceType){
switch ($PAG_SourceType) {
case '132001':
$dataTrippest['PAG_SourceType'] = "beijing";
break;
@ -722,41 +722,40 @@ class Information extends CI_Controller
break;
case '132004':
$dataTrippest['PAG_SourceType'] = "guilin";
break;
break;
case '132005':
$dataTrippest['PAG_SourceType'] = "chengdu";
break;
break;
case '132006':
$dataTrippest['PAG_SourceType'] = "zhangjiajie";
break;
$dataTrippest['PAG_SourceType'] = "zhangjiajie";
break;
case '132007':
$dataTrippest['PAG_SourceType'] = "huangshan";
break;
$dataTrippest['PAG_SourceType'] = "huangshan";
break;
default:
$dataTrippest['PAG_SourceType'] = "";
break;
}
//每周发团
$PAG_Scheme = $dataTrippest['PagDetail']->PAG_Scheme;
$dataTrippest['PAG_Scheme'] = $PAG_Scheme;
//接送
if ($dataTrippest["t_Type"]=="transfer" || 1==1)
{
if ($dataTrippest["t_Type"] == "transfer" || 1 == 1) {
$PagList = $this->trippest_model->GetTransfer($dataTrippest['PagDetail']->PAG_SN);
$dataTrippest["PagList"] = $PagList;
}
//价格
//价格
// $price = $this->trippest_model->get_price("BJALC-1",2);
$price = $this->trippest_model->get_price($dataTrippest['PagDetail']->PAG_Code,2);
$price = $this->trippest_model->get_price($dataTrippest['PagDetail']->PAG_Code, 2);
$showPrice = "";
if (!empty($price)){
if (!empty($price)) {
$PKP_AdultNetPrice = $price->PKP_AdultNetPrice;
$PKP_AdultSpecialPrice = $price->PKP_AdultSpecialPrice;
if ($PKP_AdultSpecialPrice>0){
if ($PKP_AdultSpecialPrice > 0) {
$showPrice = $PKP_AdultSpecialPrice;
}else{
} else {
$showPrice = $PKP_AdultNetPrice;
}
}
@ -774,9 +773,8 @@ class Information extends CI_Controller
//---day tours end ----
//信息推荐
$template_recommand = $this->recommand_information($information);
$template_recommand = $recommand_information; //$this->recommand_information($information); 从外面传进来pc和移动只需要查一次减少数据库负担
$template = str_replace('<!--@ARTICLENEXT@-->', $this->load->view($template_path . '-next', array('recommands' => $template_recommand), TRUE), $template);
// //广告改叫tips防止被插件屏蔽
if (!empty($template_recommand['Tips Right TOP'])) {
@ -796,7 +794,6 @@ class Information extends CI_Controller
$information->ic_content = str_replace('<!--@Content-Recommends-Tour@-->', $this->load->view($template_path . '-recommends-tour', array('recommands' => $template_recommand), TRUE), $information->ic_content);
}
//顶部全屏大图
if ($device == 'mobile') { //移动端读取移动大图
$meta_addon_picture = get_meta($information->ic_id, 'meta_addon_picture_mobile');
@ -806,7 +803,7 @@ class Information extends CI_Controller
// 火车票首页搜索框 --zp
$template_TrainSearch = "";
if ($information->ic_url =="/china-trains/hometest.htm" || $information->ic_url=="/china-trains/"){
if ($information->ic_url == "/china-trains/hometest.htm" || $information->ic_url == "/china-trains/") {
$template_TrainSearch = $this->load->view($template_path . '-train-index', null, TRUE);
}
@ -816,7 +813,7 @@ class Information extends CI_Controller
if (!empty($meta_addon_picture_url)) {
$template = str_replace('<!--@TOP-BANNER@-->', '<div class="detailtopbanner"><a href="' . $meta_addon_picture_url . '"> <img class="img-responsive" alt="' . $information->ic_title . '" src="' . $meta_addon_picture . '"></a></div>', $template);
} else {
$template = str_replace('<!--@TOP-BANNER@-->', '<div class="detailtopbanner"><img class="img-responsive" alt="' . $information->ic_title . '" src="' . $meta_addon_picture . '">'.$template_TrainSearch.'</div>', $template);
$template = str_replace('<!--@TOP-BANNER@-->', '<div class="detailtopbanner"><img class="img-responsive" alt="' . $information->ic_title . '" src="' . $meta_addon_picture . '">' . $template_TrainSearch . '</div>', $template);
}
}
//顶部视频
@ -825,15 +822,21 @@ class Information extends CI_Controller
if (!empty($meta_news_video) && !empty($meta_video_picture)) {
$template = str_replace('<!--@TOP-VIDEO@-->', '<div class="detailtopbanner"><video autoplay="" controls="controls" id="bgvid" poster="' . $meta_video_picture . '" src="' . $meta_news_video . '" style="position: inherit;"> </video></div>', $template);
}
//主样式表,内联模式
$main_css_string = compress_css(GET_HTTP('https://proxy-data.chinahighlights.com/css/mobile-first.css'));
if (is_file('D:/wwwroot/origin-data.chinahighlights.com/css/mobile-first.css')) {//主样式表,内联模式,优先读取本地,没有在从网络读取,为了加速
$main_css_string = compress_css(file_get_contents('D:/wwwroot/origin-data.chinahighlights.com/css/mobile-first.css'));
} else {
$main_css_string = compress_css(GET_HTTP('https://proxy-data.chinahighlights.com/css/mobile-first.css'));
}
$template = str_replace('<!--@MAIN-CSS@-->', '<style type="text/css">' . $main_css_string . '</style>', $template);
//额外样式
$meta_addon_css = get_meta($information->ic_id, 'meta_addon_css');
if (!empty($meta_addon_css)) {
//引用方式 $template = str_replace('<!--@ADDON-CSS@-->', '<link href="' . $meta_addon_css . '" rel="stylesheet">', $template);
//内联方式
$meta_addon_css_string = compress_css(GET_HTTP($meta_addon_css));
if ($meta_addon_css == 'https://proxy-data.chinahighlights.com/css/tour-detail-former.css' && is_file('D:/wwwroot/origin-data.chinahighlights.com/css/tour-detail-former.css')) {
$meta_addon_css_string = compress_css(file_get_contents('D:/wwwroot/origin-data.chinahighlights.com/css/tour-detail-former.css'));
} else {
$meta_addon_css_string = compress_css(GET_HTTP($meta_addon_css));
}
$template = str_replace('<!--@ADDON-CSS@-->', '<style type="text/css">' . $meta_addon_css_string . '</style>', $template);
}
//额外js
@ -854,18 +857,23 @@ class Information extends CI_Controller
$template = str_replace('<!--@Match-Content-GOOGLE@-->', '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-4231674166786366" data-ad-slot="1447610161" data-matched-content-rows-num="2,2" data-matched-content-columns-num="1,3" data-matched-content-ui-type="image_stacked,image_stacked"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script>', $template);
//}
//社媒分享图片
$full_ic_photo='';
$full_ic_photo = '';
if (!empty($information->ic_photo)) {
$full_ic_photo = $this->config->item('site_image_url') . $information->ic_photo;
}else{
$full_ic_photo = get_meta($information->ic_id, 'meta_addon_picture_mobile');
}
if(!empty($full_ic_photo)){
} else {
$full_ic_photo = get_meta($information->ic_id, 'meta_addon_picture_mobile');
}
if (!empty($full_ic_photo)) {
$template = str_replace('<!--@OG:IMAGE@-->', '<meta property="og:image" content="' . $full_ic_photo . '"><meta property="og:image:secure_url" content="' . $full_ic_photo . '"><meta property="og:image:width" content="800"><meta property="og:image:height" content="450">', $template);
}
//设置图片尺寸
$template = $this->html_optimize_lib->set_image_size($template);
if (is_dir('D:/wwwroot/origin-images.chinahighlights.com') && is_dir('D:/wwwroot/origin-www.chinahighlights.com')) {
$template = $this->html_optimize_lib->set_image_size_local($template, 'D:/wwwroot/origin-www.chinahighlights.com', 'D:/wwwroot/origin-images.chinahighlights.com');
} else {
$template = $this->html_optimize_lib->set_image_size($template);
}
// /travelguide/chinese-zodiac/monthly-fortune-for-dog.htm
$this->benchmark->mark('10');
$html_path = $this->config->item('cache')[$this->config->item('site_code')]['cache_path'] . $information->ic_url;
$html_path = str_replace("\\", "/", $html_path);
if (substr($html_path, -1, 1) == '/') {
@ -877,7 +885,8 @@ class Information extends CI_Controller
}
function make_www_cache_gh($information){
function make_www_cache_gh($information)
{
return false;
$data = array();
$data['information'] = $information;
@ -1037,17 +1046,17 @@ class Information extends CI_Controller
$template = str_replace('<!--@Match-Content-GOOGLE@-->', '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-4231674166786366" data-ad-slot="2509031466" data-matched-content-rows-num="2,2" data-matched-content-columns-num="1,3" data-matched-content-ui-type="image_stacked,image_stacked"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script>', $template);
//}
//社媒分享图片
$full_ic_photo='';
$full_ic_photo = '';
if (!empty($information->ic_photo)) {
$full_ic_photo = $this->config->item('site_image_url') . $information->ic_photo;
}else{
$full_ic_photo = get_meta($information->ic_id, 'meta_addon_picture_mobile');
}
if(!empty($full_ic_photo)){
} else {
$full_ic_photo = get_meta($information->ic_id, 'meta_addon_picture_mobile');
}
if (!empty($full_ic_photo)) {
$template = str_replace('<!--@OG:IMAGE@-->', '<meta property="og:image" content="' . $full_ic_photo . '"><meta property="og:image:secure_url" content="' . $full_ic_photo . '"><meta property="og:image:width" content="800"><meta property="og:image:height" content="450">', $template);
}
//设置图片尺寸
$template = $this->html_optimize_lib->set_image_size($template);
// /travelguide/chinese-zodiac/monthly-fortune-for-dog.htm
@ -1083,7 +1092,7 @@ class Information extends CI_Controller
/* 详细内容 */
$ic_content = $information->ic_content;
// /**替换详细内容里面的价格
// /**替换详细内容里面的价格
/* 直接使用fetch来动态获取价格这样不用替换标签直接录标签
// * // 一日游价格标签, "<!--@DayTripPrice:XASIC-41@-->" 或者<!--@DayTripPrice:XASIC-41,2@--> 逗号后面是人数
// * //精华线路标签"<!--@TourPrice:ct-1@-->"
@ -1099,7 +1108,6 @@ class Information extends CI_Controller
// return '<span class="js_getTourPrice" data="'.$matchItem.'"></span>';;
// },$ic_content);
//图片加延迟
$ic_content = $this->html_optimize_lib->set_lazy_loader($ic_content, 'https://data.chinatravel.com/images/mobile-first/grey.gif');
@ -1133,7 +1141,7 @@ class Information extends CI_Controller
//主样式表,内联模式
//$apiweb="http://202.103.68.104"; //本地测试
$apiweb = "https://data.chinatravel.com";
$apiweb = "https://data.chinatravel.com";
$cssUrl = $apiweb . "/css/newcss3/base.css?v=" . time();
$main_css_string = compress_css(GET_HTTP($cssUrl));
$data["MAINCSS"] = '<style type="text/css">' . $main_css_string . '</style>';
@ -1151,11 +1159,11 @@ class Information extends CI_Controller
//社媒分享图片
$data["OGIMAGE"] = "";
if (!empty($information->ic_photo)) {
if (strpos($information->ic_photo,"chinatravel.com")!=false){
if (strpos($information->ic_photo, "chinatravel.com") != false) {
$full_ic_photo = $information->ic_photo;
}else{
} else {
$full_ic_photo = $this->config->item('site_image_url') . $information->ic_photo;
}
}
$data["OGIMAGE"] = '<meta property="og:image" content="' . $full_ic_photo . '"><meta property="og:image:secure_url" content="' . $full_ic_photo . '"><meta property="og:image:width" content="800"><meta property="og:image:height" content="450">';
}
@ -1189,7 +1197,7 @@ class Information extends CI_Controller
if (!empty($meta_addon_js)) {
$addonJs .= '<script src="' . $meta_addon_js . '" defer></script>';
}
//价格js
//价格js
if ($meta_ct_page_price == "yes") {
$addonJs .= '<script src="' . $apiweb . '/js/mobile-first/getprice.js" defer></script>';
}
@ -1273,8 +1281,8 @@ class Information extends CI_Controller
#endregion
#region 移动端和PC端差别处理区域
$arrDevice = array("mobile","pc");
foreach($arrDevice as $device){
$arrDevice = array("mobile", "pc");
foreach ($arrDevice as $device) {
//顶部全屏大图
$data["TOPBANNER"] = "";
$ImgText = ""; //图文字
@ -1282,29 +1290,29 @@ class Information extends CI_Controller
$meta_addon_picture_text = get_meta($information->ic_id, 'meta_addon_picture_text');
if ($device == 'mobile') { //移动端读取移动大图
$meta_addon_picture = get_meta($information->ic_id, 'meta_addon_picture_mobile');
$bannerImg = '<img class="img-responsive" alt="' . $information->ic_title . '" src="' . $meta_addon_picture . '" width="750" height="500">';
$bannerImg = '<img class="img-responsive" alt="' . $information->ic_title . '" src="' . $meta_addon_picture . '" width="750" height="500">';
if (!empty($meta_addon_picture_url) && !empty($meta_addon_picture_text)) {
$ImgText = '<div class="carousel-caption"><p class="ads-title"><a href="' . $meta_addon_picture_url . '">' . $meta_addon_picture_text . '</a></p></div>';
} else if(!empty($meta_addon_picture_text)){
} else if (!empty($meta_addon_picture_text)) {
$ImgText = '<div class="carousel-caption"><p class="ads-title">' . $meta_addon_picture_text . '</p></div>';
}
} else {
$meta_addon_picture = get_meta($information->ic_id, 'meta_addon_picture');
$bannerImg = '<img class="img-responsive" alt="' . $information->ic_title . '" src="' . $meta_addon_picture . '" width="1920" height="600">';
if (!empty($meta_addon_picture_url) && !empty($meta_addon_picture_text) ) {
if (!empty($meta_addon_picture_url) && !empty($meta_addon_picture_text)) {
$ImgText = '<div class="carousel-caption"><p class="ads-title"><a href="' . $meta_addon_picture_url . '">' . $meta_addon_picture_text . '</a></p></div>';
} else if(!empty($meta_addon_picture_text)) {
} else if (!empty($meta_addon_picture_text)) {
$ImgText = '<div class="carousel-caption"><p class="ads-title">' . $meta_addon_picture_text . '</p></div>';
}
}
// 搜索框
$template_TrainSearch = "";
if ($information->ic_url =="/china-trains/hometest" || $information->ic_url=="/china-trains"){
if ($information->ic_url == "/china-trains/hometest" || $information->ic_url == "/china-trains") {
//火车票首页
$template_TrainSearch = $this->load->view($template_path . '-train-index', null, TRUE);
$ImgText = "";
}else if ($information->ic_url =="/china-flights"){
} else if ($information->ic_url == "/china-flights") {
//机票首页
$template_TrainSearch = $this->load->view($template_path . '-flight-index', null, TRUE);
$ImgText = "";
@ -1319,7 +1327,7 @@ class Information extends CI_Controller
}
//两种设备分别调用两种模板 ,静态化
//两种设备分别调用两种模板 ,静态化
switch ($device) {
case 'mobile':
//获取移动优先的模板,如果有的话
@ -1354,8 +1362,8 @@ class Information extends CI_Controller
$html_path .= $html_path_ext;
create_folder_by_path(dirname($html_path));
file_put_contents($html_path, $template, LOCK_EX);
}
}
#endregion
}
@ -1423,7 +1431,7 @@ class Information extends CI_Controller
$data->mobile_photo = get_meta($data->ic_id, 'meta_addon_picture_mobile');
if (empty($data->mobile_photo)) {
if (!empty($data->ic_photo)) {
$data->mobile_photo = $this->config->item('site_image_url').$data->ic_photo;
$data->mobile_photo = $this->config->item('site_image_url') . $data->ic_photo;
} else {
$data->mobile_photo = 'https://data.chinahighlights.com/image/travelguide/article/recommended-info-bottom-cover.jpg';
}

@ -31,7 +31,7 @@ class html_optimize_lib
/**有些图片不需要延迟加载比如头部第一张图延迟加载会影响CLS的评分所以图片加loader="nolazy"属性就可以不用替换为延迟加载代码。 */
$loader = $image->loader;
if (!empty($loader) && $loader=="nolazy"){
if (!empty($loader) && $loader == "nolazy") {
continue;
}
/** 无需延迟加载结束 20210527 zp */
@ -99,8 +99,41 @@ class html_optimize_lib
}
return $html_object->save();
}
//获取图片尺寸的本地版本,图片存在相同服务器上,速度比较快
//$dataPath data文件夹本地路径
//$imagesPath images文件夹本地路径
public function set_image_size_local($html, $dataPath, $imagesPath)
{
$html_object = str_get_html($html);
foreach ($html_object->find('img') as $image) {
$img_src = $image->src;
if (!empty($image->originalsrc)) {
$img_src = $image->originalsrc;
}
//图片已经设置了尺寸的不再修改
if (empty($image->width) && (strpos($img_src, '//data.') !== false || strpos($img_src, '//images.') !== false)) {//以data或者images开头的域名才能获取尺寸
$img_src_urls = parse_url(trim($img_src));
$request_size[$img_src_urls['host']][] = $img_src_urls['path'];
$file_path = '';
if (strpos($img_src, '//data.') !== false) {
$file_path = $dataPath . $img_src_urls['path'];
}
if (strpos($img_src, '//images.') !== false) {
$file_path = $imagesPath . $img_src_urls['path'];
}
if (is_file($file_path)) {
$properties = getimagesize($file_path);//获取图片属性
if (!empty($properties[0]) && !empty($properties[1])) {
$image->width = $properties[0];
$image->height = $properties[1];
}
}
}
}
return $html_object->save();
}
}

Loading…
Cancel
Save