request.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. wx.setStorageSync('username', res.data.username);
  76. }
  77. console.log("token请求成功,值为: " + res.data.access_token);
  78. console.log(`继续完成请求:path:${request.url},baseURL:${request.baseURL}`)
  79. return request
  80. }).finally(() => fly.unlock()) //解锁后,会继续发起请求队列中的任务
  81. }
  82. })
  83. }
  84. }, (error, promise) => {
  85. // Do something with request error
  86. console.log(error); // for debug
  87. promise.reject(error)
  88. });
  89. fly.interceptors.response.use(
  90. (response) => {
  91. //正常返回
  92. if(isShowing)
  93. {
  94. wx.hideLoading();
  95. isShowing =false;
  96. }
  97. reqCount =0;
  98. return response
  99. },
  100. (err, promise) => {
  101. if(isShowing)
  102. {
  103. wx.hideLoading();
  104. isShowing =false;
  105. }
  106. let msg = '';
  107. if (err.status === 0) {
  108. msg = '网络连接异常'
  109. }
  110. else if (err.status === 1) {
  111. msg = '网络连接超时'
  112. }
  113. else if (err.status === 401) {
  114. if (err.request.url.indexOf('mobil') === -1) {
  115. reqCount += 1
  116. if (reqCount < 3) {
  117. console.log("401错误,我需要重新请求+我现在使用的过期token"+ wx.getStorageSync('token'));
  118. wx.clearStorageSync('token')
  119. return fly.request(err.request);
  120. }
  121. }
  122. }
  123. else if (err.status === 502) {
  124. reqCount += 1
  125. if (reqCount < 3) {
  126. console.log("502错误,我需要重新请求重试次数" + reqCount.toString());
  127. msg = '网络错误,正在进行自动重试,重试次数'+reqCount.toString();
  128. return fly.request(err.request);
  129. }
  130. }
  131. else {
  132. if (err.response.data.message) {
  133. msg = err.response.data.message
  134. } else {
  135. msg = '程序已3次连接失败,请稍后手动登录后再试'
  136. }
  137. }
  138. wx.showToast({
  139. title: msg,
  140. icon: 'none',
  141. duration: 3000
  142. });
  143. return promise.resolve(err)
  144. }
  145. )
  146. export default fly;