UploadService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\other;
  12. use app\services\system\config\SystemStorageServices;
  13. use crmeb\exceptions\UploadException;
  14. use crmeb\services\SystemConfigService;
  15. use crmeb\services\upload\Upload;
  16. use crmeb\utils\DownloadImage;
  17. /**
  18. * Class UploadService
  19. * @package crmeb\services
  20. */
  21. class UploadService
  22. {
  23. /**
  24. * @var array
  25. */
  26. protected static $upload = [];
  27. /**
  28. * @param null $type
  29. * @return Upload|mixed
  30. */
  31. public static function init($type = null)
  32. {
  33. if (is_null($type)) {
  34. $type = (int)sys_config('upload_type', 1);
  35. }
  36. if (isset(self::$upload['upload_' . $type])) {
  37. return self::$upload['upload_' . $type];
  38. }
  39. $type = (int)$type;
  40. $config = [];
  41. switch ($type) {
  42. case 2://七牛
  43. $config = [
  44. 'accessKey' => sys_config('qiniu_accessKey'),
  45. 'secretKey' => sys_config('qiniu_secretKey'),
  46. ];
  47. break;
  48. case 3:// oss 阿里云
  49. $config = [
  50. 'accessKey' => sys_config('accessKey'),
  51. 'secretKey' => sys_config('secretKey'),
  52. ];
  53. break;
  54. case 4:// cos 腾讯云
  55. $config = [
  56. 'accessKey' => sys_config('tengxun_accessKey'),
  57. 'secretKey' => sys_config('tengxun_secretKey'),
  58. 'appid' => sys_config('tengxun_appid'),
  59. ];
  60. break;
  61. case 5://京东云
  62. $config = [
  63. 'accessKey' => sys_config('jd_accessKey'),
  64. 'secretKey' => sys_config('jd_secretKey'),
  65. ];
  66. break;
  67. case 6://华为云
  68. $config = [
  69. 'accessKey' => sys_config('hw_accessKey'),
  70. 'secretKey' => sys_config('hw_secretKey'),
  71. ];
  72. break;
  73. case 7://天翼云
  74. $config = [
  75. 'accessKey' => sys_config('ty_accessKey'),
  76. 'secretKey' => sys_config('ty_secretKey'),
  77. ];
  78. break;
  79. case 1:
  80. break;
  81. default:
  82. throw new UploadException(400733);
  83. }
  84. //除了本地存储其他都去获取配置信息
  85. if (1 !== $type) {
  86. /** @var SystemStorageServices $make */
  87. $make = app()->make(SystemStorageServices::class);
  88. $res = $make->getConfig($type);
  89. $config['uploadUrl'] = $res['domain'];
  90. $config['storageName'] = $res['name'];
  91. $config['storageRegion'] = $res['region'];
  92. }
  93. $thumb = SystemConfigService::more(['thumb_big_height', 'thumb_big_width', 'thumb_mid_height', 'thumb_mid_width', 'thumb_small_height', 'thumb_small_width',]);
  94. $water = SystemConfigService::more([
  95. 'image_watermark_status',
  96. 'watermark_type',
  97. 'watermark_image',
  98. 'watermark_opacity',
  99. 'watermark_position',
  100. 'watermark_rotate',
  101. 'watermark_text',
  102. 'watermark_text_angle',
  103. 'watermark_text_color',
  104. 'watermark_text_size',
  105. 'watermark_x',
  106. 'watermark_y']);
  107. $config = array_merge($config, ['thumb' => $thumb], ['water' => $water]);
  108. return self::$upload['upload_' . $type] = new Upload($type, $config);
  109. }
  110. /**
  111. * 生辰缩略图水印实例化
  112. * @param string $filePath
  113. * @param bool $is_remote_down
  114. * @return Upload
  115. */
  116. public static function getOssInit(string $filePath, bool $is_remote_down = false)
  117. {
  118. //本地
  119. $uploadUrl = sys_config('site_url');
  120. if ($uploadUrl && strpos($filePath, $uploadUrl) !== false) {
  121. $filePath = explode($uploadUrl, $filePath)[1] ?? '';
  122. return self::init(1)->setFilepath($filePath);
  123. }
  124. $fileArr = parse_url($filePath);
  125. $fileHost = $fileArr['scheme'] . '://' . $fileArr['host'];
  126. /** @var SystemStorageServices $storageServices */
  127. $storageServices = app()->make(SystemStorageServices::class);
  128. $storageArr = $storageServices->cacheDriver()->remember('storage_list', function () use ($storageServices) {
  129. return $storageServices->selectList([], 'domain,type')->toArray();
  130. });
  131. foreach ($storageArr as $item) {
  132. if ($fileHost == $item['domain']) {
  133. return self::init($item['type'])->setFilepath($filePath);
  134. }
  135. }
  136. //远程图片 下载到本地处理
  137. if ($is_remote_down) {
  138. try {
  139. /** @var DownloadImage $down */
  140. $down = app()->make(DownloadImage::class);
  141. $data = $down->path('thumb_water')->downloadImage($filePath);
  142. $filePath = $data['path'] ?? '';
  143. } catch (\Throwable $e) {
  144. //下载失败 传入原地址
  145. }
  146. }
  147. return self::init(1)->setFilepath($filePath);
  148. }
  149. }