Browse Source

青爻公众号

cqp 10 months ago
parent
commit
33cb6a2ae1
2 changed files with 100 additions and 19 deletions
  1. 31 0
      app/Service/FileUploadService.php
  2. 69 19
      app/Service/Weixin/WeixinService.php

+ 31 - 0
app/Service/FileUploadService.php

@@ -3,6 +3,7 @@
 namespace App\Service;
 
 
+use App\Service\Weixin\WeixinService;
 use Illuminate\Support\Facades\Storage;
 
 class FileUploadService extends Service
@@ -163,4 +164,34 @@ class FileUploadService extends Service
             Storage::disk('public')->delete($storagePath);
         }
     }
+
+    public function createOssUploadWx($file){
+        if(! is_array($file) && empty($file)) return;
+        foreach ($file as $filename){
+            $dir = WeixinService::dir . $filename;
+            $realPath = storage_path() . "/app/public/" . $dir;
+            $savePath = self::string3 . WeixinService::dir . $filename;
+            list($status,$msg) = (new OssService())->uploadFile($realPath,$savePath);
+            if($status) Storage::disk('public')->delete($dir);
+        }
+    }
+
+    public function createOssDeleteWx($file){
+        if(! is_array($file) && empty($file)) return;
+
+        foreach ($file as $filename){
+            $delPath = self::string3 . WeixinService::dir . $filename;
+            list($status,$msg) = (new OssService())->deleteFile($delPath);
+        }
+    }
+
+    //获取文件的位置oss
+    public function getFileShowWx($file_name,$expired = 3500){
+        $path = "";
+        if(empty($file_name)) return $path;
+        $savePath = self::string3 . WeixinService::dir . $file_name;
+        list($status,$path) = (new OssService())->getTemporaryUrl($savePath,$expired);
+
+        return $path;
+    }
 }

+ 69 - 19
app/Service/Weixin/WeixinService.php

@@ -6,6 +6,7 @@ use App\Model\WxArticle;
 use App\Model\Settings;
 use App\Model\WxArticleDetail;
 use App\Model\WxArticleDetailContent;
+use App\Service\FileUploadService;
 use App\Service\Service;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
@@ -14,6 +15,7 @@ use Illuminate\Support\Facades\Storage;
 
 class WeixinService extends Service
 {
+    const dir = 'wx_img/';
     const wx_img = "app/public/wx_img/";
 
     public function getToken(){
@@ -202,6 +204,8 @@ class WeixinService extends Service
     }
 
     public function get_helper_for_img($url,$header=[],$timeout = 20){
+        if(empty($url)) return [true, ""];
+
         $ch = curl_init();
         curl_setopt_array($ch, array(
             CURLOPT_URL => $url,
@@ -255,7 +259,10 @@ class WeixinService extends Service
     public function getTemporaryUrl($body,$url)
     {
         // 定义本地文件路径
-        $name = md5($url) . '.jpg';
+        list($micro, $seconds) = explode(' ', microtime());
+        // 从微秒部分截取全部6位作为唯一标识符
+        $microSuffix = substr($micro, 2, 6);
+        $name = date('YmdHis', $seconds) . $microSuffix . '.jpg';
         $localFilePath = storage_path(self::wx_img . $name);
 
         // 写入文件前先检查文件是否存在
@@ -329,6 +336,8 @@ class WeixinService extends Service
             try {
                 DB::beginTransaction();
 
+                //保存数据
+                $upload = $delete = [];
                 foreach ($result['item'] as $value){
                     $model = WxArticle::where('del_time',0)
                         ->where('article_id', $value['article_id'])
@@ -346,8 +355,8 @@ class WeixinService extends Service
                                 // 从微秒部分截取全部6位作为唯一标识符
                                 $microSuffix = substr($micro, 2, 6);
                                 $content_name = date('YmdHis', $seconds) . $microSuffix;
-                                $content_img = $this->saveContent($val['content'], $content_name);
-                                $this->get_helper_for_img($val['thumb_url']);
+                                list($content_img,$name) = $this->saveContent($val['content'], $content_name);
+                                list($status, $my_thumb_url) = $this->get_helper_for_img($val['thumb_url']);
 
                                 $modelDetail = new WxArticleDetail();
                                 $modelDetail->wx_article_id = $id;
@@ -360,6 +369,7 @@ class WeixinService extends Service
                                 $modelDetail->show_cover_pic = $val['show_cover_pic'];
                                 $modelDetail->url = $val['url'];
                                 $modelDetail->thumb_url = $val['thumb_url'];
+                                $modelDetail->my_thumb_url = $my_thumb_url;
                                 $modelDetail->need_open_comment = $val['need_open_comment'];
                                 $modelDetail->only_fans_can_comment = $val['only_fans_can_comment'];
                                 $modelDetail->is_deleted = $val['is_deleted'];
@@ -370,6 +380,7 @@ class WeixinService extends Service
                                 if(! empty($content_img)){
                                     $insert = [];
                                     foreach ($content_img as $value){
+                                        $upload[] = $value;
                                         $insert[] = [
                                             'wx_article_detail_id' => $modelDetail->id,
                                             'url' => $value
@@ -377,6 +388,8 @@ class WeixinService extends Service
                                     }
                                     WxArticleDetailContent::insert($insert);
                                 }
+                                if(! empty($name)) $upload[] = $name;
+                                if(! empty($my_thumb_url)) $upload[] = $my_thumb_url;
                             }
                         }
                     }else{
@@ -408,8 +421,8 @@ class WeixinService extends Service
                                     // 从微秒部分截取全部6位作为唯一标识符
                                     $microSuffix = substr($micro, 2, 6);
                                     $content_name = date('YmdHis', $seconds) . $microSuffix;
-                                    $content_img = $this->saveContent($val['content'], $content_name);
-                                    $this->get_helper_for_img($val['thumb_url']);
+                                    list($content_img,$name) = $this->saveContent($val['content'], $content_name);
+                                    list($status, $my_thumb_url) = $this->get_helper_for_img($val['thumb_url']);
 
                                     $modelDetail = new WxArticleDetail();
                                     $modelDetail->wx_article_id = $id;
@@ -422,6 +435,7 @@ class WeixinService extends Service
                                     $modelDetail->show_cover_pic = $val['show_cover_pic'];
                                     $modelDetail->url = $val['url'];
                                     $modelDetail->thumb_url = $val['thumb_url'];
+                                    $modelDetail->my_thumb_url = $my_thumb_url;
                                     $modelDetail->need_open_comment = $val['need_open_comment'];
                                     $modelDetail->only_fans_can_comment = $val['only_fans_can_comment'];
                                     $modelDetail->is_deleted = $val['is_deleted'];
@@ -432,6 +446,7 @@ class WeixinService extends Service
                                     if(! empty($content_img)){
                                         $insert = [];
                                         foreach ($content_img as $value){
+                                            $upload[] = $value;
                                             $insert[] = [
                                                 'wx_article_detail_id' => $modelDetail->id,
                                                 'url' => $value
@@ -439,15 +454,27 @@ class WeixinService extends Service
                                         }
                                         WxArticleDetailContent::insert($insert);
                                     }
+                                    if(! empty($name)) $upload[] = $name;
+                                    if(! empty($my_thumb_url)) $upload[] = $my_thumb_url;
                                 }
                             }
 
-                            $this->delFile($detail);
-                            $this->delFile($detail2);
+                            foreach ($detail as $value){
+                                if(! empty($value['txt'])) $delete[] = $value['txt'];
+                                if(! empty($value['img'])) $delete[] = $value['img'];
+                            }
+                            foreach ($detail2 as $value){
+                                if(! empty($value['img'])) $delete[] = $value['img'];
+                            }
                         }
                     }
                 }
 
+                //上传阿里云
+                $service = new FileUploadService();
+                $service->createOssUploadWx($upload);
+                $service->createOssDeleteWx($delete);
+
                 DB::commit();
                 return [true, ''];
             }catch (\Throwable $exception){
@@ -484,7 +511,7 @@ class WeixinService extends Service
             file_put_contents($localFilePath, $content);
         }
 
-        return $img ?? [];
+        return [$img ?? [], $name];
     }
 
     public function dealContent1($content){
@@ -513,7 +540,7 @@ class WeixinService extends Service
         foreach ($urls as $url) {
             list($status, $msg) = $this->get_helper_for_img($url,$header);
             if($status){
-                $newImageUrl = "https://adminapi.qingyaokeji.com/weixin/getWxFileLocal/" . $msg;
+                $newImageUrl = $msg;
 
                 // 构建完整的 data-src 属性
                 $oldDataSrc = 'data-src="' . $url . '"';
@@ -531,7 +558,7 @@ class WeixinService extends Service
     public function delFile($detail){
         if(empty($detail)) return;
 
-        $dir = 'wx_img/';
+        $dir = Self::dir;
         foreach ($detail as $value){
             if(isset($value['txt'])){
                 $dir_content = $dir . $value['txt'] . '.txt';
@@ -554,7 +581,7 @@ class WeixinService extends Service
 
         $model = WxArticleDetail::where('del_time',0)
             ->where('is_deleted', 0)
-            ->select('title','author','digest','thumb_url','id','crt_time')
+            ->select('title','author','digest','thumb_url','id','crt_time','my_thumb_url')
             ->orderBy('crt_time','desc');
 
         $list = $this->limit($model,'',$data);
@@ -568,11 +595,10 @@ class WeixinService extends Service
     public function organizationData($data){
         if(empty($data['data'])) return $data;
 
+        $service = new FileUploadService();
         foreach ($data['data'] as $key => $value){
-            $my_thumb_url = "";
-            if(! empty($value['thumb_url'])) $my_thumb_url = "getWxFileLocal/" . md5($value['thumb_url']) . '.jpg';
-            $data['data'][$key]['my_thumb_url'] = $my_thumb_url;
             $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i", $value['crt_time']) : "";
+            $data['data'][$key]['my_thumb_url'] = $service->getFileShowWx($value['my_thumb_url']);
         }
 
         return $data;
@@ -591,17 +617,41 @@ class WeixinService extends Service
         if(empty($detail)) return [false, '文章不存在或已被删除'];
         $detail = $detail->toArray();
         if($detail['is_deleted']) return [false, '文章不存在或被删除'];
-
         if(empty($detail['content_name'])) return [true, ['content' => '']];
 
-        // 定义本地文件路径
-        $name = $detail['content_name'] . '.txt';
-        $localFilePath = storage_path(self::wx_img . $name);
+        //获取文件
+        $service = new FileUploadService();
+        $content = $service->getFileShowWx($detail['content_name']);
 
         // 读取文件内容
-        $fileContent = file_get_contents($localFilePath);
+        $fileContent = file_get_contents($content);
+        $fileContent = $this->dealContent2($fileContent);
 
         $detail['content'] = $fileContent;
         return [true, ['data' => $detail]];
     }
+
+    public function dealContent2($content){
+        // 正则表达式匹配 data-src 属性
+        $pattern = '/src="([^"]+)"/';
+
+        // 匹配所有 data-src 属性
+        preg_match_all($pattern, $content, $matches);
+
+        // 提取所有匹配到的 URL
+        $urls = $matches[1];
+
+        // 遍历每个匹配项并替换
+        $service = new FileUploadService();
+        foreach ($urls as $url) {
+            $newImageUrl = $service->getFileShowWx($url);
+            $oldDataSrc = 'src="' . $url . '"';
+            $newDataSrc = 'src="' . $newImageUrl . '"';
+
+            // 替换
+            $content = str_replace($oldDataSrc, $newDataSrc, $content);
+        }
+
+        return $content;
+    }
 }