c7c145dfeb0aafc15c856aabc2e125edaf3c61ea.svn-base 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. import store from '@/store'
  4. import { VueAxios } from './axios'
  5. import router from '@/router/index'
  6. import { ACCESS_TOKEN, TENANT_ID } from "@/store/mutation-types"
  7. /**
  8. * 【指定 axios的 baseURL】
  9. * 如果手工指定 baseURL: '/jeecg-boot'
  10. * 则映射后端域名,通过 vue.config.js
  11. * @type {*|string}
  12. */
  13. let apiBaseUrl = window._CONFIG['domianURL'] || "/jeecg-boot";
  14. //console.log("apiBaseUrl= ",apiBaseUrl)
  15. // 创建 axios 实例
  16. const service = axios.create({
  17. //baseURL: '/jeecg-boot',
  18. baseURL: apiBaseUrl, // api base_url
  19. timeout: 9000 // 请求超时时间
  20. })
  21. const err = (error) => {
  22. if (error.response) {
  23. let data = error.response.data
  24. const token = Vue.ls.get(ACCESS_TOKEN)
  25. console.log("------异常响应------",token)
  26. console.log("------异常响应------",error.response.status)
  27. switch (error.response.status) {
  28. case 403:
  29. Vue.prototype.$Jnotification.error({ message: '系统提示', description: '拒绝访问',duration: 4})
  30. break
  31. case 500:
  32. console.log("------error.response------",error.response)
  33. // update-begin- --- author:liusq ------ date:20200910 ---- for:处理Blob情况----
  34. let type=error.response.request.responseType;
  35. if(type === 'blob'){
  36. blobToJson(data);
  37. break;
  38. }
  39. // update-end- --- author:liusq ------ date:20200910 ---- for:处理Blob情况----
  40. if(token && data.message.includes("Token失效")){
  41. // update-begin- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式,不直接跳转----
  42. if (/wxwork|dingtalk/i.test(navigator.userAgent)) {
  43. Vue.prototype.$Jmessage.loading('登录已过期,正在重新登陆', 0)
  44. } else {
  45. Vue.prototype.$Jmodal.error({
  46. title: '登录已过期',
  47. content: '很抱歉,登录已过期,请重新登录',
  48. okText: '重新登录',
  49. mask: false,
  50. onOk: () => {
  51. store.dispatch('Logout').then(() => {
  52. Vue.ls.remove(ACCESS_TOKEN)
  53. try {
  54. let path = window.document.location.pathname
  55. console.log('location pathname -> ' + path)
  56. if (path != '/' && path.indexOf('/user/login') == -1) {
  57. window.location.reload()
  58. }
  59. } catch (e) {
  60. window.location.reload()
  61. }
  62. })
  63. }
  64. })
  65. }
  66. // update-end- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式,不直接跳转----
  67. }
  68. break
  69. case 404:
  70. Vue.prototype.$Jnotification.error({ message: '系统提示', description:'很抱歉,资源未找到!',duration: 4})
  71. break
  72. case 504:
  73. Vue.prototype.$Jnotification.error({ message: '系统提示', description: '网络超时'})
  74. break
  75. case 401:
  76. Vue.prototype.$Jnotification.error({ message: '系统提示', description:'未授权,请重新登录',duration: 4})
  77. if (token) {
  78. store.dispatch('Logout').then(() => {
  79. setTimeout(() => {
  80. window.location.reload()
  81. }, 1500)
  82. })
  83. }
  84. break
  85. default:
  86. Vue.prototype.$Jnotification.error({
  87. message: '系统提示',
  88. description: data.message,
  89. duration: 4
  90. })
  91. break
  92. }
  93. } else if (error.message) {
  94. if (error.message.includes('timeout')) {
  95. Vue.prototype.$Jnotification.error({message: '系统提示', description: '网络超时'})
  96. } else {
  97. Vue.prototype.$Jnotification.error({message: '系统提示', description: error.message})
  98. }
  99. }
  100. return Promise.reject(error)
  101. };
  102. // request interceptor
  103. service.interceptors.request.use(config => {
  104. const token = Vue.ls.get(ACCESS_TOKEN)
  105. if (token) {
  106. config.headers[ 'X-Access-Token' ] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  107. }
  108. // update-begin--author:sunjianlei---date:20200723---for 如果当前在low-app环境,并且携带了appId,就向Header里传递appId
  109. const $route = router.currentRoute
  110. if ($route && $route.name && $route.name.startsWith('low-app') && $route.params.appId) {
  111. config.headers['X-Low-App-ID'] = $route.params.appId
  112. }
  113. // update-end--author:sunjianlei---date:20200723---for 如果当前在low-app环境,并且携带了appId,就向Header里传递appId
  114. //update-begin-author:taoyan date:2020707 for:多租户
  115. let tenantid = Vue.ls.get(TENANT_ID)
  116. if (!tenantid) {
  117. tenantid = 0;
  118. }
  119. config.headers[ 'tenant-id' ] = tenantid
  120. //update-end-author:taoyan date:2020707 for:多租户
  121. if(config.method=='get'){
  122. if(config.url.indexOf("sys/dict/getDictItems")<0){
  123. config.params = {
  124. _t: Date.parse(new Date())/1000,
  125. ...config.params
  126. }
  127. }
  128. }
  129. return config
  130. },(error) => {
  131. return Promise.reject(error)
  132. })
  133. // response interceptor
  134. service.interceptors.response.use((response) => {
  135. return response.data
  136. }, err)
  137. const installer = {
  138. vm: {},
  139. install (Vue, router = {}) {
  140. Vue.use(VueAxios, router, service)
  141. }
  142. }
  143. /**
  144. * Blob解析
  145. * @param data
  146. */
  147. function blobToJson(data) {
  148. let fileReader = new FileReader();
  149. let token = Vue.ls.get(ACCESS_TOKEN);
  150. fileReader.onload = function() {
  151. try {
  152. let jsonData = JSON.parse(this.result); // 说明是普通对象数据,后台转换失败
  153. console.log("jsonData",jsonData)
  154. if (jsonData.status === 500) {
  155. console.log("token----------》",token)
  156. if(token && jsonData.message.includes("Token失效")){
  157. Modal.error({
  158. title: '登录已过期',
  159. content: '很抱歉,登录已过期,请重新登录',
  160. okText: '重新登录',
  161. mask: false,
  162. onOk: () => {
  163. store.dispatch('Logout').then(() => {
  164. Vue.ls.remove(ACCESS_TOKEN)
  165. window.location.reload()
  166. })
  167. }
  168. })
  169. }
  170. }
  171. } catch (err) {
  172. // 解析成对象失败,说明是正常的文件流
  173. console.log("blob解析fileReader返回err",err)
  174. }
  175. };
  176. fileReader.readAsText(data)
  177. }
  178. export {
  179. installer as VueAxios,
  180. service as axios
  181. }