Common.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace plugin\ueditor\controller;
  3. use app\service\ConfServiceFacade;
  4. use laytp\library\UploadDomain;
  5. use plugin\ali_oss\service\Oss;
  6. use plugin\qiniu_kodo\service\Kodo;
  7. use laytp\controller\Backend;
  8. use think\facade\Config;
  9. use think\facade\Env;
  10. use think\facade\Filesystem;
  11. class Common extends Backend
  12. {
  13. protected $noNeedLogin = ['*'];
  14. /**
  15. * ueditor上传接口
  16. */
  17. public function upload()
  18. {
  19. $config = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(root_path() . "/public/static/plugin/ueditor/php/config.json")), true);
  20. $laytpUploadConfig = ConfServiceFacade::groupGet('system.upload');
  21. $arrMimeType = explode(',', $laytpUploadConfig['mime']);
  22. foreach ($arrMimeType as $k => $v) {
  23. $arrMimeType[$k] = '.' . $v;
  24. }
  25. $config['imageAllowFiles'] = $arrMimeType;
  26. $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
  27. preg_match('/(\d+)(\w+)/', $laytpUploadConfig['size'], $matches);
  28. $type = strtolower($matches[2]);
  29. $size = (int)$laytpUploadConfig['size'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
  30. $config['imageMaxSize'] = $size;
  31. $config['scrawlMaxSize'] = $size;
  32. $config['catcherMaxSize'] = $size;
  33. $config['videoMaxSize'] = $size;
  34. $config['fileMaxSize'] = $size;
  35. $config['maxSize'] = $size;
  36. $config['allowFiles'] = $arrMimeType;
  37. $action = $this->request->param('action');
  38. switch ($action) {
  39. case 'config':
  40. return json($config);
  41. break;
  42. /* 上传图片 */
  43. case 'uploadimage':
  44. break;
  45. /* 上传涂鸦 */
  46. case 'uploadscrawl':
  47. $config["oriName"] = "scrawl.png";
  48. /* 上传视频 */
  49. case 'uploadvideo':
  50. $config["oriName"] = "scrawl.png";
  51. /* 上传文件 */
  52. case 'uploadfile':
  53. break;
  54. }
  55. try {
  56. $defaultType = ConfServiceFacade::get('system.upload.defaultType', 'local');
  57. $uploadType = $this->request->param('upload_type', 'local');
  58. if ($uploadType == 'default') $uploadType = $defaultType;
  59. if (!in_array($uploadType, ['local', 'ali-oss', 'qiniu-kodo'])) {
  60. return $this->error($uploadType . '上传方式未定义');
  61. }
  62. $file = $this->request->file('laytpUEditorUploadFile'); // 获取上传的文件
  63. if (!$file) {
  64. return $this->error('上传失败,请选择需要上传的文件');
  65. }
  66. $fileExt = strtolower($file->getOriginalExtension());
  67. $uploadDomain = new UploadDomain();
  68. if (!$uploadDomain->check($file->getOriginalName(), $file->getSize(), $fileExt, $file->getMime())) {
  69. return $this->error($uploadDomain->getError());
  70. }
  71. $saveName = date("Ymd") . "/" . md5(uniqid(mt_rand())) . ".{$fileExt}";
  72. /**
  73. * 不能以斜杆开头
  74. * - 因为OSS存储时,不允许以/开头
  75. */
  76. $uploadDir = $this->request->param('dir');
  77. $object = $uploadDir ? $uploadDir . '/' . $saveName : $saveName;//设置了上传目录的上传文件名
  78. $inputValue = "";
  79. //上传至七牛云
  80. if ($uploadType == 'qiniu-kodo') {
  81. if(ConfServiceFacade::get('plugin.qiniu_kodo.switch') != 1){
  82. return $this->error('未开启七牛云KODO存储,请到七牛云KODO配置中开启');
  83. }
  84. $kodoConf = [
  85. 'accessKey' => ConfServiceFacade::get('plugin.qiniu_kodo.accessKey'),
  86. 'secretKey' => ConfServiceFacade::get('plugin.qiniu_kodo.secretKey'),
  87. 'bucket' => ConfServiceFacade::get('plugin.qiniu_kodo.bucket'),
  88. 'domain' => ConfServiceFacade::get('plugin.qiniu_kodo.domain'),
  89. ];
  90. $kodo = Kodo::instance();
  91. $kodoRes = $kodo->upload($file->getPathname(), $object, $kodoConf);
  92. if ($kodoRes) {
  93. $inputValue = $kodoRes;
  94. } else {
  95. return $this->error($kodo->getError());
  96. }
  97. }
  98. //上传至阿里云
  99. if ($uploadType == 'ali-oss') {
  100. if(ConfServiceFacade::get('plugin.ali_oss.switch') != 1){
  101. return $this->error('未开启阿里云OSS存储,请到阿里云OSS配置中开启');
  102. }
  103. $ossConf = [
  104. 'accessKeyID' => ConfServiceFacade::get('plugin.ali_oss.accessKeyID'),
  105. 'accessKeySecret' => ConfServiceFacade::get('plugin.ali_oss.accessKeySecret'),
  106. 'bucket' => ConfServiceFacade::get('plugin.ali_oss.bucket'),
  107. 'endpoint' => ConfServiceFacade::get('plugin.ali_oss.endpoint'),
  108. 'domain' => ConfServiceFacade::get('plugin.ali_oss.domain'),
  109. ];
  110. $oss = Oss::instance();
  111. $ossUploadRes = $oss->upload($file->getPathname(), $object, $ossConf);
  112. if ($ossUploadRes) {
  113. $inputValue = $ossUploadRes;
  114. } else {
  115. return $this->error($oss->getError());
  116. }
  117. }
  118. //本地上传
  119. if ($uploadType == 'local') {
  120. $uploadDir = ltrim('/', $uploadDir);
  121. $saveName = Filesystem::putFileAs('/' . $uploadDir, $file, '/' . $object);
  122. $staticDomain = Env::get('domain.static');
  123. if ($staticDomain) {
  124. $inputValue = $staticDomain . '/storage/' . $saveName;
  125. } else {
  126. $inputValue = request()->domain() .STATIC_PATH . '/storage/' . $saveName;
  127. }
  128. }
  129. return json(['url' => $inputValue, 'state' => 'SUCCESS']);
  130. } catch (\Exception $e) {
  131. return $this->exceptionError($e);
  132. }
  133. }
  134. }