Qiniu.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace crmeb\services\storage;
  3. use crmeb\services\SystemConfigService;
  4. use Qiniu\Auth;
  5. use Qiniu\Storage\BucketManager;
  6. use Qiniu\Storage\UploadManager;
  7. use Qiniu\Config;
  8. use think\facade\Cache;
  9. /**
  10. * TODO 七牛云上传
  11. * Class Qiniu
  12. */
  13. class Qiniu
  14. {
  15. protected static $accessKey;
  16. protected static $secretKey;
  17. protected static $auth = null;
  18. //TODO 空间域名 Domain
  19. protected static $uploadUrl;
  20. //TODO 存储空间名称 公开空间
  21. protected static $storageName;
  22. /**
  23. * TODO 初始化
  24. * @return null|Auth
  25. * @throws \Exception
  26. */
  27. protected static function autoInfo()
  28. {
  29. if (($storageName = Cache::get('storageName')) && ($uploadUrl = Cache::get('uploadUrl')) && ($accessKey = Cache::get('accessKey')) && ($secretKey = Cache::get('secretKey'))) {
  30. self::$accessKey = $accessKey;
  31. self::$secretKey = $secretKey;
  32. self::$uploadUrl = $uploadUrl;
  33. self::$storageName = $storageName;
  34. } else {
  35. self::$accessKey = trim(sys_config('accessKey'));
  36. self::$secretKey = trim(sys_config('secretKey'));
  37. self::$uploadUrl = trim(sys_config('uploadUrl')) . '/';
  38. self::$storageName = trim(sys_config('storage_name'));
  39. Cache::set('accessKey', self::$accessKey);
  40. Cache::set('secretKey', self::$secretKey);
  41. Cache::set('uploadUrl', self::$uploadUrl);
  42. Cache::set('storageName', self::$storageName);
  43. }
  44. if (!self::$accessKey || !self::$secretKey || !self::$uploadUrl || !self::$storageName) {
  45. exception('请设置 secretKey 和 accessKey 和 空间域名 和 存储空间名称');
  46. }
  47. if (self::$auth == null) self::$auth = new Auth(self::$accessKey, self::$secretKey);
  48. return self::$auth;
  49. }
  50. /**
  51. * TODO 获取上传图片视频token和domain
  52. * @return array
  53. */
  54. public static function getToKenAndDomainI()
  55. {
  56. $token = self::autoInfo()->uploadToken(self::$storageName);
  57. $domain = self::$uploadUrl;
  58. $fileName = md5(rand(1000, 9999) . date('YmdHis') . rand(0, 9999));
  59. return compact('token', 'domain', 'fileName');
  60. }
  61. /**
  62. * TODO 图片上传 名称
  63. * @param string $filename
  64. * @return array
  65. * @throws \Exception
  66. */
  67. public static function uploadImage($filename = 'image')
  68. {
  69. $request = app('request');
  70. $file = $request->file($filename);
  71. $filePath = $file->getRealPath();
  72. $ext = $file->getOriginalExtension();
  73. $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;
  74. $token = self::autoInfo()->uploadToken(self::$storageName);
  75. try {
  76. $uploadMgr = new UploadManager();
  77. return $uploadMgr->putFile($token, $key, $filePath);
  78. } catch (\Exception $e) {
  79. return $e->getMessage();
  80. }
  81. }
  82. /**
  83. * TODO 图片上传 内容
  84. * @param $key
  85. * @param $content
  86. * @return array|string
  87. * @throws \Exception
  88. */
  89. public static function uploadImageStream($key, $content)
  90. {
  91. $token = self::autoInfo()->uploadToken(self::$storageName, $key);
  92. try {
  93. $uploadMgr = new UploadManager();
  94. return $uploadMgr->put($token, $key, $content);
  95. } catch (\Exception $e) {
  96. return $e->getMessage();
  97. }
  98. }
  99. /**
  100. * TODO 图片下载链接
  101. * @param $key uploadImage() 返回的key
  102. * @param string $type 是否设置图片大小
  103. * @param string $imageUrl 图片访问链接
  104. * @param int $time 图片链接最后的访问时间
  105. * @return array
  106. */
  107. public static function imageUrl($key, $type = '', $imageUrl = '', $time = 0)
  108. {
  109. return ['code' => 200, 'name' => $key, 'dir' => self::$uploadUrl . $key, 'thumb_path' => self::$uploadUrl . $key, 'time' => time()];
  110. // $imageValue = !strlen(trim($type)) ? self::$uploadUrl.$key : self::$uploadUrl.$key.self::getType($type);
  111. // if($time > time() && !strlen(trim($imageUrl))) return ['code'=>100,'dir'=>$imageUrl,'thumb_path'=>$imageUrl,'time'=>$time];
  112. // $imageUrl = self::autoInfo()->privateDownloadUrl($imageValue);
  113. // return ['code'=>200,'name'=>$key,'dir'=>$imageUrl,'thumb_path'=>$imageUrl,'time'=>bcadd(time(),3600,0)];
  114. }
  115. /**
  116. * TODO 获取图片时转换图片大小
  117. * @param $imageType
  118. * @return string
  119. */
  120. public static function getType($imageType)
  121. {
  122. $type = '';
  123. switch ($imageType) {
  124. case "8x6":
  125. $type = '?imageView2/1/w/800/h/600';
  126. break;
  127. }
  128. return $type;
  129. }
  130. /**
  131. * TODO 删除资源
  132. * @param $key
  133. * @param $bucket
  134. * @return mixed
  135. */
  136. public static function delete($key)
  137. {
  138. $bucketManager = new BucketManager(self::autoInfo(), new Config());
  139. return $bucketManager->delete(self::$storageName, $key);
  140. }
  141. }