service.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import Request from '@/common/luch-request/index.js'
  2. import {ACCESS_TOKEN} from '@/common/util/constants.js'
  3. import configService from './config.service.js'
  4. import tip from '@/common/util/tip.js';
  5. import store from '@/store/index.js';
  6. let apiUrl = configService.apiUrl;
  7. const getTokenStorage = () => {
  8. let token = ''
  9. try{
  10. token = uni.getStorageSync(ACCESS_TOKEN)
  11. }catch(e){
  12. //TODO handle the exception
  13. console.log("getTokenStorage",token)
  14. }
  15. return token
  16. }
  17. const http = new Request()
  18. http.setConfig((config) => { /* 设置全局配置 */
  19. config.baseUrl = apiUrl /* 根域名不同 */
  20. config.header = {
  21. ...config.header
  22. }
  23. return config
  24. })
  25. /**
  26. * 自定义验证器,如果返回true 则进入响应拦截器的响应成功函数(resolve),否则进入响应拦截器的响应错误函数(reject)
  27. * @param { Number } statusCode - 请求响应体statusCode(只读)
  28. * @return { Boolean } 如果为true,则 resolve, 否则 reject
  29. */
  30. // 有默认,非必写
  31. http.validateStatus = (statusCode) => {
  32. return statusCode === 200
  33. }
  34. http.interceptor.request((config, cancel) => { /* 请求之前拦截器 */
  35. //判断是否请求白名单,设置带不带token
  36. let url= config.url;
  37. //console.log(url)
  38. if(url=="/ibps/oauth2/v3/user/login/apply"
  39. ||url=="/ibps/oauth2/v3/authorize/apply"
  40. ||url=="/ibps/oauth2/v3/authentication/apply"
  41. ){
  42. console.log(url)
  43. return config;
  44. }
  45. config.header = {
  46. ...config.header,
  47. 'X-Authorization-access_token':getTokenStorage()
  48. }
  49. /* if (!token) { // 如果token不存在,调用cancel 会取消本次请求,但是该函数的catch() 仍会执行
  50. cancel('token 不存在') // 接收一个参数,会传给catch((err) => {}) err.errMsg === 'token 不存在'
  51. } */
  52. return config
  53. })
  54. // 必须使用异步函数,注意
  55. http.interceptor.response(async (response) => { /* 请求之后拦截器 */
  56. // if (response.data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
  57. // return Promise.reject(response)
  58. // }
  59. /* if (response.data.data.state !== 50002) { // 服务端返回的状态码不等于200,则reject()
  60. console.log("服务不可用")
  61. return Promise.reject(response)
  62. }
  63. if (response.data.data.state !== 200) { // 服务端返回的状态码不等于200,则reject()
  64. return Promise.reject(response)
  65. } */
  66. // console.log(response)
  67. return response
  68. }, (response) => {
  69. // 请求错误做点什么
  70. console.log("请求错误做点什么");
  71. let index=JSON.stringify(response).toString().indexOf("request:fail");
  72. if(index==11){
  73. tip.alert("网络错误!")
  74. return;
  75. }
  76. if (response) {
  77. let data = response.data
  78. const token = uni.getStorageSync(ACCESS_TOKEN)
  79. console.log("------异常响应------",token)
  80. // console.log(data.state==undefined)
  81. // console.log("------异常响应------",data.state)
  82. switch (data.state) {
  83. case 50002:
  84. tip.error('服务不可用');
  85. break
  86. case 6020301:
  87. if(!token || data.message=="Token失效,请重新登录"){
  88. let timeout=setTimeout(tip.alert('登录已过期'), 1000);
  89. store.dispatch('Logout').then(() => {
  90. clearTimeout(timeout)
  91. window.location.reload()
  92. })
  93. }
  94. break
  95. case 404:
  96. break
  97. case 504:
  98. break
  99. case 401:
  100. if (token) {
  101. /* store.dispatch('Logout').then(() => {
  102. setTimeout(() => {
  103. window.location.reload()
  104. }, 1500)
  105. }) */
  106. }
  107. break
  108. default:
  109. tip.error({
  110. duration: 0,
  111. forbidClick: true,
  112. message: data.cause
  113. });
  114. break
  115. }
  116. }
  117. return response
  118. })
  119. export {
  120. http
  121. }