axios.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * @Author : yuanrunwei
  3. * @Date : 2020-05-18 12:05:37
  4. * @LastEditors: XuTongZhang
  5. * @LastEditTime: 2020-08-12 11:55:58
  6. * @FilePath : \batterycloud-admin\src\common\api\axios.js
  7. */
  8. import axios from 'axios'
  9. import vue from '../../main'
  10. import { Message, Loading } from 'element-ui'
  11. // import { all } from 'core-js/fn/promise'
  12. let api = ''
  13. let loading
  14. const whiteList = ['test', 'production']
  15. const allowList = [ // 文件请求路径数组
  16. '/admin/export/export2Web'
  17. ]
  18. const service = axios.create({
  19. baseURL: process.env.VUE_APP_API + '/admin',
  20. // baseURL: 'http://47.100.59.52:7785',
  21. timeout: 90000
  22. })
  23. service.interceptors.request.use(
  24. config => {
  25. // if (allowList.indexOf(config.url) === -1) {
  26. let data = {
  27. 'from': 0,
  28. 'page': 1,
  29. 'rows': 10,
  30. 'companyId': 2 || localStorage.getItem('managerid') - 0,
  31. 'sidx': '',
  32. 'sord': '',
  33. 'token': localStorage.getItem('token'),
  34. 'reqdata': {}
  35. }
  36. config.data = Object.assign(data, config.data)
  37. // }
  38. loading = Loading.service({
  39. lock: true,
  40. text: 'Loading',
  41. spinner: 'el-icon-loading',
  42. background: 'rgba(0, 0, 0, 0.7)'
  43. })
  44. if (allowList.includes(config.url)) {
  45. // config.headers['Content-Type'] = 'application/vnd.ms-excel'
  46. // console.log(config)
  47. config.responseType = 'blob'
  48. }
  49. config.headers['Authorization'] = localStorage.getItem('token')
  50. api = config.url
  51. return config
  52. },
  53. error => {
  54. Promise.reject(error)
  55. }
  56. )
  57. service.interceptors.response.use(
  58. res => {
  59. console.log(res)
  60. let a = false
  61. return new Promise((resolve, reject) => {
  62. allowList.forEach(item => {
  63. if (res.config.url === process.env.VUE_APP_API + item) {
  64. // resolve(res.data)
  65. const content = res.data
  66. const blob = new Blob([content])
  67. console.log(blob)
  68. const fileName = '人才库.xls'
  69. if ('download' in document.createElement('a')) { // 非IE下载
  70. const elink = document.createElement('a')
  71. elink.download = fileName
  72. elink.style.display = 'none'
  73. elink.href = URL.createObjectURL(blob)
  74. document.body.appendChild(elink)
  75. elink.click()
  76. URL.revokeObjectURL(elink.href) // 释放URL 对象
  77. document.body.removeChild(elink)
  78. } else { // IE10+下载
  79. navigator.msSaveBlob(blob, fileName)
  80. }
  81. a = true
  82. }
  83. })
  84. if (a) {
  85. loading.close()
  86. return
  87. }
  88. if (res.data.state === 206) {
  89. localStorage.clear()
  90. // sessionStorage.claer()
  91. vue.$cookies.removeCookies()
  92. loading.close()
  93. setTimeout(() => {
  94. location.reload()
  95. }, 100)
  96. }
  97. // if (allowList.indexOf(api) !== -1) {
  98. // loading.close()
  99. // resolve(res.data)
  100. // } else {
  101. if (res.status === 200) {
  102. if (res.data.state !== 100) {
  103. Message({
  104. message: res.data.msg,
  105. type: 'error',
  106. duration: 5 * 1000
  107. })
  108. loading.close()
  109. reject(res.data.msg)
  110. } else {
  111. resolve(res.data)
  112. loading.close()
  113. }
  114. } else {
  115. loading.close()
  116. Message({
  117. message: res.statusText,
  118. type: 'error',
  119. duration: 5 * 1000
  120. })
  121. if (res.status > 100 && res.status < 200) {
  122. if (whiteList.indexOf(process.env.NODE_ENV) !== -1) {
  123. let url = `http://monitor.info666.com/monitor/alert?projectAbbr=batterycloud-test&errorMsg=接口:${api}(${res.statusText})&logPath=前端开发环境(${process.env.NODE_ENV})`
  124. axios.get(url)
  125. loading.close()
  126. }
  127. }
  128. loading.close()
  129. reject(res)
  130. }
  131. // }
  132. })
  133. },
  134. error => {
  135. loading.close()
  136. Message({
  137. message: '请求异常' + error,
  138. type: 'error',
  139. duration: 5 * 1000
  140. })
  141. }
  142. )
  143. export default service