Alioss.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\alioss\model;
  11. use app\model\BaseModel;
  12. use OSS\Core\OssException;
  13. use OSS\OssClient;
  14. /**
  15. * 阿里云OSS上传
  16. */
  17. class Alioss extends BaseModel
  18. {
  19. /**
  20. * 字节组上传
  21. * @param $data
  22. * @param $key
  23. * @return array
  24. */
  25. public function put($param)
  26. {
  27. $data = $param[ "data" ];
  28. $key = $param[ "key" ];
  29. $config_model = new Config();
  30. $config_result = $config_model->getAliossConfig();
  31. $config = $config_result[ "data" ];
  32. if ($config[ "is_use" ] == 1) {
  33. $config = $config[ "value" ];
  34. $access_key_id = $config[ "access_key_id" ];
  35. $access_key_secret = $config[ "access_key_secret" ];
  36. $bucket = $config[ "bucket" ];
  37. $endpoint = $config[ "endpoint" ];
  38. try {
  39. $ossClient = new OssClient($access_key_id, $access_key_secret, $endpoint);
  40. $result = $ossClient->putObject($bucket, $key, $data);
  41. $is_domain = $config[ 'is_domain' ] ?? 0;
  42. $path = $is_domain > 0 ? $config[ 'domain' ] . "/" . $key : $result[ "info" ][ "url" ];
  43. $data = array (
  44. "path" => $path,
  45. // "path" => $result["info"]["url"],
  46. "domain" => $endpoint,
  47. "bucket" => $bucket
  48. );
  49. return $this->success($data);
  50. } catch (OssException $e) {
  51. return $this->error('', $e->getErrorMessage());
  52. }
  53. }
  54. }
  55. /**
  56. * 设置阿里云OSS参数配置
  57. * @param unknown $filePath 上传图片路径
  58. * @param unknown $key 上传到阿里云后保存的文件名
  59. */
  60. public function putFile($param)
  61. {
  62. $file_path = $param[ "file_path" ];
  63. $key = $param[ "key" ];
  64. $config_model = new Config();
  65. $config_result = $config_model->getAliossConfig();
  66. $config = $config_result[ "data" ];
  67. if ($config[ "is_use" ] == 1) {
  68. $config = $config[ "value" ];
  69. $access_key_id = $config[ "access_key_id" ];
  70. $access_key_secret = $config[ "access_key_secret" ];
  71. $bucket = $config[ "bucket" ];
  72. //要上传的空间
  73. $endpoint = $config[ "endpoint" ];
  74. try {
  75. $ossClient = new OssClient($access_key_id, $access_key_secret, $endpoint);
  76. $result = $ossClient->uploadFile($bucket, $key, $file_path);
  77. $is_domain = $config[ 'is_domain' ] ?? 0;
  78. $path = $is_domain > 0 ? $config[ 'domain' ] . "/" . $key : $result[ "info" ][ "url" ];
  79. $path = str_replace('http://', 'https://', $path);
  80. //返回图片的完整URL
  81. $data = array (
  82. // "path" => $this->subEndpoint($endpoint, $bucket)."/". $key,
  83. "path" => $path,
  84. "domain" => $endpoint,
  85. "bucket" => $bucket
  86. );
  87. return $this->success($data);
  88. } catch (OssException $e) {
  89. return $this->error('', $e->getErrorMessage());
  90. }
  91. }
  92. }
  93. public function subEndpoint($endpoint, $bucket)
  94. {
  95. if (strpos($endpoint, 'http://') === 0) {
  96. $temp = "http://";
  97. } else {
  98. $temp = "https://";
  99. }
  100. $temp_array = explode($temp, $endpoint);
  101. return $temp . $bucket . "." . $temp_array[ 1 ];
  102. }
  103. /**
  104. * @param $file_path
  105. * @return array
  106. * 删除阿里云图片
  107. */
  108. public function deleteAlbumPic($file_path, $prefix)
  109. {
  110. $config_model = new Config();
  111. $config_result = $config_model->getAliossConfig();
  112. $config = $config_result[ "data" ];
  113. if (!empty($config)) {
  114. $config = $config[ "value" ];
  115. $access_key_id = $config[ "access_key_id" ];
  116. $access_key_secret = $config[ "access_key_secret" ];
  117. $bucket = $config[ "bucket" ];
  118. //要上传的空间
  119. $endpoint = $config[ "endpoint" ];
  120. try {
  121. $ossClient = new OssClient($access_key_id, $access_key_secret, $endpoint);
  122. $ossClient->deleteObject($bucket, str_replace($prefix . "/", "", $file_path));
  123. $ossClient->deleteObject($bucket, str_replace($prefix . "/", "", img($file_path, 'big')));
  124. $ossClient->deleteObject($bucket, str_replace($prefix . "/", "", img($file_path, 'mid')));
  125. $ossClient->deleteObject($bucket, str_replace($prefix . "/", "", img($file_path, 'small')));
  126. return $this->success();
  127. } catch (OssException $e) {
  128. return $this->error('', $e->getErrorMessage());
  129. }
  130. }
  131. }
  132. }