Ajax.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use think\Lang;
  5. use think\Response;
  6. /**
  7. * Ajax异步请求接口
  8. * @internal
  9. */
  10. class Ajax extends Frontend
  11. {
  12. protected $noNeedLogin = ['lang', 'upload'];
  13. protected $noNeedRight = ['*'];
  14. protected $layout = '';
  15. /**
  16. * 加载语言包
  17. */
  18. public function lang()
  19. {
  20. $this->request->get(['callback' => 'define']);
  21. $header = ['Content-Type' => 'application/javascript'];
  22. if (!config('app_debug')) {
  23. $offset = 30 * 60 * 60 * 24; // 缓存一个月
  24. $header['Cache-Control'] = 'public';
  25. $header['Pragma'] = 'cache';
  26. $header['Expires'] = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
  27. }
  28. $controllername = input("controllername");
  29. $this->loadlang($controllername);
  30. //强制输出JSON Object
  31. return jsonp(Lang::get(), 200, $header, ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
  32. }
  33. /**
  34. * 生成后缀图标
  35. */
  36. public function icon()
  37. {
  38. $suffix = $this->request->request("suffix");
  39. $suffix = $suffix ? $suffix : "FILE";
  40. $data = build_suffix_image($suffix);
  41. $header = ['Content-Type' => 'image/svg+xml'];
  42. $offset = 30 * 60 * 60 * 24; // 缓存一个月
  43. $header['Cache-Control'] = 'public';
  44. $header['Pragma'] = 'cache';
  45. $header['Expires'] = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
  46. $response = Response::create($data, '', 200, $header);
  47. return $response;
  48. }
  49. /**
  50. * 上传文件
  51. */
  52. public function upload()
  53. {
  54. return action('api/common/upload');
  55. }
  56. }