http.interceptor.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token
  2. // 同时,我们也可以在此使用getApp().globalData,如果你把token放在getApp().globalData的话,也是可以使用的
  3. const install = (Vue, vm) => {
  4. let baseURL;
  5. if (process.env.NODE_ENV === 'development') {
  6. // baseURL = "https://test.wms.tubaosoft.com/prod-api"
  7. // baseURL = "https://zd.tubaosoft.com/prod-api"
  8. // baseURL = "http://192.168.1.116:9011"
  9. // baseURL = "https://ap.tubaosoft.com/prod-api"
  10. // baseURL = "https://dmu.tubaosoft.com/prod-api"
  11. // baseURL = "https://jingang.tubaosoft.com/prod-api"
  12. baseURL = "https://jiyong.tubaosoft.com/prod-api"
  13. }
  14. if (process.env.NODE_ENV === 'production') {
  15. // baseURL = "https://zd.tubaosoft.com/prod-api"
  16. // baseURL = "https://jiyong.tubaosoft.com/prod-api"
  17. baseURL = "https://jingang.tubaosoft.com/prod-api"
  18. // baseURL = "https://ap.tubaosoft.com/prod-api"
  19. // baseURL = "https://dmu.tubaosoft.com/prod-api"
  20. }
  21. Vue.prototype.$u.http.setConfig({
  22. baseUrl: baseURL,
  23. // 如果将此值设置为true,拦截回调中将会返回服务端返回的所有数据response,而不是response.data
  24. // 设置为true后,就需要在this.$u.http.interceptor.response进行多一次的判断,请打印查看具体值
  25. // originalData: true,
  26. // 设置自定义头部content-type
  27. // header: {
  28. // 'content-type': 'xxx'
  29. // }
  30. });
  31. // 请求拦截,配置Token等参数
  32. Vue.prototype.$u.http.interceptor.request = (config) => {
  33. // console.log(config)
  34. uni.getNetworkType({
  35. success: function(res) {
  36. // console.log(res.networkType);
  37. if (res.networkType == 'none') {
  38. return uni.showModal({
  39. title: '警告⚠️',
  40. content: '当前无网络请检查网络信号',
  41. confirmText:'关闭',
  42. showCancel:false
  43. });
  44. }
  45. }
  46. });
  47. if (vm.vuex_token) {
  48. config.header.Authorization = "Bearer" + " " + vm.vuex_token;
  49. }
  50. // 方式一,存放在vuex的token,假设使用了uView封装的vuex方式,见:https://uviewui.com/guide/globalVariable.html
  51. // config.header.token = vm.token;
  52. // 方式二,如果没有使用uView封装的vuex方法,那么需要使用$store.state获取
  53. // config.header.token = vm.$store.state.token;
  54. // 方式三,如果token放在了globalData,通过getApp().globalData获取
  55. // config.header.token = getApp().globalData.username;
  56. // 方式四,如果token放在了Storage本地存储中,拦截是每次请求都执行的,所以哪怕您重新登录修改了Storage,下一次的请求将会是最新值
  57. // const token = uni.getStorageSync('token');
  58. // config.header.token = token;
  59. return config;
  60. }
  61. // 响应拦截,判断状态码是否通过
  62. Vue.prototype.$u.http.interceptor.response = res => {
  63. // console.log(res)
  64. // 如果把originalData设置为了true,这里得到将会是服务器返回的所有的原始数据
  65. // 判断可能变成了res.statueCode,或者res.data.code之类的,请打印查看结果
  66. // 如果把originalData设置为了true,这里return回什么,this.$u.post的then回调中就会得到什么
  67. if (res.code == 200) {
  68. return res;
  69. } else if (res.code == 401) {
  70. uni.showModal({
  71. title: '提示',
  72. content: '登录状态失效,点击确定重新登录',
  73. showCancel: false,
  74. success: function(res) {
  75. if (res.confirm) {
  76. uni.redirectTo({
  77. url: '/pages/login/login'
  78. });
  79. }
  80. }
  81. });
  82. return false
  83. } else if (res.code == 500) {
  84. uni.showToast({
  85. icon: 'none',
  86. title: res.msg,
  87. position: "bottom"
  88. })
  89. return false
  90. } else {
  91. uni.showToast({
  92. icon: 'none',
  93. title: res.msg,
  94. position: "bottom"
  95. })
  96. return false;
  97. }
  98. }
  99. }
  100. export default {
  101. install
  102. }