request.js 3.0 KB

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