移动端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

141 рядки
4.9 KiB

  1. import axios from 'axios'
  2. // import { Notification, MessageBox, Message } from 'element-ui'
  3. import { Notify, Dialog, Toast } from 'vant';
  4. import store from '@/store'
  5. import { getToken } from '@/utils/auth'
  6. import errorCode from '@/utils/errorCode'
  7. import Cookies from "js-cookie";
  8. axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
  9. // 创建axios实例
  10. const service = axios.create({
  11. // axios中请求配置有baseURL选项,表示请求URL公共部分
  12. baseURL: process.env.VUE_APP_BASE_API,
  13. // 超时
  14. timeout: 100000
  15. })
  16. // request拦截器
  17. service.interceptors.request.use(config => {
  18. Toast.loading({
  19. message: '加载中...',
  20. forbidClick: true,
  21. duration: 0
  22. });
  23. // 是否需要设置 token
  24. const isToken = (config.headers || {}).isToken === false
  25. if (getToken() && !isToken) {
  26. config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  27. }
  28. // get请求映射params参数
  29. if (config.method === 'get' && config.params) {
  30. let url = config.url + '?';
  31. for (const propName of Object.keys(config.params)) {
  32. const value = config.params[propName];
  33. var part = encodeURIComponent(propName) + "=";
  34. if (value !== null && typeof (value) !== "undefined") {
  35. if (typeof value === 'object') {
  36. for (const key of Object.keys(value)) {
  37. let params = propName + '[' + key + ']';
  38. var subPart = encodeURIComponent(params) + "=";
  39. url += subPart + encodeURIComponent(value[key]) + "&";
  40. }
  41. } else {
  42. url += part + encodeURIComponent(value) + "&";
  43. }
  44. }
  45. }
  46. url = url.slice(0, -1);
  47. config.params = {};
  48. config.url = url;
  49. }
  50. return config
  51. }, error => {
  52. console.log(error)
  53. Promise.reject(error)
  54. })
  55. // 响应拦截器
  56. service.interceptors.response.use(res => {
  57. Toast.clear();
  58. // 未设置状态码则默认成功状态
  59. const code = res.data.code || 200;
  60. // 获取错误信息
  61. const msg = errorCode[code] || res.data.msg || errorCode['default']
  62. if (code === 401) {
  63. Dialog.confirm({
  64. title: '系统提示',
  65. message: '登录状态已过期,您可以继续留在该页面,或者重新登录',
  66. confirmButtonText: '重新登录',
  67. cancelButtonText: '取消'
  68. })
  69. .then(() => {
  70. store.dispatch('LogOut').then(() => {
  71. try {
  72. let loginUrl = Cookies.get("_Login_url");
  73. if(loginUrl && loginUrl.indexOf("onlineHomeLogin") !== -1)
  74. {
  75. window.location.href = loginUrl;
  76. return;
  77. }
  78. } catch (e) { console.log(e); }
  79. if (window.location.href.indexOf('lawEnforcement') != -1) {
  80. window.location.href = '/lawEnforcement/login';
  81. } else if (window.location.href.indexOf('authenticRight') != -1) {
  82. window.location.href = '/authenticRight/login';
  83. } else if (window.location.href.indexOf('homesteadSurvey') != -1) {
  84. window.location.href = '/homesteadLogin';
  85. } else if (window.location.href.indexOf('onlineHome') != -1) {
  86. //window.location.href = '/onlineHomeLogin';
  87. window.location.href = '/zjdLogin';
  88. } else if (window.location.href.indexOf('yinnong') != -1) {
  89. window.location.href = '/yinnongLogin';
  90. } else if (window.location.href.indexOf('/sunVillage_info/') != -1) {
  91. window.location.href = '/sunVillage_info/login';
  92. } else if (window.location.href.indexOf('/sunVillage') != -1) {
  93. window.location.href = '/sunVillage/login';
  94. } else if (window.location.href.indexOf('/homestead/') != -1) {
  95. window.location.href = '/homestead/login';
  96. } else if (window.location.href.indexOf('yinnong') != -1){
  97. window.location.href = '/yinnongLogin';
  98. //window.location.href = '/zjdLogin';
  99. /*window.location.href = '/index';*/
  100. } else {
  101. window.location.href = '/agriculturalTrusteeship/login';
  102. }
  103. })
  104. })
  105. } else if (code === 500) {
  106. Dialog.alert({ type: 'warning', message: msg });
  107. return Promise.reject(new Error(msg))
  108. } else if (code !== 200) {
  109. Dialog.alert({ type: 'warning', message: msg });
  110. return Promise.reject('error')
  111. } else {
  112. return res.data
  113. }
  114. },
  115. error => {
  116. console.log('err' + error)
  117. let { message } = error;
  118. if (message == "Network Error") {
  119. message = "后端接口连接异常";
  120. }
  121. else if (message.includes("timeout")) {
  122. message = "系统接口请求超时";
  123. }
  124. else if (message.includes("Request failed with status code")) {
  125. message = "系统接口" + message.substr(message.length - 3) + "异常";
  126. }
  127. Toast.clear();
  128. // Message({
  129. // message: message,
  130. // type: 'error',
  131. // duration: 5 * 1000
  132. // })
  133. Dialog.alert({ type: 'warning', message: message });
  134. return Promise.reject(error)
  135. }
  136. )
  137. export default service