Ver Fonte

上传图片

rkljw há 3 meses atrás
pai
commit
27bbca9ecc

+ 7 - 0
.env

@@ -20,3 +20,10 @@ AMQP_HOST=103.105.201.2
 AMQP_PORT=5673
 AMQP_USER=rabbitmq
 AMQP_PASSWORD=H8eDTAk6LY7EjJ8y
+
+
+#图片服务器地址
+OSS_ENDPOINT =http://116.131.8.26:19000
+OSS_KEY = mvZzLn24wFzp4Vhj1jPM
+OSS_SECRET = EHCS83TtSGNxkihy8a5gm6wSC4sKYVECszIUWe3a
+BUCKET = dev

+ 33 - 0
app/JsonRpc/PublicRpcService.php

@@ -9,6 +9,7 @@ use App\Model\LevelUser;
 use App\Model\UserLevel;
 use App\Tools\Result;
 use Hyperf\RpcServer\Annotation\RpcService;
+use App\Service\MinioService;
 
 #[RpcService(name: "PublicRpcService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class PublicRpcService implements PublicRpcServiceInterface
@@ -486,5 +487,37 @@ class PublicRpcService implements PublicRpcServiceInterface
             return Result::success($result);
         }
     }
+
+    /**
+     * 获取所有的buckets
+     * @param array $data
+     * @return array
+     */
+    public function getBuckets(array $data) :array
+    {
+        $result = new MinioService();
+        // 调用服务层的方法获取存储桶列表
+        $bucketsResponse =$result->listBuckets();
+        // 直接返回服务层生成的响应
+        return Result::success($bucketsResponse['data']);
+    }
+
+    /**
+     * 上传文件
+     * @param array $data
+     * @return array
+     */
+    public function uploadFile(array $data) :array
+    {
+        $result = new MinioService();
+        $rep = $result->uploadFile($data);
+        if($rep['code']==200){
+            return Result::success($rep['data']);
+        }else{
+            return Result::error("上传失败!");
+        }
+
+    }
+
 }
 

+ 12 - 0
app/JsonRpc/PublicRpcServiceInterface.php

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

+ 98 - 0
app/Service/MinioService.php

@@ -0,0 +1,98 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Service;
+
+use Aws\S3\S3Client;
+use Hyperf\HttpServer\Contract\ResponseInterface;
+use function Hyperf\Support\env;
+use App\Tools\Result;
+use Hyperf\HttpMessage\Upload\UploadedFile;
+
+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) {
+            return  Result::error("报错了");
+        } catch (\Exception $e) {
+            return  Result::error("报错了");
+        }
+    }
+
+    /**
+     * 上传文件
+     * @return array|string[]
+     */
+    public function uploadFile($data)
+    {
+        $acceptExt = ['png', 'jpg', 'jpeg', 'gif', 'xls', 'xlsx', 'pdf', 'doc', 'docx', 'ppt', 'zip', 'pptx', 'mp4', 'flv','rar','tar'];
+        try {
+            if (!in_array( $data['ext'], $acceptExt)) {
+                return Result::error('文件名后缀不允许');
+            }
+            $fileName = time().mt_rand(1, 1000000) . '.' . $data['ext'];
+            $finalPath =  DIRECTORY_SEPARATOR.date('Ymd').DIRECTORY_SEPARATOR  . $fileName;
+            $key = $data['contentType'].$finalPath;
+            var_dump("key:",$key);
+            $result = $this->s3Client->putObject([
+                'Bucket' => env("BUCKET"),
+                'Key' => $key,
+                'Body' => base64_decode($data['fileContent']),
+                'ACL' => 'public-read',
+                'ContentType' => $data['contentType'],
+            ]);
+            $rep = [
+                'imgUrl'=>$result['ObjectURL'],
+                'id'=>uniqid(),
+                'src'=>$key,
+                'fileName'=>$fileName,
+                'fileType'=> $data['ext'],
+                'oldFileName'=>$data['fileName']
+            ];
+            return  Result::success($rep);
+        } catch (\Exception $e) {
+            return  Result::error($e->getMessage());
+        }
+    }
+
+}

+ 1 - 1
runtime/hyperf.pid

@@ -1 +1 @@
-29573
+20858

+ 0 - 3
runtime/logs/hyperf.log

@@ -1,3 +0,0 @@
-[2024-11-21T07:56:29.674152+00:00] sql.INFO: [87.21] select * from `letter_type` where (`type` = '1') order by `sort` asc [] []
-[2024-11-21T07:56:40.645759+00:00] sql.INFO: [18.18] select * from `letter_type` where (`type` = '1' and `pid` = '14') order by `sort` asc [] []
-[2024-11-21T07:56:56.646094+00:00] sql.INFO: [103.6] insert into `letter_of_complaint` (`title`, `nature`, `nature_level0`, `nature_level1`, `nature_level3`, `name`, `id_card`, `mobile`, `describe`, `like_remark`, `judgment`, `audio_and_video`, `contract`, `qualifications`, `money`, `other`, `user_id`) values ('我测试一条', '5', '14', '29', '113', '1', '1', '15801245755', '111', '111111111', '', '', '', '', '1', '111', '71') [] []

+ 1 - 0
vendor/composer/autoload_classmap.php

@@ -60,6 +60,7 @@ return array(
     'App\\Model\\WebsiteColumn' => $baseDir . '/app/Model/WebsiteColumn.php',
     'App\\Model\\WebsiteRole' => $baseDir . '/app/Model/WebsiteRole.php',
     'App\\Model\\WebsiteRoleUser' => $baseDir . '/app/Model/WebsiteRoleUser.php',
+    'App\\Service\\MinioService' => $baseDir . '/app/Service/MinioService.php',
     'App\\Tools\\Result' => $baseDir . '/app/Tools/Result.php',
     'Attribute' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/Attribute.php',
     'Aws\\ACMPCA\\ACMPCAClient' => $vendorDir . '/aws/aws-sdk-php/src/ACMPCA/ACMPCAClient.php',

+ 1 - 0
vendor/composer/autoload_static.php

@@ -761,6 +761,7 @@ class ComposerStaticInit88f2a4d4a4e81dc7d415bcdf39930654
         'App\\Model\\WebsiteColumn' => __DIR__ . '/../..' . '/app/Model/WebsiteColumn.php',
         'App\\Model\\WebsiteRole' => __DIR__ . '/../..' . '/app/Model/WebsiteRole.php',
         'App\\Model\\WebsiteRoleUser' => __DIR__ . '/../..' . '/app/Model/WebsiteRoleUser.php',
+        'App\\Service\\MinioService' => __DIR__ . '/../..' . '/app/Service/MinioService.php',
         'App\\Tools\\Result' => __DIR__ . '/../..' . '/app/Tools/Result.php',
         'Attribute' => __DIR__ . '/..' . '/symfony/polyfill-php80/Resources/stubs/Attribute.php',
         'Aws\\ACMPCA\\ACMPCAClient' => __DIR__ . '/..' . '/aws/aws-sdk-php/src/ACMPCA/ACMPCAClient.php',

+ 2 - 2
vendor/composer/installed.php

@@ -3,7 +3,7 @@
         'name' => 'hyperf/hyperf-skeleton',
         'pretty_version' => 'dev-master',
         'version' => 'dev-master',
-        'reference' => '28f209543fede32d85f136062cf524d01cb6f478',
+        'reference' => '110b39012a60a8c195ff5c6c4eeafd24b52db4c7',
         'type' => 'project',
         'install_path' => __DIR__ . '/../../',
         'aliases' => array(),
@@ -457,7 +457,7 @@
         'hyperf/hyperf-skeleton' => array(
             'pretty_version' => 'dev-master',
             'version' => 'dev-master',
-            'reference' => '28f209543fede32d85f136062cf524d01cb6f478',
+            'reference' => '110b39012a60a8c195ff5c6c4eeafd24b52db4c7',
             'type' => 'project',
             'install_path' => __DIR__ . '/../../',
             'aliases' => array(),