UploadService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use app\common\repositories\system\StorageRepository;
  13. use crmeb\services\upload\Upload;
  14. /**
  15. * Class UploadService
  16. * @package crmeb\services
  17. */
  18. class UploadService
  19. {
  20. const STORAGE_LOCAL = 1;
  21. const STORAGE_QINIU = 2;
  22. const STORAGE_ALIYUN = 3;
  23. const STORAGE_TENGXUN = 4;
  24. const STORAGE_HUAWEI = 5;
  25. const STORAGE_UCLOUD = 6;
  26. const STORAGE_JINGDONG = 7;
  27. const STORAGE_TIANYI = 8;
  28. public function getType()
  29. {
  30. return [
  31. ['value' => self::STORAGE_QINIU, 'label' => '七牛云存储',],
  32. ['value' => self::STORAGE_ALIYUN, 'label' => '阿里云存储',],
  33. ['value' => self::STORAGE_TENGXUN, 'label' => '腾讯云存储',],
  34. ['value' => self::STORAGE_HUAWEI, 'label' => '华为云存储',],
  35. ['value' => self::STORAGE_UCLOUD, 'label' => 'UC云存储',],
  36. ['value' => self::STORAGE_JINGDONG, 'label' => '京东云存储',],
  37. ['value' => self::STORAGE_TIANYI, 'label' => '天翼云存储',],
  38. ];
  39. }
  40. public static function getPrefix($type = 1)
  41. {
  42. $prefix = [
  43. self::STORAGE_QINIU => 'qiniu_',
  44. self::STORAGE_ALIYUN => '',
  45. self::STORAGE_TENGXUN => 'tengxun_',
  46. self::STORAGE_HUAWEI => 'obs_',
  47. self::STORAGE_UCLOUD => 'uc_',
  48. self::STORAGE_JINGDONG => 'jdoss_',
  49. self::STORAGE_TIANYI => 'ctoss_',
  50. ];
  51. if ($type != 1) {
  52. $prefix = $prefix[$type] ?? $prefix;
  53. }
  54. return $prefix;
  55. }
  56. /**
  57. * @param $type
  58. * @return Upload
  59. */
  60. public static function create($type = null)
  61. {
  62. $type = $type ? : (systemConfig('upload_type') ?: 1);
  63. $type = (int)$type;
  64. $thumb = systemConfig(['thumb_big_height', 'thumb_big_width', 'thumb_mid_height', 'thumb_mid_width', 'thumb_small_height', 'thumb_small_width','image_thumb_status']);
  65. $water = systemConfig(['image_watermark_status', 'watermark_type', 'watermark_image', 'watermark_opacity', 'watermark_position', 'watermark_rotate', 'watermark_text', 'watermark_text_angle', 'watermark_text_color', 'watermark_text_size', 'watermark_x', 'watermark_y']);
  66. $water = array_filter($water);
  67. $config = [
  68. 'image_thumb_status' => systemConfig('image_thumb_status'),
  69. ];
  70. //除了本地存储其他都去获取配置信息
  71. if ($type != self::STORAGE_LOCAL) {
  72. $prefix = self::getPrefix();
  73. $config['accessKey'] = systemConfig($prefix[$type].'accessKey');
  74. $config['secretKey'] = systemConfig($prefix[$type].'secretKey');
  75. $make = app()->make(StorageRepository::class);
  76. $res = $make->getConfig($type,$config['accessKey']);
  77. $config['uploadUrl'] = $res['domain'];
  78. $config['storageName'] = $res['name'];
  79. //京东云特殊处理
  80. if (self::STORAGE_JINGDONG !== $type){
  81. $config['storageRegion'] = $res['region'];
  82. }else{
  83. $config['storageRegion'] = systemConfig('jd_storageRegion');
  84. }
  85. if(isset($res['cdn']) && $res['cdn']){
  86. $config['cdn'] = trim($res['cdn'],'/').'/';
  87. }
  88. }
  89. if ($type == self::STORAGE_TENGXUN) {
  90. $config['appid'] = systemConfig('tengxun_appid');
  91. }
  92. $config = array_merge($config, ['thumb' => $thumb,'water' => $water,]);
  93. return new Upload($type, $config);
  94. }
  95. }