Ajax.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. // no break
  183. case 'content':
  184. //内容缓存
  185. rmdirs(CACHE_PATH, false);
  186. Cache::clear();
  187. if ($type == 'content') {
  188. break;
  189. }
  190. case 'template':
  191. // 模板缓存
  192. rmdirs(TEMP_PATH, false);
  193. if ($type == 'template') {
  194. break;
  195. }
  196. case 'addons':
  197. // 插件缓存
  198. Service::refresh();
  199. if ($type == 'addons') {
  200. break;
  201. }
  202. case 'browser':
  203. // 浏览器缓存
  204. // 只有生产环境下才修改
  205. if (!config('app_debug')) {
  206. $version = config('site.version');
  207. $newversion = preg_replace_callback("/(.*)\.([0-9]+)\$/", function ($match) {
  208. return $match[1] . '.' . ($match[2] + 1);
  209. }, $version);
  210. if ($newversion && $newversion != $version) {
  211. Db::startTrans();
  212. try {
  213. \app\common\model\Config::where('name', 'version')->update(['value' => $newversion]);
  214. \app\common\model\Config::refreshFile();
  215. Db::commit();
  216. } catch (\Exception $e) {
  217. Db::rollback();
  218. exception($e->getMessage());
  219. }
  220. }
  221. }
  222. if ($type == 'browser') {
  223. break;
  224. }
  225. }
  226. } catch (\Exception $e) {
  227. $this->error($e->getMessage());
  228. }
  229. \think\Hook::listen("wipecache_after");
  230. $this->success();
  231. }
  232. /**
  233. * 读取分类数据,联动列表
  234. */
  235. public function category()
  236. {
  237. $type = $this->request->get('type', '');
  238. $pid = $this->request->get('pid', '');
  239. $where = ['status' => 'normal'];
  240. $categorylist = null;
  241. if ($pid || $pid === '0') {
  242. $where['pid'] = $pid;
  243. }
  244. if ($type) {
  245. $where['type'] = $type;
  246. }
  247. $categorylist = Db::name('category')->where($where)->field('id as value,name')->order('weigh desc,id desc')->select();
  248. $this->success('', '', $categorylist);
  249. }
  250. /**
  251. * 读取省市区数据,联动列表
  252. */
  253. public function area()
  254. {
  255. $params = $this->request->get("row/a");
  256. if (!empty($params)) {
  257. $province = isset($params['province']) ? $params['province'] : null;
  258. $city = isset($params['city']) ? $params['city'] : null;
  259. } else {
  260. $province = $this->request->get('province');
  261. $city = $this->request->get('city');
  262. }
  263. $where = ['pid' => 0, 'level' => 1];
  264. $provincelist = null;
  265. if ($province !== null) {
  266. $where['pid'] = $province;
  267. $where['level'] = 2;
  268. if ($city !== null) {
  269. $where['pid'] = $city;
  270. $where['level'] = 3;
  271. }
  272. }
  273. $provincelist = Db::name('area')->where($where)->field('id as value,name')->select();
  274. $this->success('', '', $provincelist);
  275. }
  276. /**
  277. * 生成后缀图标
  278. */
  279. public function icon()
  280. {
  281. $suffix = $this->request->request("suffix");
  282. $suffix = $suffix ? $suffix : "FILE";
  283. $data = build_suffix_image($suffix);
  284. $header = ['Content-Type' => 'image/svg+xml'];
  285. $offset = 30 * 60 * 60 * 24; // 缓存一个月
  286. $header['Cache-Control'] = 'public';
  287. $header['Pragma'] = 'cache';
  288. $header['Expires'] = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
  289. $response = Response::create($data, '', 200, $header);
  290. return $response;
  291. }
  292. }