Ajax.php 11 KB

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