Plugs.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\api;
  15. use app\admin\service\AuthService;
  16. use think\admin\Controller;
  17. use think\admin\Storage;
  18. /**
  19. * 后台插件管理
  20. * Class Plugs
  21. * @package app\admin\controller\api
  22. */
  23. class Plugs extends Controller
  24. {
  25. /**
  26. * 系统图标选择器
  27. */
  28. public function icon()
  29. {
  30. $this->title = '图标选择器';
  31. $this->field = input('field', 'icon');
  32. $this->fetch();
  33. }
  34. /**
  35. * 获取文件上传参数
  36. * @throws \think\Exception
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function check()
  42. {
  43. $diff1 = explode(',', strtolower(input('exts', '')));
  44. $diff2 = explode(',', strtolower(sysconf('storage.allow_exts')));
  45. $exts = array_intersect($diff1, $diff2);
  46. $this->success('获取文件上传参数', [
  47. 'exts' => join('|', $exts),
  48. 'mine' => Storage::mime($exts),
  49. 'type' => $this->getUploadType(),
  50. 'data' => $this->getUploadData(),
  51. ]);
  52. }
  53. /**
  54. * 后台通用文件上传
  55. * @return \think\response\Json
  56. * @throws \think\Exception
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public function upload()
  62. {
  63. if (!AuthService::isLogin()) {
  64. $this->error('访问授权失败,请重新登录授权再试!');
  65. }
  66. if (!($file = $this->getUploadFile()) || empty($file)) {
  67. return json(['uploaded' => false, 'error' => ['message' => '文件上传异常,文件可能过大或未上传']]);
  68. }
  69. $this->extension = $file->getOriginalExtension();
  70. if (!in_array($this->extension, explode(',', sysconf('storage.allow_exts')))) {
  71. return json(['uploaded' => false, 'error' => ['message' => '文件上传类型受限,请在后台配置']]);
  72. }
  73. if (in_array($this->extension, ['php', 'sh'])) {
  74. return json(['uploaded' => false, 'error' => ['message' => '可执行文件禁止上传到本地服务器']]);
  75. }
  76. $this->safe = boolval(input('safe'));
  77. $this->uptype = $this->getUploadType();
  78. $name = Storage::name($file->getPathname(), $this->extension, '', 'md5_file');
  79. $info = Storage::instance($this->uptype)->set($name, file_get_contents($file->getRealPath()), $this->safe);
  80. if (is_array($info) && isset($info['url'])) {
  81. return json(['uploaded' => true, 'filename' => $name, 'url' => $this->safe ? $name : $info['url']]);
  82. } else {
  83. return json(['uploaded' => false, 'error' => ['message' => '文件处理失败,请稍候再试!']]);
  84. }
  85. }
  86. /**
  87. * 生成文件上传参数
  88. * @return array
  89. * @throws \think\Exception
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. private function getUploadData()
  95. {
  96. if ($this->getUploadType() === 'qiniu') {
  97. $file = Storage::instance('qiniu');
  98. return ['url' => $file->upload(), 'token' => $file->buildUploadToken(), 'uptype' => $this->getUploadType()];
  99. } else {
  100. return ['url' => '?s=admin/api.plugs/upload', 'token' => uniqid('local_upload_'), 'uptype' => $this->getUploadType()];
  101. }
  102. }
  103. /**
  104. * 获取文件上传方式
  105. * @return string
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. private function getUploadType()
  111. {
  112. $this->uptype = input('uptype');
  113. if (!in_array($this->uptype, ['local', 'oss', 'qiniu'])) {
  114. $this->uptype = sysconf('storage.type');
  115. }
  116. return $this->uptype;
  117. }
  118. /**
  119. * 获取本地文件对象
  120. * @return \think\file\UploadedFile
  121. */
  122. private function getUploadFile()
  123. {
  124. try {
  125. return $this->request->file('file');
  126. } catch (\Exception $e) {
  127. $this->error(lang($e->getMessage()));
  128. }
  129. }
  130. }