Ajax.php 10 KB

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