request.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. store.commit('user/logout');
  17. // 判断是否开启强制登录
  18. uni.showModal({
  19. title: "登陆",
  20. content: '您未登录!是否马上登录?',
  21. success: (e) => {
  22. // 判断是否点击确认按钮
  23. if (e.confirm) {
  24. // 保存当前页面地址
  25. saveUrl()
  26. // 跳转页面
  27. interceptor()
  28. }
  29. }
  30. })
  31. } else {
  32. if (res.msg != '系统出现异常') {
  33. uni.showToast({
  34. title: res.msg,
  35. duration: 1500,
  36. mask: false,
  37. icon: 'none',
  38. })
  39. }
  40. }
  41. console.log(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. if (!config.header) {
  66. config.header = {
  67. "Authori-zation": 'Bearer ' + uni.getStorageSync('token'),
  68. }
  69. } else {
  70. // 添加key请求头
  71. config.header["Authori-zation"] = 'Bearer ' + uni.getStorageSync('token');
  72. }
  73. return config
  74. },
  75. error => {
  76. // 错误处理
  77. console.log(error)
  78. return Promise.reject(error)
  79. }
  80. )
  81. let upFilse = service.upFilse;
  82. export {
  83. upFilse
  84. };
  85. export default service.open;