背景:解决手动提交小程序资源的繁琐,直接在cms后台进行提交.本文完全适用与使用pbootcms建站的网站小程序,本文采用的是php语言,所以针对php的站点也有较大的参考.其他语言的话只有一部分参考价值
主要修改的地方/文件:
前端展示:
\apps\admin\view\default\content\content.html
后端方法:
\apps\admin\controller\content\ContentController.php
\apps\common\function.php
修改内容如下:
content.html中添加按钮入口,这里添加了三个,我们实际应用中视情况添加
<button type="submit" name="submit" value="baixcx" class="layui-btn layui-btn-sm">百度小程序推送</button>
<button type="submit" name="submit" value="baixcx_chanpin" class="layui-btn layui-btn-sm">百度小程序_非新闻的推送</button>
<button type="submit" name="submit" value="baixcx_path" class="layui-btn layui-btn-sm">百度小程序输出路径——只是新闻</button>
ContentController.php中添加对应的处理逻辑:
case 'baixcx':
$flag_num = 0;
$list = post('list');
// $urls = post('urls');
if (! $list) {
alert_back('请选择要推送的内容!');
}
//$App_Key = $this->config('App_Key');
// 开始推送
$domain = "/pages/news-detail/news-detail?id=";
foreach ($list as $key => $value) {
$url =$domain . $value;
$this->log('百度小程序收录推送:' . $url);
$post_urls .= $url . ',';
$flag_num++;
}
if(strlen($post_urls) > 5){
$result = baiduMini(substr($post_urls,0,-1));
if ($result->errno == 0 || $result->errno == '0') {
alert_back('成功推送' . $flag_num . '条');
} else {
alert_back('百度小程序收录推送发生错误:' . $result->msg);
}
}else{
alert_back('百度小程序收录推送发生错误_链接获取失败');
}
case 'baixcx_path':
$flag_num = 0;
$list = post('list');
// $urls = post('urls');
if (! $list) {
alert_back('请选择要推送的内容!');
}
//$App_Key = $this->config('App_Key');
// 开始推送
$domain = "/pages/news-detail/news-detail?id=";
foreach ($list as $key => $value) {
$url =$domain . $value;
$this->log('百度小程序收录推送:' . $url);
$post_urls .= $url . '<br/>';
$flag_num++;
}
echo(substr($post_urls,0,-1));
//$result = post_baidu($api, $post_urls);
case 'baixcx_chanpin':
$flag_num = 0;
$list = post('list');
// $urls = post('urls');
if (! $list) {
alert_back('请选择要推送的内容!');
}
// 开始推送
$domain = "/pages/product-detail/product-detail?id=";
foreach ($list as $key => $value) {
$url =$domain . $value;
$this->log('百度普通收录推送:' . $url);
$post_urls .= $url . ',';
$flag_num++;
}
if(strlen($post_urls) > 5){
$result = baiduMini(substr($post_urls,0,-1));
if ($result->errno == 0 || $result->errno == '0') {
alert_back('成功推送' . $flag_num . '条');
} else {
alert_back('百度小程序收录推送发生错误:' . $result->msg);
}
}else{
alert_back('百度小程序收录推送发生错误_链接获取失败');
}
function.php中添加提交方法,这里可以多加一个参数,表示提交等级的参数
function baiduMini($url){
$str=file_get_contents('https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id='百度的key'&client_secret='百度的Secret'&scope=smartapp_snsapi_base');
$array=json_decode($str,true);
$token=$array["access_token"];
//$data['type'] = rand(0,1);//· 0:周级提交,一周左右生效; 1:天级提交,2~3 天生效; 2:小时级提交,1 小时内生效;3:优质资源提交,1 小时内生效
$data['type'] = 0;
$data['url_list'] = $url;
$api = 'https://openapi.baidu.com/rest/2.0/smartapp/access/submitsitemap/api?access_token='.$token;
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_NOBODY => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$data = json_encode($result, true);
return $data;
}
后台页面的样子:
示例代码在下面:
https://download.csdn.net/download/u014401637/81877902https://download.csdn.net/download/u014401637/81877902
====================pbootcms网站以及对应的百度小程序样式=================
钢背楞_钢支撑-剪力墙加固件-山东方正新型建筑材料有限公司http://www.sdjlq.com/小程序:
智能小程序https://mbd.baidu.com/ma/s/kxUuMfLi
可出售对应的小程序源码
|