request.js 3.7 KB

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