http.interceptor.js 3.9 KB

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