request.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import Vue from 'vue'
  2. import uni_request from './uni_request.js'
  3. const request = uni_request({
  4. timeout: 123456,
  5. baseURL: 'https://soap.cliu.cc/api', //正式版
  6. })
  7. request.interceptors.request.use(config => {
  8. config.header.Authorization = 'Bearer ' + uni.getStorageSync('token')
  9. // config.header.Authorization = 'Bearer ' +
  10. // 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9zb2FwLmNsaXUuY2NcL2FwaVwvYXV0aFwvd3hMb2dpbiIsImlhdCI6MTY4OTMwMDQ5OCwiZXhwIjoxNjkwMTY0NDk4LCJuYmYiOjE2ODkzMDA0OTgsImp0aSI6IlR2bTkyamF2bFp6RVUwMjAiLCJzdWIiOjQyMCwicHJ2IjoiMzA3M2QwNzExYzk3MGYyODhiYWI2NzJkN2FiZTMxODczZDhiODM4NCJ9.p641z1hA5jRpAsVVrBbpI7Ae62hKLc76xRKLliJiiPg'
  11. return config
  12. })
  13. request.interceptors.response.use(async (response, ...args) => { // 拦截器
  14. uni.hideLoading()
  15. if (response.data.code == 50000 ||response.data.code == 40021 || response.statusCode == 401) { //token过期或者没token
  16. // 判断是否是退差价页面
  17. let pages = getCurrentPages();
  18. let currentPage = pages[pages.length-1];
  19. let url = currentPage.route;
  20. let tip = url == 'pages/CustomerReturnPay/returnedInfo' ? '当前登录状态已过期, 点击‘确定’跳转至售后首页自动登录, 自动登录后请关闭网页重新扫码进入退差价页面' : '登录过期'
  21. uni.showModal({
  22. title: '错误提示',
  23. content: tip,
  24. showCancel: false,
  25. success: res => {
  26. uni.removeStorageSync('token')
  27. uni.removeStorageSync('userInfo')
  28. uni.switchTab({
  29. url: '../../pages/index/index'
  30. })
  31. }
  32. })
  33. // response = await doRequest(response, args[1]);
  34. return
  35. }
  36. return response
  37. })
  38. request.onerror = (...args) => { // 请求失败统一处理方法
  39. uni.hideLoading()
  40. uni.$emit('HIDELOADING') // 隐藏加载
  41. uni.showModal({
  42. title: "请求失败",
  43. content: "网络出错",
  44. showCancel: false,
  45. });
  46. return Promise.reject(args);
  47. }
  48. export default request