记住业务流程图,要不然不清楚自己封装的是什么东西。
一、本地工厂开发
在根目录下extend文件夹下Upload文件夹下channel文件夹中,我们修改LocalUpload业务控制器。具体代码如下:
<?php
/*** 本地上传工厂* User: 龙哥 三年风水* Date: 2024/11/2/0002* Time: 18:16*/
namespace Upload\channel;
use think\exception\ValidateException;
use think\facade\Filesystem;
use Upload\UploadHandle;
use Error\BaseError;
class LocalUpload extends UploadHandle
{/*** 上传操作* @ param $object 加密后的文件* @ param $filePath 临时存放的文件路径* @ return mixed*/public function uploadFile($object, $filePath){try{$savename = Filesystem::disk('public')->putFileAs( 'image', $filePath ,$object);return $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/storage/'.$savename;}catch (ValidateException $e){throw new BaseError($e->getMessage(),50000,200);}}/*** 删除操作* @ param $object 具体的文件地址* @ return mixed*/public function deleteFile($object){// TODO: Implement deleteFile() method.}
}
一定要记得配置根目录下config文件夹中的filesystem.php,具体代码如下:
<?php
return [// 默认磁盘'default' => env('filesystem.driver', 'local'),// 磁盘列表'disks' => ['local' => ['type' => 'local','root' => app()->getRuntimePath() . 'storage',],'public' => [// 磁盘类型'type' => 'local',// 磁盘路径'root' => app()->getRootPath() . 'public/storage',// 磁盘路径对应的外部URL路径'url' => '/storage',// 可见性'visibility' => 'public',],// 更多的磁盘配置信息],
];
二、阿里云工厂开发
切记在根目录下要安装阿里云OSS插件,具体代码如下:
composer require aliyuncs/oss-sdk-php
在根目录下extend文件夹下Upload文件夹下channel文件夹中,我们修改AliyunUpload业务控制器。具体代码如下:
<?php
/*** 阿里云上传工厂* User: 龙哥 三年风水* Date: 2024/11/2/0002* Time: 18:17*/
namespace Upload\channel;
use OSS\Core\OssException;
use OSS\OssClient;
use Upload\UploadHandle;
use Error\BaseError;
class AliyunUpload extends UploadHandle
{protected $accessKey = '';//access_keyprotected $secretKey = '';//secret_keyprotected $bucket = '';//包protected $name = '';//访问地址protected $domainName = '';//访问地址public function __construct($param){$this->accessKey = $param['access_key'];$this->secretKey = $param['secret_key'];$this->bucket = $param['bucket'];$this->name = $param['name'];$this->domainName = $param['domain_name'];}/*** 上传操作* @ param $object 加密后的文件* @ param $filePath 临时存放的文件路径* @ return mixed*/public function uploadFile($object, $filePath){$ossClient = new OssClient($this->accessKey, $this->secretKey, $this->name);if(!$ossClient->doesBucketExist($this->bucket))$ossClient->createBucket($this->bucket);try{$result = $ossClient->uploadFile($this->bucket, $object, $filePath);if($result==true)return empty($this->domainName) ? $result['info']['url'] : $this->domainName.'/'.$object;}catch (OssException $e){throw new BaseError($e->getErrorMessage(),50000,200);}}/*** 删除操作* @ param $object 具体的文件地址* @ return mixed*/public function deleteFile($object){// TODO: Implement deleteFile() method.}
}