Ajax.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\exception\UploadException;
  5. use app\common\library\Upload;
  6. use fast\Random;
  7. use think\addons\Service;
  8. use think\Cache;
  9. use think\Config;
  10. use think\Db;
  11. use think\Lang;
  12. use think\Validate;
  13. /**
  14. * Ajax异步请求接口
  15. * @internal
  16. */
  17. class Ajax extends Backend
  18. {
  19. protected $noNeedLogin = ['lang'];
  20. protected $noNeedRight = ['*'];
  21. protected $layout = '';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. //设置过滤方法
  26. $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);
  27. }
  28. /**
  29. * 加载语言包
  30. */
  31. public function lang()
  32. {
  33. header('Content-Type: application/javascript');
  34. header("Cache-Control: public");
  35. header("Pragma: cache");
  36. $offset = 30 * 60 * 60 * 24; // 缓存一个月
  37. header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
  38. $controllername = input("controllername");
  39. //默认只加载了控制器对应的语言名,你还根据控制器名来加载额外的语言包
  40. $this->loadlang($controllername);
  41. return jsonp(Lang::get(), 200, [], ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
  42. }
  43. /**
  44. * 上传文件
  45. */
  46. public function upload()
  47. {
  48. Config::set('default_return_type', 'json');
  49. //必须设定cdnurl为空,否则cdnurl函数计算错误
  50. Config::set('upload.cdnurl', '');
  51. $chunkid = $this->request->post("chunkid");
  52. if ($chunkid) {
  53. if (!Config::get('upload.chunking')) {
  54. $this->error(__('Chunk file disabled'));
  55. }
  56. $action = $this->request->post("action");
  57. $chunkindex = $this->request->post("chunkindex/d");
  58. $chunkcount = $this->request->post("chunkcount/d");
  59. $filename = $this->request->post("filename");
  60. $method = $this->request->method(true);
  61. if ($action == 'merge') {
  62. $attachment = null;
  63. //合并分片文件
  64. try {
  65. $upload = new Upload();
  66. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  67. } catch (UploadException $e) {
  68. $this->error($e->getMessage());
  69. }
  70. $this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  71. } elseif ($method == 'clean') {
  72. //删除冗余的分片文件
  73. try {
  74. $upload = new Upload();
  75. $upload->clean($chunkid);
  76. } catch (UploadException $e) {
  77. $this->error($e->getMessage());
  78. }
  79. $this->success();
  80. } else {
  81. //上传分片文件
  82. //默认普通上传文件
  83. $file = $this->request->file('file');
  84. try {
  85. $upload = new Upload($file);
  86. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  87. } catch (UploadException $e) {
  88. $this->error($e->getMessage());
  89. }
  90. $this->success();
  91. }
  92. } else {
  93. $attachment = null;
  94. //默认普通上传文件
  95. $file = $this->request->file('file');
  96. try {
  97. $upload = new Upload($file);
  98. $attachment = $upload->upload();
  99. } catch (UploadException $e) {
  100. $this->error($e->getMessage());
  101. }
  102. $this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  103. }
  104. }
  105. /**
  106. * 通用排序
  107. */
  108. public function weigh()
  109. {
  110. //排序的数组
  111. $ids = $this->request->post("ids");
  112. //拖动的记录ID
  113. $changeid = $this->request->post("changeid");
  114. //操作字段
  115. $field = $this->request->post("field");
  116. //操作的数据表
  117. $table = $this->request->post("table");
  118. if (!Validate::is($table, "alphaDash")) {
  119. $this->error();
  120. }
  121. //主键
  122. $pk = $this->request->post("pk");
  123. //排序的方式
  124. $orderway = strtolower($this->request->post("orderway", ""));
  125. $orderway = $orderway == 'asc' ? 'ASC' : 'DESC';
  126. $sour = $weighdata = [];
  127. $ids = explode(',', $ids);
  128. $prikey = $pk && preg_match("/^[a-z0-9\-_]+$/i", $pk) ? $pk : (Db::name($table)->getPk() ?: 'id');
  129. $pid = $this->request->post("pid", "");
  130. //限制更新的字段
  131. $field = in_array($field, ['weigh']) ? $field : 'weigh';
  132. // 如果设定了pid的值,此时只匹配满足条件的ID,其它忽略
  133. if ($pid !== '') {
  134. $hasids = [];
  135. $list = Db::name($table)->where($prikey, 'in', $ids)->where('pid', 'in', $pid)->field("{$prikey},pid")->select();
  136. foreach ($list as $k => $v) {
  137. $hasids[] = $v[$prikey];
  138. }
  139. $ids = array_values(array_intersect($ids, $hasids));
  140. }
  141. $list = Db::name($table)->field("$prikey,$field")->where($prikey, 'in', $ids)->order($field, $orderway)->select();
  142. foreach ($list as $k => $v) {
  143. $sour[] = $v[$prikey];
  144. $weighdata[$v[$prikey]] = $v[$field];
  145. }
  146. $position = array_search($changeid, $ids);
  147. $desc_id = $sour[$position]; //移动到目标的ID值,取出所处改变前位置的值
  148. $sour_id = $changeid;
  149. $weighids = array();
  150. $temp = array_values(array_diff_assoc($ids, $sour));
  151. foreach ($temp as $m => $n) {
  152. if ($n == $sour_id) {
  153. $offset = $desc_id;
  154. } else {
  155. if ($sour_id == $temp[0]) {
  156. $offset = isset($temp[$m + 1]) ? $temp[$m + 1] : $sour_id;
  157. } else {
  158. $offset = isset($temp[$m - 1]) ? $temp[$m - 1] : $sour_id;
  159. }
  160. }
  161. if (!isset($weighdata[$offset])) {
  162. continue;
  163. }
  164. $weighids[$n] = $weighdata[$offset];
  165. Db::name($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]);
  166. }
  167. $this->success();
  168. }
  169. /**
  170. * 清空系统缓存
  171. */
  172. public function wipecache()
  173. {
  174. $type = $this->request->request("type");
  175. switch ($type) {
  176. case 'all':
  177. case 'content':
  178. rmdirs(CACHE_PATH, false);
  179. Cache::clear();
  180. if ($type == 'content') {
  181. break;
  182. }
  183. case 'template':
  184. rmdirs(TEMP_PATH, false);
  185. if ($type == 'template') {
  186. break;
  187. }
  188. case 'addons':
  189. Service::refresh();
  190. if ($type == 'addons') {
  191. break;
  192. }
  193. }
  194. \think\Hook::listen("wipecache_after");
  195. $this->success();
  196. }
  197. /**
  198. * 读取分类数据,联动列表
  199. */
  200. public function category()
  201. {
  202. $type = $this->request->get('type', '');
  203. $pid = $this->request->get('pid', '');
  204. $where = ['status' => 'normal'];
  205. $categorylist = null;
  206. if ($pid || $pid === '0') {
  207. $where['pid'] = $pid;
  208. }
  209. if ($type) {
  210. $where['type'] = $type;
  211. }
  212. $categorylist = Db::name('category')->where($where)->field('id as value,name')->order('weigh desc,id desc')->select();
  213. $this->success('', '', $categorylist);
  214. }
  215. /**
  216. * 读取省市区数据,联动列表
  217. */
  218. public function area()
  219. {
  220. $params = $this->request->get("row/a");
  221. if (!empty($params)) {
  222. $province = isset($params['province']) ? $params['province'] : '';
  223. $city = isset($params['city']) ? $params['city'] : '';
  224. } else {
  225. $province = $this->request->get('province', '');
  226. $city = $this->request->get('city', '');
  227. }
  228. $where = ['pid' => 0, 'level' => 1];
  229. $provincelist = null;
  230. if ($province !== '') {
  231. $where['pid'] = $province;
  232. $where['level'] = 2;
  233. if ($city !== '') {
  234. $where['pid'] = $city;
  235. $where['level'] = 3;
  236. }
  237. }
  238. $provincelist = Db::name('area')->where($where)->field('id as value,name')->select();
  239. $this->success('', '', $provincelist);
  240. }
  241. /**
  242. * 生成后缀图标
  243. */
  244. public function icon()
  245. {
  246. $suffix = $this->request->request("suffix");
  247. header('Content-type: image/svg+xml');
  248. $suffix = $suffix ? $suffix : "FILE";
  249. echo build_suffix_image($suffix);
  250. exit;
  251. }
  252. }