request.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // import { useUserStore } from '@/store/user.js'
  2. import config from '@/config'
  3. import { getSession } from '@/utils/auth'
  4. import errorCode from '@/utils/errorCode'
  5. import { toast, showConfirm, tansParams } from '@/utils/common'
  6. import $modal from '@/plugins/modal.js'
  7. let timeout = config.timeout
  8. const baseUrl = config.baseUrl
  9. const request = config => {
  10. // 是否需要设置 token
  11. const isSession = config.isSession || false
  12. let flag = false
  13. config.header = config.header || {}
  14. if (isSession) {
  15. if (getSession()) {
  16. config.header['cookie'] = 'JSESSIONID=' + getSession()
  17. } else {
  18. flag = true
  19. uni.showModal({
  20. title: '系统提示',
  21. content: '登录状态已过期,您可以继续留在该页面,或者重新登录?',
  22. cancelText: '取消',
  23. confirmText: '确定',
  24. success: function (res) {
  25. console.log('showModal success');
  26. if (res.confirm) {
  27. uni.reLaunch({ url: '/pages/login' })
  28. return
  29. }
  30. return
  31. }
  32. })
  33. }
  34. }
  35. // get请求映射params参数
  36. if (config.params) {
  37. let url = config.url + '?' + tansParams(config.params)
  38. url = url.slice(0, -1)
  39. config.url = url
  40. }
  41. console.log('request.config', config);
  42. return new Promise((resolve, reject) => {
  43. if (flag) {
  44. reject()
  45. }
  46. 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. }).then(response => {
  54. console.log('request.response', response);
  55. const { statusCode, data } = response
  56. const returnCode = data.returnCode
  57. const code = statusCode || 200
  58. const msg = data.returnMsg || errorCode['default']
  59. if (errorCode[returnCode] != undefined) {
  60. toast(msg)
  61. reject(returnCode)
  62. return
  63. }
  64. if (code === 401) {
  65. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  66. // if (res.confirm) {
  67. // store.dispatch('LogOut').then(res => {
  68. // uni.reLaunch({ url: '/pages/login' })
  69. // })
  70. // }
  71. })
  72. reject('无效的会话,或者会话已过期,请重新登录。')
  73. } else if (code === 500) {
  74. toast(msg)
  75. reject('500')
  76. } else if (code !== 200) {
  77. toast(msg)
  78. reject(code)
  79. }
  80. resolve(data)
  81. })
  82. .catch(error => {
  83. console.log('request.error', error);
  84. let { errMsg } = error
  85. if (errMsg === 'Network Error') {
  86. errMsg = '后端接口连接异常'
  87. } else if (errMsg.includes('timeout')) {
  88. errMsg = '系统接口请求超时'
  89. } else if (errMsg.includes('Request failed with status code')) {
  90. errMsg = '系统接口' + errMsg.substr(errMsg.length - 3) + '异常'
  91. } else if (errMsg.includes('request:fail')) {
  92. errMsg = '网络请求失败'
  93. }
  94. toast(errMsg)
  95. reject(error)
  96. })
  97. })
  98. }
  99. export default request