StorageConfigService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\upload;
  12. use app\dict\sys\FileDict;
  13. use app\dict\sys\StorageDict;
  14. use app\service\core\upload\CoreStorageService;
  15. use app\service\core\sys\CoreConfigService;
  16. use core\base\BaseAdminService;
  17. use core\exception\AdminException;
  18. /**
  19. * 用户服务层
  20. * Class BaseService
  21. * @package app\service
  22. */
  23. class StorageConfigService extends BaseAdminService
  24. {
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * 获取云存储列表
  31. * @return array
  32. */
  33. public function getStorageList()
  34. {
  35. return (new CoreStorageService())->getStorageList();
  36. }
  37. /**
  38. * 获取存储配置
  39. * @param string $storage_type
  40. * @return array
  41. */
  42. public function getStorageConfig(string $storage_type)
  43. {
  44. $storage_type_list = StorageDict::getType();
  45. if(!array_key_exists($storage_type, $storage_type_list)) throw new AdminException('OSS_TYPE_NOT_EXIST');
  46. $info = (new CoreConfigService())->getConfig($this->request->defaultSiteId(), 'STORAGE');
  47. if(empty($info))
  48. {
  49. $config_type = ['default' => StorageDict::LOCAL];//初始化
  50. }else
  51. $config_type = $info['value'];
  52. $data = [
  53. 'storage_type' => $storage_type,
  54. 'is_use' => $storage_type == $config_type['default'] ? StorageDict::ON : StorageDict::OFF,
  55. 'name' => $storage_type_list[$storage_type]['name'],
  56. ];
  57. foreach ($storage_type_list[$storage_type]['params'] as $k_param => $v_param)
  58. {
  59. $data['params'][$k_param] = [
  60. 'name' => $v_param,
  61. 'value' => $config_type[$storage_type][$k_param] ?? ''
  62. ];
  63. }
  64. return $data;
  65. }
  66. /**
  67. * 云存储配置
  68. * @param string $storage_type
  69. * @param array $data
  70. * @return bool
  71. */
  72. public function setStorageConfig(string $storage_type, array $data)
  73. {
  74. $storage_type_list = StorageDict::getType();
  75. if(!array_key_exists($storage_type, $storage_type_list)) throw new AdminException('OSS_TYPE_NOT_EXIST');
  76. if($storage_type != FileDict::LOCAL){
  77. $domain = $data['domain'];
  78. if (!str_contains($domain, 'http://') && !str_contains($domain, 'https://')){
  79. throw new AdminException('STORAGE_NOT_HAS_HTTP_OR_HTTPS');
  80. }
  81. }
  82. $info = (new CoreConfigService())->getConfig($this->request->defaultSiteId(), 'STORAGE');
  83. if(empty($info))
  84. {
  85. $config['default'] = '';
  86. }else{
  87. $config = $info['value'];
  88. }
  89. //初始化数据
  90. if($data['is_use'])
  91. {
  92. $config['default'] = $storage_type;
  93. }else{
  94. $config['default'] = $config['default'] == $storage_type ? '' : $config['default'];
  95. }
  96. foreach ($storage_type_list[$storage_type]['params'] as $k_param => $v_param)
  97. {
  98. $config[$storage_type][$k_param] = $data[$k_param] ?? '';
  99. }
  100. return (new CoreConfigService())->setConfig($this->request->defaultSiteId(), 'STORAGE', $config);
  101. }
  102. }