request.js 3.4 KB

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