酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

327 lines
11 KiB

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