request.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /** axios封装
  2. * 请求拦截、相应拦截、错误统一处理
  3. */
  4. let env = 'prd'
  5. getUrlParams()
  6. const httpRequest = axios.create({
  7. // baseURL: 'http://192.168.2.155:8080', // 李瑞投前
  8. // baseURL: 'http://192.168.2.111:8080', // 苏文投后
  9. // baseURL: 'http://192.168.2.147:8080', // 旭东中间大屏
  10. baseURL: (env == 'SIT' || env == 'sit') ? 'http://172.21.3.68:8081' : (env == 'UAT' || env == 'uat') ? 'http://172.21.3.149:8081' : 'http://172.21.3.149:8081',
  11. // baseURL: 'http://172.21.3.68:8081', // SIT
  12. // baseURL: 'http://172.21.3.149:8081', // UAT
  13. timeout: 3 * 60 * 1000, // 3分钟等待
  14. withCredentials: true, // 携带cookie
  15. })
  16. // 请求拦截器
  17. httpRequest.interceptors.request.use(
  18. function (config) {
  19. if (config.data && config.data.access_token) {
  20. config.headers['accessToken'] = config.data.access_token
  21. }
  22. if (config.params && config.params.access_token) {
  23. config.headers['accessToken'] = config.params.access_token
  24. }
  25. // if (!isLocalEnv()) {
  26. // if (getQueryVariable('token')) {
  27. // config.headers['access_token'] = getQueryVariable('token')
  28. // }
  29. // } else {
  30. // config.headers['access_token'] = localStorage.getItem('token')
  31. // // config.headers['access_token'] = '4b3ed521-f509-48d9-b907-ab127479df41'
  32. // }
  33. // // 上传文件
  34. // if (config.url == '/apiSys/oss/upload') {
  35. // config.headers['Content-Type'] = 'multipart/form-data'
  36. // } else {
  37. // config.headers['Content-Type'] = 'application/json'
  38. // }
  39. return config
  40. },
  41. function (error) {
  42. return Promise.reject(error)
  43. }
  44. )
  45. // 获取地址栏参数
  46. function getUrlParams () {
  47. let url = window.location.href
  48. // 通过 ? 分割获取后面的参数字符串
  49. let urlStr = url.split('?')[1]
  50. if (!urlStr) {
  51. return
  52. }
  53. // 创建空对象存储参数
  54. let obj = {};
  55. // 再通过 & 将每一个参数单独分割出来
  56. let paramsArr = urlStr.split('&')
  57. for (let i = 0, len = paramsArr.length; i < len; i++) {
  58. // 再通过 = 将每一个参数分割为 key:value 的形式
  59. let arr = paramsArr[i].split('=')
  60. obj[arr[0]] = arr[1];
  61. }
  62. obj.env && (env = obj.env)
  63. }
  64. //响应拦截
  65. httpRequest.interceptors.response.use(
  66. function (response) {
  67. const res = response.data
  68. // if (res.code == 201) {
  69. // this.ELEMENT.Message.error(res.msg)
  70. // if (!isLocalEnv()) {
  71. // // 跳登录页
  72. // window.location = window.location.origin
  73. // } else {
  74. // localEnvLogin()
  75. // }
  76. // }
  77. return response
  78. },
  79. function (error) {
  80. this.ELEMENT.Message.error(error.message)
  81. return Promise.reject(error)
  82. }
  83. )
  84. function get (url, params) {
  85. return new Promise((resolve, reject) => {
  86. httpRequest
  87. .get(url, { params: params })
  88. .then(res => {
  89. resolve(res.data)
  90. })
  91. .catch(err => {
  92. reject(err.data)
  93. })
  94. })
  95. }
  96. // qs.stringify(data)
  97. function post (url, data, config) {
  98. return new Promise((resolve, reject) => {
  99. httpRequest
  100. .post(url, data, config)
  101. .then(res => {
  102. resolve(res.data)
  103. })
  104. .catch(err => {
  105. reject(err)
  106. })
  107. })
  108. }
  109. function postFile (url, blob) {
  110. return new Promise((resolve, reject) => {
  111. let formdata = new FormData()
  112. formdata.append('multipartFile', blob)
  113. httpRequest
  114. .post(url, formdata)
  115. .then(res => {
  116. resolve(res.data)
  117. })
  118. .catch(err => {
  119. reject(err)
  120. })
  121. })
  122. }
  123. function localEnvLogin () {
  124. post(
  125. '/apiSys/login',
  126. {
  127. username: 'admin',
  128. password: '1q2w3e4r5t',
  129. },
  130. {
  131. transformRequest: [
  132. function (data, headers) {
  133. // 将请求数据转换成功 formdata 接收格式
  134. headers['Content-Type'] = 'application/x-www-form-urlencoded'
  135. return stringify(data)
  136. },
  137. ],
  138. }
  139. ).then(res => {
  140. localStorage.setItem('token', res.data.token || getQueryVariable('token'))
  141. })
  142. }
  143. // 将参数转换成功 formdata 接收格式
  144. function stringify (data) {
  145. let ret = ''
  146. for (const it in data) {
  147. ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  148. }
  149. ret = ret.substring(0, ret.lastIndexOf('&'))
  150. return ret
  151. }
  152. // 格式化日期
  153. function formatDate (time) {
  154. let date = time ? new Date(Number(time)) : new Date();
  155. let Y = date.getFullYear() + '-';
  156. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  157. let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
  158. let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
  159. let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
  160. let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  161. return Y + M + D + h + m + s;
  162. }