控制台应用,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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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\admin\controller;
  15. use app\admin\model\Adminlog;
  16. use app\common\controller\Adminbase;
  17. use app\common\exception\UploadException;
  18. use app\common\library\Upload as UploadLib;
  19. use Exception;
  20. use think\Db;
  21. use think\Response;
  22. class Ajax extends Adminbase
  23. {
  24. protected $noNeedRight = ['*'];
  25. //编辑器初始配置
  26. private $editorConfig = [
  27. /* 上传图片配置项 */
  28. "imageActionName" => "uploadimage", /* 执行上传图片的action名称 */
  29. "imageFieldName" => "upfile", /* 提交的图片表单名称 */
  30. "imageMaxSize" => 2048000, /* 上传大小限制,单位B */
  31. "imageAllowFiles" => [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
  32. "imageCompressEnable" => true, /* 是否压缩图片,默认是true */
  33. "imageCompressBorder" => 1600, /* 图片压缩最长边限制 */
  34. "imageInsertAlign" => "none", /* 插入的图片浮动方式 */
  35. "imageUrlPrefix" => "", /* 图片访问路径前缀 */
  36. 'imagePathFormat' => '',
  37. /* 涂鸦图片上传配置项 */
  38. "scrawlActionName" => "uploadscrawl", /* 执行上传涂鸦的action名称 */
  39. "scrawlFieldName" => "upfile", /* 提交的图片表单名称 */
  40. 'scrawlPathFormat' => '',
  41. "scrawlMaxSize" => 2048000, /* 上传大小限制,单位B */
  42. 'scrawlUrlPrefix' => '',
  43. 'scrawlInsertAlign' => 'none',
  44. /* 截图工具上传 */
  45. "snapscreenActionName" => "uploadimage", /* 执行上传截图的action名称 */
  46. 'snapscreenPathFormat' => '',
  47. 'snapscreenUrlPrefix' => '',
  48. 'snapscreenInsertAlign' => 'none',
  49. /* 抓取远程图片配置 */
  50. 'catcherLocalDomain' => ['127.0.0.1', 'localhost', 'img.baidu.com'],
  51. "catcherActionName" => "catchimage", /* 执行抓取远程图片的action名称 */
  52. 'catcherFieldName' => 'source',
  53. 'catcherPathFormat' => '',
  54. 'catcherUrlPrefix' => '',
  55. 'catcherMaxSize' => 0,
  56. 'catcherAllowFiles' => ['.png', '.jpg', '.jpeg', '.gif', '.bmp'],
  57. /* 上传视频配置 */
  58. "videoActionName" => "uploadvideo", /* 执行上传视频的action名称 */
  59. "videoFieldName" => "upfile", /* 提交的视频表单名称 */
  60. 'videoPathFormat' => '',
  61. 'videoUrlPrefix' => '',
  62. 'videoMaxSize' => 0,
  63. 'videoAllowFiles' => [".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"],
  64. /* 上传文件配置 */
  65. "fileActionName" => "uploadfile", /* controller里,执行上传视频的action名称 */
  66. 'fileFieldName' => 'upfile',
  67. 'filePathFormat' => '',
  68. 'fileUrlPrefix' => '',
  69. 'fileMaxSize' => 0,
  70. 'fileAllowFiles' => [".flv", ".swf"],
  71. /* 列出指定目录下的图片 */
  72. "imageManagerActionName" => "listimage", /* 执行图片管理的action名称 */
  73. 'imageManagerListPath' => '',
  74. 'imageManagerListSize' => 20,
  75. 'imageManagerUrlPrefix' => '',
  76. 'imageManagerInsertAlign' => 'none',
  77. 'imageManagerAllowFiles' => ['.png', '.jpg', '.jpeg', '.gif', '.bmp'],
  78. /* 列出指定目录下的文件 */
  79. "fileManagerActionName" => "listfile", /* 执行文件管理的action名称 */
  80. 'fileManagerListPath' => '',
  81. 'fileManagerUrlPrefix' => '',
  82. 'fileManagerListSize' => '',
  83. 'fileManagerAllowFiles' => [".flv", ".swf"],
  84. ];
  85. protected function initialize()
  86. {
  87. parent::initialize();
  88. //图片上传大小和类型
  89. $this->editorConfig['imageMaxSize'] = $this->editorConfig['catcherMaxSize'] = 0 == config('site.upload_image_size') ? 1024 * 1024 * 1024 : config('site.upload_image_size') * 1024;
  90. if (!empty(config('site.upload_image_ext'))) {
  91. $imageallowext = parse_attr(config('site.upload_image_ext'));
  92. foreach ($imageallowext as $k => $rs) {
  93. $imageallowext[$k] = ".{$rs}";
  94. }
  95. $this->editorConfig['imageAllowFiles'] = $imageallowext;
  96. }
  97. //附件上传大小和类型
  98. $this->editorConfig['fileMaxSize'] = $this->editorConfig['videoMaxSize'] = 0 == config('site.upload_file_size') ? 1024 * 1024 * 1024 : config('site.upload_file_size') * 1024;
  99. if (!empty(config('site.upload_file_ext'))) {
  100. $fileallowext = parse_attr(config('site.upload_file_ext'));
  101. foreach ($fileallowext as $k => $rs) {
  102. $fileallowext[$k] = ".{$rs}";
  103. }
  104. $this->editorConfig['fileAllowFiles'] = $fileallowext;
  105. }
  106. }
  107. //过滤内容的敏感词
  108. public function filterWord($content)
  109. {
  110. $content = $this->request->post('content');
  111. // 获取感词库文件路径
  112. $wordFilePath = ROOT_PATH . 'data/words.txt';
  113. $handle = \util\SensitiveHelper::init()->setTreeByFile($wordFilePath);
  114. $word = $handle->getBadWord($content);
  115. if ($word) {
  116. $this->error('内容包含违禁词!', null, $word);
  117. } else {
  118. $this->success('内容没有违禁词!');
  119. }
  120. }
  121. /**
  122. * 生成后缀图标
  123. */
  124. public function icon()
  125. {
  126. $suffix = $this->request->request("suffix", 'file');
  127. $data = build_suffix_image($suffix);
  128. $header = ['Content-Type' => 'image/svg+xml'];
  129. $offset = 30 * 60 * 60 * 24; // 缓存一个月
  130. $header['Cache-Control'] = 'public';
  131. $header['Pragma'] = 'cache';
  132. $header['Expires'] = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
  133. $response = Response::create($data, '', 200, $header);
  134. return $response;
  135. }
  136. /**
  137. * 读取省市区数据,联动列表
  138. */
  139. public function area()
  140. {
  141. $params = $this->request->get("row/a");
  142. if (!empty($params)) {
  143. $province = $params['province'] ?? null;
  144. $city = $params['city'] ?? null;
  145. } else {
  146. $province = $this->request->get('province');
  147. $city = $this->request->get('city');
  148. }
  149. $where = ['pid' => 0, 'level' => 1];
  150. $provincelist = null;
  151. if ($province !== null) {
  152. $where['pid'] = $province;
  153. $where['level'] = 2;
  154. if ($city !== null) {
  155. $where['pid'] = $city;
  156. $where['level'] = 3;
  157. }
  158. }
  159. $provincelist = Db::name('area')->where($where)->field('id as value,name')->select();
  160. $this->success('', '', $provincelist);
  161. }
  162. public function upload($dir = '', $from = '')
  163. {
  164. Adminlog::setTitle('附件上传');
  165. if ($dir == '') {
  166. return json([
  167. 'code' => 0,
  168. 'msg' => '没有指定上传目录',
  169. 'state' => '没有指定上传目录', //兼容百度
  170. 'message' => '没有指定上传目录', //兼容editormd
  171. ]);
  172. }
  173. $chunkid = $this->request->post("chunkid");
  174. if ($chunkid) {
  175. if (!config('chunking')) {
  176. return json([
  177. 'code' => 0,
  178. 'msg' => '未开启分片上传功能',
  179. ]);
  180. }
  181. //分片
  182. $action = $this->request->post("action");
  183. $chunkindex = $this->request->post("chunkindex/d", 0);
  184. $chunkcount = $this->request->post("chunkcount/d", 1);
  185. $filename = $this->request->post("filename");
  186. $method = $this->request->method(true);
  187. if ($action == 'merge') {
  188. $attachment = null;
  189. //合并分片文件
  190. try {
  191. $upload = new UploadLib();
  192. $attachment = $upload->merge($chunkid, $chunkcount, $filename, $dir, $from);
  193. } catch (UploadException $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. } elseif ($method == 'clean') {
  203. //删除冗余的分片文件
  204. try {
  205. $upload = new UploadLib();
  206. $upload->clean($chunkid);
  207. } catch (UploadException $e) {
  208. return json([
  209. 'code' => 0,
  210. 'msg' => $e->getMessage(),
  211. 'state' => $e->getMessage(), //兼容百度
  212. 'message' => $e->getMessage(), //兼容editormd
  213. ]);
  214. }
  215. return json(['code' => 1]);
  216. } else {
  217. //上传分片文件
  218. $file = $this->request->file('file');
  219. try {
  220. $upload = new UploadLib($file);
  221. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  222. } catch (UploadException $e) {
  223. return json([
  224. 'code' => 0,
  225. 'msg' => $e->getMessage(),
  226. 'state' => $e->getMessage(), //兼容百度
  227. 'message' => $e->getMessage(), //兼容editormd
  228. ]);
  229. }
  230. return json(['code' => 1]);
  231. }
  232. }
  233. // 获取附件数据
  234. switch ($from) {
  235. case 'editormd':
  236. $file_input_name = 'editormd-image-file';
  237. break;
  238. case 'ueditor':
  239. $file_input_name = 'upfile';
  240. break;
  241. default:
  242. $file_input_name = 'file';
  243. }
  244. $attachment = null;
  245. try {
  246. //默认普通上传文件
  247. $file = $this->request->file($file_input_name);
  248. if ($from == 'ueditor') {
  249. return $this->ueditor($file);
  250. }
  251. $upload = new UploadLib($file);
  252. $attachment = $upload->upload($dir);
  253. } catch (UploadException | Exception $e) {
  254. return json([
  255. 'code' => 0,
  256. 'msg' => $e->getMessage(),
  257. 'state' => $e->getMessage(), //兼容百度
  258. 'message' => $e->getMessage(), //兼容editormd
  259. ]);
  260. }
  261. return $attachment;
  262. }
  263. private function ueditor($file)
  264. {
  265. $action = $this->request->get('action');
  266. switch ($action) {
  267. /* 获取配置信息 */
  268. case 'config':
  269. $result = $this->editorConfig;
  270. break;
  271. /* 上传图片 */
  272. case 'uploadimage':
  273. $upload = new UploadLib($file);
  274. return $upload->upload('images', 'ueditor');
  275. break;
  276. /* 上传涂鸦 */
  277. case 'uploadscrawl':
  278. $upload = new UploadLib($file);
  279. return $upload->upload('images', 'ueditor_scrawl');
  280. break;
  281. /* 上传视频 */
  282. case 'uploadvideo':
  283. $upload = new UploadLib($file);
  284. return $upload->upload('videos', 'ueditor');
  285. break;
  286. /* 上传附件 */
  287. case 'uploadfile':
  288. $upload = new UploadLib($file);
  289. return $upload->upload('files', 'ueditor');
  290. break;
  291. /* 列出图片
  292. /*case 'listimage':
  293. return $this->showFileList('listimage');
  294. break;
  295. 列出附件
  296. case 'listfile':
  297. return $this->showFileList('listfile');
  298. break;*/
  299. default:
  300. $result = [
  301. 'state' => '请求地址出错',
  302. ];
  303. break;
  304. }
  305. /* 输出结果 */
  306. if (isset($_GET["callback"])) {
  307. if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
  308. return htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
  309. } else {
  310. return json(['state' => 'callback参数不合法']);
  311. }
  312. } else {
  313. return json($result);
  314. }
  315. }
  316. }