request.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // 获取白名单
  10. import whiteList from './whiteList';
  11. // Add interceptors
  12. fly.interceptors.request.use(function (request) {
  13. fly.lock();
  14. // console.log('进入fly-request', request);
  15. /* wx.showLoading({
  16. 'title': '加载中',
  17. 'mask': true
  18. }); */
  19. // 不显示加载页面的接口
  20. if (request.url.indexOf(whiteList.loading) === -1) {
  21. // 隐藏loading遮罩
  22. // wx.hideLoading();
  23. }
  24. // 白名单内url不添加token
  25. if (request.url.indexOf(whiteList.nullHeaderToken) !== -1) {
  26. request.timeout = 30000; // 请求超时
  27. request.headers = {
  28. 'content-type': 'application/json',
  29. 'X-Tag': 'flyio'
  30. };
  31. console.log('nullHeaderToken()')
  32. // wx.hideLoading();
  33. fly.unlock();
  34. return request;
  35. }
  36. //头文件带Authorization为请求token,不添加token
  37. if (request.headers.Authorization) {
  38. // 给所有请求添加自定义header
  39. request.timeout = 30000;
  40. Object.assign(request.headers, {
  41. 'content-type': 'application/json',
  42. 'X-Tag': 'flyio'
  43. })
  44. //wx.hideLoading();
  45. fly.unlock();
  46. return request;
  47. }
  48. //其他请求需要在头文件中自动附加token
  49. if(wx.getStorageSync('token')) {
  50. // 给所有请求添加自定义header
  51. request.timeout = 30000;
  52. Object.assign(request.headers, {
  53. 'content-type': 'application/json',
  54. 'X-Tag': 'flyio',
  55. 'Authorization': `Bearer ${wx.getStorageSync('token')}`
  56. })
  57. // wx.hideLoading();
  58. fly.unlock();
  59. return request;
  60. }
  61. else{
  62. console.log("没有token,先请求token...");
  63. // wx.hideLoading();
  64. wx.login({
  65. success(wxres) {
  66. //锁定当天实例,后续请求会在拦截器外排队
  67. return newFly.request({
  68. url: '/auth/mobile/token/social?grant_type=mobil&mobile=MINI@'+wxres.code,
  69. method: 'post',
  70. headers:{
  71. 'content-type': 'application/json',
  72. 'X-Tag': 'flyio',
  73. 'Authorization': 'Basic dGVzdDp0ZXN0'
  74. }
  75. }).then((res) => {
  76. if (res.data.access_token) {
  77. wx.setStorageSync('token', res.data.access_token);
  78. }
  79. console.log("token请求成功,值为: " + res.data.access_token);
  80. console.log(`继续完成请求:path:${request.url},baseURL:${request.baseURL}`)
  81. return request
  82. }).finally(() => fly.unlock()) //解锁后,会继续发起请求队列中的任务
  83. }
  84. })
  85. }
  86. }, (error, promise) => {
  87. // Do something with request error
  88. console.log(error); // for debug
  89. promise.reject(error)
  90. });
  91. fly.interceptors.response.use(
  92. (response) => {
  93. //正常返回
  94. reqCount =0;
  95. // wx.hideLoading();
  96. return response
  97. },
  98. (err, promise) => {
  99. // wx.hideLoading();
  100. let msg = '';
  101. if (err.status === 0) {
  102. msg = '网络连接异常'
  103. }
  104. else if (err.status === 1) {
  105. msg = '网络连接超时'
  106. }
  107. else if (err.status === 401) {
  108. //reqCount += 1
  109. wx.clearStorageSync('token')
  110. /* if (reqCount < 3) {
  111. msg = '用户已过期,正在自动刷新用户,尝试次数'+reqCount.toString();
  112. this.lock(); //锁定响应拦截器
  113. wx.login({ success(wxres) {
  114. newFly.request(
  115. { url: '/auth/mobile/token/social?grant_type=mobil&mobile=MINI@'+ wxres.code,
  116. method: 'post',
  117. headers:{ 'Authorization': 'Basic dGVzdDp0ZXN0'}})
  118. .then(res => { if (res.data.access_token) {wx.setStorageSync('token', res.data.access_token); }})
  119. .finally(() => this.unlock())
  120. .then(() => { return fly.request(err.request); })}})
  121. } */
  122. return fly.request(err.request);
  123. }
  124. else if (err.status === 502) {
  125. reqCount += 1
  126. if (reqCount < 3) {
  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;