http.interceptor.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token
  2. const install = (Vue, vm) => {
  3. Vue.prototype.$u.http.setConfig({
  4. baseUrl: vm.config.baseUrl,
  5. loadingText: '努力加载中~',
  6. loadingTime: 800,
  7. originalData: true,
  8. dataType: 'text'
  9. });
  10. let isRefreshing = false
  11. // 请求拦截,配置Token等参数
  12. Vue.prototype.$u.http.interceptor.request = (config) => {
  13. if (vm.vuex_token) {
  14. config.header.Authorization = `Bearer ${vm.vuex_token}`;
  15. }
  16. // 请求地址加时间戳
  17. config.url = config.url + '?t=' + Date.now();
  18. let noTokenList = [
  19. '/client/wechat/h5/code/',
  20. '/client/auth/sendSmsCodeV2',
  21. '/client/auth/verifyCodeV2',
  22. '/client/auth/loginToWxOpenId',
  23. '/client/auth/sendSmsCodeV3',
  24. '/client/auth/loginToMobile'
  25. ];
  26. if (noTokenList.includes(config.url)) config.header.noToken = true;
  27. return config;
  28. };
  29. // 响应拦截,判断状态码是否通过
  30. Vue.prototype.$u.http.interceptor.response = res => {
  31. // 解决出现数据精度缺失问题
  32. const jsonBigint = require('json-bigint')({ storeAsString: true });
  33. res = jsonBigint.parse(res.data);
  34. if (res.code == 200) {
  35. return res;
  36. } else if (res.code == 401 || (res.code == 400 && res.msg == '令牌不能为空')) {
  37. if (!isRefreshing) {
  38. login(vm);
  39. isRefreshing = true;
  40. }
  41. return false
  42. } else {
  43. // 用来判断时候需要使用当前提示框
  44. const noTipMsg = ['无任何订单'];
  45. const index = noTipMsg.findIndex((item) => res?.msg.indexOf(item) > -1);
  46. if (index < 0) {
  47. uni.showToast({
  48. title: res.msg || '程序错误!',
  49. duration: 2000,
  50. icon: 'none'
  51. });
  52. }
  53. return false;
  54. }
  55. };
  56. };
  57. const login = (vm) => {
  58. const backUrl = location.href,
  59. openidPage = vm.config.onlyWxLogin, // 只需要传openid的页面集合
  60. needValidPage = openidPage.filter((item) => backUrl.indexOf(item) > -1); // 是否为需要验证的页面集合
  61. // 判断浏览器
  62. const ua = window.navigator.userAgent.toLowerCase();
  63. // 微信中打开
  64. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  65. // 防止重复跳转
  66. if (backUrl.indexOf('backUrl') === -1) {
  67. vm.$u.route('pages/wechatLogin/wechatLogin', { backUrl: encodeURIComponent(backUrl), loginType: needValidPage.length ? 2 : 1 });
  68. }
  69. } else {
  70. // 普通浏览器中打开
  71. if (backUrl.indexOf('backUrl') === -1) {
  72. localStorage.setItem('backUrl', encodeURIComponent(backUrl));
  73. }
  74. uni.showModal({
  75. title: '提示',
  76. content: '未查询到登录信息或信息已失效, 请重新登录',
  77. showCancel: false,
  78. success: function (res) {
  79. if (res.confirm) {
  80. localStorage.removeItem('lifeData');
  81. uni.redirectTo({
  82. url: '/pages/center/phoneLogin/phoneLogin'
  83. });
  84. }
  85. }
  86. });
  87. }
  88. };
  89. export default {
  90. install
  91. };