ShopStorageLogic.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\settings\shop;
  20. use app\common\enum\YesNoEnum;
  21. use app\common\logic\BaseLogic;
  22. use app\common\service\ConfigService;
  23. use app\common\service\FileService;
  24. use app\common\service\RegionService;
  25. use think\facade\Cache;
  26. /**
  27. * 存储设置逻辑层
  28. * Class ShopStorageLogic
  29. * @package app\adminapi\logic\settings\shop
  30. */
  31. class ShopStorageLogic extends BaseLogic
  32. {
  33. /**
  34. * @notes 存储引擎列表
  35. * @return array[]
  36. * @author suny
  37. * @date 2021/9/9 4:52 下午
  38. */
  39. public static function lists()
  40. {
  41. $default = ConfigService::get('storage', 'default', 'local');
  42. $data = [
  43. [
  44. 'name' => '本地存储',
  45. 'path' => '存储在本地服务器',
  46. 'engine' => 'local',
  47. 'status' => $default == 'local' ? 1 : 0
  48. ],
  49. [
  50. 'name' => '七牛云存储',
  51. 'path' => '存储在七牛云,请前往七牛云开通存储服务',
  52. 'engine' => 'qiniu',
  53. 'status' => $default == 'qiniu' ? 1 : 0
  54. ],
  55. [
  56. 'name' => '阿里云OSS',
  57. 'path' => '存储在阿里云,请前往阿里云开通存储服务',
  58. 'engine' => 'aliyun',
  59. 'status' => $default == 'aliyun' ? 1 : 0
  60. ],
  61. [
  62. 'name' => '腾讯云OSS',
  63. 'path' => '存储在腾讯云,请前往腾讯云开通存储服务',
  64. 'engine' => 'qcloud',
  65. 'status' => $default == 'qcloud' ? 1 : 0
  66. ]
  67. ];
  68. return $data;
  69. }
  70. /**
  71. * @notes 存储设置信息
  72. * @return mixed
  73. * @author suny
  74. * @date 2021/9/9 3:19 下午
  75. */
  76. public static function index($param): mixed
  77. {
  78. $default = ConfigService::get('storage', 'default', '');
  79. // 本地存储
  80. $local = ['status' => $default == 'local' ? 1 : 0];
  81. // 七牛云存储
  82. $qiniu = ConfigService::get('storage', 'qiniu', [
  83. 'bucket' => '',
  84. 'access_key' => '',
  85. 'secret_key' => '',
  86. 'domain' => '',
  87. 'status' => $default == 'qiniu' ? 1 : 0
  88. ]);
  89. // 阿里云存储
  90. $aliyun = ConfigService::get('storage', 'aliyun', [
  91. 'bucket' => '',
  92. 'access_key' => '',
  93. 'secret_key' => '',
  94. 'domain' => '',
  95. 'status' => $default == 'aliyun' ? 1 : 0
  96. ]);
  97. // 腾讯云存储
  98. $qcloud = ConfigService::get('storage', 'qcloud', [
  99. 'bucket' => '',
  100. 'region' => '',
  101. 'access_key' => '',
  102. 'secret_key' => '',
  103. 'domain' => '',
  104. 'status' => $default == 'qcloud' ? 1 : 0
  105. ]);
  106. $data = [
  107. 'local' => $local,
  108. 'qiniu' => $qiniu,
  109. 'aliyun' => $aliyun,
  110. 'qcloud' => $qcloud
  111. ];
  112. $result = $data[$param['engine']];
  113. if ($param['engine'] == $default) {
  114. $result['status'] = 1;
  115. } else {
  116. $result['status'] = 0;
  117. }
  118. return $result;
  119. }
  120. /**
  121. * @notes 设置存储参数
  122. * @param $params
  123. * @return bool
  124. * @author suny
  125. * @date 2021/9/9 3:56 下午
  126. */
  127. public static function setup($params)
  128. {
  129. if ($params['status'] == 1) { //状态为开启
  130. ConfigService::set('storage', 'default', $params['engine']);
  131. } else {
  132. ConfigService::set('storage', 'default', 'local');
  133. }
  134. switch ($params['engine']) {
  135. case 'local':
  136. ConfigService::set('storage', 'local', []);
  137. break;
  138. case 'qiniu':
  139. ConfigService::set('storage', 'qiniu', [
  140. 'bucket' => $params['bucket'] ?? '',
  141. 'access_key' => $params['access_key'] ?? '',
  142. 'secret_key' => $params['secret_key'] ?? '',
  143. 'domain' => $params['domain'] ?? ''
  144. ]);
  145. break;
  146. case 'aliyun':
  147. ConfigService::set('storage', 'aliyun', [
  148. 'bucket' => $params['bucket'] ?? '',
  149. 'access_key' => $params['access_key'] ?? '',
  150. 'secret_key' => $params['secret_key'] ?? '',
  151. 'domain' => $params['domain'] ?? ''
  152. ]);
  153. break;
  154. case 'qcloud':
  155. ConfigService::set('storage', 'qcloud', [
  156. 'bucket' => $params['bucket'] ?? '',
  157. 'region' => $params['region'] ?? '',
  158. 'access_key' => $params['access_key'] ?? '',
  159. 'secret_key' => $params['secret_key'] ?? '',
  160. 'domain' => $params['domain'] ?? '',
  161. ]);
  162. break;
  163. }
  164. Cache::delete('STORAGE_DEFAULT');
  165. Cache::delete('STORAGE_ENGINE');
  166. if($params['engine'] == 'local' && $params['status'] == 0){
  167. return '默认开启本地存储';
  168. }else{
  169. return true;
  170. }
  171. }
  172. /**
  173. * @notes 切换状态
  174. * @param $params
  175. * @author suny
  176. * @date 2021/9/10 2:58 下午
  177. */
  178. public static function change($params)
  179. {
  180. $default = ConfigService::get('storage', 'default', '');
  181. if($default == $params['engine']){
  182. ConfigService::set('storage', 'default', 'local');
  183. }else{
  184. ConfigService::set('storage', 'default', $params['engine']);
  185. }
  186. Cache::delete('STORAGE_DEFAULT');
  187. Cache::delete('STORAGE_ENGINE');
  188. }
  189. }