request.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import store from '@/store'
  2. import config from '@/config'
  3. import { getToken } from '@/utils/auth'
  4. import errorCode from '@/utils/errorCode'
  5. import { toast, showConfirm, tansParams } from '@/utils/common'
  6. let timeout = 10000
  7. const baseUrl = config.baseUrl
  8. let isRelogin = false // 防止重复登录弹窗
  9. let silentLoginStatus = true // 暂时没有静默逻辑
  10. const request = config => {
  11. // 是否需要设置 token
  12. const isToken = (config.headers || {}).isToken === false
  13. config.header = config.header || {}
  14. // 添加全局请求头
  15. config.header['X-Request-Source'] = 'mobile'
  16. if (getToken() && !isToken) {
  17. config.header['Authorization'] = 'Bearer ' + getToken()
  18. }
  19. // get请求映射params参数
  20. if (config.params) {
  21. let url = config.url + '?' + tansParams(config.params)
  22. url = url.slice(0, -1)
  23. config.url = url
  24. }
  25. return new Promise((resolve, reject) => {
  26. uni.request({
  27. method: config.method || 'get',
  28. timeout: config.timeout || timeout,
  29. url: config.baseUrl || baseUrl + config.url,
  30. data: config.data,
  31. header: config.header,
  32. dataType: 'json'
  33. }).then(response => {
  34. let [error, res] = response
  35. if (error) {
  36. // toast('后端接口连接异常')
  37. reject('后端接口连接异常')
  38. return
  39. }
  40. const code = res.data.code || 200
  41. const msg = errorCode[code] || res.data.msg || errorCode['default']
  42. if (code === 401) {
  43. // 静默登陆 等待后端实现 刷新token 或者 支持验证码后门才行
  44. // 这里静默登陆 只保留一个静默登陆的请求;防止并发请求出现多次静默登陆
  45. if (!silentLoginStatus) {
  46. silentLoginStatus = uni.request({
  47. method: config.method || 'get',
  48. timeout: config.timeout || timeout,
  49. url: config.baseUrl || baseUrl + config.url,
  50. data: config.data,
  51. header: config.header,
  52. dataType: 'json'
  53. })
  54. }
  55. // promise已经兑现 这里即使多次也只是单纯的获取以兑现的promise结果
  56. silentLoginStatus && silentLoginStatus instanceof Promise && silentLoginStatus.then(res => {
  57. // 重新请求一次将新的响应体返回给当前请求处理的resolve
  58. request(config).then(resolve).catch(reject)
  59. })
  60. // 上面实现下面可以逻辑就可以删除了
  61. // 获取当前页面信息
  62. const pages = getCurrentPages()
  63. const currentPage = pages[pages.length - 1]
  64. const currentRoute = currentPage ? currentPage.route : ''
  65. // 如果在登录页面,不显示弹窗,直接清除token
  66. if (currentRoute === 'pages/login') {
  67. const { removeToken } = require('@/utils/auth')
  68. const storage = require('@/utils/storage').default
  69. removeToken()
  70. storage.clean()
  71. store.commit('SET_TOKEN', '')
  72. store.commit('SET_ROLES', [])
  73. store.commit('SET_PERMISSIONS', [])
  74. reject('无效的会话,或者会话已过期,请重新登录。')
  75. return
  76. }
  77. if (!isRelogin) {
  78. isRelogin = true
  79. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  80. if (res.confirm) {
  81. // 直接清除本地数据,不调用logout API避免循环
  82. store.commit('SET_TOKEN', '')
  83. store.commit('SET_ROLES', [])
  84. store.commit('SET_PERMISSIONS', [])
  85. const { removeToken } = require('@/utils/auth')
  86. const storage = require('@/utils/storage').default
  87. removeToken()
  88. storage.clean()
  89. uni.reLaunch({ url: '/pages/login' })
  90. }
  91. isRelogin = false
  92. }).catch(() => {
  93. isRelogin = false
  94. })
  95. }
  96. reject('无效的会话,或者会话已过期,请重新登录。')
  97. } else if (code === 500) {
  98. console.log("util=======", msg)
  99. // toast(msg)
  100. reject({ "code": code, "msg": msg })
  101. } if (code !== 200) {
  102. // toast(msg)
  103. reject(code)
  104. }
  105. resolve(res.data)
  106. })
  107. .catch(error => {
  108. let { message } = error
  109. if (message === 'Network Error') {
  110. message = '后端接口连接异常'
  111. } else if (message.includes('timeout')) {
  112. message = '系统接口请求超时'
  113. } else if (message.includes('Request failed with status code')) {
  114. message = '系统接口' + message.substr(message.length - 3) + '异常'
  115. }
  116. // toast(message)
  117. reject(error)
  118. })
  119. })
  120. }
  121. export default request