SystemStorage.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\adminapi\controller\v1\setting;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\system\config\SystemConfigServices;
  14. use app\services\system\config\SystemStorageServices;
  15. use app\services\other\UploadService;
  16. use think\facade\App;
  17. /**
  18. * Class SystemStorage
  19. * @package app\adminapi\controller\v1\setting
  20. */
  21. class SystemStorage extends AuthController
  22. {
  23. /**
  24. * SystemStorage constructor.
  25. * @param App $app
  26. * @param SystemStorageServices $services
  27. */
  28. public function __construct(App $app, SystemStorageServices $services)
  29. {
  30. parent::__construct($app);
  31. $this->services = $services;
  32. }
  33. /**
  34. * @return mixed
  35. */
  36. public function index()
  37. {
  38. return app('json')->success($this->services->getList(['type' => $this->request->get('type')]));
  39. }
  40. /**
  41. * 获取创建数据表单
  42. * @param $type
  43. * @return mixed
  44. * @throws \FormBuilder\Exception\FormBuilderException
  45. */
  46. public function create($type)
  47. {
  48. if (!$type) {
  49. return app('json')->fail(100100);
  50. }
  51. return app('json')->success($this->services->getFormStorage((int)$type));
  52. }
  53. /**
  54. * 获取配置表单
  55. * @param $type
  56. * @return mixed
  57. * @throws \FormBuilder\Exception\FormBuilderException
  58. */
  59. public function getConfigForm($type)
  60. {
  61. return app('json')->success($this->services->getFormStorageConfig((int)$type));
  62. }
  63. /**
  64. * 获取配置类型
  65. * @return mixed
  66. */
  67. public function getConfig()
  68. {
  69. return app('json')->success(['type' => (int)sys_config('upload_type', 1)]);
  70. }
  71. /**
  72. * @return mixed
  73. */
  74. public function saveConfig( )
  75. {
  76. $type = (int)$this->request->post('type', 0);
  77. $data = $this->request->postMore([
  78. ['accessKey', ''],
  79. ['secretKey', ''],
  80. ['appid', ''],
  81. ['storageRegion', ''],
  82. ]);
  83. $this->services->saveConfig((int)$type, $data);
  84. return app('json')->success(100000);
  85. }
  86. /**
  87. * @param $type
  88. * @return mixed
  89. */
  90. public function synch($type)
  91. {
  92. $this->services->synchronization((int)$type);
  93. return app('json')->success(100038);
  94. }
  95. /**
  96. * 保存类型
  97. * @param $type
  98. * @return mixed
  99. */
  100. public function save($type)
  101. {
  102. $data = $this->request->postMore([
  103. ['accessKey', ''],
  104. ['secretKey', ''],
  105. ['appid', ''],
  106. ['name', ''],
  107. ['region', ''],
  108. ['acl', ''],
  109. ]);
  110. $type = (int)$type;
  111. if ($type === 4) {
  112. if (!$data['appid'] && !sys_config('tengxun_appid')) {
  113. return app('json')->fail(400224);
  114. }
  115. }
  116. if (!$data['accessKey']) {
  117. unset($data['accessKey'], $data['secretKey'], $data['appid']);
  118. }
  119. $this->services->saveStorage((int)$type, $data);
  120. return app('json')->success(100021);
  121. }
  122. /**
  123. * 修改状态
  124. * @param SystemConfigServices $services
  125. * @param $id
  126. * @return mixed
  127. */
  128. public function status(SystemConfigServices $services, $id)
  129. {
  130. if (!$id) {
  131. return app('json')->fail(100100);
  132. }
  133. $info = $this->services->get($id);
  134. $info->status = 1;
  135. if (!$info->domain) {
  136. return app('json')->fail(400225);
  137. }
  138. // $services->update('upload_type', ['value' => json_encode($info->type)], 'menu_name');
  139. \crmeb\services\CacheService::clear();
  140. //设置跨域规则
  141. try {
  142. $upload = UploadService::init($info->type);
  143. $res = $upload->setBucketCors($info->name, $info->region);
  144. if (false === $res) {
  145. return app('json')->fail($upload->getError());
  146. }
  147. } catch (\Throwable $e) {
  148. }
  149. //修改状态
  150. $this->services->transaction(function () use ($id, $info) {
  151. // $this->services->update(['status' => 1, 'is_delete' => 0], ['status' => 0]);
  152. $this->services->update(['type' => $info->type], ['status' => 0]);
  153. $info->save();
  154. });
  155. return app('json')->success(100001);
  156. }
  157. /**
  158. * @param $id
  159. * @return mixed
  160. * @throws \FormBuilder\Exception\FormBuilderException
  161. */
  162. public function getUpdateDomainForm($id)
  163. {
  164. return app('json')->success($this->services->getUpdateDomainForm((int)$id));
  165. }
  166. /**
  167. * @param $id
  168. * @return mixed
  169. * @throws \think\db\exception\DataNotFoundException
  170. * @throws \think\db\exception\DbException
  171. * @throws \think\db\exception\ModelNotFoundException
  172. */
  173. public function updateDomain($id)
  174. {
  175. $domain = $this->request->post('domain', '');
  176. $cdn = $this->request->post('cdn', '');
  177. $data = $this->request->postMore([
  178. ['pri', ''],
  179. ['ca', '']
  180. ]);
  181. if (!$domain) {
  182. return app('json')->fail(100100);
  183. }
  184. if (strstr($domain, 'https://') === false && strstr($domain, 'http://') === false) {
  185. return app('json')->fail(400226);
  186. }
  187. // if (strstr($domain, 'https://') !== false && !$data['pri']) {
  188. // return app('json')->fail('域名为HTTPS访问时,必须填写证书');
  189. // }
  190. $this->services->updateDomain($id, $domain, ['cdn' => $cdn]);
  191. return app('json')->success(100001);
  192. }
  193. /**
  194. * 删除
  195. * @param $id
  196. * @return mixed
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\DbException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. */
  201. public function delete($id)
  202. {
  203. if (!$id) {
  204. return app('json')->fail(100100);
  205. }
  206. if ($this->services->deleteStorage($id)) {
  207. return app('json')->success(100002);
  208. } else {
  209. return app('json')->fail(100008);
  210. }
  211. }
  212. /**
  213. * 切换存储类型
  214. * @param SystemConfigServices $services
  215. * @param $type
  216. * @return mixed
  217. */
  218. public function uploadType(SystemConfigServices $services, $type)
  219. {
  220. $status = $this->services->count(['type' => $type, 'status' => 1]);
  221. if (!$status && $type != 1) {
  222. return app('json')->success(400227);
  223. }
  224. $services->update('upload_type', ['value' => json_encode($type)], 'menu_name');
  225. \crmeb\services\CacheService::clear();
  226. if ($type != 1) {
  227. $msg = 400228;
  228. } else {
  229. $msg = 400229;
  230. }
  231. return app('json')->success($msg);
  232. }
  233. }