Ajax.php 11 KB

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