移动端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

154 lines
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. window.location.href = loginUrl;
  75. return;
  76. }
  77. } catch (e) { console.log(e); }
  78. // 农业执法
  79. if (window.location.href.indexOf('/lawEnforcement') != -1) {
  80. window.location.href = '/lawEnforcement/login';
  81. return;
  82. }
  83. // 确权回头看
  84. if (window.location.href.indexOf('/authenticRight') != -1) {
  85. window.location.href = '/authenticRight/login';
  86. return;
  87. }
  88. // 宅基地调查
  89. if (window.location.href.indexOf('/homesteadSurvey') != -1) {
  90. window.location.href = '/homesteadLogin';
  91. return;
  92. }
  93. // 事项审批
  94. if (window.location.href.indexOf('/yinnong') != -1) {
  95. window.location.href = '/yinnongLogin';
  96. return;
  97. }
  98. // 阳光村务
  99. if (window.location.href.indexOf('/sunVillage_info/') != -1) {
  100. window.location.href = '/sunVillage_info/login';
  101. return;
  102. }
  103. // 两清三化
  104. if (window.location.href.indexOf('/homestead/') != -1) {
  105. window.location.href = '/homestead/login';
  106. return;
  107. }
  108. // 生产托管
  109. if (window.location.href.indexOf('/agriculturalTrusteeship') != -1){
  110. window.location.href = '/agriculturalTrusteeship/login';
  111. return;
  112. }
  113. // 产权交易
  114. window.location.href = '/login';
  115. })
  116. })
  117. } else if (code === 500) {
  118. Dialog.alert({ type: 'warning', message: msg });
  119. return Promise.reject(new Error(msg))
  120. } else if (code !== 200) {
  121. Dialog.alert({ type: 'warning', message: msg });
  122. return Promise.reject('error')
  123. } else {
  124. return res.data
  125. }
  126. },
  127. error => {
  128. console.log('err' + error)
  129. let { message } = error;
  130. if (message == "Network Error") {
  131. message = "后端接口连接异常";
  132. }
  133. else if (message.includes("timeout")) {
  134. message = "系统接口请求超时";
  135. }
  136. else if (message.includes("Request failed with status code")) {
  137. message = "系统接口" + message.substr(message.length - 3) + "异常";
  138. }
  139. Toast.clear();
  140. // Message({
  141. // message: message,
  142. // type: 'error',
  143. // duration: 5 * 1000
  144. // })
  145. Dialog.alert({ type: 'warning', message: message });
  146. return Promise.reject(error)
  147. }
  148. )
  149. export default service