request.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import store from '@/store'
  2. import config from '@/config'
  3. import {
  4. getToken,
  5. removeToken
  6. } from '@/utils/auth'
  7. import errorCode from '@/utils/errorCode'
  8. import {
  9. toast,
  10. showConfirm,
  11. tansParams
  12. } from '@/utils/common'
  13. import storage from '@/utils/storage'
  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. // get请求映射params参数
  24. if (config.params) {
  25. let url = config.url + '?' + tansParams(config.params)
  26. url = url.slice(0, -1)
  27. config.url = url
  28. }
  29. return new Promise((resolve, reject) => {
  30. uni.request({
  31. method: config.method || 'get',
  32. timeout: config.timeout || timeout,
  33. url: config.baseUrl || baseUrl + config.url,
  34. data: config.data,
  35. header: config.header,
  36. dataType: 'json'
  37. }).then(response => {
  38. let [error, res] = response
  39. if (error) {
  40. toast('后端接口连接异常')
  41. reject('后端接口连接异常')
  42. return
  43. }
  44. const code = res.data.code || 200
  45. const msg = errorCode[code] || res.data.msg || errorCode['default']
  46. if (code === 401) {
  47. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  48. console.log(res);
  49. if (res.confirm) {
  50. // store.dispatch('LogOut').then(res => {
  51. // uni.reLaunch({ url: '/pages/login/index' })
  52. // })
  53. store.commit('SET_TOKEN', '')
  54. store.commit('SET_ROLES', [])
  55. store.commit('SET_PERMISSIONS', [])
  56. store.commit('SET_ROLESNAME', '')
  57. store.commit('SET_USERID', '')
  58. store.commit('SET_COMPANYID', '')
  59. store.commit('SET_COMPANYNAME', '')
  60. store.commit('SET_COMPANYABBREVIATION', '')
  61. store.commit('SET_USERNAME', '')
  62. store.commit('SET_NICKNAME', '')
  63. store.commit('SET_DRIVERID', '')
  64. removeToken()
  65. storage.clean()
  66. uni.reLaunch({
  67. url: '/pages/login/index'
  68. })
  69. }
  70. })
  71. reject('无效的会话,或者会话已过期,请重新登录。')
  72. } else if (code === 500) {
  73. toast(msg)
  74. reject('500')
  75. } else if (code !== 200) {
  76. toast(msg)
  77. reject(code)
  78. }
  79. resolve(res.data)
  80. })
  81. .catch(error => {
  82. let {
  83. message
  84. } = error
  85. if (message === 'Network Error') {
  86. message = '后端接口连接异常'
  87. } else if (message.includes('timeout')) {
  88. message = '系统接口请求超时'
  89. } else if (message.includes('Request failed with status code')) {
  90. message = '系统接口' + message.substr(message.length - 3) + '异常'
  91. }
  92. toast(message)
  93. reject(error)
  94. })
  95. })
  96. }
  97. export default request