cqp 10 месяцев назад
Родитель
Сommit
16c74319cc

+ 2 - 2
app/Http/Controllers/Api/WeixinController.php

@@ -88,8 +88,8 @@ class WeixinController extends BaseController
     }
 
     //获取文件的位置
-    public function getFileLocal($file_name){
-        $path = storage_path() . "/app/public/upload_files/".$file_name;
+    public function getWxFileLocal($file_name){
+        $path = storage_path() . '/' . WeixinService::wx_img . $file_name;
         if(file_exists($path)) return response()->file($path);
 
         return "";

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

@@ -143,11 +143,11 @@ class WeixinService extends Service
     }
 
     public function getWxFile($data){
-//        list($status, $msg) = $this->rule($data);
-//        if(! $status) {
-//            file_put_contents('record_ip.txt',date("Y-m-d H:i:s",time()).json_encode($data) . PHP_EOL."来源IP".$msg.PHP_EOL,8);
-//            return [false, 'IP未入白名单'];
-//        }
+        list($status, $msg) = $this->rule($data);
+        if(! $status) {
+            file_put_contents('record_ip.txt',date("Y-m-d H:i:s",time()).json_encode($data) . PHP_EOL."来源IP".$msg.PHP_EOL,8);
+            return [false, 'IP未入白名单'];
+        }
 
         if(empty($data['wx_url'])) return [false, "URL不存在"];
 
@@ -155,7 +155,7 @@ class WeixinService extends Service
         list($status,$msg) = $this->get_helper_for_img($data['wx_url'],$header);
         if(! $status) return [false, $msg];
 
-        dd($msg);
+        return [true, $msg];
     }
 
     public function rule($data){
@@ -220,16 +220,16 @@ class WeixinService extends Service
             return [false, $message];
         }
 
-        $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
-        $header = substr($r, 0, $headerSize);
-        $body = substr($r, $headerSize);
         $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+
         curl_close($ch);
 
-        $img = "";
+        // 检查是否为图片
+        if (! $this->isImage($r)) return [false, "资源不是图片"];
+
         if ($httpCode == 200) {
             // 检查是否为图片
-            list($status, $msg) = $this->getTemporaryUrl($body, $url);
+            list($status, $msg) = $this->getTemporaryUrl($r, $url);
             if(! $status) return [false, $msg];
             $img = $msg;
         } else {
@@ -248,25 +248,26 @@ class WeixinService extends Service
     public function getTemporaryUrl($body,$url)
     {
         // 定义本地文件路径
-        $name = md5($url) . '.jpeg';
+        $name = md5($url) . '.jpg';
         $localFilePath = storage_path(self::wx_img . $name);
 
-        if(! file_exists($localFilePath)){
-            $directoryPath = dirname($localFilePath);
+        // 写入文件前先检查文件是否存在
+        if (! file_exists($localFilePath)) {
+
             // 检查目录是否存在,如果不存在则创建
-            if (! is_dir($directoryPath)) {
+            $directoryPath = dirname($localFilePath);
+            if (!is_dir($directoryPath)) {
                 // 设置目录权限,可以根据需要更改
                 $mode = 0777;
                 // 使用递归选项创建目录
-                if (! mkdir($directoryPath, $mode, true)) return [false, '目录创建失败'];
+                if (!mkdir($directoryPath, $mode, true)) {
+                    return [false, '目录创建失败'];
+                }
             }
 
             file_put_contents($localFilePath, $body);
         }
 
-
-        if(! file_exists($localFilePath)) file_put_contents($localFilePath, $body);
-
         return [true, $name];
     }
 }

+ 1 - 0
routes/weixin.php

@@ -17,6 +17,7 @@ Route::any('getPublicWxArticleDetail', 'Api\WeixinController@getPublicWxArticleD
 Route::any('getPublicWxMaterial', 'Api\WeixinController@getPublicWxMaterial');
 Route::any('getPublicWxDraft', 'Api\WeixinController@getPublicWxDraft');
 Route::any('getWxFile', 'Api\WeixinController@getWxFile');
+Route::any('getWxFileLocal/{file_name}', 'Api\WeixinController@getWxFileLocal');
 
 Route::group(['middleware'=> ['checkWeixin']],function ($route){