QiniuService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use Qiniu\Auth;
  13. use Qiniu\Storage\BucketManager;
  14. use Qiniu\Storage\UploadManager;
  15. /**
  16. * 七牛存储接口管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * @package App\Services
  20. */
  21. class QiniuService extends BaseService
  22. {
  23. // 静态对象
  24. protected static $instance = null;
  25. protected $host = '';
  26. protected $accessKey = '';
  27. protected $secretKey = '';
  28. protected $bucket = '';
  29. public function __construct()
  30. {
  31. $this->accessKey = ConfigService::make()->getConfigByCode('qiniu_access_key');
  32. $this->secretKey = ConfigService::make()->getConfigByCode('qiniu_secret_key');
  33. $this->bucket = ConfigService::make()->getConfigByCode('qiniu_bucket', '');
  34. $this->host = ConfigService::make()->getConfigByCode('qiniu_host', '');
  35. }
  36. /**
  37. * 静态入口
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = new static();
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 上传文件/图片
  48. * @param $filename
  49. * @param $file
  50. * @return false|string
  51. * @throws \Exception
  52. */
  53. public function upload($filename, $file)
  54. {
  55. // 配置参数
  56. if (empty($this->accessKey) || empty($this->secretKey) || empty($this->bucket) || empty($this->host)) {
  57. $this->error = 'QINIU接口参数未配置';
  58. return false;
  59. }
  60. // 初始化鉴权对象
  61. $auth = new Auth($this->accessKey, $this->secretKey);
  62. // 生成上传策略
  63. $policy = null;
  64. $uptoken = $auth->uploadToken($this->bucket, $filename, 3600, $policy);
  65. // 初始化 UploadManager 对象并进行文件上传
  66. $uploadMgr = new UploadManager();
  67. list($ret, $err) = $uploadMgr->putFile($uptoken, $filename, $file->getPathname());
  68. if ($err !== null) {
  69. $this->error = 'QINIU文件上传失败:' . (is_object($err) ? $err->getMessage() : $err);
  70. return false;
  71. } else {
  72. return '/qiniu/' . $filename;
  73. }
  74. }
  75. /**
  76. * 上传base图片
  77. * @param $filename
  78. * @param $base64
  79. * @return false|string
  80. */
  81. public function uploadBase64($filename, $base64)
  82. {
  83. // 配置参数
  84. if (empty($this->accessKey) || empty($this->secretKey) || empty($this->bucket) || empty($this->host)) {
  85. $this->error = 'QINIU接口参数未配置';
  86. return false;
  87. }
  88. // 初始化鉴权对象
  89. $auth = new Auth($this->accessKey, $this->secretKey);
  90. // 生成上传策略
  91. $policy = null;
  92. $uptoken = $auth->uploadToken($this->bucket, $filename, 3600, $policy);
  93. // 初始化 UploadManager 对象并进行文件上传
  94. $uploadMgr = new UploadManager();
  95. list($ret, $err) = $uploadMgr->put($uptoken, $filename, $base64, null, 'image/png');
  96. if ($err !== null) {
  97. $this->error = 'QINIU文件上传失败';
  98. return false;
  99. } else {
  100. return '/qiniu/' . $filename;
  101. }
  102. }
  103. public function deleteImage($url)
  104. {
  105. // 配置参数
  106. if (empty($this->accessKey) || empty($this->secretKey) || empty($this->bucket) || empty($this->host)) {
  107. $this->error = 'QINIU接口参数未配置';
  108. return false;
  109. }
  110. // 初始化鉴权对象
  111. $auth = new Auth($this->accessKey, $this->secretKey);
  112. // 初始化 UploadManager 对象并进行文件上传
  113. $bucketManager = new BucketManager($auth);
  114. $res = $bucketManager->delete($this->bucket, $url);
  115. if ($res === null) {
  116. $this->error = '删除成功';
  117. return true;
  118. } else {
  119. $this->error = '删除失败';
  120. return false;
  121. }
  122. }
  123. /**
  124. * 图片路径
  125. * @param $path
  126. * @return string|string[]
  127. */
  128. public function getImageUrl($path)
  129. {
  130. return str_replace('/qiniu', $this->host, $path);
  131. }
  132. /**
  133. * 图片路径
  134. * @param $path
  135. * @return string|string[]
  136. */
  137. public function getImagePath($path)
  138. {
  139. return str_replace($this->host, '/qiniu', $path);
  140. }
  141. }