utils.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. $(document).ready(function() {
  2. initData();
  3. checkToken();
  4. })
  5. /**
  6. * 初始化数据
  7. */
  8. function initData() {
  9. var rootUrl = 'rootUrl'
  10. if (!localStorage.getItem(rootUrl))
  11. localStorage.setItem(rootUrl, 'http://localhost:8080/eorder/admin')
  12. }
  13. /**
  14. * 得到请求跟路径
  15. */
  16. function getRootUrl() {
  17. var rootUrl = localStorage.getItem('rootUrl');
  18. return rootUrl;
  19. }
  20. /**
  21. * 服务器ip:port
  22. */
  23. function getServerIPPrefix(){
  24. var serverPrefix='http://localhost:8080';
  25. return serverPrefix;
  26. }
  27. /**
  28. * 获取上传文件接口
  29. */
  30. function getUploadUrl(){
  31. return 'http://localhost:8080/eorder/file/upload'
  32. }
  33. /**
  34. * 获取token
  35. */
  36. function getToken() {
  37. var token = localStorage.getItem('token');
  38. return token;
  39. }
  40. var loginPath = '/eorder-admin/page/login.html';
  41. /**
  42. * 校验token合法性
  43. */
  44. function checkToken() {
  45. var token = localStorage.getItem('token');
  46. var rootUrl = getRootUrl();
  47. $.ajax({
  48. url: rootUrl + '/token/check',
  49. type: 'GET',
  50. headers: {
  51. token: token
  52. },
  53. dataType: 'json',
  54. async: false,
  55. success(e) {
  56. if (!e.success) {
  57. if (top.location.pathname != loginPath) {
  58. window.location = loginPath
  59. }
  60. localStorage.removeItem('token')
  61. }
  62. },
  63. error(e) {
  64. if (top.location.pathname != loginPath) {
  65. window.location = loginPath
  66. }
  67. localStorage.removeItem('token')
  68. }
  69. })
  70. }
  71. /**
  72. * POST请求服务器
  73. * @param {String} uri 请求路径
  74. * @param {JSON} data JSON数据
  75. * @return {Object} 接口返回数据
  76. */
  77. function postData(uri, data) {
  78. var token = localStorage.getItem('token');
  79. var rootUrl = getRootUrl();
  80. var temp;
  81. $.ajax({
  82. url: rootUrl + uri,
  83. type: 'POST',
  84. headers: {
  85. token: token
  86. },
  87. data: data,
  88. contentType: 'application/json',
  89. dataType: 'json',
  90. async: false,
  91. success(e) {
  92. if (e.success) {
  93. temp = e.data
  94. }else{
  95. layer.msg(e.message)
  96. }
  97. },
  98. error(e) {
  99. layer.msg('连接超时')
  100. }
  101. });
  102. return temp;
  103. }
  104. /**
  105. * 向服务器发送GET请求
  106. * @param {String} uri url
  107. * @param {JSON} data 数据
  108. * @return {Object} 接口返回数据
  109. */
  110. function getData(uri, data) {
  111. var token = localStorage.getItem('token');
  112. var rootUrl = getRootUrl();
  113. var temp;
  114. $.ajax({
  115. url: rootUrl + uri,
  116. type: 'GET',
  117. headers: {
  118. token: token
  119. },
  120. data: data,
  121. dataType: 'json',
  122. async: false,
  123. success(e) {
  124. if (e.success) {
  125. temp = e
  126. }else{
  127. layer.msg(e.message)
  128. }
  129. },
  130. error(e) {
  131. layer.msg('连接超时')
  132. }
  133. });
  134. return temp;
  135. }