Ajax.php 11 KB

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