request.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import axios from 'axios'
  2. import { getToken, deleteToken } from '@/utils/auth'
  3. import { blobToJson } from '@/utils'
  4. import { checkToken } from '@/api/system'
  5. import {
  6. instanceWorkflow
  7. } from '@/api/workflow'
  8. import route from '@/router'
  9. import qs from '@/utils/qs'
  10. import Vue from 'vue'
  11. import store from '@/store'
  12. // create an axios instance
  13. const service = axios.create({
  14. // baseURL: 'http://192.168.3.162:7654',
  15. baseURL: process.env.VUE_APP_BASE_API,
  16. // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  17. // withCredentials: true, // send cookies when cross-domain requests
  18. timeout: 120000 // request timeout
  19. })
  20. // request interceptor
  21. // 发送请求拦截器
  22. service.interceptors.request.use(
  23. config => {
  24. // do something before request is sent
  25. // if (getToken()) {
  26. // config.headers.token = getToken()
  27. // }
  28. config.headers.token = getToken()
  29. return config
  30. },
  31. error => {
  32. // do something with request error
  33. // console.error(error) // for debug
  34. return Promise.reject(error)
  35. }
  36. )
  37. // response interceptor
  38. // 请求返回之后的拦截器
  39. service.interceptors.response.use(
  40. async response => {
  41. const res = response.data
  42. if (response.request.responseType === 'blob') {
  43. // 返回的文件流当报错时转化成json
  44. if (response.headers['content-type'] === 'application/json') {
  45. try {
  46. const result = await blobToJson(res)
  47. return Promise.reject(result.msg)
  48. } catch (error) {
  49. return Promise.reject(error)
  50. }
  51. }
  52. const name = response.headers['content-disposition']
  53. // console.log(name)
  54. return {
  55. data: res,
  56. name: name ? decodeURI(name.replace('attachment;filename=', '')) : '未命名'
  57. }
  58. }
  59. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  60. if ([50008, 50012, 50014, 50015, 402000, 401].includes(res.code)) {
  61. // Vue.prototype.$message.error('登陆过期,请重新登陆')
  62. const str = '登陆过期,请重新登陆'
  63. checkToken().catch(error => Vue.prototype.$message.error(error))
  64. return Promise.reject(str)
  65. }
  66. if (res.code === 302) {
  67. // const str = '登陆过期,返回登录'
  68. deleteToken()
  69. const url = res.data?.redirectUrl === 'local' ? `/login-local?redirect=${route.history.current.fullPath}` : (res.data?.redirectUrl || '/login-local')
  70. // setTimeout(() => {
  71. // Vue.prototype.$message.error(str)
  72. // })
  73. window.location.href = url
  74. return
  75. }
  76. // 登录验证码过期
  77. if (res.code === 60902) {
  78. return Promise.reject(res)
  79. }
  80. if (res.code === 72000) {
  81. // 审核拦截
  82. try {
  83. await Vue.prototype.$confirm('当前操作需要执行审核流程,是否继续', '提示')
  84. const obj = store.getters.workOrder
  85. await instanceWorkflow({
  86. ...res.data,
  87. createUserPageRoute: obj[2] ?? '/work-order/my-order',
  88. approvePageRoute: obj[1] ?? '/work-order/approval'
  89. })
  90. const str = '已发起工作流,请等待审核'
  91. const fn = Vue.prototype.$message.success
  92. Vue.prototype.$message.success = () => {
  93. fn(str)
  94. }
  95. setTimeout(() => {
  96. Vue.prototype.$message.success = fn
  97. })
  98. return Promise.resolve(str)
  99. } catch (error) {
  100. if (error === 'cancel') {
  101. const fn = Vue.prototype.$message.error
  102. Vue.prototype.$message.error = () => {}
  103. setTimeout(() => {
  104. Vue.prototype.$message.error = fn
  105. })
  106. const str = '取消操作'
  107. return Promise.reject(str)
  108. }
  109. return Promise.reject(error)
  110. }
  111. }
  112. if (res.code !== 20000 && res.code !== 200) {
  113. if (res.data && Object.keys(res.data).length) {
  114. return Promise.reject(res)
  115. }
  116. return Promise.reject(res.msg?.error || res.msg || `Error ${res.code}`)
  117. }
  118. return res
  119. },
  120. error => {
  121. // error = error.message || error
  122. return Promise.reject(error.message || error)
  123. }
  124. )
  125. // 请求方法
  126. const http = {
  127. post (url, params, config = {}) {
  128. return service.post(url, params, {
  129. transformRequest: [(params) => {
  130. return JSON.stringify(params)
  131. }],
  132. headers: {
  133. 'Content-Type': 'application/json'
  134. },
  135. ...config
  136. })
  137. },
  138. get (url, params) {
  139. return service.get(url, {
  140. params: params,
  141. paramsSerializer: (params) => {
  142. return qs.stringify(params)
  143. }
  144. })
  145. },
  146. delete (url, params) {
  147. return service.delete(url)
  148. },
  149. put (url, params) {
  150. return service.put(url, params)
  151. },
  152. formData (url, params) {
  153. return service.post(url, params, {
  154. timeout: 600000,
  155. headers: {
  156. 'Content-Type': 'application/x-www-form-urlencoded'
  157. }
  158. })
  159. },
  160. upload (url, params, options = {}) {
  161. return service.post(url, params, {
  162. timeout: 600000,
  163. headers: {
  164. 'Content-Type': 'multipart/form-data'
  165. },
  166. ...options
  167. })
  168. },
  169. download (url, params) {
  170. return service.post(url, params, {
  171. timeout: 10000,
  172. headers: {
  173. 'Content-Type': 'application/json'
  174. },
  175. responseType: 'blob'
  176. })
  177. }
  178. }
  179. export default http