request.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. fly.unlock();
  33. return request;
  34. }
  35. //头文件带Authorization为请求token,不添加token
  36. if (request.headers.Authorization) {
  37. // 给所有请求添加自定义header
  38. request.timeout = 30000;
  39. Object.assign(request.headers, {
  40. 'content-type': 'application/json',
  41. 'X-Tag': 'flyio'
  42. })
  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. fly.unlock();
  56. return request;
  57. }
  58. else{
  59. console.log("没有token,先请求token...");
  60. wx.login({
  61. success(wxres) {
  62. /* requestAPI.userLogin(wxres.code).then(res => {
  63. if (res.status === 401) {
  64. self.setData({
  65. ifUser: false
  66. })
  67. } else {
  68. if (res.data.access_token) {
  69. wx.setStorageSync('token', res.data.access_token);
  70. wx.switchTab({
  71. url: '/pages/home/home',
  72. })
  73. }
  74. }
  75. }) */
  76. //锁定当天实例,后续请求会在拦截器外排队
  77. return newFly.request({
  78. url: '/auth/mobile/token/social?grant_type=mobil&mobile=MINI@'+wxres.code,
  79. method: 'post',
  80. headers:{
  81. 'content-type': 'application/json',
  82. 'X-Tag': 'flyio',
  83. 'Authorization': 'Basic dGVzdDp0ZXN0'
  84. }
  85. }).then((res) => {
  86. if (res.data.access_token) {
  87. wx.setStorageSync('token', res.data.access_token);
  88. }
  89. console.log("token请求成功,值为: " + res.data.access_token);
  90. console.log(`继续完成请求:path:${request.url},baseURL:${request.baseURL}`)
  91. return request
  92. }).finally(() => fly.unlock()) //解锁后,会继续发起请求队列中的任务
  93. }
  94. })
  95. }
  96. }, (error, promise) => {
  97. // Do something with request error
  98. console.log(error); // for debug
  99. promise.reject(error)
  100. });
  101. fly.interceptors.response.use(
  102. (response) => {
  103. //正常返回
  104. reqCount =0;
  105. wx.hideLoading();
  106. return response
  107. },
  108. (err, promise) => {
  109. wx.hideLoading();
  110. let msg = '';
  111. if (err.status === 0) {
  112. msg = '网络连接异常'
  113. }
  114. else if (err.status === 1) {
  115. msg = '网络连接超时'
  116. }
  117. else if (err.status === 401) {
  118. //reqCount += 1
  119. wx.clearStorageSync('token')
  120. /* if (reqCount < 3) {
  121. msg = '用户已过期,正在自动刷新用户,尝试次数'+reqCount.toString();
  122. this.lock(); //锁定响应拦截器
  123. wx.login({ success(wxres) {
  124. newFly.request(
  125. { url: '/auth/mobile/token/social?grant_type=mobil&mobile=MINI@'+ wxres.code,
  126. method: 'post',
  127. headers:{ 'Authorization': 'Basic dGVzdDp0ZXN0'}})
  128. .then(res => { if (res.data.access_token) {wx.setStorageSync('token', res.data.access_token); }})
  129. .finally(() => this.unlock())
  130. .then(() => { return fly.request(err.request); })}})
  131. } */
  132. return fly.request(err.request);
  133. }
  134. else if (err.status === 502) {
  135. reqCount += 1
  136. if (reqCount < 3) {
  137. msg = '网络错误,正在进行自动重试,重试次数'+reqCount.toString();
  138. return fly.request(err.request);
  139. }
  140. }
  141. else {
  142. if (err.response.data.message) {
  143. msg = err.response.data.message
  144. } else {
  145. msg = '程序已3次连接失败,请稍后手动登录后再试'
  146. }
  147. }
  148. wx.showToast({
  149. title: msg,
  150. icon: 'none',
  151. duration: 3000
  152. });
  153. return promise.resolve(err)
  154. }
  155. )
  156. export default fly;