SystemStorage.php 7.4 KB

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