Config.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use library\Controller;
  16. use think\Db;
  17. use think\exception\HttpResponseException;
  18. use think\facade\Request;
  19. /**
  20. * 系统参数配置
  21. * Class Config
  22. * @package app\admin\controller
  23. */
  24. class Config extends Controller
  25. {
  26. /**
  27. * 默认数据模型
  28. * @var string
  29. */
  30. protected $table = 'Config';
  31. /**
  32. * 阿里云OSS上传点
  33. * @var array
  34. */
  35. protected $ossPoints = [
  36. 'oss-cn-hangzhou.aliyuncs.com' => '华东 1 杭州',
  37. 'oss-cn-shanghai.aliyuncs.com' => '华东 2 上海',
  38. 'oss-cn-qingdao.aliyuncs.com' => '华北 1 青岛',
  39. 'oss-cn-beijing.aliyuncs.com' => '华北 2 北京',
  40. 'oss-cn-zhangjiakou.aliyuncs.com' => '华北 3 张家口',
  41. 'oss-cn-huhehaote.aliyuncs.com' => '华北 5 呼和浩特',
  42. 'oss-cn-shenzhen.aliyuncs.com' => '华南 1 深圳',
  43. 'oss-cn-hongkong.aliyuncs.com' => '香港 1',
  44. 'oss-us-west-1.aliyuncs.com' => '美国西部 1 硅谷',
  45. 'oss-us-east-1.aliyuncs.com' => '美国东部 1 弗吉尼亚',
  46. 'oss-ap-southeast-1.aliyuncs.com' => '亚太东南 1 新加坡',
  47. 'oss-ap-southeast-2.aliyuncs.com' => '亚太东南 2 悉尼',
  48. 'oss-ap-southeast-3.aliyuncs.com' => '亚太东南 3 吉隆坡',
  49. 'oss-ap-southeast-5.aliyuncs.com' => '亚太东南 5 雅加达',
  50. 'oss-ap-northeast-1.aliyuncs.com' => '亚太东北 1 日本',
  51. 'oss-ap-south-1.aliyuncs.com' => '亚太南部 1 孟买',
  52. 'oss-eu-central-1.aliyuncs.com' => '欧洲中部 1 法兰克福',
  53. 'oss-eu-west-1.aliyuncs.com' => '英国 1 伦敦',
  54. 'oss-me-east-1.aliyuncs.com' => '中东东部 1 迪拜',
  55. ];
  56. /**
  57. * 系统参数配置
  58. * @auth true
  59. * @menu true
  60. */
  61. public function info()
  62. {
  63. $this->title = '系统参数配置';
  64. $this->fetch();
  65. }
  66. /**
  67. * 修改系统能数配置
  68. * @auth true
  69. * @throws \think\Exception
  70. * @throws \think\exception\PDOException
  71. */
  72. public function config()
  73. {
  74. $this->applyCsrfToken();
  75. if (Request::isGet()) {
  76. $this->fetch('system-config');
  77. }
  78. $uptype = Db::name('config')->where('name','storage_type')->value('value');
  79. $post = Request::post();
  80. if ($uptype!='local'){
  81. $post['bot_img'] = 'http://'.$_SERVER['SERVER_NAME'].self::crabImage($post['bot_img']);
  82. }
  83. foreach ($post as $key => $value) {
  84. sysconf($key, $value);
  85. }
  86. $this->success('系统参数配置成功!');
  87. }
  88. /**
  89. * 文件存储引擎
  90. * @auth true
  91. * @throws \think\Exception
  92. * @throws \think\exception\PDOException
  93. */
  94. public function file()
  95. {
  96. $this->applyCsrfToken();
  97. if (Request::isGet()) {
  98. $this->type = input('type', 'local');
  99. $this->fetch("storage-{$this->type}");
  100. }
  101. $post = Request::post();
  102. if (isset($post['storage_type']) && isset($post['storage_local_exts'])) {
  103. $exts = array_unique(explode(',', strtolower($post['storage_local_exts'])));
  104. sort($exts);
  105. if (in_array('php', $exts)) $this->error('禁止上传可执行文件到本地服务器!');
  106. $post['storage_local_exts'] = join(',', $exts);
  107. }
  108. foreach ($post as $key => $value) sysconf($key, $value);
  109. if (isset($post['storage_type']) && $post['storage_type'] === 'oss') {
  110. try {
  111. $local = sysconf('storage_oss_domain');
  112. $bucket = $this->request->post('storage_oss_bucket');
  113. $domain = \library\File::instance('oss')->setBucket($bucket);
  114. if (empty($local) || stripos($local, '.aliyuncs.com') !== false) {
  115. sysconf('storage_oss_domain', $domain);
  116. }
  117. $this->success('阿里云OSS存储配置成功!');
  118. } catch (HttpResponseException $exception) {
  119. throw $exception;
  120. } catch (\Exception $e) {
  121. $this->error("阿里云OSS存储配置失效,{$e->getMessage()}");
  122. }
  123. } else {
  124. $this->success('文件存储配置成功!');
  125. }
  126. }
  127. /**
  128. * PHP将网页上的图片攫取到本地存储
  129. * @param $imgUrl 图片url地址
  130. * @param string $saveDir 本地存储路径 默认存储在当前路径
  131. * @param null $fileName 图片存储到本地的文件名
  132. * @return mix
  133. */
  134. public static function crabImage($imgUrl, $saveDir='./upload/', $fileName=null){
  135. if(empty($imgUrl)){
  136. return false;
  137. }
  138. //获取图片信息大小
  139. $imgSize = getImageSize($imgUrl);
  140. if(!in_array($imgSize['mime'],array('image/jpg', 'image/gif', 'image/png', 'image/jpeg'),true)){
  141. return false;
  142. }
  143. //获取后缀名
  144. $_mime = explode('/', $imgSize['mime']);
  145. $_ext = '.'.end($_mime);
  146. if(empty($fileName)){ //生成唯一的文件名
  147. $fileName = uniqid(time(),true).$_ext;
  148. }
  149. //开始攫取
  150. ob_start();
  151. readfile($imgUrl);
  152. $imgInfo = ob_get_contents();
  153. ob_end_clean();
  154. if(!file_exists($saveDir)){
  155. mkdir($saveDir,0777,true);
  156. }
  157. $fp = fopen($saveDir.$fileName, 'a');
  158. $imgLen = strlen($imgInfo); //计算图片源码大小
  159. $_inx = 2048; //每次写入2k
  160. $_time = ceil($imgLen/$_inx);
  161. for($i=0; $i<$_time; $i++){
  162. fwrite($fp,substr($imgInfo, $i*$_inx, $_inx));
  163. }
  164. fclose($fp);
  165. return substr($saveDir.$fileName, 1);
  166. //return array('file_name'=>$fileName,'save_path'=>$saveDir.$fileName);
  167. }
  168. }