控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Ajax.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | ajax
  13. // +----------------------------------------------------------------------
  14. namespace app\member\controller;
  15. use app\common\exception\UploadException;
  16. use app\common\library\Upload as UploadLib;
  17. class Ajax extends MemberBase
  18. {
  19. protected $noNeedLogin = ['upload'];
  20. protected $noNeedRight = ['*'];
  21. //编辑器初始配置
  22. private $editorConfig = [
  23. /* 上传图片配置项 */
  24. "imageActionName" => "uploadimage", /* 执行上传图片的action名称 */
  25. "imageFieldName" => "upfile", /* 提交的图片表单名称 */
  26. "imageMaxSize" => 2048000, /* 上传大小限制,单位B */
  27. "imageAllowFiles" => [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
  28. "imageCompressEnable" => true, /* 是否压缩图片,默认是true */
  29. "imageCompressBorder" => 1600, /* 图片压缩最长边限制 */
  30. "imageInsertAlign" => "none", /* 插入的图片浮动方式 */
  31. "imageUrlPrefix" => "", /* 图片访问路径前缀 */
  32. 'imagePathFormat' => '',
  33. /* 涂鸦图片上传配置项 */
  34. "scrawlActionName" => "uploadscrawl", /* 执行上传涂鸦的action名称 */
  35. "scrawlFieldName" => "upfile", /* 提交的图片表单名称 */
  36. 'scrawlPathFormat' => '',
  37. "scrawlMaxSize" => 2048000, /* 上传大小限制,单位B */
  38. 'scrawlUrlPrefix' => '',
  39. 'scrawlInsertAlign' => 'none',
  40. /* 截图工具上传 */
  41. "snapscreenActionName" => "uploadimage", /* 执行上传截图的action名称 */
  42. 'snapscreenPathFormat' => '',
  43. 'snapscreenUrlPrefix' => '',
  44. 'snapscreenInsertAlign' => 'none',
  45. /* 抓取远程图片配置 */
  46. 'catcherLocalDomain' => ['127.0.0.1', 'localhost', 'img.baidu.com'],
  47. "catcherActionName" => "catchimage", /* 执行抓取远程图片的action名称 */
  48. 'catcherFieldName' => 'source',
  49. 'catcherPathFormat' => '',
  50. 'catcherUrlPrefix' => '',
  51. 'catcherMaxSize' => 0,
  52. 'catcherAllowFiles' => ['.png', '.jpg', '.jpeg', '.gif', '.bmp'],
  53. /* 上传视频配置 */
  54. "videoActionName" => "uploadvideo", /* 执行上传视频的action名称 */
  55. "videoFieldName" => "upfile", /* 提交的视频表单名称 */
  56. 'videoPathFormat' => '',
  57. 'videoUrlPrefix' => '',
  58. 'videoMaxSize' => 0,
  59. 'videoAllowFiles' => [".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"],
  60. /* 上传文件配置 */
  61. "fileActionName" => "uploadfile", /* controller里,执行上传视频的action名称 */
  62. 'fileFieldName' => 'upfile',
  63. 'filePathFormat' => '',
  64. 'fileUrlPrefix' => '',
  65. 'fileMaxSize' => 0,
  66. 'fileAllowFiles' => [".flv", ".swf"],
  67. /* 列出指定目录下的图片 */
  68. "imageManagerActionName" => "listimage", /* 执行图片管理的action名称 */
  69. 'imageManagerListPath' => '',
  70. 'imageManagerListSize' => 20,
  71. 'imageManagerUrlPrefix' => '',
  72. 'imageManagerInsertAlign' => 'none',
  73. 'imageManagerAllowFiles' => ['.png', '.jpg', '.jpeg', '.gif', '.bmp'],
  74. /* 列出指定目录下的文件 */
  75. "fileManagerActionName" => "listfile", /* 执行文件管理的action名称 */
  76. 'fileManagerListPath' => '',
  77. 'fileManagerUrlPrefix' => '',
  78. 'fileManagerListSize' => '',
  79. 'fileManagerAllowFiles' => [".flv", ".swf"],
  80. ];
  81. protected function initialize()
  82. {
  83. parent::initialize();
  84. //图片上传大小和类型
  85. $this->editorConfig['imageMaxSize'] = $this->editorConfig['catcherMaxSize'] = 0 == config('site.upload_image_size') ? 1024 * 1024 * 1024 : config('site.upload_image_size') * 1024;
  86. if (!empty(config('site.upload_image_ext'))) {
  87. $imageallowext = parse_attr(config('site.upload_image_ext'));
  88. foreach ($imageallowext as $k => $rs) {
  89. $imageallowext[$k] = ".{$rs}";
  90. }
  91. $this->editorConfig['imageAllowFiles'] = $imageallowext;
  92. }
  93. //附件上传大小和类型
  94. $this->editorConfig['fileMaxSize'] = $this->editorConfig['videoMaxSize'] = 0 == config('site.upload_file_size') ? 1024 * 1024 * 1024 : config('site.upload_file_size') * 1024;
  95. if (!empty(config('site.upload_file_ext'))) {
  96. $fileallowext = parse_attr(config('site.upload_file_ext'));
  97. foreach ($fileallowext as $k => $rs) {
  98. $fileallowext[$k] = ".{$rs}";
  99. }
  100. $this->editorConfig['fileAllowFiles'] = $fileallowext;
  101. }
  102. }
  103. public function upload($dir = '', $from = '')
  104. {
  105. if ($dir == '') {
  106. return json([
  107. 'code' => 0,
  108. 'msg' => '没有指定上传目录',
  109. 'state' => '没有指定上传目录', //兼容百度
  110. 'message' => '没有指定上传目录', //兼容editormd
  111. ]);
  112. }
  113. $chunkid = $this->request->post("chunkid");
  114. if ($chunkid) {
  115. if (!config('chunking')) {
  116. return json([
  117. 'code' => 0,
  118. 'msg' => '未开启分片上传功能',
  119. ]);
  120. }
  121. //分片
  122. $action = $this->request->post("action");
  123. $chunkindex = $this->request->post("chunkindex/d", 0);
  124. $chunkcount = $this->request->post("chunkcount/d", 1);
  125. $filename = $this->request->post("filename");
  126. $method = $this->request->method(true);
  127. if ($action == 'merge') {
  128. $attachment = null;
  129. //合并分片文件
  130. try {
  131. $upload = new UploadLib();
  132. $attachment = $upload->merge($chunkid, $chunkcount, $filename, $dir, $from);
  133. } catch (UploadException $e) {
  134. return json([
  135. 'code' => 0,
  136. 'msg' => $e->getMessage(),
  137. 'state' => $e->getMessage(), //兼容百度
  138. 'message' => $e->getMessage(), //兼容editormd
  139. ]);
  140. }
  141. return $attachment;
  142. } elseif ($method == 'clean') {
  143. //删除冗余的分片文件
  144. try {
  145. $upload = new UploadLib();
  146. $upload->clean($chunkid);
  147. } catch (UploadException $e) {
  148. return json([
  149. 'code' => 0,
  150. 'msg' => $e->getMessage(),
  151. 'state' => $e->getMessage(), //兼容百度
  152. 'message' => $e->getMessage(), //兼容editormd
  153. ]);
  154. }
  155. return json(['code' => 1]);
  156. } else {
  157. //上传分片文件
  158. $file = $this->request->file('file');
  159. try {
  160. $upload = new UploadLib($file);
  161. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  162. } catch (UploadException $e) {
  163. return json([
  164. 'code' => 0,
  165. 'msg' => $e->getMessage(),
  166. 'state' => $e->getMessage(), //兼容百度
  167. 'message' => $e->getMessage(), //兼容editormd
  168. ]);
  169. }
  170. return json(['code' => 1]);
  171. }
  172. }
  173. // 获取附件数据
  174. switch ($from) {
  175. case 'editormd':
  176. $file_input_name = 'editormd-image-file';
  177. break;
  178. case 'ueditor':
  179. $file_input_name = 'upfile';
  180. break;
  181. default:
  182. $file_input_name = 'file';
  183. }
  184. $attachment = null;
  185. try {
  186. //默认普通上传文件
  187. $file = $this->request->file($file_input_name);
  188. if ($from == 'ueditor') {
  189. return $this->ueditor($file);
  190. }
  191. $upload = new UploadLib($file);
  192. $attachment = $upload->upload($dir);
  193. } catch (UploadException | Exception $e) {
  194. return json([
  195. 'code' => 0,
  196. 'msg' => $e->getMessage(),
  197. 'state' => $e->getMessage(), //兼容百度
  198. 'message' => $e->getMessage(), //兼容editormd
  199. ]);
  200. }
  201. return $attachment;
  202. }
  203. private function ueditor($file)
  204. {
  205. $action = $this->request->get('action');
  206. switch ($action) {
  207. /* 获取配置信息 */
  208. case 'config':
  209. $result = $this->editorConfig;
  210. break;
  211. /* 上传图片 */
  212. case 'uploadimage':
  213. $upload = new UploadLib($file);
  214. return $upload->upload('images', 'ueditor');
  215. break;
  216. /* 上传涂鸦 */
  217. case 'uploadscrawl':
  218. $upload = new UploadLib($file);
  219. return $upload->upload('images', 'ueditor_scrawl');
  220. break;
  221. /* 上传视频 */
  222. case 'uploadvideo':
  223. $upload = new UploadLib($file);
  224. return $upload->upload('videos', 'ueditor');
  225. break;
  226. /* 上传附件 */
  227. case 'uploadfile':
  228. $upload = new UploadLib($file);
  229. return $upload->upload('files', 'ueditor');
  230. break;
  231. /* 列出图片
  232. /*case 'listimage':
  233. return $this->showFileList('listimage');
  234. break;
  235. 列出附件
  236. case 'listfile':
  237. return $this->showFileList('listfile');
  238. break;*/
  239. default:
  240. $result = [
  241. 'state' => '请求地址出错',
  242. ];
  243. break;
  244. }
  245. /* 输出结果 */
  246. if (isset($_GET["callback"])) {
  247. if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
  248. return htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
  249. } else {
  250. return json(['state' => 'callback参数不合法']);
  251. }
  252. } else {
  253. return json($result);
  254. }
  255. }
  256. }