request.js 3.0 KB

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