request.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // src/utils/resquest.ts
  2. import axios from 'axios'
  3. import { useStore } from '../store/modules/user'
  4. import { showDialog, showNotify, showToast } from 'vant'
  5. // @ts-ignore
  6. import router from '@/router'
  7. const store = useStore()
  8. const service = axios.create({
  9. baseURL: import.meta.env.VITE_APP_BASE_API, // 设置基础 API 地址
  10. timeout: 50000, // 设置请求超时时间
  11. });
  12. // 请求拦截器
  13. service.interceptors.request.use(
  14. (config:any) => {
  15. // 在请求发送之前做处理,如添加 token 等
  16. if (store.token) {
  17. config.headers['Authorization'] =store.token
  18. }
  19. config.headers['Source'] = 'app'
  20. config.headers['Version'] = 'V6'
  21. config.headers['timestamp'] = JSON.stringify(Date.now())
  22. if (!config.headers['Content-Type']) {
  23. config.headers['Content-Type'] = 'application/json'
  24. }
  25. return config;
  26. },
  27. (error:any) => {
  28. // 请求错误时做处理
  29. showToast({duration:5000,message:'网络开小车了,检查网络'})
  30. return Promise.reject(error);
  31. }
  32. );
  33. // 响应拦截器
  34. service.interceptors.response.use(
  35. (response:any) => {
  36. // 处理响应数据
  37. const res=response.data
  38. if (res.code !== 200) {
  39. if (res.code == 600 || res.code == 601 || res.code == 401 ) {
  40. try {
  41. // @ts-ignore
  42. window.android.logout()
  43. }catch (e) {
  44. showDialog({
  45. title: '温馨提示',
  46. message: '当前登录已过期,请重新登录!',
  47. }).then(() => {
  48. router.push('/login')
  49. });
  50. }
  51. return
  52. }
  53. if(res.code===305 && response.config.url=='api/entryOrder/app/blind/checkBarcodeIgnoreCase'){
  54. return res
  55. }
  56. if(res.message=='找不到此盲收任务'){
  57. return res
  58. }
  59. showNotify({
  60. message: res.message || '系统异常,请联系技术支持!',
  61. duration: 5000,
  62. });
  63. // showToast({duration:10000,message:res.message})
  64. return Promise.reject(res);
  65. }
  66. return res;
  67. },
  68. (error:any) => {
  69. if (error.code === 'ECONNABORTED') {
  70. // 请求超时处理
  71. showNotify({duration:5000,message:'请求超时,请稍后重试!'})
  72. }else if(error.code === 'ERR_NETWORK'){
  73. showNotify({duration:5000,message:'网络开小车了, 请稍后重试!'})
  74. } else {
  75. showNotify({
  76. message: error.message,
  77. duration: 8000,
  78. });
  79. }
  80. return Promise.reject(error);
  81. }
  82. );
  83. export default service;