Ajax.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 后台常用ajax
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use app\admin\model\Adminlog;
  16. use app\common\controller\Backend;
  17. use app\common\exception\UploadException;
  18. use app\common\library\Upload as UploadLib;
  19. use Exception;
  20. use think\facade\Db;
  21. use think\facade\Validate;
  22. use think\Response;
  23. class Ajax extends Backend
  24. {
  25. protected $noNeedRight = ['*'];
  26. //过滤内容的敏感词
  27. public function filterWord($content)
  28. {
  29. $content = $this->request->post('content');
  30. // 获取感词库文件路径
  31. $wordFilePath = app()->getRootPath() . 'data/words.txt';
  32. $handle = \util\SensitiveHelper::init()->setTreeByFile($wordFilePath);
  33. $word = $handle->getBadWord($content);
  34. if ($word) {
  35. $this->error('内容包含违禁词!', null, $word);
  36. } else {
  37. $this->success('内容没有违禁词!');
  38. }
  39. }
  40. /**
  41. * 生成后缀图标
  42. */
  43. public function icon()
  44. {
  45. $suffix = $this->request->request("suffix", 'file');
  46. $data = build_suffix_image($suffix);
  47. $header = ['Content-Type' => 'image/svg+xml'];
  48. $offset = 30 * 60 * 60 * 24; // 缓存一个月
  49. $header['Cache-Control'] = 'public';
  50. $header['Pragma'] = 'cache';
  51. $header['Expires'] = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
  52. $response = Response::create($data)->header($header);
  53. return $response;
  54. }
  55. /**
  56. * 读取省市区数据,联动列表
  57. */
  58. public function area()
  59. {
  60. $params = $this->request->get("row/a");
  61. if (!empty($params)) {
  62. $province = $params['province'] ?? null;
  63. $city = $params['city'] ?? null;
  64. } else {
  65. $province = $this->request->get('province');
  66. $city = $this->request->get('city');
  67. }
  68. $where = ['pid' => 0, 'level' => 1];
  69. $provincelist = null;
  70. if ($province !== null) {
  71. $where['pid'] = $province;
  72. $where['level'] = 2;
  73. if ($city !== null) {
  74. $where['pid'] = $city;
  75. $where['level'] = 3;
  76. }
  77. }
  78. $provincelist = Db::name('area')->where($where)->field('id as value,name')->select();
  79. $this->success('', '', $provincelist);
  80. }
  81. public function upload($dir = '', $from = '')
  82. {
  83. Adminlog::setTitle('附件上传');
  84. if ($dir == '') {
  85. $this->error('没有指定上传目录');
  86. }
  87. $chunkid = $this->request->post("chunkid");
  88. if ($chunkid) {
  89. if (!config('upload.chunking')) {
  90. $this->error('未开启分片上传功能');
  91. }
  92. //分片
  93. $action = $this->request->post("action");
  94. $chunkindex = $this->request->post("chunkindex/d", 0);
  95. $chunkcount = $this->request->post("chunkcount/d", 1);
  96. $filename = $this->request->post("filename");
  97. $method = $this->request->method(true);
  98. if ($action == 'merge') {
  99. $attachment = null;
  100. //合并分片文件
  101. try {
  102. $upload = new UploadLib();
  103. $attachment = $upload->merge($chunkid, $chunkcount, $filename, $dir, $from);
  104. } catch (UploadException $e) {
  105. $this->error($e->getMessage());
  106. }
  107. $this->success('上传成功', '', $attachment->getBaseData());
  108. } elseif ($method == 'clean') {
  109. //删除冗余的分片文件
  110. try {
  111. $upload = new UploadLib();
  112. $upload->clean($chunkid);
  113. } catch (UploadException $e) {
  114. $this->error($e->getMessage());
  115. }
  116. $this->success();
  117. } else {
  118. //上传分片文件
  119. $file = $this->request->file('file');
  120. try {
  121. $upload = new UploadLib($file);
  122. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  123. } catch (UploadException $e) {
  124. $this->error($e->getMessage());
  125. }
  126. $this->success();
  127. }
  128. }
  129. $attachment = null;
  130. try {
  131. //默认普通上传文件
  132. $file = $this->request->file('file');
  133. $upload = new UploadLib($file);
  134. $attachment = $upload->upload($dir);
  135. } catch (UploadException | Exception $e) {
  136. $this->error($e->getMessage());
  137. }
  138. $this->success('上传成功', '', $attachment->getBaseData());
  139. }
  140. //通用排序
  141. public function weigh()
  142. {
  143. //排序的数组
  144. $ids = $this->request->post("ids");
  145. //拖动的记录ID
  146. $changeid = $this->request->post("changeid");
  147. //操作字段
  148. $field = $this->request->post("field");
  149. //操作的数据表
  150. $table = $this->request->post("table");
  151. if (!Validate::is($table, "alphaDash")) {
  152. $this->error('表名规则错误');
  153. }
  154. //主键
  155. $pk = $this->request->post("pk");
  156. //排序的方式
  157. $orderway = strtolower($this->request->post("orderway", ""));
  158. $orderway = $orderway == 'asc' ? 'ASC' : 'DESC';
  159. $sour = $weighdata = [];
  160. $ids = explode(',', $ids);
  161. $prikey = $pk && preg_match("/^[a-z0-9\-_]+$/i", $pk) ? $pk : (Db::name($table)->getPk() ?: 'id');
  162. $parentid = $this->request->post("pid", "");
  163. //限制更新的字段
  164. $field = in_array($field, ['listorder']) ? $field : 'listorder';
  165. // 如果设定了parentid的值,此时只匹配满足条件的ID,其它忽略
  166. if ($parentid !== '') {
  167. $hasids = [];
  168. $list = Db::name($table)->where($prikey, 'in', $ids)->where('parentid', 'in', $parentid)->field("{$prikey},parentid")->select();
  169. foreach ($list as $k => $v) {
  170. $hasids[] = $v[$prikey];
  171. }
  172. $ids = array_values(array_intersect($ids, $hasids));
  173. }
  174. $list = Db::name($table)->field("$prikey,$field")->where($prikey, 'in', $ids)->order($field, $orderway)->select();
  175. foreach ($list as $k => $v) {
  176. $sour[] = $v[$prikey];
  177. $weighdata[$v[$prikey]] = $v[$field];
  178. }
  179. $position = array_search($changeid, $ids);
  180. $desc_id = $sour[$position] ?? end($sour); //移动到目标的ID值,取出所处改变前位置的值
  181. $sour_id = $changeid;
  182. $weighids = [];
  183. $temp = array_values(array_diff_assoc($ids, $sour));
  184. foreach ($temp as $m => $n) {
  185. if ($n == $sour_id) {
  186. $offset = $desc_id;
  187. } else {
  188. if ($sour_id == $temp[0]) {
  189. $offset = $temp[$m + 1] ?? $sour_id;
  190. } else {
  191. $offset = $temp[$m - 1] ?? $sour_id;
  192. }
  193. }
  194. if (!isset($weighdata[$offset])) {
  195. continue;
  196. }
  197. $weighids[$n] = $weighdata[$offset];
  198. Db::name($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]);
  199. }
  200. $this->success('排序成功');
  201. }
  202. }