request.js 958 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {
  2. $http
  3. } from '@escook/request-miniprogram'
  4. //$http.baseUrl = "http://localhost:8081"
  5. //$http.baseUrl = "http://192.168.0.12:8081"
  6. $http.baseUrl = "http://192.168.1.99:8089"
  7. uni.$http = $http
  8. import {
  9. getToken,
  10. removeAll
  11. } from "./auth.js"
  12. // 请求拦截器
  13. $http.beforeRequest = function(options) {
  14. if (getToken()) {
  15. options.header['Authorization'] = 'Bearer ' + getToken()
  16. options.header['token'] = getToken()
  17. }
  18. // uni.showLoading({
  19. // title: "数据加载中..."
  20. // })
  21. }
  22. // 响应拦截器
  23. $http.afterRequest = function(res) {
  24. try {
  25. const code = res.data.code
  26. const msg = res.data.msg
  27. if (code === 401 || code === 403) {
  28. uni.showLoading({
  29. title: msg
  30. })
  31. removeAll()
  32. uni.navigateTo({
  33. url: "/pages/login/index"
  34. })
  35. } else if (code !== 200) {
  36. uni.showToast({
  37. title: msg,
  38. duration: 2000,
  39. icon: "error"
  40. });
  41. uni.hideToast();
  42. } else {
  43. }
  44. uni.hideLoading()
  45. } catch (e) {
  46. }
  47. }