SystemBucket.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\setting;
  12. use service\JsonService as Json;
  13. use think\Request;
  14. use think\Url;
  15. use app\admin\controller\AuthController;
  16. use service\AlipayDisposeService;
  17. use app\admin\model\system\SystemBucket as SystemBucketModel;
  18. use service\SystemConfigService;
  19. /**
  20. * Class SystemBucket
  21. * @package app\admin\controller\setting
  22. */
  23. class SystemBucket extends AuthController
  24. {
  25. protected static $endpoint = [
  26. '华东1(杭州)' => 'oss-cn-hangzhou.aliyuncs.com',
  27. '华东2(上海)' => 'oss-cn-shanghai.aliyuncs.com',
  28. '华北1(青岛)' => 'oss-cn-qingdao.aliyuncs.com',
  29. '华北2(北京)' => 'oss-cn-beijing.aliyuncs.com',
  30. '华北3(张家口)' => 'oss-cn-zhangjiakou.aliyuncs.com',
  31. '华北5(呼和浩特)' => 'oss-cn-huhehaote.aliyuncs.com',
  32. '华北6(乌兰察布)' => 'oss-cn-wulanchabu.aliyuncs.com',
  33. '华南1(深圳)' => 'oss-cn-shenzhen.aliyuncs.com',
  34. '华南2(河源)' => 'oss-cn-heyuan.aliyuncs.com',
  35. '华南3(广州)' => 'oss-cn-guangzhou.aliyuncs.com',
  36. '西南1(成都)' => 'oss-cn-chengdu.aliyuncs.com',
  37. '中国(香港)' => 'oss-cn-hongkong.aliyuncs.com',
  38. '新加坡' => 'oss-ap-southeast-1.aliyuncs.com',
  39. '澳大利亚(悉尼)' => 'oss-ap-southeast-2.aliyuncs.com',
  40. '马来西亚(吉隆坡)' => 'oss-ap-southeast-3.aliyuncs.com',
  41. '印度尼西亚(雅加达)' => 'oss-ap-southeast-5.aliyuncs.com',
  42. '日本(东京)' => 'oss-ap-northeast-1.aliyuncs.com',
  43. '印度(孟买)' => 'oss-ap-south-1.aliyuncs.com',
  44. '德国(法兰克福)' => 'oss-eu-central-1.aliyuncs.com',
  45. '英国(伦敦)' => 'oss-eu-west-1.aliyuncs.com',
  46. '美国(硅谷)' => 'oss-us-west-1.aliyuncs.com',
  47. '美国(佛吉尼亚)' => 'oss-us-east-1.aliyuncs.com',
  48. '阿联酋(迪拜)' => 'oss-me-east-1.aliyuncs.com',
  49. ];
  50. /**
  51. * 对象存储OSS配置页面
  52. * @return \think\Response
  53. */
  54. public function index()
  55. {
  56. $endpoint = self::$endpoint;
  57. $this->assign(['endpoint' => $endpoint]);
  58. return $this->fetch();
  59. }
  60. /**
  61. * Bucket列表
  62. */
  63. public function bucket_list()
  64. {
  65. $where = parent::getMore([
  66. ['title', ''],
  67. ['endpoint', ''],
  68. ['types', 1],
  69. ], $this->request);
  70. return Json::successlayui(SystemBucketModel::bucKetList($where));
  71. }
  72. /**
  73. * 拉取储存空间
  74. */
  75. public function pullBucket()
  76. {
  77. $where = parent::getMore([
  78. ['endpoint', ''],
  79. ], $this->request);
  80. $list = AlipayDisposeService::ossBucketList($where['endpoint']);
  81. if (count($list) > 0) {
  82. SystemBucketModel::addListBucket($list);
  83. }
  84. return Json::successful('ok');
  85. }
  86. /**
  87. * 添加存储空间
  88. */
  89. public function create()
  90. {
  91. $endpoint = self::$endpoint;
  92. $this->assign(['endpoint' => json_encode($endpoint)]);
  93. return $this->fetch();
  94. }
  95. /**
  96. * 添加存储空间
  97. */
  98. public function save()
  99. {
  100. $data = parent::postMore([
  101. ['bucket_name', ''],
  102. ['endpoint', ''],
  103. ['type', 1],
  104. ['jurisdiction', 1],
  105. ]);
  106. if (!$data['bucket_name']) return Json::fail('请输入存储空间名称');
  107. if (AlipayDisposeService::doesBucketExist($data['endpoint'], $data['bucket_name'])) {
  108. return Json::fail('存储空间已存在');
  109. }
  110. SystemBucketModel::beginTrans();
  111. $res = AlipayDisposeService::ossDispose($data['endpoint'], $data['bucket_name'], $data['jurisdiction'], $data['type']);
  112. if ($res) {
  113. $res1 = AlipayDisposeService::putBucketCors($data['endpoint'], $data['bucket_name']);
  114. if ($res1 && SystemBucketModel::addBucket($data)) {
  115. SystemBucketModel::commitTrans();
  116. return Json::successful('添加存储空间成功!');
  117. } else {
  118. SystemBucketModel::rollbackTrans();
  119. return Json::fail('添加到数据库失败或设置跨域规则失败!');
  120. }
  121. } else {
  122. SystemBucketModel::rollbackTrans();
  123. return Json::fail('添加存储空间失败!');
  124. }
  125. }
  126. /**
  127. * 删除存储空间
  128. */
  129. public function delete($id = 0)
  130. {
  131. if (!$id) return Json::fail('参数错误');
  132. $bucket = SystemBucketModel::where('id', $id)->where('is_del', 0)->find();
  133. if (!$bucket) return Json::fail('删除存储空间不存在');
  134. $res = AlipayDisposeService::deleteBucket($bucket['endpoint'], $bucket['bucket_name']);
  135. if ($res) {
  136. SystemBucketModel::where('id', $id)->update(['is_del' => 1]);
  137. return Json::successful('删除存储空间成功');
  138. } else {
  139. return Json::fail('删除存储空间失败');
  140. }
  141. }
  142. /**使用储存空间
  143. * @param int $id
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\ModelNotFoundException
  146. * @throws \think\exception\DbException
  147. */
  148. public function userUse($id = 0)
  149. {
  150. if (!$id) return Json::fail('参数错误');
  151. $bucket = SystemBucketModel::where('id', $id)->where('is_del', 0)->find();
  152. if (!$bucket) return Json::fail('存储空间不存在');
  153. $res = SystemConfigService::setOneValue('uploadUrl', $bucket['domain_name']);
  154. $res1 = SystemConfigService::setOneValue('OssBucket', $bucket['bucket_name']);
  155. $res2 = SystemConfigService::setOneValue('end_point', $bucket['endpoint']);
  156. if ($res && $res1 && $res2) {
  157. SystemBucketModel::where('is_use', 1)->where('is_del', 0)->update(['is_use' => 0]);
  158. SystemBucketModel::where('id', $id)->where('is_del', 0)->update(['is_use' => 1]);
  159. return Json::successful('设置存储空间成功');
  160. } else {
  161. return Json::fail('设置存储空间失败');
  162. }
  163. }
  164. }