request.js 4.2 KB

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