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' => cdnurl($attachment->url,true), '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. //替换为完整路径
  107. $this->success(__('Uploaded successful'), '', ['url' => cdnurl($attachment->url,true), 'fullurl' => cdnurl($attachment->url, true)]);
  108. }
  109. }
  110. /**
  111. * 通用排序
  112. */
  113. public function weigh()
  114. {
  115. //排序的数组
  116. $ids = $this->request->post("ids");
  117. //拖动的记录ID
  118. $changeid = $this->request->post("changeid");
  119. //操作字段
  120. $field = $this->request->post("field");
  121. //操作的数据表
  122. $table = $this->request->post("table");
  123. if (!Validate::is($table, "alphaDash")) {
  124. $this->error();
  125. }
  126. //主键
  127. $pk = $this->request->post("pk");
  128. //排序的方式
  129. $orderway = strtolower($this->request->post("orderway", ""));
  130. $orderway = $orderway == 'asc' ? 'ASC' : 'DESC';
  131. $sour = $weighdata = [];
  132. $ids = explode(',', $ids);
  133. $prikey = $pk && preg_match("/^[a-z0-9\-_]+$/i", $pk) ? $pk : (Db::name($table)->getPk() ?: 'id');
  134. $pid = $this->request->post("pid", "");
  135. //限制更新的字段
  136. $field = in_array($field, ['weigh']) ? $field : 'weigh';
  137. // 如果设定了pid的值,此时只匹配满足条件的ID,其它忽略
  138. if ($pid !== '') {
  139. $hasids = [];
  140. $list = Db::name($table)->where($prikey, 'in', $ids)->where('pid', 'in', $pid)->field("{$prikey},pid")->select();
  141. foreach ($list as $k => $v) {
  142. $hasids[] = $v[$prikey];
  143. }
  144. $ids = array_values(array_intersect($ids, $hasids));
  145. }
  146. $list = Db::name($table)->field("$prikey,$field")->where($prikey, 'in', $ids)->order($field, $orderway)->select();
  147. foreach ($list as $k => $v) {
  148. $sour[] = $v[$prikey];
  149. $weighdata[$v[$prikey]] = $v[$field];
  150. }
  151. $position = array_search($changeid, $ids);
  152. $desc_id = isset($sour[$position]) ? $sour[$position] : end($sour); //移动到目标的ID值,取出所处改变前位置的值
  153. $sour_id = $changeid;
  154. $weighids = array();
  155. $temp = array_values(array_diff_assoc($ids, $sour));
  156. foreach ($temp as $m => $n) {
  157. if ($n == $sour_id) {
  158. $offset = $desc_id;
  159. } else {
  160. if ($sour_id == $temp[0]) {
  161. $offset = isset($temp[$m + 1]) ? $temp[$m + 1] : $sour_id;
  162. } else {
  163. $offset = isset($temp[$m - 1]) ? $temp[$m - 1] : $sour_id;
  164. }
  165. }
  166. if (!isset($weighdata[$offset])) {
  167. continue;
  168. }
  169. $weighids[$n] = $weighdata[$offset];
  170. Db::name($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]);
  171. }
  172. $this->success();
  173. }
  174. /**
  175. * 清空系统缓存
  176. */
  177. public function wipecache()
  178. {
  179. try {
  180. $type = $this->request->request("type");
  181. switch ($type) {
  182. case 'all':
  183. // no break
  184. case 'content':
  185. //内容缓存
  186. rmdirs(CACHE_PATH, false);
  187. Cache::clear();
  188. if ($type == 'content') {
  189. break;
  190. }
  191. case 'template':
  192. // 模板缓存
  193. rmdirs(TEMP_PATH, false);
  194. if ($type == 'template') {
  195. break;
  196. }
  197. case 'addons':
  198. // 插件缓存
  199. Service::refresh();
  200. if ($type == 'addons') {
  201. break;
  202. }
  203. case 'browser':
  204. // 浏览器缓存
  205. // 只有生产环境下才修改
  206. if (!config('app_debug')) {
  207. $version = config('site.version');
  208. $newversion = preg_replace_callback("/(.*)\.([0-9]+)\$/", function ($match) {
  209. return $match[1] . '.' . ($match[2] + 1);
  210. }, $version);
  211. if ($newversion && $newversion != $version) {
  212. Db::startTrans();
  213. try {
  214. \app\common\model\Config::where('name', 'version')->update(['value' => $newversion]);
  215. \app\common\model\Config::refreshFile();
  216. Db::commit();
  217. } catch (\Exception $e) {
  218. Db::rollback();
  219. exception($e->getMessage());
  220. }
  221. }
  222. }
  223. if ($type == 'browser') {
  224. break;
  225. }
  226. }
  227. } catch (\Exception $e) {
  228. $this->error($e->getMessage());
  229. }
  230. \think\Hook::listen("wipecache_after");
  231. $this->success();
  232. }
  233. /**
  234. * 读取分类数据,联动列表
  235. */
  236. public function category()
  237. {
  238. $type = $this->request->get('type', '');
  239. $pid = $this->request->get('pid', '');
  240. $where = ['status' => 'normal'];
  241. $categorylist = null;
  242. if ($pid || $pid === '0') {
  243. $where['pid'] = $pid;
  244. }
  245. if ($type) {
  246. $where['type'] = $type;
  247. }
  248. $categorylist = Db::name('category')->where($where)->field('id as value,name')->order('weigh desc,id desc')->select();
  249. $this->success('', '', $categorylist);
  250. }
  251. /**
  252. * 读取省市区数据,联动列表
  253. */
  254. public function area()
  255. {
  256. $params = $this->request->get("row/a");
  257. if (!empty($params)) {
  258. $province = isset($params['province']) ? $params['province'] : null;
  259. $city = isset($params['city']) ? $params['city'] : null;
  260. } else {
  261. $province = $this->request->get('province');
  262. $city = $this->request->get('city');
  263. }
  264. $where = ['pid' => 0, 'level' => 1];
  265. $provincelist = null;
  266. if ($province !== null) {
  267. $where['pid'] = $province;
  268. $where['level'] = 2;
  269. if ($city !== null) {
  270. $where['pid'] = $city;
  271. $where['level'] = 3;
  272. }
  273. }
  274. $provincelist = Db::name('area')->where($where)->field('id as value,name')->select();
  275. $this->success('', '', $provincelist);
  276. }
  277. /**
  278. * 生成后缀图标
  279. */
  280. public function icon()
  281. {
  282. $suffix = $this->request->request("suffix");
  283. $suffix = $suffix ? $suffix : "FILE";
  284. $data = build_suffix_image($suffix);
  285. $header = ['Content-Type' => 'image/svg+xml'];
  286. $offset = 30 * 60 * 60 * 24; // 缓存一个月
  287. $header['Cache-Control'] = 'public';
  288. $header['Pragma'] = 'cache';
  289. $header['Expires'] = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
  290. $response = Response::create($data, '', 200, $header);
  291. return $response;
  292. }
  293. }