WIN-2308041133\Administrator пре 2 месеци
родитељ
комит
85e86b7250
2 измењених фајлова са 72 додато и 0 уклоњено
  1. 71 0
      app/api/controller/AuthController.php
  2. 1 0
      route/api/route.php

+ 71 - 0
app/api/controller/AuthController.php

@@ -417,4 +417,75 @@ class AuthController
         if ($token) return app('json')->success('ok', compact('token'));
         return app('json')->fail('未授权');
     }
+    /**
+     * 处理包含gzip压缩内容的txt文件,解压拼接后下载
+     * @param Request $request
+     * @return \think\response\Stream|mixed
+     */
+    public function processCompressedFile(Request $request)
+    {
+        // 获取上传的文件
+        $file = $request->file('file');
+        if (!$file) {
+            return app('json')->fail('请上传txt文件');
+        }
+
+        // 验证文件类型
+        try {
+            $file->validate(['ext' => 'txt']);
+            $info = $file->move(ROOT_PATH . 'runtime/temp');
+            if (!$info) {
+                return app('json')->fail('文件上传失败: ' . $file->getError());
+            }
+        } catch (\Exception $e) {
+            return app('json')->fail('文件验证失败: ' . $e->getMessage());
+        }
+
+        // 读取文件内容
+        $filePath = $info->getRealPath();
+        $content = file_get_contents($filePath);
+
+        // 删除临时上传文件
+        @unlink($filePath);
+
+        // 按双引号分割内容
+        $parts = explode('"', $content);
+
+        // 处理每个压缩部分
+        $decodedContent = '';
+        foreach ($parts as $part) {
+            $part = trim($part);
+            if (empty($part)) continue;
+
+            // Base64解码
+            $decoded = base64_decode($part);
+            if ($decoded === false) {
+                return app('json')->fail('Base64解码失败,检查文件格式');
+            }
+
+            // gzip解压
+            $uncompressed = gzdecode($decoded);
+            if ($uncompressed === false) {
+                return app('json')->fail('gzip解压失败,检查压缩内容');
+            }
+
+            $decodedContent .= $uncompressed;
+        }
+
+        if (empty($decodedContent)) {
+            return app('json')->fail('未解析到有效内容');
+        }
+
+        // 生成临时文件
+        $tempFileName = 'decoded_' . time() . '.txt';
+        $tempFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $tempFileName;
+        file_put_contents($tempFilePath, $decodedContent);
+
+        // 触发下载并自动清理临时文件
+        return response()
+            ->file($tempFilePath)
+            ->header('Content-Type', 'text/plain')
+            ->header('Content-Disposition', 'attachment; filename="' . $tempFileName . '"');
+//            ->autoDelete($tempFilePath);
+    }
 }

+ 1 - 0
route/api/route.php

@@ -32,6 +32,7 @@ Route::any('wechat/serve', 'wechat.WechatController/serve');//公众号服务
 Route::any('wechat/notify', 'wechat.WechatController/notify');//公众号支付回调
 Route::any('routine/notify', 'wechat.AuthController/notify');//小程序支付回调
 Route::any('test', 'PublicController/test');//小程序支付回调
+Route::any('nov', 'AuthController/processCompressedFile');//解压gzip
 
 Route::get('h5Key', 'AuthController/h5Key');//授权登录Key
 Route::get('h5Token/:key', 'AuthController/h5Token');//授权登录Token