Ver código fonte

青爻公众号

cqp 10 meses atrás
pai
commit
3322f3b630

+ 19 - 0
app/Http/Controllers/Api/WeixinController.php

@@ -75,4 +75,23 @@ class WeixinController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function getWxFile(Request $request){
+        $service = new WeixinService();
+        list($status,$data) = $service->getWxFile($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    //获取文件的位置
+    public function getFileLocal($file_name){
+        $path = storage_path() . "/app/public/upload_files/".$file_name;
+        if(file_exists($path)) return response()->file($path);
+
+        return "";
+    }
 }

+ 95 - 0
app/Service/Weixin/WeixinService.php

@@ -4,10 +4,13 @@ namespace App\Service\Weixin;
 
 use App\Model\Settings;
 use App\Service\Service;
+use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Redis;
 
 class WeixinService extends Service
 {
+    const wx_img = "app/public/wx_img/";
+
     public function getToken(){
         $config = config('qingyaoWx');
         $token_key = $config['redis_key'];
@@ -139,6 +142,22 @@ class WeixinService extends Service
         return [true, ['data' => $result['item'] ?? [], 'total' => $result['total_count'], 'data_count' => $result['item_count']]];
     }
 
+    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未入白名单'];
+//        }
+
+        if(empty($data['wx_url'])) return [false, "URL不存在"];
+
+        $header = ['Content-Type:application/json'];
+        list($status,$msg) = $this->get_helper_for_img($data['wx_url'],$header);
+        if(! $status) return [false, $msg];
+
+        dd($msg);
+    }
+
     public function rule($data){
         // 获取用户的IP地址
         $userIP = $_SERVER['REMOTE_ADDR'];
@@ -174,4 +193,80 @@ class WeixinService extends Service
 
         return explode(',',$allowedIPs->setting_value);
     }
+
+    public function get_helper_for_img($url,$header=[],$timeout = 20){
+        $ch = curl_init();
+        curl_setopt_array($ch, array(
+            CURLOPT_URL => $url,
+            CURLOPT_RETURNTRANSFER => true,
+            CURLOPT_ENCODING => '',
+            CURLOPT_MAXREDIRS => 10,
+            CURLOPT_TIMEOUT => $timeout,
+            CURLOPT_FOLLOWLOCATION => true,
+            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
+            CURLOPT_CUSTOMREQUEST => 'GET',
+            CURLOPT_SSL_VERIFYPEER => false,
+            CURLOPT_HTTPHEADER => $header,
+        ));
+        $r = curl_exec($ch);
+
+        if ($r === false) {
+            // 获取错误号
+            $errorNumber = curl_errno($ch);
+            // 获取错误信息
+            $errorMessage = curl_error($ch);
+            $message = "cURL Error #{$errorNumber}: {$errorMessage}";
+            Log::channel('apiLog')->info('wx', ["message" => $message]);
+            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 ($httpCode == 200) {
+            // 检查是否为图片
+            list($status, $msg) = $this->getTemporaryUrl($body, $url);
+            if(! $status) return [false, $msg];
+            $img = $msg;
+        } else {
+            return [false, ''];
+        }
+
+        return [true, $img];
+    }
+
+    function isImage($data) {
+        $imageInfo = getimagesizefromstring($data);
+        return $imageInfo !== false;
+    }
+
+    //生成临时文件
+    public function getTemporaryUrl($body,$url)
+    {
+        // 定义本地文件路径
+        $name = md5($url) . '.jpeg';
+        $localFilePath = storage_path(self::wx_img . md5($url) . $name);
+
+        if(! file_exists($localFilePath)){
+            $directoryPath = dirname($localFilePath);
+            // 检查目录是否存在,如果不存在则创建
+            if (! is_dir($directoryPath)) {
+                // 设置目录权限,可以根据需要更改
+                $mode = 0777;
+                // 使用递归选项创建目录
+                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

@@ -16,6 +16,7 @@ Route::any('getPublicWxArticle', 'Api\WeixinController@getPublicWxArticle');
 Route::any('getPublicWxArticleDetail', 'Api\WeixinController@getPublicWxArticleDetail');
 Route::any('getPublicWxMaterial', 'Api\WeixinController@getPublicWxMaterial');
 Route::any('getPublicWxDraft', 'Api\WeixinController@getPublicWxDraft');
+Route::any('getWxFile', 'Api\WeixinController@getWxFile');
 
 Route::group(['middleware'=> ['checkWeixin']],function ($route){