移动端
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

175 líneas
6.3 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. let requestInterceptor;
  17. if(requestInterceptor === undefined){
  18. // request拦截器
  19. requestInterceptor = service.interceptors.request.use(config => {
  20. // Toast.loading({
  21. // message: '加载中...',
  22. // forbidClick: true,
  23. // duration: 0
  24. // });
  25. // 是否需要设置 token
  26. const isToken = (config.headers || {}).isToken === false
  27. if (getToken() && !isToken) {
  28. config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  29. }
  30. // get请求映射params参数
  31. if (config.method === 'get' && config.params) {
  32. let url = config.url + '?';
  33. for (const propName of Object.keys(config.params)) {
  34. const value = config.params[propName];
  35. var part = encodeURIComponent(propName) + "=";
  36. if (value !== null && typeof (value) !== "undefined") {
  37. if (typeof value === 'object') {
  38. for (const key of Object.keys(value)) {
  39. let params = propName + '[' + key + ']';
  40. var subPart = encodeURIComponent(params) + "=";
  41. url += subPart + encodeURIComponent(value[key]) + "&";
  42. }
  43. } else {
  44. url += part + encodeURIComponent(value) + "&";
  45. }
  46. }
  47. }
  48. url = url.slice(0, -1);
  49. config.params = {};
  50. config.url = url;
  51. }
  52. return config
  53. }, error => {
  54. console.log(error)
  55. Promise.reject(error)
  56. })
  57. }
  58. // 响应拦截器
  59. let responseInterceptor;
  60. // let gqnum = 0;
  61. if(responseInterceptor === undefined){
  62. responseInterceptor = service.interceptors.response.use(res => {
  63. Toast.clear();
  64. // 未设置状态码则默认成功状态
  65. const code = res.data.code || 200 || '200';
  66. // 获取错误信息
  67. const msg = errorCode[code] || res.data.msg || errorCode['default']
  68. // console.info(gqnum); && gqnum === 0
  69. // console.log(code)
  70. if (code === 401) {
  71. // gqnum++;
  72. Dialog.confirm({
  73. title: '系统提示',
  74. message: '登录状态已过期,请重新登录',
  75. confirmButtonText: '重新登录',
  76. cancelButtonText: '取消'
  77. })
  78. .then(() => {
  79. store.dispatch('LogOut').then(() => {
  80. console.log('退出')
  81. try {
  82. let loginUrl = Cookies.get("_Login_url");
  83. if(loginUrl && loginUrl.indexOf("onlineHomeLogin") !== -1) {
  84. window.location.href = loginUrl;
  85. return;
  86. }
  87. } catch (e) { console.log(e); }
  88. if (window.location.href.indexOf('/lawEnforcement') != -1) {
  89. // 农业执法
  90. window.location.href = '/lawEnforcement/login';
  91. return;
  92. }else if (window.location.href.indexOf('/authenticRight') != -1) {
  93. // 确权回头看
  94. window.location.href = '/authenticRight/login';
  95. return;
  96. }else if (window.location.href.indexOf('/homesteadSurvey') != -1) {
  97. // 宅基地调查
  98. window.location.href = '/homesteadLogin';
  99. return;
  100. }else if (window.location.href.indexOf('/yinnong') != -1) {
  101. // 事项审批
  102. window.location.href = '/yinnongLogin';
  103. return;
  104. }else if (window.location.href.indexOf('/sunVillage_info/') != -1) {
  105. // 阳光村务
  106. window.location.href = '/sunVillage_info/login';
  107. return;
  108. }else if (window.location.href.indexOf('/homestead/') != -1) {
  109. // 两清三化
  110. window.location.href = '/homestead/login';
  111. return;
  112. }else if (window.location.href.indexOf('/agriculturalTrusteeship') != -1){
  113. // 生产托管
  114. window.location.href = '/agriculturalTrusteeship/login';
  115. return;
  116. } else if (window.location.href.indexOf('/contracted') != -1){
  117. // 土地确权
  118. window.location.href = '/contracted/login';
  119. return;
  120. }else{
  121. console.log('退出login')
  122. console.log(window.location.href.indexOf('/Login') < 0)
  123. console.log(window.location.href.indexOf('/login') < 0)
  124. if(window.location.href.indexOf('/Login') < 0 && window.location.href.indexOf('/login') < 0){
  125. // 产权交易
  126. window.location.href = '/login';
  127. }
  128. }
  129. })
  130. })
  131. } else if (code === 500) {
  132. Dialog.alert({ type: 'warning', message: msg });
  133. return Promise.reject(new Error(msg))
  134. } else if (code !== 200 && code !== 401 && code !== '200') {
  135. Dialog.alert({ type: 'warning', message: msg });
  136. return Promise.reject('error')
  137. } else {
  138. return res.data
  139. }
  140. },
  141. error => {
  142. console.log('err' + error)
  143. let { message } = error;
  144. if (message == "Network Error") {
  145. message = "后端接口连接异常";
  146. }
  147. else if (message.includes("timeout")) {
  148. message = "系统接口请求超时";
  149. }
  150. else if (message.includes("Request failed with status code")) {
  151. message = "系统接口" + message.substr(message.length - 3) + "异常";
  152. }
  153. Toast.clear();
  154. // Message({
  155. // message: message,
  156. // type: 'error',
  157. // duration: 5 * 1000
  158. // })
  159. Dialog.alert({ type: 'warning', message: message });
  160. return Promise.reject(error)
  161. })
  162. }
  163. // // 取消请求拦截器
  164. // instance.interceptors.request.eject(requestInterceptor);
  165. // // 取消响应拦截器
  166. // instance.interceptors.response.eject(responseInterceptor);
  167. export default service