request.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. 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(new Error(res.msg || 'Error'))
  42. } else {
  43. return res
  44. }
  45. } catch (e) {
  46. console.log(e);
  47. }
  48. },
  49. error => {
  50. uni.showToast({
  51. title: "加载错误请重试",
  52. duration: 1500,
  53. mask: false,
  54. icon: 'none',
  55. })
  56. return Promise.reject(error)
  57. }
  58. )
  59. // 请求前拦截器
  60. service.interceptors.request(
  61. config => {
  62. // config.data.token = getToken();
  63. // console.log(config);
  64. let token = uni.getStorageSync('token');
  65. // console.log(token);
  66. if (!config.header) {
  67. config.header = {
  68. "Authori-zation": 'Bearer ' + token,
  69. }
  70. } else {
  71. // 添加key请求头
  72. config.header["Authori-zation"] = 'Bearer ' + token;
  73. }
  74. return config
  75. },
  76. error => {
  77. // 错误处理
  78. console.log(error)
  79. return Promise.reject(error)
  80. }
  81. )
  82. let upFilse = service.upFilse;
  83. export {
  84. upFilse
  85. };
  86. export default service.open;