解决了500错误

This commit is contained in:
LiuZhengYu 2026-04-27 16:46:44 +08:00
parent ffc60259e5
commit 462bb010be
3 changed files with 217 additions and 97 deletions

View File

@ -80,6 +80,7 @@ function verifyuser() {
}
if ($onlinerow['usrchg'] == 2) {
header('_re: true');
exit;
}
if ($onlinerow['exptimes'] > time())
return $auth;
@ -91,7 +92,7 @@ function verifyuser() {
$authstr = json_encode($auth, JSON_PARTIAL_OUTPUT_ON_ERROR);
$enauth = encrypt($authstr, 'E', $_token['salt']);
if ($_token['type'] == 'cookie') {
if (!headers_sent() && $_token['type'] == 'cookie') {
$headercookie = 'Set-Cookie: ' . $_token['field'] . '=' . $enauth . '; expires=' . gmdate('D, d-M-Y H:i:s T', time() + $_token['swapsec'] + $_token['expsec']) . '; path=/; httponly; SameSite=None; Secure';
header($headercookie);
} else {

View File

@ -546,6 +546,94 @@ class keywordManage
}
//AI相关功能
// public static function json_AIFunction()
// {
// global $db;
// $rsuser = verifyfast();
// $post = new \ciy\post();
// $function_type = trim($post->get('param'));
// // AI配置 阿里云通义千问
// $aiConfig = [
// // https://dashscope-inner.aliyuncs.com
// 'baseurl' => 'https://dashscope.aliyuncs.com/compatible-mode/v1',
// 'aikey' => 'sk-0cd7b8c72ae04d049306e345f17c6790',
// 'model' => 'qwen-turbo',
// 'maxtoken' => 2000
// ];
// try {
// $openai = new \ciy\openai($aiConfig);
// // 角色定位
// if ($function_type === '修改') {
// $content = trim($post->get('content'));
// if (empty($content)) {
// return errjson('请先等待生成关键词后再操作');
// }
// $system = '你是SEO关键词清洗优化助手严格遵守
// 1. 核心词绝对不能修改、删除、替换;
// 2. 只剔除不通顺、重复、无意义、垃圾关键词;
// 3. 可优化前后修饰词,不改变核心;
// 4. 输出严格一行一个,无解释、无符号、无序号;
// 5. 只净化,不新增。';
// $prompt = "请清洗优化以下关键词:\n{$content}";
// } else {
// $cateid = trim($post->get('cateid'));
// $dim1 = trim($post->get('dim1'));
// $new_dim1 = empty($dim1) ? "自行补充,与核心词高度关联" : $dim1;
// $dim2 = trim($post->get('dim2'));
// $core_words = explode("\n", $dim2);
// $core_words = array_filter(array_map('trim', $core_words));
// $filter_str = implode('、', $core_words);
// $dim3 = trim($post->get('dim3'));
// $new_dim3 = empty($dim3) ? "自行补充,与核心词高度关联" : $dim3;
// if (empty($cateid)) {
// return errjson('请选择所属栏目');
// }
// if(empty($dim2)){
// return errjson('请填写核心关键词');
// }
// // AI 联想
// $system = '你是专业的SEO关键词生成助手严格遵守
// 1. 核心词绝对不变;
// 2. 根据核心词自动生成合理的前缀词+核心词+后缀词组合的关键词;
// 3. 剔除不通顺、重复、无意义内容;
// 4. 输出一行一个,无多余内容;
// 5. 生成数量 7-12 个。';
// $prompt = "前缀词:{$new_dim1} ,核心词:{$filter_str},后缀词:{$new_dim3}。请根据提供的前缀词、核心词、后缀词生成优质关键词";
// }
// // 发送请求
// $openai->newsystem($system);
// $res = $openai->completion($prompt);
// // 修复:统一处理返回值(字符串/数组都兼容)
// $resultContent = '';
// if (is_array($res)) {
// $resultContent = isset($res['content']) ? trim($res['content']) : '';
// } else {
// $resultContent = trim($res);
// }
// if (!empty($resultContent)) {
// return succjson(['data' => $resultContent]);
// } else {
// return errjson('AI处理失败未返回有效内容');
// }
// } catch (\Exception $e) {
// return errjson('接口异常:' . $e->getMessage());
// }
// }
// }
public static function json_AIFunction()
{
global $db;
@ -554,17 +642,38 @@ class keywordManage
$function_type = trim($post->get('param'));
// AI配置
// 日志函数:把信息写到文件里
$logPath = '/var/log/ai_debug.log';
$writeLog = function ($msg) use ($logPath) {
$time = date('Y-m-d H:i:s');
file_put_contents($logPath, "[{$time}] {$msg}\n", FILE_APPEND);
};
// 记录开始
$writeLog("=== 开始执行AI联想 ===");
$aiConfig = [
'baseurl' => 'https://dashscope.aliyuncs.com/compatible-mode/v1',
'aikey' => 'sk-0cd7b8c72ae04d049306e345f17c6790',
'model' => 'qwen-turbo',
'maxtoken' => 2000
];
// ==========================================================
$writeLog("配置加载完成baseurl: " . $aiConfig['baseurl']);
try {
// 1. 检查openai类是否存在
if (!class_exists('\ciy\openai')) {
throw new \Exception("关键错误:\ciy\openai 类不存在!请检查文件是否加载");
}
$writeLog("openai类存在准备实例化");
// 2. 实例化
$openai = new \ciy\openai($aiConfig);
// 角色定位
$writeLog("openai实例化成功");
// 3. 处理参数
if ($function_type === '修改') {
$content = trim($post->get('content'));
if (empty($content)) {
@ -576,27 +685,27 @@ class keywordManage
3. 可优化前后修饰词,不改变核心;
4. 输出严格一行一个,无解释、无符号、无序号;
5. 只净化,不新增。';
$prompt = "请清洗优化以下关键词:\n{$content}";
} else {
$cateid = trim($post->get('cateid'));
$dim1 = trim($post->get('dim1'));
$new_dim1 = empty($dim1) ? "自行补充,与核心词高度关联" : $dim1;
$dim2 = trim($post->get('dim2'));
$dim3 = trim($post->get('dim3'));
$new_dim1 = empty($dim1) ? "自行补充,与核心词高度关联" : $dim1;
$new_dim3 = empty($dim3) ? "自行补充,与核心词高度关联" : $dim3;
$core_words = explode("\n", $dim2);
$core_words = array_filter(array_map('trim', $core_words));
$filter_str = implode('、', $core_words);
$dim3 = trim($post->get('dim3'));
$new_dim3 = empty($dim3) ? "自行补充,与核心词高度关联" : $dim3;;
if (empty($cateid)) {
return errjson('请选择所属栏目');
}
if (empty($dim2)) {
return errjson('请填写核心关键词');
}
// AI 联想
$system = '你是专业的SEO关键词生成助手严格遵守
1. 核心词绝对不变;
2. 根据核心词自动生成合理的前缀词+核心词+后缀词组合的关键词;
@ -604,17 +713,36 @@ class keywordManage
4. 输出一行一个,无多余内容;
5. 生成数量 7-12 个。';
$prompt = "前缀词:{$new_dim1} ,核心词:{$filter_str},后缀词:{$new_dim3}。请根据提供的前缀词、核心词、后缀词生成优质关键词";
$prompt = "前缀词:{$new_dim1},核心词:{$filter_str},后缀词:{$new_dim3}。请根据提供的内容生成优质关键词";
}
$openai->newsystem($system);
$res = $openai->completion($prompt);
$writeLog("参数处理完成prompt长度" . strlen($prompt));
if (is_array($res) && !empty($res['content'])) {
return succjson(['data' => trim($res['content'])]);
} else {
return errjson('AI处理失败' . $res);
// 4. 设置system并调用
$openai->newsystem($system);
$writeLog("system设置完成");
$res = $openai->completion($prompt);
$writeLog("completion调用返回结果类型" . gettype($res) . ",内容:" . json_encode($res));
// 5. 处理返回
$resultContent = '';
if (is_array($res)) {
$resultContent = isset($res['content']) ? trim($res['content']) : '';
} elseif (is_string($res)) {
$resultContent = trim($res);
}
if (!empty($resultContent)) {
$writeLog("成功获取内容,长度:" . strlen($resultContent));
return succjson(['data' => $resultContent]);
} else {
throw new \Exception("AI处理失败返回内容为空原始结果" . json_encode($res));
}
} catch (\Exception $e) {
// 把错误完整写入日志
$errMsg = "异常捕获:" . $e->getMessage() . " 堆栈:" . $e->getTraceAsString();
$writeLog($errMsg);
return errjson('接口异常:' . $e->getMessage());
}
}

View File

@ -96,7 +96,7 @@ class openai {
$data['max_tokens'] = toint($this->aicfg['maxtoken']);
if ($this->fp > -999)
$data['frequency_penalty'] = $this->fp;
if ($this->pp > -999)
if ($pp > -999)
$data['presence_penalty'] = $this->pp;
if ($this->tt > -999)
$data['temperature'] = $this->tt;
@ -119,17 +119,27 @@ class openai {
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// ====================== 修复:加了超时,不会再卡死!======================
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
// =====================================================================
$response = curl_exec($ch);
if ($this->debug) {
savelogfile('openai', '--------------------------------');
savelogfile('openai', $response);
savelogfile('openai', '');
}
curl_close($ch);
if ($response === false) {
$err = curl_error($ch);
$errno = curl_errno($ch);
curl_close($ch);
return 'ERR: ' . curl_error($ch) . ' (' . curl_errno($ch) . ')';
return 'ERR: '.$err.' ('.$errno.')';
}
curl_close($ch);
$json = json_decode($response, true);
if (isset($json['error_msg']))
return 'ERR: ' . $json['error_msg'];
@ -159,31 +169,6 @@ class openai {
if ($finish == 'length') {
if ($isjson) {
$message['content'] = self::fixjson($message['content']);
//以下暂时AI能力有限
// $context = mb_substr($content, -200, 200);
// $last_char = mb_substr($context, -1);
// $expected = '';
// switch ($last_char) {
// case '{': $expected = '对象未闭合'; break;
// case '[': $expected = '数组未闭合'; break;
// case ':': $expected = '值未完成'; break;
// case ',': $expected = '需要下一个元素'; break;
// }
// $tmp = <<<data
// 请严格作为JSON生成器继续输出必须遵循以下规则
// 1. 仅续写以下JSON片段绝对不要重复已有内容
// 2. 当前状态:{$expected},最后字符是"{$last_char}"
// 3. 保持语法正确,自动闭合未完成的字符串/括号
// 4. 如果原JSON是对象继续添加属性如果是数组继续添加元素
// 当前JSON片段仅作上下文参考不要重复
// {$context}
// 请直接输出JSON续写内容不要包含上下文片段
// data;
// $this->messages[] = array('role' => 'user', 'content' => $tmp);
} else {
$resultcontent .= $message['content'];
$this->messages[] = array('role' => 'user', 'content' => '请继续完成之前的对话');
@ -242,6 +227,12 @@ class openai {
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// ====================== 修复:加了超时 ======================
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
// ==========================================================
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) use ($cb) {
if ($this->debug) {
savelogfile('openai', substr($data, 6, -2));
@ -249,7 +240,7 @@ class openai {
$cb($data);
return strlen($data);
});
curl_exec($ch);
$response = curl_exec($ch);
if (curl_errno($ch))
return '错误: ' . curl_error($ch);
curl_close($ch);