添加AMP语法检测功能

hotfix/远程访问多媒体中心
尹诚诚 7 years ago
parent c1ee3e1e60
commit e6fb20bc14

@ -1,12 +1,11 @@
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Amp extends CI_Controller
{
class Amp extends CI_Controller {
function __construct()
{
function __construct() {
parent::__construct();
$this->permission->is_admin();
//$this->output->enable_profiler(TRUE);
@ -14,16 +13,14 @@ class Amp extends CI_Controller
$this->load->model('InfoMetas_model');
}
public function index()
{
public function index() {
$data = array();
$this->load->view('bootstrap3/header', $data);
$this->load->view('amp_editor');
$this->load->view('bootstrap3/footer');
}
public function edit($ic_id)
{
public function edit($ic_id) {
$data = array();
$data['information'] = $this->Information_model->detail_by_ic_id($ic_id);
if ($data['information'] == FALSE) {
@ -34,8 +31,7 @@ class Amp extends CI_Controller
$this->load->view('bootstrap3/footer');
}
public function auto_create($ic_id)
{
public function auto_create($ic_id) {
$data = array();
$data['information'] = $this->Information_model->detail_by_ic_id($ic_id);
if ($data['information'] == FALSE) {
@ -43,7 +39,6 @@ class Amp extends CI_Controller
}
//144.76.185.44:8029
//根据站点不同,配置不同参数
$site_code = strtolower($this->config->item('site_code'));
switch ($site_code) {
@ -79,8 +74,7 @@ class Amp extends CI_Controller
echo json_encode(array('result' => 'no', 'data' => '不知道哪里错了,看代码'));
}
public function edit_save()
{
public function edit_save() {
$data = array();
$ic_id = $this->input->post('ic_id');
$textarea_htmlcode = $this->input->post('textarea_htmlcode');
@ -92,8 +86,20 @@ class Amp extends CI_Controller
}
//CH的页面没有canonical内容在这里帮补上
$textarea_htmlcode=str_replace('<!--@CANONICAL@-->',$this->config->item('site_url'). $data['information']->ic_url, $textarea_htmlcode);
$textarea_htmlcode = str_replace('<!--@CANONICAL@-->', $this->config->item('site_url') . $data['information']->ic_url, $textarea_htmlcode);
//AMP格式验证
//把AMP网页内容到purifycss处理内置了AMP-Validator
$purifycss_server = 'http://184.172.113.216:33033/';
if ($this->config->item('site_code' == 'gm')) {//德语站点使用自己的css处理服务器
$purifycss_server = 'http://158.177.67.52:33033/';
}
$validator_result = GET_HTTP($purifycss_server, 'amp_source=' . urlencode($textarea_htmlcode), 'POST');
$validator_result = json_decode($validator_result);
if ($validator_result->status == 'FAIL') {
echo json_encode(array('name' => 'no', 'value' => $validator_result->errors));
return;
}
$amp = $this->InfoMetas_model->get($ic_id, 'AMP');
if ($amp === false) {
$this->InfoMetas_model->add($ic_id, 'AMP', $textarea_htmlcode);

@ -18,7 +18,9 @@
<div class="preview_box" id="iframe_box"></div>
<div class="code_editor" id="editor"></div>
</div>
<hr/>
<form name="form_amp_code" id="form_amp_code" method="post" action="<?php echo site_url('amp/edit_save') ?>">
<textarea id="textarea_htmlcode" name="textarea_htmlcode" style="display: none;"></textarea>
<input type="hidden" name="ic_id" value="<?php echo $information->ic_id; ?>"/>
@ -51,10 +53,10 @@
<div class="col-sm-4">
<button type="button" class="btn btn-primary" onclick="submit_amp_editor();">保存</button>
<?php if($this->config->item('site_code')=='cht'){ ?>
<a href="http://192.155.224.195:8080/guide-use.php/amp_loader/test/?info_url=<?php echo $information->ic_url ?>"
target="_blank">看看</a>
<?php } ?>
<?php if ($this->config->item('site_code') == 'cht') { ?>
<a href="http://192.155.224.195:8080/guide-use.php/amp_loader/test/?info_url=<?php echo $information->ic_url ?>"
target="_blank">看看</a>
<?php } ?>
</div>
<div class="col-sm-4">
@ -65,6 +67,9 @@
</div>
</div>
</form>
<div id="amp_error">
</div>
<hr/>
</div>
@ -73,57 +78,77 @@
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.9/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/eclipse");
editor.session.setMode("ace/mode/html");
editor.setOptions({
autoScrollEditorIntoView: true,
wrap: true
});
editor.getSession().on('change', function (e) {
update_preview();
});
var temp_div = document.createElement("div");
temp_div.innerHTML = $('#amp_html').html();
editor.session.setValue(temp_div.textContent);
function update_preview() {
//考虑做成异步操作,降低页面卡顿
var iframe = document.createElement('iframe');
iframe.setAttribute('frameBorder', '0');
iframe.setAttribute('id', 'previewIframe');
iframe.setAttribute('height', '100%');
iframe.setAttribute('width', '100%');
iframe.setAttribute('name', 'AMP Playground Output');
$('#iframe_box').html('');
$('#iframe_box').append(iframe);
var doc = iframe.contentDocument || iframe.document;
doc.open();
doc.write(editor.getValue());
doc.close();
}
var editor = ace.edit("editor");
editor.setTheme("ace/theme/eclipse");
editor.session.setMode("ace/mode/html");
editor.setOptions({
autoScrollEditorIntoView: true,
wrap: true
});
function submit_amp_editor() {
$('#textarea_htmlcode').text(editor.getValue()).html();
submitForm('form_amp_code');
}
editor.getSession().on('change', function (e) {
update_preview();
});
function auto_create_amp(obj) {
$(obj).text('转换中...');
$(obj).enable(false);
$.getJSON("<?php echo site_url('amp/auto_create/' . $information->ic_id) ?>", function (result) {
$.each(result, function (i, field) {
if (i == 'data') {
editor.setValue(field.amp);
}
});
$(obj).text('转换完成!');
$(obj).enable(true);
});
var temp_div = document.createElement("div");
temp_div.innerHTML = $('#amp_html').html();
editor.session.setValue(temp_div.textContent);
}
function update_preview() {
//考虑做成异步操作,降低页面卡顿
var iframe = document.createElement('iframe');
iframe.setAttribute('frameBorder', '0');
iframe.setAttribute('id', 'previewIframe');
iframe.setAttribute('height', '100%');
iframe.setAttribute('width', '100%');
iframe.setAttribute('name', 'AMP Playground Output');
$('#iframe_box').html('');
$('#iframe_box').append(iframe);
var doc = iframe.contentDocument || iframe.document;
doc.open();
doc.write(editor.getValue());
doc.close();
}
function submit_amp_editor() {
$('#amp_error').html('');
$('#textarea_htmlcode').text(editor.getValue()).html();
$("#form_amp_code").ajaxSubmit({
success: function (responseText, statusText, xhr, form) {
if (responseText.name === 'no') {
error_msg='<h3>AMP validation FAIL</h3>';
$.each(responseText.value, function (index, element) {
error_msg=error_msg+'<p>line '+element.line+' col '+element.col+' :'+element.message+'</p>';
});
$.modaldialog.error('AMP语法错误请看下方错误详情');
$('#amp_error').html(error_msg);
} else {
$.modaldialog.success(responseText.value);
}
},
error: function (responseText, statusText, xhr, form) {
$.modaldialog.error("Action unsuccessful. Try again later.");
},
dataType: "json",
timeout: 30000
});
return false
}
function auto_create_amp(obj) {
$(obj).text('转换中...');
$(obj).enable(false);
$.getJSON("<?php echo site_url('amp/auto_create/' . $information->ic_id) ?>", function (result) {
$.each(result, function (i, field) {
if (i == 'data') {
editor.setValue(field.amp);
}
});
$(obj).text('转换完成!');
$(obj).enable(true);
});
}
</script>

@ -42,7 +42,7 @@
<li><a href="<?php echo site_url('sendmail') ?>" >订单邮件管理</a> </li>
<li><a href="http://share.chtcdn.com/info.php/infoshare/" target="_blank" >信息分享平台</a> </li>
<li><a href="<?php echo site_url('welcome/create_all_urls') ?>" target="_blank" >全站静态化更新</a> </li>
<li><a href="<?php echo site_url('welcome/create_all_urls/amp') ?>" target="_blank" >全站AMP更新</a> </li>
<!--<li><a href="<?php echo site_url('welcome/create_all_urls/amp') ?>" target="_blank" >全站AMP更新</a> </li>-->
<li class="divider"></li>

Loading…
Cancel
Save