request.js 4.0 KB

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