index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import axios from "axios";
  2. import {timeout, baseUrl, baseUrl_Address,withCredentials} from "@/service/config";
  3. import {UserInfo} from "@/unit/UserInfo";
  4. import {showMessage} from "@/unit/element-ui/tip";
  5. import {closeShowLoading, showLoading} from "@/data/element-loading/element-loader";
  6. import router from "@/router";
  7. const requestInstance = axios.create({
  8. baseURL:import.meta.env.VITE_BASE_API,
  9. timeout,
  10. method:"post",
  11. headers:{
  12. Systemtype:'ebb42096345340359741dcb9c039f15c'
  13. },
  14. withCredentials,
  15. // headers:{
  16. //
  17. // "Content-Type":"application/x-www-form-urlencoded"
  18. // },
  19. // transformRequest:[(data) => {
  20. //
  21. // if(data) {
  22. // let params = new FormData()
  23. // let keys = Object.keys(data)
  24. // keys.forEach(key => {
  25. // params.append(key,data[key])
  26. // })
  27. // return params
  28. // }
  29. // return data
  30. // }
  31. // ]
  32. })
  33. export function setConfigInstance(config) {
  34. let instance = axios.create({
  35. baseURL:baseUrl,
  36. timeout,
  37. // method:"post",
  38. ...config
  39. })
  40. return instance
  41. }
  42. requestInstance.interceptors.request.use((config) => {
  43. if (!config.url.includes('/user/updateLoginLasttime')){
  44. showLoading({
  45. fullscreen:true
  46. })
  47. }
  48. let reg = /appkey=.*/
  49. //let isMatch = reg.test(config.url.split('?')[1])
  50. let isMatch = config.data.hasOwnProperty('appkey');
  51. return {
  52. ...config,
  53. data:new UserInfo().GetUserKeyCode()?(isMatch?config.data:{...config.data,appkey:new UserInfo().GetUserKeyCode()}):config.data
  54. }
  55. },(error) => {
  56. return new Error(error)
  57. })
  58. requestInstance.interceptors.response.use(function (res) {
  59. if (!res.config.url.includes('/user/updateLoginLasttime')){
  60. closeShowLoading()
  61. }
  62. let isAppkeyIsFailed = false
  63. try {
  64. if (+res.data.msg[0].code === 401){
  65. isAppkeyIsFailed= true
  66. }else {
  67. isAppkeyIsFailed= false
  68. }
  69. }catch {
  70. isAppkeyIsFailed = false
  71. }
  72. if (isAppkeyIsFailed){
  73. showMessage({
  74. type:'warning',
  75. message:'登录验证过期,请重新登录'
  76. })
  77. router.replace('/login')
  78. router.go(0)
  79. }
  80. return res.data
  81. },function (error) {
  82. closeShowLoading()
  83. return new Error(error)
  84. })
  85. export {
  86. requestInstance
  87. }
  88. const requestInstance_Get = axios.create({
  89. baseURL:import.meta.env.VITE_BASE_API,
  90. timeout,
  91. method:"get",
  92. withCredentials,
  93. responseType: "blob",
  94. headers:{
  95. Systemtype:'ebb42096345340359741dcb9c039f15c'
  96. },
  97. // headers:{
  98. //
  99. // "Content-Type":"application/x-www-form-urlencoded"
  100. // },
  101. // transformRequest:[(data) => {
  102. //
  103. // if(data) {
  104. // let params = new FormData()
  105. // let keys = Object.keys(data)
  106. // keys.forEach(key => {
  107. // params.append(key,data[key])
  108. // })
  109. // return params
  110. // }
  111. // return data
  112. // }
  113. // ]
  114. })
  115. requestInstance_Get.interceptors.request.use((config) => {
  116. return config
  117. },(error) => {
  118. return new Error(error)
  119. })
  120. requestInstance_Get.interceptors.response.use(function (res) {
  121. return res.data
  122. },function (error) {
  123. return new Error(error)
  124. })
  125. export {
  126. requestInstance_Get
  127. }
  128. const requestInstance_Html = axios.create({
  129. baseURL:import.meta.env.VITE_BASE_API,
  130. timeout,
  131. method:"get",
  132. withCredentials,
  133. headers:{
  134. Systemtype:'ebb42096345340359741dcb9c039f15c'
  135. },
  136. // headers:{
  137. //
  138. // "Content-Type":"application/x-www-form-urlencoded"
  139. // },
  140. // transformRequest:[(data) => {
  141. //
  142. // if(data) {
  143. // let params = new FormData()
  144. // let keys = Object.keys(data)
  145. // keys.forEach(key => {
  146. // params.append(key,data[key])
  147. // })
  148. // return params
  149. // }
  150. // return data
  151. // }
  152. // ]
  153. })
  154. requestInstance_Html.interceptors.request.use((config) => {
  155. return config
  156. },(error) => {
  157. return new Error(error)
  158. })
  159. requestInstance_Html.interceptors.response.use(function (res) {
  160. return res
  161. },function (error) {
  162. return new Error(error)
  163. })
  164. export {
  165. requestInstance_Html
  166. }