http.interceptor.js 4.2 KB

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