SystemStorage.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 app\controller\admin\system;
  12. use app\common\repositories\system\config\ConfigClassifyRepository;
  13. use app\common\repositories\system\config\ConfigRepository;
  14. use app\common\repositories\system\config\ConfigValueRepository;
  15. use app\common\repositories\system\StorageRepository;
  16. use crmeb\basic\BaseController;
  17. use crmeb\exceptions\AdminException;
  18. use crmeb\services\UploadService;
  19. use think\App;
  20. class SystemStorage extends BaseController
  21. {
  22. protected $repository;
  23. public function __construct(App $app, StorageRepository $repository)
  24. {
  25. parent::__construct($app);
  26. $this->repository = $repository;
  27. }
  28. /**
  29. * 获得所有的云存储类型
  30. * @return \think\response\Json
  31. * @author Qinii
  32. * @day 2024/3/9
  33. */
  34. public function typeList()
  35. {
  36. $data = $this->repository->getType();
  37. return app('json')->success($data);
  38. }
  39. /**
  40. * 获取存储配置信息
  41. * @return \think\response\Json
  42. * @author Qinii
  43. * @day 2024/3/9
  44. */
  45. public function getConfig()
  46. {
  47. $config = [
  48. "upload_type", "thumb_big_width", "thumb_big_height", "thumb_mid_width", "thumb_mid_height", "thumb_small_width", "thumb_small_height", "image_watermark_status", "watermark_type", "watermark_image", "watermark_text", "watermark_position", "watermark_text_size", "watermark_opacity", "watermark_text_color", "watermark_rotate", "watermark_text_angle", "watermark_x", "watermark_y","image_thumb_status"
  49. ];
  50. $data = systemConfig($config);
  51. return app('json')->success($data);
  52. }
  53. /**
  54. * 保存存储配置信息
  55. * @param ConfigValueRepository $configValueRepository
  56. * @return \think\response\Json
  57. * @author Qinii
  58. * @day 2024/3/9
  59. */
  60. public function setConfig(ConfigValueRepository $configValueRepository)
  61. {
  62. $config = [
  63. ["upload_type",1], "thumb_big_width", "thumb_big_height", "thumb_mid_width", "thumb_mid_height", "thumb_small_width", "thumb_small_height", "image_watermark_status", "watermark_type", "watermark_image", "watermark_text", "watermark_position", "watermark_text_size", "watermark_opacity", "watermark_text_color", "watermark_rotate", "watermark_text_angle", "watermark_x", "watermark_y","image_thumb_status",
  64. ];
  65. $data = $this->request->params($config);
  66. if(!isset($data['upload_type']) || !$data['upload_type']){
  67. throw new AdminException('参数错误');
  68. }
  69. if(isset($data['image_watermark_status']) && $data['image_watermark_status']){
  70. if(!isset($data['watermark_type']) || !$data['watermark_type']){
  71. throw new AdminException('参数错误');
  72. }
  73. if(!isset($data['watermark_opacity']) || $data['watermark_opacity'] < 0){
  74. throw new AdminException('参数错误');
  75. }
  76. }
  77. $configValueRepository->setFormData($data, 0);
  78. return app('json')->success('设置成功');
  79. }
  80. /**
  81. * 填写各云存储accessKey secretKey的表单
  82. * @param $type
  83. * @return \think\response\Json
  84. * @author Qinii
  85. * @day 2024/3/9
  86. */
  87. public function form($type)
  88. {
  89. $form = $this->repository->form($type);
  90. return app('json')->success(formToData($form));
  91. }
  92. /**
  93. * 各云存储accessKey secretKey
  94. * @param ConfigValueRepository $configValueRepository
  95. * @return \think\response\Json
  96. * @author Qinii
  97. * @day 2024/3/9
  98. */
  99. public function setForm(ConfigValueRepository $configValueRepository)
  100. {
  101. $type = $this->request->param('upload_type');
  102. $prefix = $this->repository->getPrefix($type);
  103. $paramNameArray = [
  104. 'accessKey', 'secretKey'
  105. ];
  106. if ($type == UploadService::STORAGE_TENGXUN) {
  107. $paramNameArray[] = 'tengxun_appid';
  108. }
  109. if ($type == UploadService::STORAGE_JINGDONG) {
  110. $paramNameArray[] = 'jd_storageRegion';
  111. }
  112. $params = $this->request->params($paramNameArray);
  113. if (!isset($params['accessKey']) || !$params['accessKey'] || !isset($params['secretKey']) || !$params['secretKey']) {
  114. return app('json')->fail('参数错误');
  115. }
  116. $accessKey = $params['accessKey'];
  117. $secretKey = $params['secretKey'];
  118. unset($params['accessKey'],$params['secretKey']);
  119. $params[$prefix . 'accessKey'] = $accessKey;
  120. $params[$prefix . 'secretKey'] = $secretKey;
  121. $configValueRepository->setFormData($params, 0);
  122. return app('json')->success('提交成功');
  123. }
  124. /**
  125. * 同步存储空间
  126. * @param $type
  127. * @return \think\response\Json
  128. * @author Qinii
  129. * @day 2024/3/14
  130. */
  131. public function sync($type)
  132. {
  133. try {
  134. $this->repository->synchRegion((int)$type);
  135. }catch (\Exception $e){
  136. return app('json')->fail('同步存储空间失败,'.$e->getMessage());
  137. }
  138. return app('json')->success('同步成功');
  139. }
  140. /**
  141. * 存储空间列表
  142. * @param $type
  143. * @return \think\response\Json
  144. * @author Qinii
  145. * @day 2024/3/14
  146. */
  147. public function lstRegion($type)
  148. {
  149. [$page, $limit] = $this->getPage();
  150. $data = $this->repository->lstRegion(['type' => $type], $page, $limit);
  151. return app('json')->success($data);
  152. }
  153. /**
  154. * 创建存储空间表单
  155. * @param $type
  156. * @return \think\response\Json
  157. * @author Qinii
  158. * @day 2024/3/14
  159. */
  160. public function createRegionForm($type)
  161. {
  162. $form = $this->repository->createRegionForm($type);
  163. return app('json')->success(formToData($form));
  164. }
  165. /**
  166. * 创建存储空间
  167. * @param $type
  168. * @return \think\response\Json
  169. * @author Qinii
  170. * @day 2024/3/14
  171. */
  172. public function createRegion($type)
  173. {
  174. $param = ['name', 'region', 'acl'];
  175. $data = $this->request->params($param);
  176. $paramNameArray = [
  177. 'accessKey', 'secretKey'
  178. ];
  179. if ($type == UploadService::STORAGE_TENGXUN) {
  180. $paramNameArray[] = 'tengxun_appid';
  181. }
  182. if ($type == UploadService::STORAGE_JINGDONG) {
  183. $paramNameArray[] = 'jd_storageRegion';
  184. }
  185. $params = $this->request->params($paramNameArray);
  186. if ($type == UploadService::STORAGE_TENGXUN) {
  187. if (!$params['tengxun_appid'] && !systemConfig('tengxun_appid')) {
  188. return app('json')->fail('请填写APPDID');
  189. }
  190. }
  191. try {
  192. $this->repository->createRegion((int)$type, $data,$params);
  193. }catch (\Exception $e){
  194. return app('json')->fail('创建存储空间失败,'.$e->getMessage());
  195. }
  196. return app('json')->success('添加成功');
  197. }
  198. /**
  199. * 修改空间域名表单
  200. * @param $id
  201. * @return \think\response\Json
  202. * @author Qinii
  203. * @day 2024/3/14
  204. */
  205. public function editDomainForm($id)
  206. {
  207. $form = $this->repository->editDomainForm($id);
  208. return app('json')->success(formToData($form));
  209. }
  210. /**
  211. * 修改空间域名
  212. * @param $id
  213. * @return \think\response\Json
  214. * @author Qinii
  215. * @day 2024/3/14
  216. */
  217. public function editDomain($id)
  218. {
  219. $domain = $this->request->post('domain', '');
  220. $cdn = $this->request->post('cdn', '');
  221. if (!$domain) {
  222. return app('json')->fail('空间域名');
  223. }
  224. if (strstr($domain, 'https://') === false && strstr($domain, 'http://') === false) {
  225. return app('json')->fail('请输入正确域名:http/https');
  226. }
  227. $this->repository->updateDomain($id, $domain, ['cdn' => $cdn]);
  228. return app('json')->success('修改成功');
  229. }
  230. /**
  231. * 选择使用存储空间
  232. * @param $id
  233. * @return \think\response\Json
  234. * @author Qinii
  235. * @day 2024/3/14
  236. */
  237. public function swtichStatus($id)
  238. {
  239. $info = $this->repository->get($id);
  240. if (!$info) return app('json')->fail('数据不存在');
  241. if (!$info->domain) {
  242. return app('json')->fail('未配置空间域名');
  243. }
  244. $this->repository->status($id, $info);
  245. return app('json')->success('修改成功');
  246. }
  247. /**
  248. * 删除存储空间
  249. * @param $id
  250. * @return \think\response\Json
  251. * @author Qinii
  252. * @day 2024/3/14
  253. */
  254. public function deleteRegion($id)
  255. {
  256. if ($this->repository->deleteRegion($id)) {
  257. return app('json')->success('删除成功');
  258. } else {
  259. return app('json')->fail('删除失败');
  260. }
  261. }
  262. }