Bläddra i källkod

青爻公众号

cqp 10 månader sedan
förälder
incheckning
68afafc21a
2 ändrade filer med 88 tillägg och 8 borttagningar
  1. 14 0
      app/Model/WxArticleDetailContent.php
  2. 74 8
      app/Service/Weixin/WeixinService.php

+ 14 - 0
app/Model/WxArticleDetailContent.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class WxArticleDetailContent extends Model
+{
+    protected $table = "wx_article_detail_content"; //指定表
+    const CREATED_AT = null;
+    const UPDATED_AT = null;
+    protected $dateFormat = 'U';
+
+}

+ 74 - 8
app/Service/Weixin/WeixinService.php

@@ -5,6 +5,7 @@ namespace App\Service\Weixin;
 use App\Model\WxArticle;
 use App\Model\Settings;
 use App\Model\WxArticleDetail;
+use App\Model\WxArticleDetailContent;
 use App\Service\Service;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
@@ -339,7 +340,7 @@ class WeixinService extends Service
                                 // 从微秒部分截取全部6位作为唯一标识符
                                 $microSuffix = substr($micro, 2, 6);
                                 $content_name = date('YmdHis', $seconds) . $microSuffix;
-                                $this->saveContent($val['content'], $content_name);
+                                $content_img = $this->saveContent($val['content'], $content_name);
                                 $this->get_helper_for_img($val['thumb_url']);
 
                                 $modelDetail = new WxArticleDetail();
@@ -359,6 +360,17 @@ class WeixinService extends Service
                                 $modelDetail->upd_time = $value['content']['update_time'];
                                 $modelDetail->crt_time = $value['content']['create_time'];
                                 $modelDetail->save();
+
+                                if(! empty($content_img)){
+                                    $insert = [];
+                                    foreach ($content_img as $value){
+                                        $insert[] = [
+                                            'wx_article_detail_id' => $modelDetail->id,
+                                            'url' => $value
+                                        ];
+                                    }
+                                    WxArticleDetailContent::insert($insert);
+                                }
                             }
                         }
                     }else{
@@ -369,12 +381,20 @@ class WeixinService extends Service
 
                             $detail = WxArticleDetail::where('del_time',0)
                                 ->where('wx_article_id',$id)
-                                ->select('content_name','thumb_url')
+                                ->select('content_name as txt','thumb_url as img','id')
                                 ->get()->toArray();
 
                             WxArticleDetail::where('del_time',0)
                                 ->where('wx_article_id',$id)
                                 ->update(['del_time' => time()]);
+
+                            $detail2 = WxArticleDetailContent::where('del_time',0)
+                                ->whereIn('id', array_column($detail,'id'))
+                                ->select('url as img')
+                                ->get()->toArray();
+                            WxArticleDetailContent::where('del_time',0)
+                                ->whereIn('id', array_column($detail,'id'))
+                                ->update(['del_time' => time()]);
                             if(! empty($value['content']['news_item'])){
                                 foreach ($value['content']['news_item'] as $val){
                                     // 获取当前时间的微秒级时间戳
@@ -382,7 +402,7 @@ class WeixinService extends Service
                                     // 从微秒部分截取全部6位作为唯一标识符
                                     $microSuffix = substr($micro, 2, 6);
                                     $content_name = date('YmdHis', $seconds) . $microSuffix;
-                                    $this->saveContent($val['content'], $content_name);
+                                    $content_img = $this->saveContent($val['content'], $content_name);
                                     $this->get_helper_for_img($val['thumb_url']);
 
                                     $modelDetail = new WxArticleDetail();
@@ -402,10 +422,22 @@ class WeixinService extends Service
                                     $modelDetail->upd_time = $value['content']['update_time'];
                                     $modelDetail->crt_time = $value['content']['create_time'];
                                     $modelDetail->save();
+
+                                    if(! empty($content_img)){
+                                        $insert = [];
+                                        foreach ($content_img as $value){
+                                            $insert[] = [
+                                                'wx_article_detail_id' => $modelDetail->id,
+                                                'url' => $value
+                                            ];
+                                        }
+                                        WxArticleDetailContent::insert($insert);
+                                    }
                                 }
                             }
 
                             $this->delFile($detail);
+                            $this->delFile($detail2);
                         }
                     }
                 }
@@ -442,10 +474,42 @@ class WeixinService extends Service
                 }
             }
 
+            list($content, $img) = $this->dealContent($content);
             file_put_contents($localFilePath, $content);
         }
 
-        return [true, $name];
+        return [true, $img ?? []];
+    }
+
+    public function dealContent($content){
+        // 正则表达式匹配 data-src 属性
+        $pattern = '/data-src="([^"]+)"/';
+
+        // 匹配所有 data-src 属性
+        preg_match_all($pattern, $content, $matches);
+
+        // 提取所有匹配到的 URL
+        $urls = $matches[1];
+
+        $return = [];
+        $header = ['Content-Type:application/json'];
+        // 遍历每个匹配项并替换
+        foreach ($urls as $url) {
+            list($status, $msg) = $this->get_helper_for_img($url,$header);
+            if($status){
+                $newImageUrl = "https://adminapi.qingyaokeji.com/weixin/getWxFileLocal/" . $msg;
+
+                // 构建完整的 data-src 属性
+                $oldDataSrc = 'data-src="' . $url . '"';
+                $newDataSrc = 'data-src="' . $newImageUrl . '"';
+
+                // 替换
+                $content = str_replace($oldDataSrc, $newDataSrc, $content);
+                $return[] = $msg;
+            }
+        }
+
+        return [$content,$return];
     }
 
     public function delFile($detail){
@@ -453,11 +517,13 @@ class WeixinService extends Service
 
         $dir = 'wx_img/';
         foreach ($detail as $value){
-            $dir_content = $dir . $value['content_name'] . '.txt';
-            if(Storage::disk('public')->exists($dir_content)) Storage::disk('public')->delete($dir_content);
+            if(isset($value['txt'])){
+                $dir_content = $dir . $value['txt'] . '.txt';
+                if(Storage::disk('public')->exists($dir_content)) Storage::disk('public')->delete($dir_content);
+            }
 
-            if(! empty($value['thumb_url'])){
-                $dir_thumb = $dir . md5($value['thumb_url']) . '.jpg';
+            if(! empty($value['img'])){
+                $dir_thumb = $dir . md5($value['img']) . '.jpg';
                 if(Storage::disk('public')->exists($dir_thumb)) Storage::disk('public')->delete($dir_thumb);
             }
         }