request.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import {baseApiUrl} from './config.js'
  2. import {apiWechatLogin} from '@/api/user'
  3. import CryptoJS from './crypto'
  4. import store from '@/store/index'
  5. const logger = require('./log.js')
  6. const ENV=uni.getAccountInfoSync().miniProgram
  7. // 请求错误消息提示
  8. const showError=error=>{
  9. let errMsg=''
  10. switch(error.code){
  11. case 400:
  12. errMsg=error.msg;
  13. break;
  14. default:
  15. errMsg=error.msg;
  16. break;
  17. }
  18. setTimeout(()=>{
  19. uni.showToast({
  20. title:errMsg,
  21. icon:'none'
  22. })
  23. },0)
  24. }
  25. // 微信登录
  26. const wechatLogin=()=>{
  27. return new Promise((resolve,reject)=>{
  28. uni.login({
  29. provider: 'weixin',
  30. success: function (loginRes) {
  31. apiWechatLogin({code:loginRes.code}).then(res=>{
  32. if(res.code===200){
  33. store.commit('setToken', res.data.authorization)
  34. store.dispatch('getUserInfo')
  35. resolve(res.data)
  36. }
  37. })
  38. },
  39. fail:function(loginErr){
  40. uni.showToast({
  41. title:"微信登录失败",
  42. icon:"none"
  43. })
  44. }
  45. })
  46. })
  47. }
  48. let requestList=[]//存放token失效时请求队列
  49. let isRefreshing=false//是否正在刷新token
  50. /**
  51. * 刷新token
  52. */
  53. const refreshToken=async (url,params,method,resolve)=>{
  54. requestList.push(()=>{resolve(http(url,params,method))})
  55. if(!isRefreshing){
  56. isRefreshing=true
  57. const wechatLoginRes=await wechatLogin()
  58. console.log(wechatLoginRes);
  59. // if(!wechatLoginRes.is_bind){
  60. // uni.reLaunch({
  61. // url:'/pages/login'
  62. // })
  63. // isRefreshing=false
  64. // return
  65. // }
  66. // 重新请求队列
  67. requestList.map(MT=>{MT()})
  68. requestList=[]
  69. isRefreshing=false
  70. }
  71. }
  72. let LOADINGCOUNT = 0;// 请求数
  73. const http=(url,params,method)=>{
  74. // 设置loading
  75. if(url!='/public/get_share_poster'){
  76. if (LOADINGCOUNT === 0) {
  77. uni.showLoading({
  78. title:'加载中...'
  79. })
  80. }
  81. LOADINGCOUNT++;
  82. }
  83. return new Promise((resolve,reject)=>{
  84. uni.request({
  85. url:baseApiUrl+url,
  86. data:params,
  87. method:method,
  88. header:{
  89. Authorization:store.state.user.token,
  90. version:'yb11.9',
  91. thirdCode:store.state.user.thirdCode
  92. },
  93. success(e) {
  94. // 接口404
  95. if(e.statusCode===404){
  96. setTimeout(()=>{
  97. uni.showToast({
  98. title:'network:404',
  99. icon:'none'
  100. })
  101. },0)
  102. return
  103. }
  104. let res
  105. if(ENV.envVersion==='release'){
  106. res=JSON.parse(CryptoJS.Des3Decrypt(e.data));//解密
  107. }else{
  108. res=e.data
  109. }
  110. if(res.code!==200&&res.code!==403&&res.code!==4001&&res.code!==401){
  111. showError(res)
  112. }
  113. // 401 token失效
  114. if(res.code===401){
  115. refreshToken(url,params,method,resolve)
  116. return
  117. }
  118. resolve(res)
  119. },
  120. fail(error) {
  121. console.log(error);
  122. if(ENV.envVersion==='release'){
  123. logger.error('接口请求fail',error)
  124. }
  125. setTimeout(()=>{
  126. uni.showToast({
  127. title:'网络异常,稍后重试!',
  128. icon:'none'
  129. })
  130. },0)
  131. },
  132. complete() {
  133. // 关闭loading
  134. LOADINGCOUNT--;
  135. if (LOADINGCOUNT === 0) {
  136. uni.hideLoading()
  137. }
  138. }
  139. })
  140. })
  141. }
  142. // get 请求
  143. export const httpGet=(url,params)=>{
  144. return http(url,params,'GET')
  145. }
  146. // post 请求
  147. export const httpPost=(url,params)=>{
  148. return http(url,params,'POST')
  149. }