request.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import {baseUrl} from './config.js'
  2. import store from '@/store/index.js'
  3. import {apiWXLogin} from '@/api/user.js'
  4. // 请求错误消息提示
  5. const showError=error=>{
  6. let errMsg=''
  7. switch(error.code){
  8. case 400:
  9. errMsg=error.msg;
  10. break;
  11. default:
  12. errMsg=error.msg;
  13. break;
  14. }
  15. uni.showToast({
  16. title:errMsg,
  17. icon:'none',
  18. duration:1000
  19. })
  20. }
  21. // 刷新token 微信登录
  22. const wechatLogin=()=>{
  23. return new Promise((resolve,reject)=>{
  24. uni.login({
  25. provider: 'weixin',
  26. success: function (loginRes) {
  27. apiWXLogin({Code:loginRes.code}).then(res=>{
  28. if(res.code===200){
  29. store.commit('addToken', res.data.Authorization)
  30. store.commit('addUserData',res.data.UserInfo)
  31. resolve(res.data)
  32. }
  33. })
  34. },
  35. fail:function(loginErr){
  36. uni.showToast({
  37. title:"微信登录失败",
  38. icon:"none"
  39. })
  40. }
  41. })
  42. })
  43. }
  44. let requestList=[]//存放token失效时请求队列
  45. let isRefreshing=false//是否正在刷新token
  46. /**
  47. * 刷新token
  48. */
  49. const refreshToken=async (url,params,method,resolve)=>{
  50. requestList.push(()=>{resolve(http(url,params,method))})
  51. if(!isRefreshing){
  52. isRefreshing=true
  53. const wechatLoginRes=await wechatLogin()
  54. console.log(wechatLoginRes);
  55. // 重新请求队列
  56. requestList.map(MT=>{MT()})
  57. requestList=[]
  58. isRefreshing=false
  59. }
  60. }
  61. // 请求数
  62. let LOADINGCOUNT = 0;
  63. const http=(url,params,method)=>{
  64. // 设置loading
  65. if (LOADINGCOUNT === 0) {
  66. uni.showLoading({
  67. title:'加载中...'
  68. })
  69. }
  70. LOADINGCOUNT++;
  71. return new Promise((resolve,reject)=>{
  72. uni.request({
  73. url:baseUrl+url,
  74. data:params,
  75. method:method,
  76. header:{
  77. Authorization:store.state.token,
  78. },
  79. success(res) {
  80. if(![200,401,403].includes(res.data.code)){
  81. setTimeout(()=>{showError(res.data)},0)//解决 hideloading 冲突问题
  82. }
  83. //401 代表token异常,用户需要重新静默授权,获取最新的token
  84. //403 用户需要进行绑定操作,需要跳转到输入账号密码绑定页面用户需要进行绑定操作
  85. if(res.data.code===401){
  86. refreshToken(url,params,method,resolve)
  87. return
  88. }
  89. if(res.data.code===403){
  90. uni.reLaunch({
  91. url:"/pages/login/login"
  92. })
  93. }
  94. resolve(res.data)
  95. },
  96. fail(error) {
  97. console.log(error);
  98. setTimeout(() => {
  99. uni.showToast({
  100. title:'网络异常',
  101. icon:'none'
  102. })
  103. }, 0);
  104. },
  105. complete() {
  106. // 关闭loading
  107. LOADINGCOUNT--;
  108. if (LOADINGCOUNT === 0) {
  109. uni.hideLoading()
  110. }
  111. }
  112. })
  113. })
  114. }
  115. // get 请求
  116. export const httpGet=(url,params)=>{
  117. return http(url,params,'GET')
  118. }
  119. // post 请求
  120. export const httpPost=(url,params)=>{
  121. return http(url,params,'POST')
  122. }