service.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 =store.getters.token
  11. /* uni.getStorageSync(ACCESS_TOKEN) */
  12. }catch(e){
  13. //TODO handle the exception
  14. console.log("getTokenStorage",token)
  15. }
  16. return token
  17. }
  18. const http = new Request()
  19. http.setConfig((config) => { /* 设置全局配置 */
  20. config.baseUrl = apiUrl /* 根域名不同 */
  21. config.header = {
  22. ...config.header
  23. }
  24. return config
  25. })
  26. /**
  27. * 自定义验证器,如果返回true 则进入响应拦截器的响应成功函数(resolve),否则进入响应拦截器的响应错误函数(reject)
  28. * @param { Number } statusCode - 请求响应体statusCode(只读)
  29. * @return { Boolean } 如果为true,则 resolve, 否则 reject
  30. */
  31. // 有默认,非必写
  32. http.validateStatus = (statusCode) => {
  33. return statusCode === 200
  34. }
  35. http.interceptor.request((config, cancel) => { /* 请求之前拦截器 */
  36. /* tip.alert(config.baseUrl) */
  37. config.header = {
  38. ...config.header,
  39. 'X-Access-Token':getTokenStorage()
  40. }
  41. /*
  42. if (!token) { // 如果token不存在,调用cancel 会取消本次请求,但是该函数的catch() 仍会执行
  43. cancel('token 不存在') // 接收一个参数,会传给catch((err) => {}) err.errMsg === 'token 不存在'
  44. }
  45. */
  46. return config
  47. })
  48. // 必须使用异步函数,注意
  49. http.interceptor.response(async (response) => { /* 请求之后拦截器 */
  50. // if (response.data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
  51. // return Promise.reject(response)
  52. // }
  53. return response
  54. }, (response) => {
  55. // 请求错误做点什么
  56. console.log("请求错误做点什么",response);
  57. if (response) {
  58. let data = response.data
  59. const token = store.getters.token
  60. //uni.getStorageSync(ACCESS_TOKEN)
  61. console.log("------异常响应------",token)
  62. console.log("------异常响应------",data.status)
  63. switch (data.status) {
  64. case 403:
  65. tip.error('拒绝访问');
  66. break
  67. case 500:
  68. if(!token || data.message=="Token失效,请重新登录"){
  69. uni.clearStorageSync()
  70. store.commit('SET_TOKEN',"");
  71. store.commit('SET_USERID',"");
  72. // uni.navigateTo({
  73. // url:'/pages/login/login'
  74. // })
  75. /* window.location.reload() */
  76. // uni.navigateTo({
  77. // url:'/pages/login/login'
  78. // })
  79. window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww7c43b4b5f33945ad&redirect_uri=hr3.gksports.net.cn:8082&response_type=code&scope=snsapi_userinfo&agentid=1000018&state=STATE#wechat_redirect";
  80. //window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wwd372e7740b9b586f&redirect_uri=zdd.g.dr1997.com&response_type=code&scope=snsapi_userinfo&agentid=1000003&state=STATE#wechat_redirect";
  81. /* store.dispatch('Logout').then(() => {
  82. clearTimeout(timeout)
  83. uni.navigateTo({
  84. url:'/pages/login/login'
  85. })
  86. }) */
  87. }if(data.message=="该账号已在其它地方登录!"){
  88. store.dispatch('Logout').then(() => {
  89. clearTimeout(timeout)
  90. window.location.reload()
  91. })
  92. }
  93. break
  94. case 404:
  95. break
  96. case 504:
  97. break
  98. case 401:
  99. if (token) {
  100. /* store.dispatch('Logout').then(() => {
  101. setTimeout(() => {
  102. window.location.reload()
  103. }, 1500)
  104. }) */
  105. }
  106. break
  107. default:
  108. tip.error({
  109. duration: 0,
  110. forbidClick: true,
  111. message: data.message
  112. });
  113. break
  114. }
  115. }
  116. return response
  117. })
  118. export {
  119. http
  120. }