123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace app\index\controller;
- use think\Db;
- use think\Queue;
- use think\Controller;
-
- /*
- *
- */
- class Cms extends Controller
- {
- /*
- * 队列处理标签
- * https://queue.dev.zx2049.com/index/Cms/tagHandle
- */
- public function tagHandle()
- {
- $h = date('H',time());
- $h = (int)$h;
- if($h > 8 && $h < 18){
- //总共10小时
- // 1.当前任务将由哪个类来负责处理。
- // 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法
- $jobHandlerClassName = 'app\jobs\cms\Tag';
-
- // 2.当前任务归属的队列名称,如果为新队列,会自动创建
- $jobQueueName = "TagJobQueue";
-
- // 3.当前任务所需的业务数据, 不能为 resource 类型,其他类型最终将转化为json形式的字符串
- // (jobData 为对象时,存储其public属性的键值对 )
-
-
- $area = Db::connect('db1')
- ->table('ey_citysite')
- ->where(['level'=>1,'is_open'=>1,'status'=>1])
- //->limit(1)
- ->select();
- //dump($area);
- $area[] = [
- 'id' => 10000,
- 'name' => '主站',
- ];
-
- foreach ($area as $k=>$v){
- //http://console.zx2049.com/api/cms/tagHandle?taskid=10&diqu=1
- $jobData = ['ts' => time(),'url'=>'http://console.zx2049.com/api/cms/tagHandle','taskid'=>10, 'area_id' => $v['id'], 'area_name' => $v['name']];//当前任务所需的业务数据
- // 4.将该任务推送到消息队列,等待对应的消费者去执行
- $isPushed = Queue::push($jobHandlerClassName, $jobData, $jobQueueName);
-
- // database 驱动时,返回值为 1|false;redis 驱动时,返回值为 随机字符串|false
- //域名解析存在两条 *.dev
- //这里明明返回0 0 !== false 是成立的
- //以为一直成功 结果根本没插入数据库
- //既然能执行到这里的代码 为何无法插入数据库 返回0
- if( $isPushed !== false ){
- echo date('Y-m-d H:i:s') . " a new Hello Job is Pushed to the MQ" . "<br>";
- }else{
- echo 'Oops, something went wrong.';
- }
- echo "<br/>";
- }
- }else{
- echo "不在任务时间段内".$h;
- }
- }
-
- /*
- * 队列处理任务
- * https://queue.dev.zx2049.com/index/Cms/taskHandle
- */
- public function taskHandle()
- {
- $h = date('H', time());
- $h = (int)$h;
- if ($h > 8 && $h < 18) {
- // 1.当前任务将由哪个类来负责处理。
- // 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法
- $jobHandlerClassName = 'app\jobs\cms\Task';
-
- // 2.当前任务归属的队列名称,如果为新队列,会自动创建
- $jobQueueName = "TaskJobQueue";
-
- // 3.当前任务所需的业务数据, 不能为 resource 类型,其他类型最终将转化为json形式的字符串
- // (jobData 为对象时,存储其public属性的键值对 )
-
-
- $area = Db::connect('db1')
- ->table('ey_citysite')
- ->where(['level' => 1, 'is_open' => 1, 'status' => 1])
- //->limit(1)
- ->select();
- //dump($area);
- $area[] = [
- 'id' => 10000,
- 'name' => '主站',
- ];
-
- foreach ($area as $k => $v) {
- //http://console.zx2049.com/api/cms/tagHandle?taskid=10&diqu=1
- $jobData = ['ts' => time(), 'url' => 'http://console.zx2049.com/api/cms/taskHandle', 'taskid' => 10, 'area_id' => $v['id'], 'area_name' => $v['name']];//当前任务所需的业务数据
- // 4.将该任务推送到消息队列,等待对应的消费者去执行
- $isPushed = Queue::push($jobHandlerClassName, $jobData, $jobQueueName);
-
- // database 驱动时,返回值为 1|false;redis 驱动时,返回值为 随机字符串|false
- //域名解析存在两条 *.dev
- //这里明明返回0 0 !== false 是成立的
- //以为一直成功 结果根本没插入数据库
- //既然能执行到这里的代码 为何无法插入数据库 返回0
- if ($isPushed !== false) {
- echo date('Y-m-d H:i:s') . " a new Hello Job is Pushed to the MQ" . "<br>";
- } else {
- echo 'Oops, something went wrong.';
- }
- echo "<br/>";
- }
- }else{
- echo "不在任务时间段内".$h;
- }
- }
-
- /*
- * 百度提交api
- * 主动推送
- * 晚上12点执行
- * https://queue.dev.zx2049.com/index/Cms/baiduSearchApi
- */
- public function baiduSearchApi(){
- $num = 1;
-
- $area = Db::connect('db1')
- ->table('ey_citysite')
- ->where(['level' => 1, 'is_open' => 1, 'status' => 1])
- //->limit(1)
- ->select();
- //dump($area);
- $area[] = [
- 'id' => 10000,
- 'name' => '主站',
- 'domain' => 'www'
- ];
- //dump($area);
- $arr = [];
- foreach ($area as $k => $v) {
-
- //31区域(30+1-www) 山东关闭, 11个无法添加站点 , 剩余19个
- //'yunnan','guizhou','zhongqing','sichuan' 这4个未添加站点
- $pl = ['sx','xinjiangweiwuer','ningxia','qinghai','gansu','xicangzizhi','hainan','heilongjiang','jilin','liaoning','namenggu','yunnan','guizhou','zhongqing','sichuan'];
- //echo $v['id'].'<br/>';
-
- if(in_array($v['domain'], $pl)){
- //echo "---------------------<br/>";
- echo $num.'-'.$v['domain']."<font style='color:red;'>不存在站点,无法添加</font>!<br/>";
- //echo "---------------------<br/>";
- $num++;
- }else{
- $list = Db::connect('console')->table('yzn_task')
- ->where('diqu','=',$v['id'])
- ->where('is_run','=',1)
- ->where('is_ts','=',0)
- ->limit(1)
- ->select();
-
- foreach ($list as $in => $row){
- //echo $row['link'].'<br/>';
- $arr[$v['id']]['list'][] = $row['link'];
- $arr[$v['id']]['ids'][] = $row['id'];
- }
- $arr[$v['id']]['domain'] = $v['domain'];
-
- if(empty($arr[$v['id']]['list'])){
- //echo "---------------------<br/>";
- echo $num.'-'.$v['domain']."<font style='color:green;'>获取执行记录为空,无最新记录!</font><br/>";
- $num++;
- //echo "---------------------<br/>";
- }
- }
-
- }
-
- foreach ($arr as $k => $v){
-
- //查询今日已发了几条
- $count = Db::connect('console')->table('yzn_task')->where([
- 'diqu' => $k,
- 'is_run' => 1,
- 'is_ts' => 1,
- ])
- ->whereTime('ts_time', 'today') //推送时间
- ->count();
-
- // dump($count); die;
- if($count >= 10){
- //超过10条
- //假设没有手动提交的记录 只有api提交
- echo $num.'-'.$v['domain']."<font style='color:blue;'>已提交达到10条记录</font><br/>";
- $num++;
- }else {
- $urls = array();
- if (!empty($v['list'])) {
-
- foreach ($v['list'] as $key => $item) {
- $urls[] = $item;
- }
-
- /* dump($urls);
- dump($v['domain']);
- dump($v['ids']);*/
-
- if (!empty($urls)) {
- $api = 'http://data.zz.baidu.com/urls?site=https://' . $v['domain'] . '.gdzcfw.com&token=Jc7Qp1sfpQYGcaqY';
- $ch = curl_init();
- $options = array(
- CURLOPT_URL => $api,
- CURLOPT_POST => true,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_POSTFIELDS => implode("\n", $urls),
- CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
- );
- curl_setopt_array($ch, $options);
- $result_s = curl_exec($ch);
- $result = json_decode($result_s, true);
- if (isset($result['success']) && (int)$result['success'] > 0) {
- //推送成功 按站点批量提交
- Db::connect('console')->table('yzn_task')->where('id', 'in', $v['ids'])->update(['is_ts' => 1, 'ts_time' => time()]);
- //插入日志
- $insert_data = [
- 'catid' => 11,
- 'title' => '域名' . $v['domain'] . '提交情况-本次条数(' . $result['success'] . ')-剩余('.$result['remain'].')',
- 'content' => json_encode($arr) . '<br>百度原始响应结果:<br/>' . $result_s,
- 'create_time' => time(),
- 'update_time' => time(),
- 'status' => 1,
- 'site_id' => 1,
- 'cid' => 0,
- ];
- Db::connect('console')->table('yzn_log')->insert($insert_data);
- echo $num.'-'.$insert_data['title']."<br/>";
- $num++;
- } else {
- $insert_data = [
- 'catid' => 11,
- 'title' => '域名' . $v['domain'] . '提交情况(失败)',
- 'content' => $result_s,
- 'create_time' => time(),
- 'update_time' => time(),
- 'status' => 0, //标识 失败
- 'site_id' => 1,
- 'cid' => 0,
- ];
- Db::connect('console')->table('yzn_log')->insert($insert_data);
- echo $num.'-'.$insert_data['title']."<br/>";
- $num++;
- }
- }
- }
- }
- }
- //dump($arr);
- }
- }
|