http.interceptor.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // const serverAddress = 'http://localhost:8099/store-api/';
  2. // const serverAddress = 'http://localhost:6060';
  3. // const serverAddress = 'http://192.168.0.206:6060';
  4. // const serverAddress = 'https://test-plateform.` uxianxin.com/home-api';
  5. const serverAddress = 'https://test-server.uxianxin.com:8888/api';
  6. const config = {
  7. // redirect_uri:'https://test-plateform.uxianxin.com/%23/pages/login/index',
  8. redirect_uri:'https://test-server.uxianxin.com:8888/%23/pages/login/index',
  9. }
  10. let bindToGlobal = (obj, key) => {
  11. if (typeof window[key] === 'undefined') {
  12. window[key] = {};
  13. }
  14. for (let i in obj) {
  15. window[key][i] = obj[i]
  16. }
  17. }
  18. bindToGlobal(config,'_const')
  19. const install = (Vue, vm) => {
  20. Vue.prototype.$u.http.setConfig({
  21. // baseUrl: 'http://youanju.yajkj.com',
  22. baseUrl:serverAddress,
  23. dataType: 'json',
  24. loadingText: '请求中...', // 请求loading中的文字提示
  25. loadingTime: 100, // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
  26. originalData: false, // 是否在拦截器中返回服务端的原始数据
  27. loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
  28. // header:{
  29. // 'Authorization': uni.getStorageSync("claims")
  30. // }
  31. });
  32. //新系统图片域名前缀
  33. Vue.prototype.$fileHost = 'http://fresh-life.oss-cn-shanghai.aliyuncs.com/'
  34. // 请求拦截,配置Token等参数
  35. Vue.prototype.$u.http.interceptor.request = (config) => {
  36. const token = uni.getStorageSync('token');
  37. const secret = uni.getStorageSync('secret');
  38. const claims = uni.getStorageSync("claims");
  39. config.header.token = token;
  40. config.header.secret = secret;
  41. config.header.Authorization = claims;
  42. Vue.prototype.$u.http.setConfig({
  43. showLoading: config.header.showLoading || false, // 是否显示请求中的loading
  44. });
  45. return config;
  46. }
  47. // 响应拦截,判断状态码是否通过
  48. Vue.prototype.$u.http.interceptor.response = (res) => {
  49. // console.log(JSON.stringify("------------"))
  50. // console.log(JSON.stringify(res))
  51. // console.log(res)
  52. if(res.code == 200) {
  53. // console.log("11111")
  54. return res;
  55. }else if(res.code == 401){
  56. // console.log("22222")
  57. uni.showToast({
  58. title:"登录过期,请重新登录",
  59. duration:1500, mask:false, icon:'none'
  60. });
  61. //此时应重新登录
  62. // console.log("goto--login")
  63. uni.navigateTo({
  64. url: '/pages/index/login'
  65. })
  66. return res;
  67. } else {
  68. // console.log("33333")
  69. uni.showToast({
  70. title: res.msg == null ? "出错了" : res.msg,
  71. duration:1500, mask:false, icon:'none'
  72. });
  73. return res;
  74. }
  75. }
  76. }
  77. export default {
  78. install,
  79. }