request.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if (res.status == 410000) {
  15. // 存储当前地址
  16. saveUrl()
  17. // 调用退出登录方法清空用户信息
  18. store.commit('user/logout');
  19. // 判断是否开启强制登录
  20. // 跳转页面
  21. if(navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger') {
  22. interceptor()
  23. uni.showModal({
  24. title: "您未登录!是否马上登录?",
  25. success: (e) => {
  26. if (e.confirm) {
  27. // 保存当前页面地址
  28. }
  29. }
  30. })
  31. }else {
  32. uni.switchTab({
  33. url:'/pages/index/index'
  34. })
  35. }
  36. } else {
  37. if(res.msg!='系统出现异常'){
  38. uni.showToast({
  39. title: res.msg,
  40. duration: 1500,
  41. mask: false,
  42. icon: 'none',
  43. })
  44. }
  45. }
  46. console.log(res);
  47. //return Promise.reject(new Error(res.msg || 'Error'))
  48. } else {
  49. return res
  50. }
  51. } catch (e) {
  52. console.log(e);
  53. }
  54. },
  55. error => {
  56. uni.showToast({
  57. title: "加载错误请重试",
  58. duration: 1500,
  59. mask: false,
  60. icon: 'none',
  61. })
  62. return Promise.reject(error)
  63. }
  64. )
  65. // 请求前拦截器
  66. service.interceptors.request(
  67. config => {
  68. // config.data.token = getToken();
  69. // console.log(config);
  70. let token = uni.getStorageSync('token');
  71. // console.log(token);
  72. if (!config.header) {
  73. config.header = {
  74. "Authori-zation": 'Bearer ' + token,
  75. }
  76. } else {
  77. // 添加key请求头
  78. config.header["Authori-zation"] = 'Bearer ' + token;
  79. }
  80. return config
  81. },
  82. error => {
  83. // 错误处理
  84. console.log(error)
  85. return Promise.reject(error)
  86. }
  87. )
  88. let upFilse = service.upFilse;
  89. export {
  90. upFilse
  91. };
  92. export default service.open;