// +---------------------------------------------------------------------- namespace App\Services; use Qiniu\Auth; use Qiniu\Storage\UploadManager; /** * 七牛存储接口管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services */ class QiniuService extends BaseService { // 静态对象 protected static $instance = null; protected $host = ''; protected $accessKey = ''; protected $secretKey = ''; protected $bucket = ''; public function __construct() { $this->accessKey = ConfigService::make()->getConfigByCode('qiniu_access_key'); $this->secretKey = ConfigService::make()->getConfigByCode('qiniu_secret_key'); $this->bucket = ConfigService::make()->getConfigByCode('qiniu_bucket',''); $this->host = ConfigService::make()->getConfigByCode('qiniu_host',''); } /** * 静态入口 */ public static function make(){ if(!self::$instance){ self::$instance = new static(); } return self::$instance; } /** * 查询 * @param $no 快递单号 * @param string $phone // 手机号 * @param string $code 快递公司编号 * @return bool */ public function upload($filename,$file) { // 配置参数 if(empty($this->apiKey) || empty($this->apiCode) || empty($this->bucket) || empty($this->host)){ $this->error = '接口参数未配置'; return false; } // 初始化鉴权对象 $auth = new Auth($this->accessKey, $this->secretKey); // 生成上传策略 $policy = null; $uptoken = $auth->uploadToken($this->bucket, $filename, 3600, $policy); // 初始化 UploadManager 对象并进行文件上传 $uploadMgr = new UploadManager(); list($ret, $err) = $uploadMgr->putFile($uptoken, $filename, $file->getPathname()); if ($err !== null) { $this->error = '图片上传失败:' . $err->getMessage(); return false; } else { return $this->host . '/' . $filename; } } }