request.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const { envDomain } = require('../utils/config')
  2. const sessionStorageKey = 'shiningnongyeReqSessionId'
  3. const req = {
  4. baseURL: envDomain.apiDomain,
  5. header: {
  6. 'content-type': 'application/json'
  7. }
  8. }
  9. let loading = 0
  10. // 当前正在请求的数量
  11. let needLoadingRequestCount = 0
  12. // 显示loading
  13. function _showLoading() {
  14. if (needLoadingRequestCount === 0 && !loading) {
  15. wx.showToast({
  16. title: '拼命获取中',
  17. icon: 'loading',
  18. duration: 86400000 // 设置1小时:类似一直显示
  19. })
  20. loading = 1
  21. }
  22. needLoadingRequestCount++
  23. }
  24. // 隐藏loading
  25. function _hideLoading() {
  26. needLoadingRequestCount--
  27. needLoadingRequestCount = Math.max(needLoadingRequestCount, 0) // 做个保护
  28. if (needLoadingRequestCount === 0) {
  29. // 关闭loading
  30. wx.hideToast()
  31. loading = 0
  32. }
  33. }
  34. /**
  35. * 登录
  36. */
  37. function login() {
  38. return new Promise((resolve, reject) => {
  39. // 微信登录
  40. wx.login({
  41. success(res) {
  42. if (res.code) {
  43. resolve(res.code)
  44. } else {
  45. reject(res)
  46. }
  47. },
  48. fail: reject
  49. })
  50. })
  51. }
  52. /**
  53. * 判断请求状态是否成功
  54. * @param {number} status
  55. */
  56. function isHttpSuccess(status) {
  57. return (status >= 200 && status < 300) || status === 304
  58. }
  59. /**
  60. * wx.request二次封装
  61. * @param options
  62. * @param keepLogin
  63. * @returns {Promise<unknown>}
  64. */
  65. function request(options = {}, keepLogin = true) {
  66. const header = Object.assign({ 'access-token': wx.getStorageSync(sessionStorageKey) }, req.header,
  67. options.header)
  68. const url = /^http/.test(options.url) ? options.url : (req.baseURL + options.url)
  69. // 判断当前请求是否设置了不显示Loading
  70. if (options.showLoading) {
  71. _showLoading()
  72. }
  73. return new Promise((resolve, reject) => {
  74. wx.request(Object.assign({}, options, {
  75. url,
  76. header,
  77. success(r) {
  78. const isSuccess = isHttpSuccess(r.statusCode)
  79. const err = {
  80. msg: `服务器好像出了点小问题,请与客服联系~(错误代码:${r.statusCode})`,
  81. detail: r
  82. }
  83. const pages = getCurrentPages()
  84. if (isSuccess) {
  85. if (r.data.code === 403) {
  86. wx.removeStorage({
  87. key: sessionStorageKey
  88. })
  89. }
  90. if (r.data.code === 403 && keepLogin) {
  91. const { route } = Array.isArray(pages) && pages.length ? pages[pages.length - 1] : {}
  92. if (!/^pages\/login\/login/.test(route)) {
  93. wx.navigateTo({
  94. url: `/pages/login/login`
  95. })
  96. }
  97. reject(err)
  98. return
  99. }
  100. resolve(r.data)
  101. return
  102. }
  103. reject(err)
  104. },
  105. fail(err) {
  106. reject(err)
  107. },
  108. complete() {
  109. if (loading) {
  110. _hideLoading()
  111. }
  112. }
  113. }
  114. ))
  115. })
  116. }
  117. function handleUpload(file) {
  118. return new Promise((resolve, reject) => {
  119. wx.uploadFile({
  120. url: `${req.baseURL}/api/upload/img`,
  121. filePath: file.filePath,
  122. name: 'file',
  123. formData: {
  124. 'user': 'test'
  125. },
  126. success(r) {
  127. const isSuccess = isHttpSuccess(r.statusCode)
  128. if (isSuccess) {
  129. const temp = JSON.parse(r.data)
  130. if (temp.status && Object.prototype.toString.call(temp.data) === '[object Object]' && temp.data.path) {
  131. resolve({ ...file, url: temp.data.path })
  132. } else {
  133. reject(r)
  134. }
  135. } else {
  136. reject(r)
  137. }
  138. },
  139. fail(err) {
  140. reject(err)
  141. }
  142. })
  143. })
  144. }
  145. module.exports = {
  146. request,
  147. handleUpload,
  148. login,
  149. sessionStorageKey
  150. }