Qiniu.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\qiniu\model;
  11. use app\model\BaseModel;
  12. // 引入鉴权类
  13. use \Qiniu\Auth;
  14. // 引入上传类
  15. use Qiniu\Storage\BucketManager;
  16. use \Qiniu\Storage\UploadManager;
  17. /**
  18. * 七牛云上传
  19. */
  20. class Qiniu extends BaseModel
  21. {
  22. /**
  23. * 字节组上传
  24. * @param $data
  25. * @param $key
  26. * @return array
  27. */
  28. public function put($param)
  29. {
  30. $data = $param["data"];
  31. $key = $param["key"];
  32. $config_model = new Config();
  33. $config_result = $config_model->getQiniuConfig();
  34. $config = $config_result["data"];
  35. if ($config["is_use"] == 1) {
  36. $config = $config["value"];
  37. $accessKey = $config["access_key"];
  38. $secretKey = $config["secret_key"];
  39. $bucket = $config["bucket"];
  40. $auth = new Auth($accessKey, $secretKey);
  41. $token = $auth->uploadToken($bucket);
  42. (new BucketManager($auth))->delete($bucket, $key);
  43. $uploadMgr = new UploadManager();
  44. //----------------------------------------upload demo1 ----------------------------------------
  45. // 上传字符串到七牛
  46. list($ret, $err) = $uploadMgr->put($token, $key, $data);
  47. if ($err !== null) {
  48. return $this->error('', $err->getResponse()->error);
  49. } else {
  50. //返回图片的完整URL
  51. $domain = $config["domain"];//自定义域名
  52. $data = array(
  53. "path" => $domain . "/" . $key,
  54. "domain" => $domain,
  55. "bucket" => $bucket
  56. );
  57. return $this->success($data);
  58. }
  59. }
  60. }
  61. /**
  62. * 设置七牛参数配置
  63. * @param unknown $filePath 上传图片路径
  64. * @param unknown $key 上传到七牛后保存的文件名
  65. */
  66. public function putFile($param)
  67. {
  68. $file_path = $param["file_path"];
  69. $key = $param["key"];
  70. $config_model = new Config();
  71. $config_result = $config_model->getQiniuConfig();
  72. $config = $config_result["data"];
  73. if ($config["is_use"] == 1) {
  74. $config = $config["value"];
  75. $accessKey = $config["access_key"];
  76. $secretKey = $config["secret_key"];
  77. $bucket = $config["bucket"];
  78. $auth = new Auth($accessKey, $secretKey);
  79. (new BucketManager($auth))->delete($bucket, $key);
  80. //要上传的空间
  81. $domain = $config["domain"];
  82. $token = $auth->uploadToken($bucket);
  83. // 初始化 UploadManager 对象并进行文件的上传
  84. $uploadMgr = new UploadManager();
  85. // 调用 UploadManager 的 putFile 方法进行文件的上传
  86. list($ret, $err) = $uploadMgr->putFile($token, $key, $file_path);
  87. if ($err !== null) {
  88. return $this->error('', $err->getResponse()->error);
  89. } else {
  90. //返回图片的完整URL
  91. $domain = $config["domain"];//自定义域名
  92. $data = array(
  93. "path" => $domain . "/" . $key,
  94. "domain" => $domain,
  95. "bucket" => $bucket
  96. );
  97. return $this->success($data);
  98. }
  99. }
  100. }
  101. /**
  102. * @param $file_path
  103. * 删除七牛云图片
  104. */
  105. public function deleteAlbumPic($file_path, $prefix){
  106. $config_model = new Config();
  107. $config_result = $config_model->getQiniuConfig();
  108. $config = $config_result["data"];
  109. if(!empty($config)){
  110. $config = $config["value"];
  111. $accessKey = $config["access_key"];
  112. $secretKey = $config["secret_key"];
  113. $bucket = $config["bucket"];
  114. $auth = new Auth($accessKey, $secretKey);
  115. // $prefix = substr($file_path,0,strripos($file_path, "/"));
  116. // dump(str_replace($prefix."/", "",$file_path));
  117. (new BucketManager($auth))->delete($bucket, str_replace($prefix."/", "",$file_path));
  118. (new BucketManager($auth))->delete($bucket, str_replace($prefix."/", "",img($file_path, 'big')));
  119. (new BucketManager($auth))->delete($bucket, str_replace($prefix."/", "",img($file_path, 'mid')));
  120. (new BucketManager($auth))->delete($bucket, str_replace($prefix."/", "",img($file_path, 'small')));
  121. }
  122. }
  123. }