request.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. fly.config.timeout = 20000;
  5. fly.config.baseURL = "https://wx.sgsino.cn"
  6. const newFly = new Fly();
  7. newFly.config = fly.config;
  8. let reqCount = 0 ;// 重发请求的次数
  9. let isShowing =false;
  10. // 获取白名单
  11. import whiteList from './whiteList';
  12. // Add interceptors
  13. fly.interceptors.request.use(function (request) {
  14. fly.lock();
  15. // 不显示加载页面的接口
  16. if (request.url.indexOf(whiteList.loading) === -1) {
  17. isShowing =true;
  18. wx.showLoading({
  19. 'title': '加载中',
  20. 'mask': true
  21. });
  22. }
  23. // 白名单内url不添加token
  24. if (request.url.indexOf(whiteList.nullHeaderToken) !== -1) {
  25. request.timeout = 30000; // 请求超时
  26. request.headers = {
  27. 'content-type': 'application/json',
  28. 'X-Tag': 'flyio'
  29. };
  30. console.log('nullHeaderToken()')
  31. fly.unlock();
  32. return request;
  33. }
  34. //头文件带Authorization为请求token,不添加token
  35. if (request.headers.Authorization) {
  36. // 给所有请求添加自定义header
  37. request.timeout = 30000;
  38. Object.assign(request.headers, {
  39. 'content-type': 'application/json',
  40. 'X-Tag': 'flyio'
  41. })
  42. console.log("我携带Authorization,不要给我添加token,我会请求回token给其他请求授权用");
  43. fly.unlock();
  44. return request;
  45. }
  46. //其他请求需要在头文件中自动附加token
  47. if(wx.getStorageSync('token')) {
  48. // 给所有请求添加自定义header
  49. request.timeout = 30000;
  50. Object.assign(request.headers, {
  51. 'content-type': 'application/json',
  52. 'X-Tag': 'flyio',
  53. 'Authorization': `Bearer ${wx.getStorageSync('token')}`
  54. })
  55. // wx.hideLoading();
  56. console.log("我请求到了token,携带token请求数据,我的token为" + wx.getStorageSync('token'));
  57. fly.unlock();
  58. return request;
  59. }
  60. else{
  61. console.log("没有token也没有授权,我需要重新生成token,先请求token...");
  62. wx.login({
  63. success(wxres) {
  64. return newFly.request({
  65. url: '/auth/mobile/token/social?grant_type=mobil&mobile=MINI@'+wxres.code,
  66. method: 'post',
  67. headers:{
  68. 'content-type': 'application/json',
  69. 'X-Tag': 'flyio',
  70. 'Authorization': 'Basic dGVzdDp0ZXN0'
  71. }
  72. }).then((res) => {
  73. if (res.data.access_token) {
  74. wx.setStorageSync('token', res.data.access_token);
  75. }
  76. console.log("token请求成功,值为: " + res.data.access_token);
  77. console.log(`继续完成请求:path:${request.url},baseURL:${request.baseURL}`)
  78. return request
  79. }).finally(() => fly.unlock()) //解锁后,会继续发起请求队列中的任务
  80. }
  81. })
  82. }
  83. }, (error, promise) => {
  84. // Do something with request error
  85. console.log(error); // for debug
  86. promise.reject(error)
  87. });
  88. fly.interceptors.response.use(
  89. (response) => {
  90. //正常返回
  91. if(isShowing)
  92. {
  93. wx.hideLoading();
  94. isShowing =false;
  95. }
  96. reqCount =0;
  97. return response
  98. },
  99. (err, promise) => {
  100. if(isShowing)
  101. {
  102. wx.hideLoading();
  103. isShowing =false;
  104. }
  105. let msg = '';
  106. if (err.status === 0) {
  107. msg = '网络连接异常'
  108. }
  109. else if (err.status === 1) {
  110. msg = '网络连接超时'
  111. }
  112. else if (err.status === 401) {
  113. if (err.request.url.indexOf('mobil') === -1) {
  114. reqCount += 1
  115. if (reqCount < 3) {
  116. console.log("401错误,我需要重新请求+我现在使用的过期token"+ wx.getStorageSync('token'));
  117. wx.clearStorageSync('token')
  118. return fly.request(err.request);
  119. }
  120. }
  121. }
  122. else if (err.status === 502) {
  123. reqCount += 1
  124. if (reqCount < 3) {
  125. console.log("502错误,我需要重新请求重试次数" + reqCount.toString());
  126. msg = '网络错误,正在进行自动重试,重试次数'+reqCount.toString();
  127. return fly.request(err.request);
  128. }
  129. }
  130. else {
  131. if (err.response.data.message) {
  132. msg = err.response.data.message
  133. } else {
  134. msg = '程序已3次连接失败,请稍后手动登录后再试'
  135. }
  136. }
  137. wx.showToast({
  138. title: msg,
  139. icon: 'none',
  140. duration: 3000
  141. });
  142. return promise.resolve(err)
  143. }
  144. )
  145. export default fly;