帝国CMS 8.0 二次开发自动秒收录网址导航 AIseo网站收录
https://www.zhe7.com/ 正在开发中
e\class\userfun.php
/** * 自动生成静态文章函数 * 用于在API插入数据后自动执行静态生成 * @param int $classid 栏目ID * @param int $id 文章ID * @return array 生成结果 */ function user_auto_generate_html($classid, $id) { global $empire, $class_r, $public_r, $dbtbpre; try { // 1. 验证参数 if (empty($classid) || empty($id)) { throw new Exception('参数错误:栏目ID和文章ID不能为空'); } // 1.5 加载栏目配置 if (empty($class_r)) { // 加载栏目缓存文件 $class_cache_file = dirname(dirname(__FILE__)) . '/data/dbcache/class.php'; if (file_exists($class_cache_file)) { require_once($class_cache_file); } else { throw new Exception('无法加载栏目配置缓存文件'); } } // 2. 检查栏目配置 if (!isset($class_r[$classid])) { throw new Exception("栏目配置不存在:栏目ID={$classid}"); } if (empty($class_r[$classid]['tbname'])) { throw new Exception("栏目表名为空:栏目ID={$classid}"); } $tbname = $class_r[$classid]['tbname']; $sql = "SELECT * FROM {$dbtbpre}ecms_{$tbname} WHERE id = {$id} AND classid = {$classid}"; $article = $empire->fetch1($sql); if (empty($article)) { throw new Exception("文章不存在:栏目ID={$classid}, 文章ID={$id}"); } // 3. 检查是否已生成静态文件 if ($article['havehtml'] == 1) { return [ 'code' => 200, 'message' => '文章已生成静态文件', 'data' => [ 'classid' => $classid, 'id' => $id, 'titleurl' => $article['titleurl'] ] ]; } // 4. 直接调用帝国CMS的静态生成函数 // 加载帝国CMS的核心函数文件 $functions_file = dirname(__FILE__) . '/functions.php'; $t_functions_file = dirname(__FILE__) . '/t_functions.php'; if (file_exists($functions_file) && file_exists($t_functions_file)) { require_once($functions_file); require_once($t_functions_file); } else { throw new Exception('无法找到帝国CMS核心函数文件'); } // 检查DoGetHtml函数是否存在 if (!function_exists('DoGetHtml')) { throw new Exception('帝国CMS核心函数 DoGetHtml 缺失'); } // 5. 执行静态生成 // 使用输出缓冲捕获可能的错误输出 ob_start(); try { // 直接调用帝国CMS的静态生成函数 $result = DoGetHtml($classid, $id); $output = ob_get_clean(); if ($result) { // 重新查询文章获取生成的静态URL $updated_article = $empire->fetch1("SELECT titleurl, havehtml FROM {$dbtbpre}ecms_{$tbname} WHERE id = {$id}"); $titleurl = $updated_article['titleurl'] ?? ''; } else { throw new Exception('静态文件生成失败'); } } catch (Exception $e) { $output = ob_get_clean(); throw new Exception('DoGetHtml函数执行异常: ' . $e->getMessage()); } // 6. 验证生成结果 if (empty($titleurl)) { throw new Exception('静态文件生成失败'); } return [ 'code' => 200, 'message' => '静态文件生成成功', 'data' => [ 'classid' => $classid, 'id' => $id, 'titleurl' => $titleurl ] ]; } catch (Exception $e) { return [ 'code' => 500, 'message' => $e->getMessage(), 'data' => [ 'classid' => $classid, 'id' => $id ] ]; } }
接口端
// ========== 自动生成静态文件功能 ========== // 只有在文章不需要审核时才生成静态文件 if (!$need_audit) { safe_log("开始尝试自动生成静态文件,栏目ID: $classid, 文章ID: $id"); // 加载userfun.php中的静态生成函数 $userfun_file = dirname(__FILE__) . '/../e/class/userfun.php'; if (file_exists($userfun_file)) { require_once($userfun_file); safe_log("成功加载userfun.php文件"); // 检查静态生成函数是否存在 if (function_exists('user_auto_generate_html')) { safe_log("调用user_auto_generate_html函数生成静态文件"); $generate_result = user_auto_generate_html($classid, $id); if ($generate_result['code'] == 200) { safe_log("静态文件生成成功: " . $generate_result['message']); $html_generated = true; $titleurl = $generate_result['data']['titleurl'] ?? ''; // 静态生成成功后,更新数据库中的havehtml字段 $update_html_sql = "UPDATE `$main_table` SET havehtml = 1 WHERE id = $id"; safe_log("更新静态生成状态SQL: $update_html_sql"); $empire->query($update_html_sql); safe_log("已更新文章ID=$id的静态生成状态为已生成"); } else { safe_log("静态文件生成失败: " . $generate_result['message']); $html_generated = false; $generate_error = $generate_result['message']; } } else { safe_log("user_auto_generate_html函数不存在,跳过静态生成"); $html_generated = false; $generate_error = '生成函数未找到'; } } else { safe_log("userfun.php文件不存在,跳过静态生成"); $html_generated = false; $generate_error = 'userfun.php文件不存在'; } } else { safe_log("文章需要审核,跳过静态文件生成"); $html_generated = false; $generate_error = '文章待审核,暂不生成静态文件'; } // ========== 自动生成静态文件功能结束 ==========
|