SystemBucket.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\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. $where = parent::getMore([
  58. ['endpoint', ''],
  59. ['types', 1],
  60. ], $this->request);
  61. $this->assign(['list'=>SystemBucketModel::bucKetList($where),'endpoint'=>$endpoint,'where'=>$where]);
  62. return $this->fetch();
  63. }
  64. /**
  65. * 拉取储存空间
  66. */
  67. public function pullBucket()
  68. {
  69. $endpoint=self::$endpoint;
  70. foreach ($endpoint as $key=>$value){
  71. $list=AlipayDisposeService::ossBucketList($value);
  72. if(count($list)>0){
  73. SystemBucketModel::addListBucket($list);
  74. }
  75. }
  76. return Json::successful('ok');
  77. }
  78. /**
  79. * 添加存储空间
  80. */
  81. public function create()
  82. {
  83. $endpoint=self::$endpoint;
  84. $this->assign(['endpoint'=>json_encode($endpoint)]);
  85. return $this->fetch();
  86. }
  87. /**
  88. * 添加存储空间
  89. */
  90. public function save()
  91. {
  92. $data = parent::postMore([
  93. ['bucket_name', ''],
  94. ['endpoint', ''],
  95. ['type', 1],
  96. ['jurisdiction', 1],
  97. ]);
  98. if (!$data['bucket_name']) return Json::fail('请输入存储空间名称');
  99. if(AlipayDisposeService::doesBucketExist($data['endpoint'],$data['bucket_name'])){
  100. return Json::fail('存储空间已存在');
  101. }
  102. $res=AlipayDisposeService::ossDispose($data['endpoint'],$data['bucket_name'],$data['jurisdiction'],$data['type']);
  103. if($res){
  104. $res1=AlipayDisposeService::putBucketCors($data['endpoint'],$data['bucket_name']);
  105. if($res1 && SystemBucketModel::addBucket($data)){
  106. return Json::successful('添加存储空间成功!');
  107. }else{
  108. return Json::fail('添加到数据库失败或设置跨域规则失败!');
  109. }
  110. }else{
  111. return Json::fail('添加存储空间失败!');
  112. }
  113. }
  114. /**
  115. * 删除存储空间
  116. */
  117. public function delete($id=0)
  118. {
  119. if (!$id) return Json::fail('参数错误');
  120. $bucket=SystemBucketModel::where('id',$id)->where('is_del',0)->find();
  121. if(!$bucket) return Json::fail('删除存储空间不存在');
  122. $res=AlipayDisposeService::deleteBucket($bucket['endpoint'],$bucket['bucket_name']);
  123. if($res){
  124. SystemBucketModel::where('id',$id)->update(['is_del'=>1]);
  125. return Json::successful('删除存储空间成功');
  126. }else{
  127. return Json::fail('删除存储空间失败');
  128. }
  129. }
  130. /**使用储存空间
  131. * @param int $id
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. * @throws \think\exception\DbException
  135. */
  136. public function userUse($id=0)
  137. {
  138. if (!$id) return Json::fail('参数错误');
  139. $bucket=SystemBucketModel::where('id',$id)->where('is_del',0)->find();
  140. if(!$bucket) return Json::fail('存储空间不存在');
  141. $res=SystemConfigService::setOneValue('uploadUrl',$bucket['domain_name']);
  142. $res1=SystemConfigService::setOneValue('OssBucket',$bucket['bucket_name']);
  143. $res2=SystemConfigService::setOneValue('end_point',$bucket['endpoint']);
  144. if($res && $res1 && $res2){
  145. SystemBucketModel::where('is_use',1)->where('is_del',0)->update(['is_use'=>0]);
  146. SystemBucketModel::where('id',$id)->where('is_del',0)->update(['is_use'=>1]);
  147. return Json::successful('设置存储空间成功');
  148. }else{
  149. return Json::fail('设置存储空间失败');
  150. }
  151. }
  152. public function crossDomainRules()
  153. {
  154. $res=AlipayDisposeService::addLiveDomain('oss-cn-shanghai.aliyuncs.com','123456789ldw');
  155. }
  156. }