request.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 (request.url.indexOf(whiteList.nullHeaderToken) !== -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 (request.headers.Authorization) {
  32. // 给所有请求添加自定义header
  33. request.timeout = 30000;
  34. Object.assign(request.headers, {
  35. 'content-type': 'application/json',
  36. 'X-Tag': 'flyio'
  37. })
  38. // fly.unlock(); //解锁请求
  39. return request;
  40. }
  41. if(wx.getStorageSync('token')) {
  42. // 给所有请求添加自定义header
  43. request.timeout = 30000;
  44. Object.assign(request.headers, {
  45. 'content-type': 'application/json',
  46. 'X-Tag': 'flyio',
  47. 'Authorization': `Bearer ${wx.getStorageSync('token')}`
  48. })
  49. // fly.unlock(); //解锁请求
  50. return request;
  51. }
  52. // else if(!wx.getStorageSync('token')&&app.getCurrentPages()=='pages/login/login') {
  53. // request.headers = {
  54. // 'content-type': 'application/json',
  55. // 'X-Tag': 'flyio',
  56. // 'Authorization': 'Basic dGVzdDp0ZXN0'
  57. // };
  58. // fly.unlock();//解锁请求
  59. // return request;
  60. // }
  61. else {
  62. console.log(app.getCurrentPages())
  63. }
  64. }, (error, promise) => {
  65. // Do something with request error
  66. console.log(error); // for debug
  67. promise.reject(error)
  68. });
  69. fly.interceptors.response.use(
  70. (response) => {
  71. wx.hideLoading();
  72. //只将请求结果的data字段返回
  73. return response
  74. },
  75. (err, promise) => {
  76. wx.hideLoading();
  77. let msg = '';
  78. if (err.status === 0) {
  79. msg = '网络连接异常'
  80. } else if (err.status === 1) {
  81. msg = '网络连接超时'
  82. } else if (err.status === 401) {
  83. msg = '用户未登录'
  84. wx.clearStorageSync('token')
  85. // wx.navigateTo({
  86. // url: 'pages/me/me'
  87. // });
  88. } else {
  89. if (err.response.data.message) {
  90. msg = err.response.data.message
  91. } else {
  92. msg = '请求数据失败,请稍后再试'
  93. }
  94. }
  95. wx.showToast({
  96. title: msg,
  97. icon: 'none',
  98. duration: 3000
  99. });
  100. // setTimeout(() => {
  101. // console.log('fly.interceptors.err-toLogin')
  102. // wx.redirectTo({
  103. // url: '../login/login'
  104. // });
  105. // }, 500)
  106. return promise.resolve(err)
  107. }
  108. )
  109. // Set the base url
  110. fly.config.baseURL = "http://192.168.20.52:9999"
  111. export default fly;