Ajax.php 8.6 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(['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. $chunkid = $this->request->post("chunkid");
  50. if ($chunkid) {
  51. if (!Config::get('upload.chunking')) {
  52. $this->error(__('Chunk file disabled'));
  53. }
  54. $action = $this->request->post("action");
  55. $chunkindex = $this->request->post("chunkindex/d");
  56. $chunkcount = $this->request->post("chunkcount/d");
  57. $filename = $this->request->post("filename");
  58. $method = $this->request->method(true);
  59. if ($action == 'merge') {
  60. $attachment = null;
  61. //合并分片文件
  62. try {
  63. $upload = new Upload();
  64. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  65. } catch (UploadException $e) {
  66. $this->error($e->getMessage());
  67. }
  68. $this->success(__('Uploaded successful'), '', ['url' => $attachment->url]);
  69. } elseif ($method == 'clean') {
  70. //删除冗余的分片文件
  71. try {
  72. $upload = new Upload();
  73. $upload->clean($chunkid);
  74. } catch (UploadException $e) {
  75. $this->error($e->getMessage());
  76. }
  77. $this->success();
  78. } else {
  79. //上传分片文件
  80. //默认普通上传文件
  81. $file = $this->request->file('file');
  82. try {
  83. $upload = new Upload($file);
  84. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  85. } catch (UploadException $e) {
  86. $this->error($e->getMessage());
  87. }
  88. $this->success();
  89. }
  90. } else {
  91. $attachment = null;
  92. //默认普通上传文件
  93. $file = $this->request->file('file');
  94. try {
  95. $upload = new Upload($file);
  96. $attachment = $upload->upload();
  97. } catch (UploadException $e) {
  98. $this->error($e->getMessage());
  99. }
  100. $this->success(__('Uploaded successful'), '', ['url' => $attachment->url]);
  101. }
  102. }
  103. /**
  104. * 通用排序
  105. */
  106. public function weigh()
  107. {
  108. //排序的数组
  109. $ids = $this->request->post("ids");
  110. //拖动的记录ID
  111. $changeid = $this->request->post("changeid");
  112. //操作字段
  113. $field = $this->request->post("field");
  114. //操作的数据表
  115. $table = $this->request->post("table");
  116. if (!Validate::is($table, "alphaDash")) {
  117. $this->error();
  118. }
  119. //主键
  120. $pk = $this->request->post("pk");
  121. //排序的方式
  122. $orderway = strtolower($this->request->post("orderway", ""));
  123. $orderway = $orderway == 'asc' ? 'ASC' : 'DESC';
  124. $sour = $weighdata = [];
  125. $ids = explode(',', $ids);
  126. $prikey = $pk ? $pk : (Db::name($table)->getPk() ?: 'id');
  127. $pid = $this->request->post("pid");
  128. //限制更新的字段
  129. $field = in_array($field, ['weigh']) ? $field : 'weigh';
  130. // 如果设定了pid的值,此时只匹配满足条件的ID,其它忽略
  131. if ($pid !== '') {
  132. $hasids = [];
  133. $list = Db::name($table)->where($prikey, 'in', $ids)->where('pid', 'in', $pid)->field("{$prikey},pid")->select();
  134. foreach ($list as $k => $v) {
  135. $hasids[] = $v[$prikey];
  136. }
  137. $ids = array_values(array_intersect($ids, $hasids));
  138. }
  139. $list = Db::name($table)->field("$prikey,$field")->where($prikey, 'in', $ids)->order($field, $orderway)->select();
  140. foreach ($list as $k => $v) {
  141. $sour[] = $v[$prikey];
  142. $weighdata[$v[$prikey]] = $v[$field];
  143. }
  144. $position = array_search($changeid, $ids);
  145. $desc_id = $sour[$position]; //移动到目标的ID值,取出所处改变前位置的值
  146. $sour_id = $changeid;
  147. $weighids = array();
  148. $temp = array_values(array_diff_assoc($ids, $sour));
  149. foreach ($temp as $m => $n) {
  150. if ($n == $sour_id) {
  151. $offset = $desc_id;
  152. } else {
  153. if ($sour_id == $temp[0]) {
  154. $offset = isset($temp[$m + 1]) ? $temp[$m + 1] : $sour_id;
  155. } else {
  156. $offset = isset($temp[$m - 1]) ? $temp[$m - 1] : $sour_id;
  157. }
  158. }
  159. $weighids[$n] = $weighdata[$offset];
  160. Db::name($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]);
  161. }
  162. $this->success();
  163. }
  164. /**
  165. * 清空系统缓存
  166. */
  167. public function wipecache()
  168. {
  169. $type = $this->request->request("type");
  170. switch ($type) {
  171. case 'all':
  172. case 'content':
  173. rmdirs(CACHE_PATH, false);
  174. Cache::clear();
  175. if ($type == 'content') {
  176. break;
  177. }
  178. case 'template':
  179. rmdirs(TEMP_PATH, false);
  180. if ($type == 'template') {
  181. break;
  182. }
  183. case 'addons':
  184. Service::refresh();
  185. if ($type == 'addons') {
  186. break;
  187. }
  188. }
  189. \think\Hook::listen("wipecache_after");
  190. $this->success();
  191. }
  192. /**
  193. * 读取分类数据,联动列表
  194. */
  195. public function category()
  196. {
  197. $type = $this->request->get('type');
  198. $pid = $this->request->get('pid');
  199. $where = ['status' => 'normal'];
  200. $categorylist = null;
  201. if ($pid !== '') {
  202. if ($type) {
  203. $where['type'] = $type;
  204. }
  205. if ($pid) {
  206. $where['pid'] = $pid;
  207. }
  208. $categorylist = Db::name('category')->where($where)->field('id as value,name')->order('weigh desc,id desc')->select();
  209. }
  210. $this->success('', null, $categorylist);
  211. }
  212. /**
  213. * 读取省市区数据,联动列表
  214. */
  215. public function area()
  216. {
  217. $params = $this->request->get("row/a");
  218. if (!empty($params)) {
  219. $province = isset($params['province']) ? $params['province'] : '';
  220. $city = isset($params['city']) ? $params['city'] : null;
  221. } else {
  222. $province = $this->request->get('province');
  223. $city = $this->request->get('city');
  224. }
  225. $where = ['pid' => 0, 'level' => 1];
  226. $provincelist = null;
  227. if ($province !== '') {
  228. if ($province) {
  229. $where['pid'] = $province;
  230. $where['level'] = 2;
  231. }
  232. if ($city !== '') {
  233. if ($city) {
  234. $where['pid'] = $city;
  235. $where['level'] = 3;
  236. }
  237. $provincelist = Db::name('area')->where($where)->field('id as value,name')->select();
  238. }
  239. }
  240. $this->success('', null, $provincelist);
  241. }
  242. /**
  243. * 生成后缀图标
  244. */
  245. public function icon()
  246. {
  247. $suffix = $this->request->request("suffix");
  248. header('Content-type: image/svg+xml');
  249. $suffix = $suffix ? $suffix : "FILE";
  250. echo build_suffix_image($suffix);
  251. exit;
  252. }
  253. }