Api.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace addons\ueditor\controller;
  3. use app\common\exception\UploadException;
  4. use app\common\library\Upload as UploadLib;
  5. use think\addons\Controller;
  6. /**
  7. * ueditor接口控制器
  8. */
  9. class Api extends Controller
  10. {
  11. //编辑器初始配置
  12. private $editorConfig = [
  13. /* 上传图片配置项 */
  14. "imageActionName" => "uploadimage", /* 执行上传图片的action名称 */
  15. "imageFieldName" => "upfile", /* 提交的图片表单名称 */
  16. "imageMaxSize" => 1024 * 1024 * 10, /* 上传大小限制,单位B */
  17. "imageAllowFiles" => [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
  18. "imageCompressEnable" => true, /* 是否压缩图片,默认是true */
  19. "imageCompressBorder" => 1600, /* 图片压缩最长边限制 */
  20. "imageInsertAlign" => "none", /* 插入的图片浮动方式 */
  21. "imageUrlPrefix" => "", /* 图片访问路径前缀 */
  22. 'imagePathFormat' => '',
  23. // 涂鸦图片上传配置项
  24. "scrawlActionName" => "uploadscrawl", /* 执行上传涂鸦的action名称 */
  25. "scrawlFieldName" => "upfile", /* 提交的图片表单名称 */
  26. 'scrawlPathFormat' => '',
  27. "scrawlMaxSize" => 1024 * 1024 * 10, /* 上传大小限制,单位B */
  28. 'scrawlUrlPrefix' => '',
  29. 'scrawlInsertAlign' => 'none',
  30. /* 抓取远程图片配置 */
  31. 'catcherLocalDomain' => ['127.0.0.1', 'localhost', 'img.baidu.com'],
  32. "catcherActionName" => "catchimage", /* 执行抓取远程图片的action名称 */
  33. 'catcherFieldName' => 'source',
  34. 'catcherPathFormat' => '',
  35. 'catcherUrlPrefix' => '',
  36. 'catcherMaxSize' => 1024 * 1024 * 10, /* 上传大小限制,单位B */
  37. 'catcherAllowFiles' => ['.png', '.jpg', '.jpeg', '.gif', '.bmp'],
  38. /* 上传视频配置 */
  39. "videoActionName" => "uploadvideo", /* 执行上传视频的action名称 */
  40. "videoFieldName" => "upfile", /* 提交的视频表单名称 */
  41. 'videoPathFormat' => '',
  42. 'videoUrlPrefix' => '',
  43. 'videoMaxSize' => 1024 * 1024 * 100, /* 上传大小限制,单位B */
  44. 'videoAllowFiles' => [".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"],
  45. // 上传音频配置
  46. "audioActionName" => "audio",
  47. "audioFieldName" => "file",
  48. "audioUrlPrefix" => "",
  49. "audioMaxSize" => 1024 * 1024 * 100, /* 上传大小限制,单位B */
  50. "audioAllowFiles" => ['.mp3'],
  51. /* 上传文件配置 */
  52. "fileActionName" => "uploadfile", /* controller里,执行上传视频的action名称 */
  53. 'fileFieldName' => 'upfile',
  54. 'filePathFormat' => '',
  55. 'fileUrlPrefix' => '',
  56. 'fileMaxSize' => 1024 * 1024 * 100, /* 上传大小限制,单位B */
  57. 'fileAllowFiles' => [".flv", ".swf", ".zip", ".rar", ".xls", ".xlsx", ".wav", ".mp4", ".mp3", ".webm"],
  58. ];
  59. public function index()
  60. {
  61. $file = $this->request->file('upfile');
  62. $action = $this->request->get('action');
  63. try {
  64. switch ($action) {
  65. case 'config':
  66. return json($this->editorConfig);
  67. case 'uploadimage':
  68. return $this->uploadFile($file, 'images', 'ueditor');
  69. case 'uploadscrawl':
  70. return $this->uploadFile($file, 'images', 'ueditor_scrawl');
  71. case 'uploadvideo':
  72. return $this->uploadFile($file, 'videos', 'ueditor');
  73. case 'uploadfile':
  74. return $this->uploadFile($file, 'files', 'ueditor');
  75. default:
  76. return json(['state' => '请求地址出错']);
  77. }
  78. } catch (UploadException | Exception $e) {
  79. return json(['state' => $e->getMessage()]);
  80. }
  81. }
  82. /**
  83. * 统一文件上传处理
  84. */
  85. protected function uploadFile($file, $category, $subdir)
  86. {
  87. $upload = new UploadLib($file);
  88. $attachment = $upload->upload($category, $subdir);
  89. return json([
  90. "state" => "SUCCESS",
  91. 'url' => $attachment->path,
  92. 'fullurl' => cdnurl($attachment->path, true),
  93. 'title' => $attachment->name,
  94. ]);
  95. }
  96. }