request.js 2.9 KB

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