request.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. console.log(app.getCurrentPages())
  62. }
  63. }, (error, promise) => {
  64. // Do something with request error
  65. console.log(error); // for debug
  66. promise.reject(error)
  67. });
  68. fly.interceptors.response.use(
  69. (response) => {
  70. wx.hideLoading();
  71. //只将请求结果的data字段返回
  72. return response
  73. },
  74. (err, promise) => {
  75. wx.hideLoading();
  76. let msg = '';
  77. if (err.status === 0) {
  78. msg = '网络连接异常'
  79. } else if (err.status === 1) {
  80. msg = '网络连接超时'
  81. } else if (err.status === 401) {
  82. msg = '用户未登录'
  83. wx.clearStorageSync('token')
  84. // wx.navigateTo({
  85. // url: 'pages/me/me'
  86. // });
  87. } else {
  88. if (err.response.data.message) {
  89. msg = err.response.data.message
  90. } else {
  91. msg = '请求数据失败,请稍后再试'
  92. }
  93. }
  94. wx.showToast({
  95. title: msg,
  96. icon: 'none',
  97. duration: 3000
  98. });
  99. // setTimeout(() => {
  100. // console.log('fly.interceptors.err-toLogin')
  101. // wx.redirectTo({
  102. // url: '../login/login'
  103. // });
  104. // }, 500)
  105. return promise.resolve(err)
  106. }
  107. )
  108. // Set the base url
  109. fly.config.baseURL = "http://192.168.20.52:9999"
  110. export default fly;