service.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. config.header = {
  36. ...config.header,
  37. 'X-Access-Token':getTokenStorage()
  38. }
  39. /*
  40. if (!token) { // 如果token不存在,调用cancel 会取消本次请求,但是该函数的catch() 仍会执行
  41. cancel('token 不存在') // 接收一个参数,会传给catch((err) => {}) err.errMsg === 'token 不存在'
  42. }
  43. */
  44. return config
  45. })
  46. // 必须使用异步函数,注意
  47. var that = this
  48. http.interceptor.response(async (response) => { /* 请求之后拦截器 */
  49. // if (response.data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
  50. // return Promise.reject(response)
  51. // }
  52. return response
  53. }, (response) => {
  54. // 请求错误做点什么
  55. console.log("请求错误做点什么",response);
  56. if (response) {
  57. let data = response.data
  58. const token = uni.getStorageSync(ACCESS_TOKEN)
  59. console.log("------异常响应------",token)
  60. console.log("------异常响应------",data.code)
  61. switch (data.code) {
  62. case 403:
  63. tip.error('拒绝访问');
  64. break
  65. case 500:
  66. if(!token || data.message=="Token失效,请重新登录"){
  67. let timeout=setTimeout(tip.alert('登录已过期'), 1000);
  68. store.dispatch('Logout').then(() => {
  69. clearTimeout(timeout)
  70. window.location.reload()
  71. })
  72. }
  73. break
  74. case 404:
  75. break
  76. case 504:
  77. break
  78. case 401:
  79. console.log("------异常响应-401-----",401)
  80. console.log("------uni-----",uni)
  81. uni.navigateTo({
  82. url: '/pages/login/login'
  83. });
  84. return
  85. if (token) {
  86. /* store.dispatch('Logout').then(() => {
  87. setTimeout(() => {
  88. window.location.reload()
  89. }, 1500)
  90. }) */
  91. }
  92. break
  93. default:
  94. tip.error({
  95. duration: 0,
  96. forbidClick: true,
  97. message: data.message
  98. });
  99. break
  100. }
  101. }
  102. return response
  103. })
  104. export {
  105. http
  106. }