http.interceptor.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token
  2. // 同时,我们也可以在此使用getApp().globalData,如果你把token放在getApp().globalData的话,也是可以使用的
  3. const install = (Vue, vm) => {
  4. Vue.prototype.$u.http.setConfig({
  5. // baseUrl: '/api',//本地使用代理
  6. baseUrl: 'https://api.lab.site.xmnk.cn/admin',
  7. downloadUrl:'https://api.lab.site.xmnk.cn',
  8. // baseUrl: 'https://api.dianli.site.xmnk.cn/admin', //打包公司服务器接口地址
  9. // baseUrl:'http://bcsyds.hactcm.edu.cn/api',//学校服务器接口地址
  10. originalData: true,
  11. dataType: 'json',
  12. showLoading: true, // 是否显示请求中的loading
  13. loadingText: '请求中...', // 请求loading中的文字提示
  14. loadingTime: 100, // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
  15. loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
  16. // 设置自定义头部content-type
  17. header: {
  18. 'content-type': 'application/json',
  19. 'Term-Id': '',
  20. 'type':'weapp'
  21. }
  22. });
  23. // 请求拦截,配置Token等参数
  24. Vue.prototype.$u.http.interceptor.request = (config) => {
  25. // config.header.Token = 'xxxxxx';
  26. // 方式一,存放在vuex的token,假设使用了uView封装的vuex方式,见:https://uviewui.com/components/globalVariable.html
  27. // config.header.token = vm.token;
  28. config.header.Authorization = vm.vuex_token;
  29. config.header['Term-Id']=Vue.prototype.$store.state.vuex_term_id
  30. // 方式二,如果没有使用uView封装的vuex方法,那么需要使用$store.state获取
  31. // config.header.token = vm.$store.state.token;
  32. // 方式三,如果token放在了globalData,通过getApp().globalData获取
  33. // config.header.token = getApp().globalData.username;
  34. // 方式四,如果token放在了Storage本地存储中,拦截是每次请求都执行的,所以哪怕您重新登录修改了Storage,下一次的请求将会是最新值
  35. // const token = uni.getStorageSync('token');
  36. // config.header.token = token;
  37. return config;
  38. }
  39. // 响应拦截,判断状态码是否通过
  40. Vue.prototype.$u.http.interceptor.response = (res) => {
  41. // 如果把originalData设置为了true,这里得到将会是服务器返回的所有的原始数据
  42. // 判断可能变成了res.statueCode,或者res.data.code之类的,请打印查看结果
  43. if (res.statusCode == 401) {
  44. Vue.prototype.$u.toast('登录过期请重新登录')
  45. Vue.prototype.$u.vuex('vuex_token', '');
  46. Vue.prototype.$u.vuex('vuex_user', '');
  47. Vue.prototype.$u.vuex('vuex_id', '');
  48. uni.reLaunch({
  49. url: '/pages/login/login'
  50. })
  51. return
  52. }
  53. if (res.statusCode == 422) {
  54. Vue.prototype.$u.toast(res.data.message)
  55. return false
  56. }
  57. if (res.statusCode == 400) {
  58. Vue.prototype.$u.toast(res.data.message)
  59. return false
  60. }
  61. if (res.statusCode == 500) {
  62. Vue.prototype.$u.toast(res.data.message)
  63. return false
  64. }
  65. if (res.statusCode == 423) {
  66. Vue.prototype.$u.toast(res.data.message)
  67. return false
  68. }
  69. // if (res.statusCode == 423) {
  70. // return res;
  71. // }
  72. if (res.statusCode < 300 && res.statusCode >= 200) {
  73. // 如果把originalData设置为了true,这里return回什么,this.$u.post的then回调中就会得到什么
  74. return res.data;
  75. } else return false;
  76. }
  77. }
  78. export default {
  79. install
  80. }