http.interceptor.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const install = (Vue, vm) => {
  2. Vue.prototype.$u.http.setConfig({
  3. baseUrl: 'http://192.168.0.119:6060/member',
  4. // baseUrl: 'http://test-member.uxianxin.com:8888/api/member',// 测试
  5. dataType: 'json',
  6. loadingText: '请求中...', // 请求loading中的文字提示
  7. loadingTime: 100, // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
  8. originalData: false, // 是否在拦截器中返回服务端的原始数据
  9. loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
  10. });
  11. //是否是H5公众号--是否显示头部导航
  12. // #ifdef H5
  13. Vue.prototype.$isH5 = true;
  14. // #endif
  15. // #ifdef APP-PLUS
  16. Vue.prototype.$isH5 = false;
  17. // #endif
  18. //系统图片域名前缀
  19. Vue.prototype.$fileHost = 'http://fresh-life.oss-cn-shanghai.aliyuncs.com/'
  20. // 请求拦截,配置Token等参数
  21. Vue.prototype.$u.http.interceptor.request = (config) => {
  22. const token = uni.getStorageSync('token');
  23. config.header.token = token;
  24. Vue.prototype.$u.http.setConfig({
  25. showLoading: config.header.showLoading || false, // 是否显示请求中的loading
  26. });
  27. return config;
  28. }
  29. // 响应拦截,判断状态码是否通过
  30. Vue.prototype.$u.http.interceptor.response = (res) => {
  31. // console.log(JSON.stringify("------------"))
  32. // console.log(JSON.stringify(res))
  33. // console.log(res)
  34. if(res.code == 200) {
  35. // console.log("11111")
  36. return res;
  37. }else if(res.code == 401){
  38. // console.log("22222")
  39. uni.showToast({
  40. title:"登录过期,请重新登录",
  41. duration:1500, mask:false, icon:'none'
  42. });
  43. //此时应重新登录
  44. // console.log("goto--login")
  45. uni.navigateTo({
  46. url: '/pages/login/login'
  47. })
  48. return res;
  49. } else {
  50. // console.log("33333")
  51. uni.showToast({
  52. title: res.msg == null ? "出错了" : res.msg,
  53. duration:1500, mask:false, icon:'none'
  54. });
  55. return res;
  56. }
  57. }
  58. }
  59. export default {
  60. install
  61. }