request.js 1.8 KB

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