wesmiler 1 year ago
parent
commit
dae56d3843
4 changed files with 67 additions and 2 deletions
  1. 1 1
      .env
  2. 61 0
      app/Helpers/common.php
  3. 3 1
      app/Services/Api/ArticleService.php
  4. 2 0
      app/Services/Common/ArticleService.php

+ 1 - 1
.env

@@ -70,4 +70,4 @@ SITE_NAME = UTC
 NICK_NAME = UTC
 VERSION = v1.2.0
 
-IMG_URL = http://localhost:8000/uploads
+IMG_URL = https://api.dyangsheng.com/uploads

+ 61 - 0
app/Helpers/common.php

@@ -875,6 +875,67 @@ if (!function_exists('get_web_url')) {
     }
 }
 
+
+if (!function_exists('set_format_content')) {
+
+    /**
+     * 获取文章图片格式化后内容
+     * @param $content
+     * @return false|mixed|string
+     */
+    function set_format_content($content)
+    {
+        if (empty($content)) {
+            return false;
+        }
+
+        $domain = request()->header('HOST');
+        if(preg_match("/127/", $domain)){
+            $host = env('IMG_URL','');
+            if(empty($host)){
+                $https = request()->secure();
+                $host = ($https ? 'https://' : 'http://') . $host . '/uploads';
+            }
+        }else{
+            $https = request()->secure();
+            $host = ($https ? 'https://' : 'http://') . $domain . '/uploads';
+        }
+
+        $content = str_replace("{$host}",'/uploads', htmlspecialchars_decode($content));
+        return  $content;
+    }
+}
+
+if (!function_exists('get_format_content')) {
+
+    /**
+     * 获取文章图片格式化后内容
+     * @param $content
+     * @return false|mixed|string
+     */
+    function get_format_content($content)
+    {
+        if (empty($content)) {
+            return false;
+        }
+
+        $domain = request()->header('HOST');
+        if(preg_match("/127/", $domain)){
+            $host = env('IMG_URL','');
+            if(empty($host)){
+                $https = request()->secure();
+                $host = ($https ? 'https://' : 'http://') . $host . '/uploads';
+            }
+        }else{
+            $https = request()->secure();
+            $host = ($https ? 'https://' : 'http://') . $domain . '/uploads';
+        }
+
+        $content = str_replace(["\"/uploads","'/uploads"],["\"{$host}","'{$host}"], htmlspecialchars_decode($content));
+        return  $content;
+    }
+}
+
 if (!function_exists('get_hash')) {
 
     /**

+ 3 - 1
app/Services/Api/ArticleService.php

@@ -95,6 +95,7 @@ class ArticleService extends BaseService
                 $item['time_text'] = $item['publish_at']? dateFormat(strtotime($item['publish_at'])) : '';
                 $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
                 $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
+                $item['content'] = $item['content']? get_format_content($item['content']) : '';
             }
         }
 
@@ -175,7 +176,7 @@ class ArticleService extends BaseService
             $info['time_text'] = $info['publish_at']? dateFormat(strtotime($info['publish_at'])) : '';
             $info['cover'] = get_image_url($info['cover']);
             $info['file_url'] = get_image_url($info['file_url']);
-            $info['content'] = htmlspecialchars_decode($info['content']);
+            $info['content'] = get_format_content($info['content']);
             $this->model->where(['id'=> $id])->increment('views',1);
             $info['views'] += intval($info['views']);
             RedisService::set($cacheKey, $info, rand(5,10));
@@ -207,6 +208,7 @@ class ArticleService extends BaseService
                 $info['cover'] = get_image_url($info['cover']);
                 $info['file_url'] = get_image_url($info['file_url']);
                 $info['content'] = $info['content']? str_replace("\n","</br>", $info['content']):'';
+                $info['content'] = get_format_content($info['content']);
                 RedisService::set($cacheKey, $info, rand(30,60));
             }
         }

+ 2 - 0
app/Services/Common/ArticleService.php

@@ -77,6 +77,7 @@ class ArticleService extends BaseService
         foreach ($list as &$item) {
             $item['cover'] = get_image_url($item['cover']);
             $item['file_url'] = get_image_url($item['file_url']);
+            $item['content'] = get_format_content($item['content']);
         }
         //返回结果
         $message = array(
@@ -111,6 +112,7 @@ class ArticleService extends BaseService
         }
 
         $data['author'] = isset($data['author']) && $data['author']? $data['author'] : '懂养生';
+        $data['content'] = isset($data['content']) && $data['content']?set_format_content($data['content']) : '';
         $data['publish_at'] = isset($data['publish_at']) && $data['publish_at'] && $data['publish_at'] != null? $data['publish_at'] : date('Y-m-d H:i:s');
 
         return parent::edit($data); // TODO: Change the autogenerated stub