base.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta content="width=device-width, initial-scale=1.0" name="viewport">
  6. <title>{% block title %}{{ app_name }}{% endblock %}</title>
  7. <!-- Tailwind CSS -->
  8. <script src="{{ url_for('static', filename='js/tailwindcss.js') }}"></script>
  9. <!-- Font Awesome -->
  10. <link href="{{ url_for('static', filename='fontawesome/css/all.min.css') }}" rel="stylesheet">
  11. <!-- ECharts -->
  12. <script src="{{ url_for('static', filename='echarts/dist/echarts.min.js') }}"></script>
  13. <!-- jQuery -->
  14. <script src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
  15. <!-- Custom CSS -->
  16. <link href="{{ url_for('static', filename='css/style.css') }}" rel="stylesheet">
  17. {% block head %}{% endblock %}
  18. </head>
  19. <body class="min-h-screen flex flex-col">
  20. {% block content %}{% endblock %}
  21. {% block scripts %}{% endblock %}
  22. <!-- 全局 Token 自动刷新 -->
  23. <script>
  24. (function () {
  25. var refreshing = false;
  26. var pendingRequests = [];
  27. $.ajaxSetup({
  28. beforeSend: function (xhr) {
  29. var token = localStorage.getItem('token') || localStorage.getItem('jwt_token');
  30. if (token) {
  31. xhr.setRequestHeader('Authorization', 'Bearer ' + token);
  32. }
  33. }
  34. });
  35. $(document).ajaxError(function (event, jqXHR, settings) {
  36. if (jqXHR.status === 401 && !refreshing) {
  37. var rt = localStorage.getItem('refresh_token');
  38. if (!rt) {
  39. window.location.href = '/login';
  40. return;
  41. }
  42. refreshing = true;
  43. // 暂停后续请求重试队列
  44. pendingRequests.push({ settings: settings });
  45. $.ajax({
  46. url: '/api/v1/auth/refresh',
  47. method: 'POST',
  48. contentType: 'application/json',
  49. data: JSON.stringify({ refresh_token: rt }),
  50. success: function (resp) {
  51. if (resp.code === '000000') {
  52. localStorage.setItem('token', resp.data.access_token);
  53. localStorage.setItem('refresh_token', resp.data.refresh_token);
  54. // 重试所有挂起的请求
  55. pendingRequests.forEach(function (p) {
  56. $.ajax(p.settings);
  57. });
  58. } else {
  59. window.location.href = '/login';
  60. }
  61. },
  62. error: function () {
  63. window.location.href = '/login';
  64. },
  65. complete: function () {
  66. refreshing = false;
  67. pendingRequests = [];
  68. }
  69. });
  70. return false;
  71. }
  72. });
  73. })();
  74. </script>
  75. </body>
  76. </html>