request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import store from '@/store'
  2. import config from '@/config'
  3. import {
  4. getToken,
  5. getSsoAdminToken
  6. } from '@/utils/auth'
  7. import errorCode from '@/utils/errorCode'
  8. import {
  9. toast,
  10. showConfirm,
  11. tansParams
  12. } from '@/utils/common'
  13. const ssoToken = getSsoAdminToken()
  14. let timeout = 10000
  15. const baseUrl = config.baseUrl
  16. const request = config => {
  17. // 是否需要设置 token
  18. const isToken = (config.headers || {}).isToken === false
  19. config.header = config.header || {}
  20. if (getToken() && !isToken) {
  21. config.header['Authorization'] = 'Bearer ' + getToken()
  22. }
  23. // 存在ssoToken
  24. // console.log(ssoToken,getSsoAdminToken(),'ssoTokenrequest')
  25. if (getSsoAdminToken()) {
  26. config.header['xxl_sso_sessionid'] = getSsoAdminToken() // 让每个请求携带自定义ssoToken 请根据实际情况自行修改
  27. }
  28. // get请求映射params参数
  29. if (config.params) {
  30. let url = config.url + '?' + tansParams(config.params)
  31. url = url.slice(0, -1)
  32. config.url = url
  33. }
  34. return new Promise((resolve, reject) => {
  35. uni.request({
  36. method: config.method || 'get',
  37. timeout: config.timeout || timeout,
  38. url: config.baseUrl || baseUrl + config.url,
  39. data: config.data,
  40. header: config.header,
  41. dataType: 'json'
  42. }).then(response => {
  43. let [error, res] = response
  44. if (error) {
  45. console.log(error,config.baseUrl || baseUrl + config.url,'error')
  46. toast('后端接口连接异常')
  47. reject('后端接口连接异常')
  48. return
  49. }
  50. const code = res.data.code || 200
  51. const msg = errorCode[code] || res.data.msg || errorCode['default']
  52. if (code === 401) {
  53. showConfirm('登录状态已过期,请重新登录!').then(res => {
  54. if (res.confirm) {
  55. store.dispatch('LogOut').then(res => {
  56. uni.reLaunch({
  57. url: '/pages/login'
  58. })
  59. })
  60. }
  61. })
  62. reject('无效的会话,或者会话已过期,请重新登录。')
  63. } else if (code === 500) {
  64. toast(msg)
  65. reject('500')
  66. } else if (code === 501) {
  67. if (msg.indexOf('sso not login') > -1) {
  68. showConfirm('单点秘钥不存在,请重新登录!').then(res => {
  69. if (res.confirm) {
  70. store.dispatch('LogOut').then(res => {
  71. uni.reLaunch({
  72. url: '/pages/login'
  73. })
  74. })
  75. }
  76. })
  77. reject('无效的会话,或者会话已过期,请重新登录。')
  78. } else {
  79. toast(msg)
  80. reject('501')
  81. }
  82. } else if (code !== 200) {
  83. toast(msg)
  84. reject(code)
  85. }
  86. resolve(res.data)
  87. })
  88. .catch(error => {
  89. let {
  90. message
  91. } = error
  92. if (message === 'Network Error') {
  93. message = '后端接口连接异常'
  94. } else if (message.includes('timeout')) {
  95. message = '系统接口请求超时'
  96. } else if (message.includes('Request failed with status code')) {
  97. message = '系统接口' + message.substr(message.length - 3) + '异常'
  98. }
  99. toast(message)
  100. reject(error)
  101. })
  102. })
  103. }
  104. export default request