micro_request.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import axios from 'axios'
  2. import { Notification, MessageBox, Message } from 'element-ui'
  3. import store from '@/store'
  4. import { getToken } from '@/utils/token'
  5. import errorCode from '@/utils/errorCode'
  6. axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
  7. const service = axios.create({
  8. // axios中请求配置有baseURL选项,表示请求URL公共部分
  9. baseURL: process.env.VUE_APP_MicroSrvProxy_API,
  10. // 超时
  11. timeout: 60000,
  12. })
  13. // request拦截器
  14. service.interceptors.request.use(
  15. (config) => {
  16. config.headers['Tenant'] = process.env.VUE_APP_TENANT
  17. // 是否需要设置 token
  18. const isToken = (config.headers || {}).isToken === false
  19. if (getToken() && !isToken) {
  20. config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  21. }
  22. return config
  23. },
  24. (error) => {
  25. console.log(error)
  26. Promise.reject(error)
  27. }
  28. )
  29. // 响应拦截器
  30. service.interceptors.response.use(
  31. (res) => {
  32. if (res.request.responseType == 'blob') {
  33. // 判断是否是文件流
  34. let fileReader = new FileReader()
  35. fileReader.readAsText(res.data)
  36. fileReader.onload = function () {
  37. try {
  38. let jsonData = JSON.parse(this.result) // 解析成功说明是普通对象数据,后端返回的是文件上传的错误信息
  39. res.data = jsonData
  40. processResponse(res)
  41. return
  42. } catch (err) {
  43. // 解析成对象失败,说明是文件流
  44. downLoadBlobFile(res)
  45. return
  46. }
  47. }
  48. }
  49. // 常规响应处理
  50. return processResponse(res)
  51. },
  52. (error) => {
  53. console.log('err' + error)
  54. Message({
  55. message: error.message,
  56. type: 'error',
  57. duration: 5 * 1000,
  58. })
  59. return Promise.reject(error)
  60. }
  61. )
  62. service.postRequest = function postRequest(basePath, srvName, funcName, data) {
  63. if (data == undefined) {
  64. let nullParam = {
  65. nullparam: 0,
  66. }
  67. data = nullParam
  68. }
  69. // console.log( basePath ,' basePath ')
  70. var base_Path = ''
  71. if (basePath == process.env.VUE_APP_FOSHAN_PATH) {
  72. base_Path = process.env.VUE_APP_MicroSrvProxy_foshan_API + basePath
  73. } else if (
  74. basePath == process.env.VUE_APP_AdminPath ||
  75. process.env.VUE_APP_ParentPath
  76. ) {
  77. base_Path = process.env.VUE_APP_MicroSrvProxy_API + basePath
  78. }
  79. // console.log(base_Path, ' base_Path ')
  80. return service.request({
  81. url: base_Path,
  82. method: 'post',
  83. headers: {
  84. 'Content-Type': 'application/rpcx',
  85. 'X-RPCX-SerializeType': '1',
  86. 'X-RPCX-ServicePath': srvName,
  87. 'X-RPCX-ServiceMethod': funcName,
  88. },
  89. data: data,
  90. })
  91. }
  92. // 发出请求并要求把客户端信息传输给后端(IP和User-Agent)
  93. service.postRequestWithClientInfo = function postRequest(
  94. basePath,
  95. srvName,
  96. funcName,
  97. data
  98. ) {
  99. if (data == undefined) {
  100. let nullParam = { nullparam: 0 }
  101. data = nullParam
  102. }
  103. return service.request({
  104. url: basePath,
  105. method: 'post',
  106. headers: {
  107. 'Content-Type': 'application/rpcx',
  108. 'X-RPCX-SerializeType': '1',
  109. 'X-RPCX-ServicePath': srvName,
  110. 'X-RPCX-ServiceMethod': funcName,
  111. 'X-RPCX-Meta': 'need_clint_Info=1',
  112. },
  113. data: data,
  114. })
  115. }
  116. // Excel文件下载(服务端生成文件流)
  117. service.downloadExcel = function downloadExcel(
  118. basePath,
  119. srvName,
  120. funcName,
  121. data
  122. ) {
  123. if (data == undefined) {
  124. let nullParam = {
  125. nullparam: 0,
  126. }
  127. data = nullParam
  128. }
  129. var base_Path = ''
  130. if (basePath == process.env.VUE_APP_FOSHAN_PATH) {
  131. base_Path = process.env.VUE_APP_MicroSrvProxy_foshan_API + basePath
  132. } else if (basePath == process.env.VUE_APP_AdminPath) {
  133. base_Path = process.env.VUE_APP_MicroSrvProxy_API + basePath
  134. }
  135. service.request({
  136. url: base_Path,
  137. method: 'post',
  138. responseType: 'blob',
  139. headers: {
  140. 'Content-Type': 'application/rpcx',
  141. 'X-RPCX-SerializeType': '1',
  142. 'X-RPCX-ServicePath': srvName,
  143. 'X-RPCX-ServiceMethod': funcName,
  144. },
  145. data: data,
  146. })
  147. }
  148. function processResponse(res) {
  149. // 未设置状态码则默认成功状态
  150. const code = res.data.code || 200
  151. // 获取错误信息
  152. const message = errorCode[code] || res.data.msg || errorCode['default']
  153. if (code === 401) {
  154. MessageBox.confirm(
  155. '登录状态已过期,您可以继续留在该页面,或者重新登录',
  156. '系统提示',
  157. {
  158. confirmButtonText: '重新登录',
  159. cancelButtonText: '取消',
  160. type: 'warning',
  161. }
  162. ).then(() => {
  163. store.dispatch('user/logout').then(() => {
  164. if (process.env.SSO_LOGIN) {
  165. sessionStorage.removeItem('SSO_Token')
  166. location.href = process.env.SSO_HREF
  167. } else {
  168. location.reload()
  169. }
  170. })
  171. })
  172. } else if (code === 500) {
  173. Message({
  174. message: message,
  175. type: 'error',
  176. })
  177. return Promise.reject(new Error(message))
  178. } else if (code !== 200) {
  179. Notification.error({
  180. title: message,
  181. })
  182. return Promise.reject('error')
  183. } else {
  184. // if (res.data.msg) {
  185. // Message({
  186. // message: res.data.msg,
  187. // type: 'success'
  188. // })
  189. // }
  190. return res.data
  191. }
  192. }
  193. function downLoadBlobFile(res, mimeType) {
  194. if (mimeType == undefined) {
  195. mimeType = 'application/vnd.ms-excel'
  196. }
  197. const aLink = document.createElement('a')
  198. var blob = new Blob([res.data], {
  199. type: mimeType,
  200. })
  201. // //从response的headers中获取filename, 后端response.setHeader('Content-disposition', 'attachment filename=xxxx.docx') 设置的文件名
  202. var patt = new RegExp('filename=([^]+\\.[^\\.]+)*')
  203. var contentDisposition = decodeURI(
  204. res.headers['content-disposition'] || res.headers['Content-Disposition']
  205. )
  206. var result = patt.exec(contentDisposition)
  207. var fileName = 'data.xlsx'
  208. if (result != undefined) {
  209. fileName = result[1]
  210. fileName = decodeURI(escape(fileName))
  211. fileName = fileName.replace(/'/g, '')
  212. }
  213. aLink.href = URL.createObjectURL(blob)
  214. aLink.setAttribute('download', fileName) // 设置下载文件名称
  215. document.body.appendChild(aLink)
  216. aLink.click()
  217. document.body.appendChild(aLink)
  218. }
  219. export default service