request.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { getToken } from './auth.js'
  2. import { toast } from './common.js'
  3. const BASE_URL = 'http://121.36.73.159:807/prod-api'
  4. const request = config => {
  5. config.header = config.header || {}
  6. if (getToken()) {
  7. config.header['Authorization'] = 'Bearer ' + getToken()
  8. }
  9. uni.showLoading({
  10. title:'请求中...'
  11. })
  12. return new Promise((resolve, reject) => {
  13. uni.request({
  14. header: config.header,
  15. method: config.method.toUpperCase() || 'GET',
  16. dataType: 'json',
  17. timeout: config.timeout || 10000,
  18. url: config.baseUrl || BASE_URL + config.url,
  19. data: config.data,
  20. }).then((response) => {
  21. let { data, statusCode } = response
  22. const code = statusCode || 200
  23. const msg = data.msg || '未知错误,请反馈给管理员'
  24. if (code === 401) {
  25. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录').then(res => {
  26. if (res.confirm) {
  27. uni.reLaunch({ url: '/pages/login/login' })
  28. }
  29. })
  30. reject('无效的会话,或者会话已过期,请重新登录')
  31. } else if (code !== 200) {
  32. toast(msg)
  33. reject(code)
  34. }
  35. uni.hideLoading()
  36. resolve(data)
  37. }).catch(error => {
  38. let { message } = error
  39. if (message === 'Network Error') {
  40. message = '后端接口连接异常'
  41. } else if (message.includes('timeout')) {
  42. message = '系统接口请求超时'
  43. } else if (message.includes('Request failed with status code')) {
  44. message = '系统接口' + message.substr(message.length - 3) + '异常'
  45. }
  46. uni.hideLoading()
  47. toast(message)
  48. reject(error)
  49. })
  50. })
  51. }
  52. // function request(req) {
  53. // let data = req.data
  54. // const method = req.method
  55. // const url = req.url
  56. // let loading = false
  57. // if (req.data.loading) {
  58. // loading = data.loading
  59. // delete data.loading
  60. // }
  61. // let header = {
  62. // 'Content-Type': 'application/json',
  63. // 'timestamp': Date.parse(new Date()),
  64. // // 'token': uni.getStorageSync('user').access_token,
  65. // 'Authorization': 'Bearer ' + uni.getStorageSync('user').access_token
  66. // }
  67. // if (loading) {
  68. // uni.hideLoading()
  69. // uni.showLoading({
  70. // title: '请求中',
  71. // mask: true
  72. // })
  73. // }
  74. // return new Promise((resolve, reject) => {
  75. // console.log('REQ ==>', url)
  76. // console.log(data)
  77. // uni.request({
  78. // url: BASE_URL + url,
  79. // data: data,
  80. // header: header,
  81. // method: method.toUpperCase(),
  82. // timeout: 1000 * 60 * 3, //超时三分钟
  83. // success: async (res) => {
  84. // uni.hideLoading()
  85. // console.log("RES <==", url)
  86. // console.log(res)
  87. // if (res.statusCode == 200 && res.data.code == 200) {
  88. // resolve(res.data.data)
  89. // } else if (res.statusCode == 401) {
  90. // //处理token验证出错
  91. // console.log('Token过期')
  92. // if (!uni.getStorageSync('user')) {
  93. // //其他设备登录了
  94. // uni.reLaunch({
  95. // url: '/pages/login/login'
  96. // });
  97. // } else {
  98. // //token续租
  99. // let r = await login(uni.getStorageSync('user').login)
  100. // let result = await request(req)
  101. // }
  102. // } else {
  103. // console.error(res.data.msg)
  104. // if (res.data.msg == '登录状态已过期') {
  105. // uni.reLaunch({
  106. // url: '/pages/login/pages/login'
  107. // })
  108. // }
  109. // uni.showToast({
  110. // title: res.data.msg,
  111. // icon: "none",
  112. // position: "bottom",
  113. // duration: 3000
  114. // })
  115. // reject(false)
  116. // }
  117. // },
  118. // fail: (err) => {
  119. // uni.hideLoading()
  120. // uni.showToast({
  121. // title: '服务器休息中,请稍后再试',
  122. // icon: "none",
  123. // position: "center",
  124. // duration: 3000
  125. // });
  126. // console.error(url, err)
  127. // reject(err)
  128. // }
  129. // });
  130. // })
  131. // }
  132. export default request