service.js 3.8 KB

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