| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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->accessKey) || empty($this->secretKey) || empty($this->bucket) || empty($this->host)){
- $this->error = 'QINIU接口参数未配置';
- 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 = 'QINIU图片上传失败:' . $err->getMessage();
- return false;
- } else {
- return '/qiniu/' . $filename;
- }
- }
- /**
- * 查询
- * @param $no 快递单号
- * @param string $phone // 手机号
- * @param string $code 快递公司编号
- * @return bool
- */
- public function uploadBase64($filename,$base64)
- {
- // 配置参数
- if(empty($this->accessKey) || empty($this->secretKey) || empty($this->bucket) || empty($this->host)){
- $this->error = 'QINIU接口参数未配置';
- 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->put($uptoken,$filename, $base64,null,'image/png');
- if ($err !== null) {
- $this->error = 'QINIU图片上传失败';
- return false;
- } else {
- return '/qiniu/' . $filename;
- }
- }
- /**
- * 图片路径
- * @param $path
- * @return string|string[]
- */
- public function getImageUrl($path)
- {
- return str_replace('/qiniu', $this->host, $path);
- }
- }
|