request.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /** axios封装
  2. * 请求拦截、相应拦截、错误统一处理
  3. */
  4. const httpRequest = axios.create({
  5. baseURL: 'http://10.0.0.198:30701', // 测试环境
  6. timeout: 3 * 60 * 1000, // 3分钟等待
  7. withCredentials: true, // 携带cookie
  8. })
  9. // 请求拦截器
  10. httpRequest.interceptors.request.use(
  11. function (config) {
  12. // config.headers['accessToken'] = ''
  13. // if (!isLocalEnv()) {
  14. // if (getQueryVariable('token')) {
  15. // config.headers['access_token'] = getQueryVariable('token')
  16. // }
  17. // } else {
  18. // config.headers['access_token'] = localStorage.getItem('token')
  19. // // config.headers['access_token'] = '4b3ed521-f509-48d9-b907-ab127479df41'
  20. // }
  21. // // 上传文件
  22. // if (config.url == '/apiSys/oss/upload') {
  23. // config.headers['Content-Type'] = 'multipart/form-data'
  24. // } else {
  25. // config.headers['Content-Type'] = 'application/json'
  26. // }
  27. return config
  28. },
  29. function (error) {
  30. return Promise.reject(error)
  31. }
  32. )
  33. //响应拦截
  34. httpRequest.interceptors.response.use(
  35. function (response) {
  36. const res = response.data
  37. // if (res.code == 201) {
  38. // this.ELEMENT.Message.error(res.msg)
  39. // if (!isLocalEnv()) {
  40. // // 跳登录页
  41. // window.location = window.location.origin
  42. // } else {
  43. // localEnvLogin()
  44. // }
  45. // }
  46. return response
  47. },
  48. function (error) {
  49. // this.ELEMENT.Message.error(error.message)
  50. return Promise.reject(error)
  51. }
  52. )
  53. function get(url, params,headers) {
  54. return new Promise((resolve, reject) => {
  55. httpRequest
  56. .get(url, { params: params,headers:headers })
  57. .then(res => {
  58. resolve(res.data)
  59. })
  60. .catch(err => {
  61. reject(err.data)
  62. })
  63. })
  64. }
  65. // qs.stringify(data)
  66. function post(url, data, config) {
  67. return new Promise((resolve, reject) => {
  68. httpRequest
  69. .post(url, data, config)
  70. .then(res => {
  71. resolve(res.data)
  72. })
  73. .catch(err => {
  74. reject(err)
  75. })
  76. })
  77. }
  78. function postFile(url, blob) {
  79. return new Promise((resolve, reject) => {
  80. let formdata = new FormData()
  81. formdata.append('multipartFile', blob)
  82. httpRequest
  83. .post(url, formdata)
  84. .then(res => {
  85. resolve(res.data)
  86. })
  87. .catch(err => {
  88. reject(err)
  89. })
  90. })
  91. }
  92. function localEnvLogin() {
  93. post(
  94. '/apiSys/login',
  95. {
  96. username: 'admin',
  97. password: '1q2w3e4r5t',
  98. },
  99. {
  100. transformRequest: [
  101. function (data, headers) {
  102. // 将请求数据转换成功 formdata 接收格式
  103. headers['Content-Type'] = 'application/x-www-form-urlencoded'
  104. return stringify(data)
  105. },
  106. ],
  107. }
  108. ).then(res => {
  109. localStorage.setItem('token', res.data.token || getQueryVariable('token'))
  110. })
  111. }
  112. // 将参数转换成功 formdata 接收格式
  113. function stringify(data) {
  114. let ret = ''
  115. for (const it in data) {
  116. ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  117. }
  118. ret = ret.substring(0, ret.lastIndexOf('&'))
  119. return ret
  120. }
  121. // 格式化日期
  122. function formatDate(time) {
  123. let date = time ? new Date(Number(time)) : new Date();
  124. let Y = date.getFullYear() + '-';
  125. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  126. let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
  127. let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
  128. let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
  129. let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  130. return Y + M + D + h + m + s;
  131. }
  132. function getQueryVariable(variable) {
  133. const query = window.location.search.substring(1); // 获取查询字符串,去除开头的 "?"
  134. const vars = query.split('&'); // 按 "&" 分割查询字符串
  135. for (let i = 0; i < vars.length; i++) {
  136. const pair = vars[i].split('='); // 按 "=" 分割键值对
  137. if (decodeURIComponent(pair[0]) === variable) {
  138. return decodeURIComponent(pair[1]);
  139. }
  140. }
  141. // 如果没有找到变量,则返回 null 或者默认值
  142. return null;
  143. }