AI 1 maand geleden
bovenliggende
commit
82a9acbdc4
1 gewijzigde bestanden met toevoegingen van 36 en 0 verwijderingen
  1. 36 0
      app/JsonRpc/NewsService.php

+ 36 - 0
app/JsonRpc/NewsService.php

@@ -269,6 +269,24 @@ class NewsService implements NewsServiceInterface
 
             $articleData = $data;
             unset($articleData['content']);
+            //自动处理缩略图、关键字、描述
+            if ($articleData['imgurl'] == '') {
+                //content中提取图片第一个图,正则提取
+                $reg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
+                preg_match_all($reg, $data['content'], $matches);
+                if (isset($matches[1][0])) {
+                    $articleData['imgurl'] = $matches[1][0];
+                }
+            }
+            if ($articleData['keyword'] == '') {
+                //提取标题+内容中的关键词
+                $articleData['keyword'] = $data['title'] . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
+            }
+            if ($articleData['introduce'] == '') {
+                //提取内容中的描述
+                $articleData['introduce'] = substr(str_replace(' ', '', strip_tags($data['content'])), 0, 100);
+            }
+
             $id = Article::insertGetId($articleData);
             $articleDataContent = [
                 'article_id' => $id,
@@ -393,6 +411,24 @@ class NewsService implements NewsServiceInterface
             unset($articleData['content']);
             unset($articleData['pid_arr']);
             unset($articleData['pid']);
+            //自动处理缩略图、关键字、描述
+            if ($articleData['imgurl'] == '') {
+                //content中提取图片第一个图,正则提取
+                $reg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
+                preg_match_all($reg, $data['content'], $matches);
+                if (isset($matches[1][0])) {
+                    $articleData['imgurl'] = $matches[1][0];
+                }
+            }
+            if ($articleData['keyword'] == '') {
+                //提取标题+内容中的关键词
+                $articleData['keyword'] = $data['title'] . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
+            }
+            if ($articleData['introduce'] == '') {
+                //提取内容中的描述
+                $articleData['introduce'] = substr(str_replace(' ', '', strip_tags($data['content'])), 0, 100);
+            }
+
             $id = Article::where(['id' => $data['id']])->update($articleData);
             $articleDataContent = [
                 'content' => $data['content'],