request.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var Fly = require("../lib/wx.umd.min") //wx.js is your downloaded code
  2. var fly = new Fly(); //Create an instance of Fly
  3. const app = getApp()
  4. //设置超时
  5. fly.config.timeout = 20000;
  6. // 获取白名单
  7. import whiteList from './whiteList';
  8. // Add interceptors
  9. fly.interceptors.request.use((request) => {
  10. wx.showLoading({
  11. title: '加载中',
  12. mask: true
  13. })
  14. // 不显示加载页面的接口
  15. if (whiteList.loading.indexOf(request.url) === -1) {
  16. // 隐藏loading遮罩
  17. wx.hideLoading();
  18. }
  19. // 请求资源服务器时,不添加token
  20. if (whiteList.nullHeaderToken.indexOf(request.url) !== -1) {
  21. request.timeout = 30000; // 请求超时
  22. request.headers = {
  23. 'content-type': 'application/json',
  24. 'X-Tag': 'flyio'
  25. };
  26. console.log('nullHeaderToken()')
  27. return request;
  28. }
  29. fly.lock()
  30. // 延迟发请求 等 getStorageSync 存储
  31. if(wx.getStorageSync('token')) {
  32. // 给所有请求添加自定义header
  33. request.timeout = 30000;
  34. Object.assign(request.headers, {
  35. 'content-type': 'application/json',
  36. 'X-Tag': 'flyio',
  37. 'Authorization': `Bearer ${wx.getStorageSync('token')}`
  38. })
  39. fly.unlock(); //解锁请求
  40. return request;
  41. } else if (request.headers.Authorization) {
  42. // 给所有请求添加自定义header
  43. request.timeout = 30000;
  44. Object.assign(request.headers, {
  45. 'content-type': 'application/json',
  46. 'X-Tag': 'flyio'
  47. })
  48. fly.unlock(); //解锁请求
  49. return request;
  50. }
  51. // else if(!wx.getStorageSync('token')&&app.getCurrentPages()=='pages/login/login') {
  52. // request.headers = {
  53. // 'content-type': 'application/json',
  54. // 'X-Tag': 'flyio',
  55. // 'Authorization': 'Basic dGVzdDp0ZXN0'
  56. // };
  57. // fly.unlock();//解锁请求
  58. // return request;
  59. // }
  60. else {
  61. setTimeout(() => {
  62. wx.redirectTo({
  63. url: '../login/login'
  64. });
  65. }, 300)
  66. }
  67. }, (error, promise) => {
  68. // Do something with request error
  69. console.log(error); // for debug
  70. promise.reject(error)
  71. });
  72. fly.interceptors.response.use(
  73. (response) => {
  74. wx.hideLoading();
  75. //只将请求结果的data字段返回
  76. return response
  77. },
  78. (err, promise) => {
  79. wx.hideLoading();
  80. let msg = '';
  81. if (err.status === 0) {
  82. msg = '网络连接异常'
  83. } else if (err.status === 1) {
  84. msg = '网络连接超时'
  85. } else if (err.status === 401) {
  86. msg = '用户未登录'
  87. wx.clearStorageSync('token')
  88. wx.redirectTo({
  89. url: '../login/login'
  90. });
  91. } else {
  92. if (err.response.data.message) {
  93. msg = err.response.data.message
  94. } else {
  95. msg = '请求数据失败,请稍后再试'
  96. }
  97. }
  98. wx.showToast({
  99. title: msg,
  100. icon: 'none',
  101. duration: 3000
  102. });
  103. // setTimeout(() => {
  104. // console.log('fly.interceptors.err-toLogin')
  105. // wx.redirectTo({
  106. // url: '../login/login'
  107. // });
  108. // }, 500)
  109. return promise.resolve(err)
  110. }
  111. )
  112. // Set the base url
  113. fly.config.baseURL = "http://192.168.20.52:9999"
  114. export default fly;