request.js 7.4 KB

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