request.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import axios from 'axios'
  2. import {
  3. Notify,
  4. Toast,
  5. Dialog,
  6. } from "vant";
  7. import store from '@/store'
  8. import {
  9. getToken
  10. } from '@/utils/auth'
  11. import errorCode from '@/utils/errorCode'
  12. axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
  13. // 创建axios实例
  14. const service = axios.create({
  15. // axios中请求配置有baseURL选项,表示请求URL公共部分
  16. baseURL: process.env.VUE_APP_BASE_API,
  17. // 超时
  18. timeout: 60000
  19. })
  20. // request拦截器
  21. service.interceptors.request.use(config => {
  22. // 是否需要设置 token
  23. const isToken = (config.headers || {}).isToken === false
  24. const whiteList=['/h5/home/selectTypeList','/h5/home/selectPreferredList','/h5/home/selectSalesTop10','/h5/system/getBusCompanyList','/h5/system/login','/h5/system/lineLogin','/h5/system/register'];//不需携带token的请求路径
  25. if (getToken() && !isToken) {
  26. if(whiteList.indexOf(config.url) === -1){
  27. config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  28. }
  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. if (value[key] !== null && typeof (value[key]) !== 'undefined') {
  40. let params = propName + '[' + key + ']';
  41. let subPart = encodeURIComponent(params) + '=';
  42. url += subPart + encodeURIComponent(value[key]) + '&';
  43. }
  44. }
  45. } else {
  46. url += part + encodeURIComponent(value) + "&";
  47. }
  48. }
  49. }
  50. url = url.slice(0, -1);
  51. config.params = {};
  52. config.url = url;
  53. }
  54. return config
  55. }, error => {
  56. console.log('error:', error)
  57. Promise.reject(error)
  58. })
  59. // 响应拦截器
  60. service.interceptors.response.use(res => {
  61. // 未设置状态码则默认成功状态
  62. const code = res.data.code || 200;
  63. // 获取错误信息
  64. const msg = errorCode[code] || res.data.msg || errorCode['default']
  65. if (code === 401) {
  66. console.log(res,'codecodecode---')
  67. // MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
  68. // confirmButtonText: '重新登录',
  69. // cancelButtonText: '取消',
  70. // type: 'warning'
  71. // }).then(() => {
  72. // store.dispatch('LogOut').then(() => {
  73. // location.href = '/index';
  74. // })
  75. // }).catch(() => {});
  76. //Dialog
  77. // .confirm({
  78. // title: "系统提示",
  79. // message: "登录状态已过期,您可以继续留在该页面,或者重新登录",
  80. // confirmButtonText: '重新登录',
  81. // cancelButtonText: '取消',
  82. // })
  83. // .then(() => {
  84. // location.href = '/login';
  85. store.dispatch('LogOut').then(() => {
  86. location.href = '/index';
  87. })
  88. // })
  89. // .catch(() => {
  90. // });
  91. return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
  92. } else if (code === 500) {
  93. // Message({
  94. // message: msg,
  95. // dangerouslyUseHTMLString: true,
  96. // type: 'error'
  97. // })
  98. Toast({
  99. type: "fail",
  100. message: msg
  101. });
  102. return Promise.reject(new Error(msg))
  103. } else if (code !== 200) {
  104. Notify({
  105. type: "danger",
  106. message: msg
  107. });
  108. // Notification.error({
  109. // title: msg
  110. // })
  111. return Promise.reject('error')
  112. } else {
  113. return res.data
  114. }
  115. },
  116. error => {
  117. console.log('err' + error)
  118. let {
  119. message
  120. } = error;
  121. if (message == "Network Error") {
  122. message = "后端接口连接异常";
  123. } else if (message.includes("timeout")) {
  124. message = "系统接口请求超时";
  125. } else if (message.includes("Request failed with status code")) {
  126. message = "系统接口" + message.substr(message.length - 3) + "异常";
  127. }
  128. // Message({
  129. // message: message,
  130. // type: 'error',
  131. // duration: 5 * 1000
  132. // })
  133. Toast({
  134. type: "fail",
  135. message: message,
  136. duration: 5 * 1000
  137. });
  138. return Promise.reject(error)
  139. }
  140. )
  141. export default service;