| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace addons\ueditor\controller;
- use app\common\exception\UploadException;
- use app\common\library\Upload as UploadLib;
- use think\addons\Controller;
- /**
- * ueditor接口控制器
- */
- class Api extends Controller
- {
- //编辑器初始配置
- private $editorConfig = [
- /* 上传图片配置项 */
- "imageActionName" => "uploadimage", /* 执行上传图片的action名称 */
- "imageFieldName" => "upfile", /* 提交的图片表单名称 */
- "imageMaxSize" => 1024 * 1024 * 10, /* 上传大小限制,单位B */
- "imageAllowFiles" => [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
- "imageCompressEnable" => true, /* 是否压缩图片,默认是true */
- "imageCompressBorder" => 1600, /* 图片压缩最长边限制 */
- "imageInsertAlign" => "none", /* 插入的图片浮动方式 */
- "imageUrlPrefix" => "", /* 图片访问路径前缀 */
- 'imagePathFormat' => '',
- // 涂鸦图片上传配置项
- "scrawlActionName" => "uploadscrawl", /* 执行上传涂鸦的action名称 */
- "scrawlFieldName" => "upfile", /* 提交的图片表单名称 */
- 'scrawlPathFormat' => '',
- "scrawlMaxSize" => 1024 * 1024 * 10, /* 上传大小限制,单位B */
- 'scrawlUrlPrefix' => '',
- 'scrawlInsertAlign' => 'none',
- /* 抓取远程图片配置 */
- 'catcherLocalDomain' => ['127.0.0.1', 'localhost', 'img.baidu.com'],
- "catcherActionName" => "catchimage", /* 执行抓取远程图片的action名称 */
- 'catcherFieldName' => 'source',
- 'catcherPathFormat' => '',
- 'catcherUrlPrefix' => '',
- 'catcherMaxSize' => 1024 * 1024 * 10, /* 上传大小限制,单位B */
- 'catcherAllowFiles' => ['.png', '.jpg', '.jpeg', '.gif', '.bmp'],
- /* 上传视频配置 */
- "videoActionName" => "uploadvideo", /* 执行上传视频的action名称 */
- "videoFieldName" => "upfile", /* 提交的视频表单名称 */
- 'videoPathFormat' => '',
- 'videoUrlPrefix' => '',
- 'videoMaxSize' => 1024 * 1024 * 100, /* 上传大小限制,单位B */
- 'videoAllowFiles' => [".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"],
- // 上传音频配置
- "audioActionName" => "audio",
- "audioFieldName" => "file",
- "audioUrlPrefix" => "",
- "audioMaxSize" => 1024 * 1024 * 100, /* 上传大小限制,单位B */
- "audioAllowFiles" => ['.mp3'],
- /* 上传文件配置 */
- "fileActionName" => "uploadfile", /* controller里,执行上传视频的action名称 */
- 'fileFieldName' => 'upfile',
- 'filePathFormat' => '',
- 'fileUrlPrefix' => '',
- 'fileMaxSize' => 1024 * 1024 * 100, /* 上传大小限制,单位B */
- 'fileAllowFiles' => [".flv", ".swf", ".zip", ".rar", ".xls", ".xlsx", ".wav", ".mp4", ".mp3", ".webm"],
- ];
- public function index()
- {
- $file = $this->request->file('upfile');
- $action = $this->request->get('action');
- try {
- switch ($action) {
- case 'config':
- return json($this->editorConfig);
- case 'uploadimage':
- return $this->uploadFile($file, 'images', 'ueditor');
- case 'uploadscrawl':
- return $this->uploadFile($file, 'images', 'ueditor_scrawl');
- case 'uploadvideo':
- return $this->uploadFile($file, 'videos', 'ueditor');
- case 'uploadfile':
- return $this->uploadFile($file, 'files', 'ueditor');
- default:
- return json(['state' => '请求地址出错']);
- }
- } catch (UploadException | Exception $e) {
- return json(['state' => $e->getMessage()]);
- }
- }
- /**
- * 统一文件上传处理
- */
- protected function uploadFile($file, $category, $subdir)
- {
- $upload = new UploadLib($file);
- $attachment = $upload->upload($category, $subdir);
- return json([
- "state" => "SUCCESS",
- 'url' => $attachment->path,
- 'fullurl' => cdnurl($attachment->path, true),
- 'title' => $attachment->name,
- ]);
- }
- }
|