request.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import service from './newRequest.js'
  2. import store from '../store';
  3. import {
  4. saveUrl,
  5. interceptor
  6. } from '@/utils/loginUtils.js';
  7. // 请求完成后拦截
  8. service.interceptors.response(
  9. response => {
  10. try {
  11. let res = response.data;
  12. // 解析字符串为数字
  13. if (res.status !== 200) {
  14. console.log(res.status);
  15. if (res.status == 410000) {
  16. // 调用退出登录方法清空用户信息
  17. store.commit('user/logout');
  18. saveUrl();
  19. uni.navigateTo({
  20. url:"/pages/public/login"
  21. })
  22. } else {
  23. uni.showToast({
  24. title: res.msg,
  25. duration: 1500,
  26. mask: false,
  27. icon: 'none',
  28. })
  29. }
  30. return Promise.reject(res)
  31. } else {
  32. return res
  33. }
  34. } catch (e) {
  35. console.log(e);
  36. }
  37. },
  38. error => {
  39. uni.showToast({
  40. title: "当前用户量过多,请稍后重试",
  41. duration: 1500,
  42. mask: false,
  43. icon: 'none',
  44. })
  45. return Promise.reject(error)
  46. }
  47. )
  48. // 请求前拦截器
  49. service.interceptors.request(
  50. config => {
  51. let token = uni.getStorageSync('token') || '';
  52. // console.log(config);
  53. if (!config.header) {
  54. config.header = {
  55. "Authori-zation": 'Bearer ' + token,
  56. }
  57. } else {
  58. // 添加key请求头
  59. config.header["Authori-zation"] = 'Bearer ' + token;
  60. }
  61. return config
  62. },
  63. error => {
  64. // 错误处理
  65. console.log(error)
  66. return Promise.reject(error)
  67. }
  68. )
  69. let upFilse = service.upFilse;
  70. export {
  71. upFilse
  72. };
  73. export default service.open;