移动端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

181 行
6.5 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. console.log('登录页面: ' + loginUrl)
  84. if(loginUrl) {
  85. if(loginUrl.indexOf("onlineHomeLogin") !== -1
  86. || loginUrl.indexOf('yinnongLiteLogin') !== -1
  87. )
  88. {
  89. window.location.href = loginUrl;
  90. return;
  91. }
  92. }
  93. } catch (e) { console.log(e); }
  94. if (window.location.href.indexOf('/lawEnforcement') != -1) {
  95. // 农业执法
  96. window.location.href = '/lawEnforcement/login';
  97. return;
  98. }else if (window.location.href.indexOf('/authenticRight') != -1) {
  99. // 确权回头看
  100. window.location.href = '/authenticRight/login';
  101. return;
  102. }else if (window.location.href.indexOf('/homesteadSurvey') != -1) {
  103. // 宅基地调查
  104. window.location.href = '/homesteadLogin';
  105. return;
  106. }else if (window.location.href.indexOf('/yinnong') != -1) {
  107. // 事项审批
  108. window.location.href = '/yinnongLogin';
  109. return;
  110. }else if (window.location.href.indexOf('/sunVillage_info/') != -1) {
  111. // 阳光村务
  112. window.location.href = '/sunVillage_info/login';
  113. return;
  114. }else if (window.location.href.indexOf('/homestead/') != -1) {
  115. // 两清三化
  116. window.location.href = '/homestead/login';
  117. return;
  118. }else if (window.location.href.indexOf('/agriculturalTrusteeship') != -1){
  119. // 生产托管
  120. window.location.href = '/agriculturalTrusteeship/login';
  121. return;
  122. } else if (window.location.href.indexOf('/contracted') != -1){
  123. // 土地确权
  124. window.location.href = '/contracted/login';
  125. return;
  126. }else{
  127. console.log('退出login')
  128. console.log(window.location.href.indexOf('/Login') < 0)
  129. console.log(window.location.href.indexOf('/login') < 0)
  130. if(window.location.href.indexOf('/Login') < 0 && window.location.href.indexOf('/login') < 0){
  131. // 产权交易
  132. window.location.href = '/login';
  133. }
  134. }
  135. })
  136. })
  137. } else if (code === 500) {
  138. Dialog.alert({ type: 'warning', message: msg });
  139. return Promise.reject(new Error(msg))
  140. } else if (code !== 200 && code !== 401 && code !== '200') {
  141. Dialog.alert({ type: 'warning', message: msg });
  142. return Promise.reject('error')
  143. } else {
  144. return res.data
  145. }
  146. },
  147. error => {
  148. console.log('err' + error)
  149. let { message } = error;
  150. if (message == "Network Error") {
  151. message = "后端接口连接异常";
  152. }
  153. else if (message.includes("timeout")) {
  154. message = "系统接口请求超时";
  155. }
  156. else if (message.includes("Request failed with status code")) {
  157. message = "系统接口" + message.substr(message.length - 3) + "异常";
  158. }
  159. Toast.clear();
  160. // Message({
  161. // message: message,
  162. // type: 'error',
  163. // duration: 5 * 1000
  164. // })
  165. Dialog.alert({ type: 'warning', message: message });
  166. return Promise.reject(error)
  167. })
  168. }
  169. // // 取消请求拦截器
  170. // instance.interceptors.request.eject(requestInterceptor);
  171. // // 取消响应拦截器
  172. // instance.interceptors.response.eject(responseInterceptor);
  173. export default service