request.js 1.8 KB

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