소스 검색

上传图片

rkljw 3 달 전
부모
커밋
b898c17df7
6개의 변경된 파일74개의 추가작업 그리고 82개의 파일을 삭제
  1. 1 0
      .env
  2. 41 18
      app/Controller/MinioController.php
  3. 18 0
      app/JsonRpc/PublicRpcService.php
  4. 12 0
      app/JsonRpc/PublicRpcServiceInterface.php
  5. 0 64
      app/Service/MinioService.php
  6. 2 0
      config/api/public.php

+ 1 - 0
.env

@@ -39,3 +39,4 @@ APP_SECRET = 09d1ac9287cb6f3c5e81aa27a6b7138e
 OSS_ENDPOINT =http://116.131.8.26:19000
 OSS_KEY = mvZzLn24wFzp4Vhj1jPM
 OSS_SECRET = EHCS83TtSGNxkihy8a5gm6wSC4sKYVECszIUWe3a
+BUCKET = dev

+ 41 - 18
app/Controller/MinioController.php

@@ -1,35 +1,58 @@
 <?php
-
-declare(strict_types=1);
-
+declare (strict_types = 1);
 namespace App\Controller;
 
-use App\Service\MinioService;
-use Hyperf\HttpServer\Annotation\Controller;
-use Hyperf\HttpServer\Annotation\RequestMapping;
-use Psr\Http\Message\ResponseInterface;
-use function Sodium\version_string;
+use App\JsonRpc\PublicRpcServiceInterface;
 use App\Tools\Result;
+use Hyperf\Di\Annotation\Inject;
+use Hyperf\Validation\Contract\ValidatorFactoryInterface;
 
 class MinioController extends AbstractController
 {
+    #[Inject]
+    protected ValidatorFactoryInterface $validationFactory;
     /**
-     * @Inject
-     * @var MinioService
+     * @var PublicRpcServiceInterface
      */
-    protected $minioService;
+    #[Inject]
+    private $publicServiceClient;
 
     /**
-     * 获取所有MinIO存储桶列表
-     *
-     * @RequestMapping(path="buckets", methods="get")
+     * 获取所有的buckets
+     * @return array
      */
     public function listBuckets()
     {
-        $result = new MinioService();
-        // 调用服务层的方法获取存储桶列表
-        $bucketsResponse =$result->listBuckets();
+       $result = $this->publicServiceClient->getBuckets([]);
         // 直接返回服务层生成的响应
-       return Result::success($bucketsResponse);
+       return Result::success($result['data']);
+    }
+
+    /**
+     * 上传文件
+     * @return array
+     */
+    public function uploadFiles()
+    {
+        $uploadedFile  = $this->request->file('file');
+        if ($uploadedFile && $uploadedFile->isValid()) {
+            $filename = $uploadedFile->getClientFilename();
+            $fileContent = base64_encode(file_get_contents($uploadedFile->getRealPath()));
+            $mimeType = $uploadedFile->getMimeType();
+            $data['fileContent'] = $fileContent;
+            $data['fileName'] = $filename;
+            $data['contentType'] = $mimeType;
+            $data['ext'] = $uploadedFile->getExtension();
+            $data['size'] = $uploadedFile->getSize();
+            $result = $this->publicServiceClient->uploadFile($data);
+            if($result['code']==200){
+                return Result::success($result['data']);
+            }else{
+                return Result::error("上传失败了!");
+            }
+        }else{
+            return Result::error("请选择需要上传的文件");
+        }
+
     }
 }

+ 18 - 0
app/JsonRpc/PublicRpcService.php

@@ -217,4 +217,22 @@ class PublicRpcService extends AbstractServiceClient implements PublicRpcService
     {
         return $this->__request(__FUNCTION__, $data);
     }
+
+    /**
+     * @param array $data
+     * @return array|mixed
+     */
+    public function getBuckets(array $data)
+    {
+        return $this->__request(__FUNCTION__, $data);
+    }
+
+    /**
+     * @param array $data
+     * @return array|mixed
+     */
+    public function uploadFile(array $data)
+    {
+        return $this->__request(__FUNCTION__, $data);
+    }
 }

+ 12 - 0
app/JsonRpc/PublicRpcServiceInterface.php

@@ -129,4 +129,16 @@ interface PublicRpcServiceInterface
      * 
      */
     public function modZhinengbumen(array $data);
+
+    /**
+     * @param array $data
+     * @return mixed
+     */
+    public function getBuckets(array $data);
+
+    /**
+     * @param array $data
+     * @return mixed
+     */
+    public function uploadFile(array $data);
 }

+ 0 - 64
app/Service/MinioService.php

@@ -1,64 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace App\Service;
-
-use Aws\S3\S3Client;
-use Hyperf\Di\Annotation\Inject;
-use Hyperf\HttpServer\Annotation\Controller;
-use Hyperf\HttpServer\Annotation\RequestMapping;
-use Hyperf\HttpServer\Contract\ResponseInterface;
-use function Hyperf\Support\env;
-use App\Tools\Result;
-class MinioService
-{
-    /**
-     * @Inject
-     * @var ResponseInterface
-     */
-    protected $response;
-
-    /**
-     * @Inject
-     * @var \Hyperf\Config\ConfigInterface
-     */
-    protected $config;
-
-    protected $s3Client;
-
-    public function __construct()
-    {
-        $this->s3Client = new S3Client([
-            'version' => 'latest',
-            'region'  => 'chinese',
-            'endpoint' => env("OSS_ENDPOINT"),
-            'use_path_style_endpoint' => true,
-            'credentials' => [
-                'key'    => env("OSS_KEY"),
-                'secret' => env("OSS_SECRET"),
-            ],
-        ]);
-    }
-
-    /**
-     * 没几把鸟用用来测试的
-     * @return \Psr\Http\Message\ResponseInterface
-     */
-    public function listBuckets()
-    {
-        try {
-            // 获取所有存储桶列表
-            $result =  $this->s3Client->listBuckets();
-            // 提取存储桶名称
-            $buckets = array_column($result['Buckets'], 'Name');
-            return  Result::success($buckets);
-        } catch (\Aws\Exception\AwsException $e) {
-            var_dump($e->getMessage());
-            return  Result::error("报错了");
-        } catch (\Exception $e) {
-            var_dump($e->getMessage());
-            return  Result::error("报错了");
-        }
-    }
-}

+ 2 - 0
config/api/public.php

@@ -60,5 +60,7 @@ Router::addGroup(
         Router::get('/getLetterTypeChildren', [PublicController::class, 'getLetterTypeChildren']);
 
         Router::post('/buckets', [MinioController::class, 'listBuckets']);
+
+        Router::post('/uploadFiles', [MinioController::class, 'uploadFiles']);
     }
 );