From d28f445990854823aaf69b205afebdd9c38934b0 Mon Sep 17 00:00:00 2001 From: chengxl Date: Sat, 17 May 2025 19:30:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/controller/Aiarticle.php | 33 +++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/application/api/controller/Aiarticle.php b/application/api/controller/Aiarticle.php index 3585b9c..e296272 100644 --- a/application/api/controller/Aiarticle.php +++ b/application/api/controller/Aiarticle.php @@ -1087,11 +1087,22 @@ class Aiarticle extends Base } //查询文章是否存在 $aWhere = ['article_id' => $iArticleId,'is_delete' => 2]; - $aAiArticle = Db::name('ai_article')->field('title_chinese as title,author,journal_id')->where($aWhere)->find(); + $aAiArticle = Db::name('ai_article')->field('title_chinese as title,author,journal_id,digest')->where($aWhere)->find(); if(empty($aAiArticle)){ return json_encode(['status' => 3, 'msg' => 'The article does not exist']); } + //数据处理 + $sTitle = empty($aAiArticle['title']) ? '' : $aAiArticle['title']; + $sTitle = mb_convert_encoding($sTitle, 'UTF-8', 'auto'); + // 截断处理 + $aAiArticle['title'] = $this->truncateByBytes($sTitle, 64); + + $sDigest = empty($aAiArticle['digest']) ? '' : $aAiArticle['digest']; + // 编码转换 + $sDigest = mb_convert_encoding($sDigest, 'UTF-8', 'auto'); + $aAiArticle['digest'] = $this->truncateByBytes($sDigest, 120); + //查询该模版是否推送到微信公众号 $aWhere['template_id'] = $iTemplateId; $aWhere['wechat_id'] = $sWechatId; @@ -1132,6 +1143,26 @@ class Aiarticle extends Base return $aResult; } + private function truncateByBytes($str, $maxBytes) { + $len = 0; + $result = ''; + $i = 0; + + while ($i < mb_strlen($str, 'UTF-8')) { + $char = mb_substr($str, $i, 1, 'UTF-8'); + $charBytes = strlen($char); // UTF-8 下实际字节数 + + if ($len + $charBytes > $maxBytes) { + break; + } + + $result .= $char; + $len += $charBytes; + $i++; + } + + return $result; + } /** * 获取Ai生成文章状态