Ajax.php 11 KB

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