httputils.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import axios from 'axios'
  2. import store from '@/store'
  3. import router from '@/router'
  4. // import { MessageBox } from 'element-ui'
  5. var CancelToken = axios.CancelToken
  6. var cancel
  7. // 创建axios实例
  8. const axiosservice = axios.create({
  9. timeout: 60 * 1000, // 请求超时时间
  10. retry: 4, //重新请求次数
  11. retryDelay: 1000, //重新请求的间隔
  12. credentials: true, // 接口每次请求会跨域携带cookie
  13. cancelToken: new CancelToken(function executor(c) {
  14. // executor 函数接收一个 cancel 函数作为参数
  15. cancel = c
  16. }),
  17. })
  18. axiosservice.interceptors.request.use(
  19. (config) => {
  20. config.withCredentials = true // 允许携带token ,这个是解决跨域产生的相关问题
  21. let isPreview = null
  22. let token = store.getters['ssoToken']
  23. if (config.url.indexOf('/data') > -1) {
  24. config.headers = {
  25. 'sso-token': token,
  26. // isPreview, //默认false,当预览开启的时候是true
  27. }
  28. }
  29. return config
  30. },
  31. (error) => {
  32. return Promise.reject(error)
  33. }
  34. )
  35. axiosservice.interceptors.response.use(
  36. function(res) {
  37. //在这里对返回的数据进行处理
  38. let resp = res.data
  39. // if (resp.result === 'unauthc') {
  40. // store.commit('logined', false)
  41. // MessageBox.confirm('未登陆或登陆信息已失效, 是否重新登陆?', '提示', {
  42. // confirmButtonText: '确定',
  43. // cancelButtonText: '取消',
  44. // type: 'error',
  45. // })
  46. // .then((resp) => {
  47. // window.location.reload()
  48. // })
  49. // .catch((error) => {
  50. // console.log('asdfasdfsad')
  51. // })
  52. // } else if (resp.error == 'Unauthorized') {
  53. // MessageBox.alert('无权操作', {
  54. // title: '警告',
  55. // type: 'error',
  56. // })
  57. // }
  58. return res
  59. },
  60. function(err) {
  61. if (err.response.status == 401) {
  62. // store.commit('SETISREQUESTtAUTH', 'err')
  63. // router.push({
  64. // path: '404',
  65. // })
  66. }
  67. return Promise.reject(err)
  68. }
  69. )
  70. /* 下载方法 */
  71. function downFile(blob, fileName) {
  72. // 非IE下载
  73. if ('download' in document.createElement('a')) {
  74. let link = document.createElement('a')
  75. link.href = window.URL.createObjectURL(blob) // 创建下载的链接
  76. link.download = fileName // 下载后文件名
  77. link.style.display = 'none'
  78. document.body.appendChild(link)
  79. link.click() // 点击下载
  80. window.URL.revokeObjectURL(link.href) // 释放掉blob对象
  81. document.body.removeChild(link) // 下载完成移除元素
  82. } else {
  83. // IE10+下载
  84. window.navigator.msSaveBlob(blob, fileName)
  85. }
  86. }
  87. export default {
  88. //获取cookie
  89. getCookie(name) {
  90. var arr,
  91. reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
  92. if ((arr = document.cookie.match(reg))) {
  93. return unescape(arr[2])
  94. } else {
  95. /* 如果没有参数,那么就获取本域下的所有cookie */
  96. return document.cookie
  97. }
  98. },
  99. async getJson(url, params) {
  100. // console.log(url, params)
  101. try {
  102. let response = await axiosservice({
  103. url,
  104. params,
  105. method: 'get',
  106. })
  107. return response.data
  108. } catch (err) {
  109. throw err
  110. }
  111. },
  112. async postJson(url, data) {
  113. try {
  114. let response = await axiosservice({
  115. url,
  116. data,
  117. method: 'post',
  118. })
  119. return response.data
  120. } catch (err) {
  121. throw err
  122. }
  123. },
  124. async fetchJson(url, params, data) {
  125. try {
  126. let response = await axiosservice({
  127. url,
  128. params,
  129. data,
  130. method: 'post',
  131. })
  132. return response
  133. } catch (err) {
  134. throw err
  135. }
  136. },
  137. async postupload(url, data) {
  138. try {
  139. let response = await axiosservice({
  140. url,
  141. data,
  142. method: 'post',
  143. headers: {
  144. 'Content-Type': 'multipart/form-data',
  145. },
  146. })
  147. return response.data
  148. } catch (err) {
  149. throw err
  150. }
  151. },
  152. download(url, requestData) {
  153. // 响应类型:arraybuffer, blob
  154. axiosservice
  155. .post(url, requestData, {
  156. responseType: 'blob',
  157. })
  158. .then((resp) => {
  159. let headers = resp.headers
  160. let contentType = headers['content-type']
  161. if (!resp.data) {
  162. console.error('响应异常:', resp)
  163. return false
  164. } else {
  165. //console.log("下载文件:", resp)
  166. const blob = new Blob([resp.data], {
  167. type: contentType,
  168. })
  169. const contentDisposition = resp.headers['content-disposition']
  170. let fileName = 'unknown'
  171. if (contentDisposition) {
  172. fileName = window.decodeURI(resp.headers['content-disposition'].split('=')[1])
  173. }
  174. downFile(blob, fileName)
  175. }
  176. })
  177. .catch(function(error) {})
  178. },
  179. axios: axiosservice,
  180. }