联系官方销售客服

1835022288

028-61286886

二次开发 版主:官方研发技术组
文件原始地址新建地址在文件中添加新方法用于定时发布文章代码逻
类型:迅睿CMS 更新时间:2020-01-20 11:59:26 文章
PHP程序里面



PHP文件:dayrui\Core\Controllers\Api\Run.php

原始URL地址:/index.php?s=api&c=run


新建URL地址:/index.php?s=api&c=run&m=time_post


在Run.php文件中添加新方法time_post,用于定时发布文章,代码逻辑是抽取index方法的发布文章逻辑

public function time_post() {

   // 未到时间
   if (\Phpcmf\Service::L('input')->get_cookie('cron')) {
       exit('未到发布文章时间');
   }

   foreach ($this->site_info as $siteid => $site) {
       // 删除网站首页
       if ($site['SITE_INDEX_HTML']) {
           @unlink(\Phpcmf\Service::L('html')->get_webpath($siteid,'site', 'index.html'));
           @unlink(\Phpcmf\Service::L('html')->get_webpath($siteid,'site', 'mobile/index.html'));
       }
       // 模块
       $module = \Phpcmf\Service::L('cache')->get('module-'.$siteid.'-content');
       if ($module) {
           foreach ($module as $dir => $mod) {
               // 删除模块首页
               if ($mod['is_index_html']) {
                   if ($mod['domain']) {
                       // 绑定域名时
                       $file = 'index.html';
                   } else {
                       $file = ltrim(\Phpcmf\Service::L('Router')->remove_domain(MODULE_URL), '/'); // 从地址中获取要生成的文件名;
                   }
                   if ($file) {
                       @unlink(\Phpcmf\Service::L('html')->get_webpath($siteid, $dir, $file));
                       @unlink(\Phpcmf\Service::L('html')->get_webpath($siteid, $dir, 'mobile/'.$file));
                   }
               }
               // 定时发布动作
               $db = \Phpcmf\Service::M('Content', $dir);
               $db->_init($dir, $siteid);
               $this->module = \Phpcmf\Service::L('cache')->get('module-'.$siteid.'-'.$dir);
               $times = $db->table($siteid.'_'.$dir.'_time')->where('posttime < '.SYS_TIME)->getAll();
               if ($times) {
                   foreach ($times as $t) {
                       $rt = $db->post_time($t);
                       if (!$rt['code']) {
                           log_message('error', '定时发布('.$t['id'].')失败:'.$rt['msg']);
                       }
                   }
               }

           }
       }

   }

   exit('Run time_post');
}


index方法注释了原来发布文章的逻辑

public function index() {

       // 未到时间
       if (\Phpcmf\Service::L('input')->get_cookie('cron')) {
           exit('未到执行时间');
       }

       // 批量执行站点动作
//        foreach ($this->site_info as $siteid => $site) {
//            // 删除网站首页
//            if ($site['SITE_INDEX_HTML']) {
//                @unlink(\Phpcmf\Service::L('html')->get_webpath($siteid,'site', 'index.html'));
//                @unlink(\Phpcmf\Service::L('html')->get_webpath($siteid,'site', 'mobile/index.html'));
//            }
//            // 模块
//            $module = \Phpcmf\Service::L('cache')->get('module-'.$siteid.'-content');
//            if ($module) {
//                foreach ($module as $dir => $mod) {
//                    // 删除模块首页
//                    if ($mod['is_index_html']) {
//                        if ($mod['domain']) {
//                            // 绑定域名时
//                            $file = 'index.html';
//                        } else {
//                            $file = ltrim(\Phpcmf\Service::L('Router')->remove_domain(MODULE_URL), '/'); // 从地址中获取要生成的文件名;
//                        }
//                        if ($file) {
//                            @unlink(\Phpcmf\Service::L('html')->get_webpath($siteid, $dir, $file));
//                            @unlink(\Phpcmf\Service::L('html')->get_webpath($siteid, $dir, 'mobile/'.$file));
//                        }
//                    }
//                    // 定时发布动作
//                    $db = \Phpcmf\Service::M('Content', $dir);
//                    $db->_init($dir, $siteid);
//                    $this->module = \Phpcmf\Service::L('cache')->get('module-'.$siteid.'-'.$dir);
//                    $times = $db->table($siteid.'_'.$dir.'_time')->where('posttime < '.SYS_TIME)->getAll();
//                    if ($times) {
//                        foreach ($times as $t) {
//                            $rt = $db->post_time($t);
//                            if (!$rt['code']) {
//                                log_message('error', '定时发布('.$t['id'].')失败:'.$rt['msg']);
//                            }
//                        }
//                    }
//
//                }
//            }
//
//        }

       // 执行队列
       $i = \Phpcmf\Service::M('cron')->run_cron();

       // 3天未付款的清理
       \Phpcmf\Service::M('pay')->clear_paylog();

       // 100秒调用本程序
       \Phpcmf\Service::L('input')->set_cookie('cron', 1, 100);

       // 任务计划
       \Phpcmf\Hooks::trigger('cron');

       // 项目计划
       if (is_file(MYPATH.'Config/Cron.php')) {
           require MYPATH.'Config/Cron.php';
       }

       // 为插件单独执行计划
       $local = dr_dir_map(dr_get_app_list(), 1);
       if ($local) {
           foreach ($local as $dir) {
               $path = dr_get_app_dir($dir);
               if (is_file($path.'Config/Cron.php')
                   && is_file($path.'Config/App.php')) {
                   require $path.'Config/Cron.php';
               }
           }
       }


       exit('Run '.$i);

  }


但是执行/index.php?s=api&c=run&m=time_post时,未到发布时间,文章就发布出去了,而且是重复的两篇文章


希望管理员帮忙看看是什么原因,十分感谢!