http.interceptor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token
  2. // 同时,我们也可以在此使用getApp().globalData,如果你把token放在getApp().globalData的话,也是可以使用的
  3. import {
  4. devUrl,
  5. prodUrl,
  6. contentType,
  7. showLoading,
  8. loadingText,
  9. loadingTime,
  10. headerTokenName,
  11. userTokenName,
  12. codeName,
  13. successCode,
  14. invalidCode,
  15. } from '@/common/settings'
  16. const install = (Vue, vm) => {
  17. Vue.prototype.$u.http.setConfig({
  18. // baseUrl: '/api', //本地使用代理
  19. // downloadUrl: 'http://api.lesson.site.xmnk.cn',
  20. // production
  21. // baseUrl: 'http://lesson.site.xmnk.cn/pap/admin',
  22. // downloadUrl: 'http://lesson.site.xmnk.cn/pap/admin',
  23. // // online
  24. //#ifndef H5
  25. //除h5以外
  26. baseUrl: process.env.NODE_ENV === 'development' ? devUrl : prodUrl, // 请求的本域名
  27. //#endif
  28. //#ifdef H5
  29. //h5执行
  30. baseUrl: '/api',
  31. //#endif
  32. downloadUrl: '',
  33. originalData: true,
  34. dataType: 'json',
  35. showLoading: true, // 是否显示请求中的loading
  36. loadingText, // 请求loading中的文字提示
  37. loadingTime, // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
  38. loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
  39. // 设置自定义头部content-type
  40. header: {
  41. 'content-type': contentType,
  42. 'device': 'weapp',
  43. 'Accept': 'application/json',
  44. 'Cross-Origin-Embedder-Policy': 'require-corp',
  45. 'Cross-Origin-Opener-Policy': 'same-origin'
  46. }
  47. });
  48. // 请求拦截,配置Token等参数
  49. Vue.prototype.$u.http.interceptor.request = (config) => {
  50. // config.header.Token = 'xxxxxx';
  51. // 方式一,存放在vuex的token,假设使用了uView封装的vuex方式,见:https://uviewui.com/components/globalVariable.html
  52. // config.header.token = vm.token;
  53. // console.log(vm.vuex_token)
  54. config.header.Authorization = uni.getStorageSync('token');
  55. // config.header['Term-Id'] = vm.vuex_term_id
  56. // 方式二,如果没有使用uView封装的vuex方法,那么需要使用$store.state获取
  57. // config.header.token = vm.$store.state.token;
  58. // 方式三,如果token放在了globalData,通过getApp().globalData获取
  59. // config.header.token = getApp().globalData.username;
  60. // 方式四,如果token放在了Storage本地存储中,拦截是每次请求都执行的,所以哪怕您重新登录修改了Storage,下一次的请求将会是最新值
  61. // const token = uni.getStorageSync('token');
  62. // config.header.token = token;
  63. return config;
  64. }
  65. // 响应拦截,判断状态码是否通过
  66. Vue.prototype.$u.http.interceptor.response = (res) => {
  67. // console.log(JSON.parse(res.data))
  68. // 如果把originalData设置为了true,这里得到将会是服务器返回的所有的原始数据
  69. // 判断可能变成了res.statueCode,或者res.data.code之类的,请打印查看结果
  70. if (res.statusCode == invalidCode) {
  71. // Vue.prototype.$u.toast('登录过期请重新登录')
  72. // Vue.prototype.$u.vuex('vuex_token', '');
  73. // Vue.prototype.$u.vuex('vuex_user', '');
  74. // Vue.prototype.$u.vuex('vuex_id', '');
  75. uni.removeStorageSync('token')
  76. uni.removeStorageSync('lifeData')
  77. uni.reLaunch({
  78. url: '/pages/login/login'
  79. })
  80. return
  81. }
  82. if (res.statusCode == 422 || res.statusCode == 400 || res.statusCode == 500 || res.statusCode == 423 ||
  83. res.statusCode == 501) {
  84. uni.showToast({
  85. title: res.data.message,
  86. icon: 'none'
  87. })
  88. return false
  89. }
  90. if (res.statusCode < 300 && res.statusCode >= successCode) {
  91. // 如果把originalData设置为了true,这里return回什么,this.$u.post的then回调中就会得到什么
  92. return res.data;
  93. } else return false;
  94. }
  95. }
  96. export default {
  97. install
  98. }