request.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. class request {
  2. static BASE_URL = 'https://llzlovesh.top/api/'
  3. static HEAD = {
  4. "Content-Type": "application/json",
  5. "token": wx.getStorageSync('token')
  6. }
  7. static constructor() {
  8. }
  9. /**
  10. * 设置统一的异常处理
  11. */
  12. static setErrorHandler(handler) {
  13. this._errorHandler = handler
  14. }
  15. /**
  16. * GET类型的网络请求
  17. */
  18. static getRequest(url, data, header = this.HEAD ) {
  19. if (!this.HEAD.token){
  20. header.token = wx.getStorageSync('token')
  21. }
  22. return this.requestAll(url, data, header, 'GET')
  23. }
  24. /**
  25. * DELETE类型的网络请求
  26. */
  27. static deleteRequest(url, data, header = this.HEAD ) {
  28. if (!this.HEAD.token){
  29. header.token = wx.getStorageSync('token')
  30. }
  31. return this.requestAll(url, data, header, 'DELETE')
  32. }
  33. /**
  34. * PUT类型的网络请求
  35. */
  36. static putRequest(url, data, header = this.HEAD ) {
  37. if (!this.HEAD.token){
  38. header.token = wx.getStorageSync('token')
  39. }
  40. return this.requestAll(url, data, header, 'PUT')
  41. }
  42. /**
  43. * POST类型的网络请求
  44. */
  45. static postRequest(url, data, header = this.HEAD) {
  46. if (!this.HEAD.token){
  47. header.token = wx.getStorageSync('token')
  48. }
  49. return this.requestAll(url, data, header, 'POST')
  50. }
  51. static upload(url, name, path, ortherData, header = this.HEAD){
  52. if (!this.HEAD.token){
  53. header.token = wx.getStorageSync('token')
  54. }
  55. return new Promise((resolve, reject) => {
  56. wx.uploadFile({
  57. url: url,
  58. header: header,
  59. filePath: path,
  60. name: name,
  61. formData: ortherData,
  62. success: (res => {
  63. res.data = JSON.parse(res.data);
  64. if (res.data.code === 1) {
  65. //200: 服务端业务处理正常结束
  66. resolve(res.data)
  67. } else {
  68. if (res.data.code === 0){
  69. wx.showToast({
  70. title: res.data.message,
  71. })
  72. }
  73. //其它错误,提示用户错误信息
  74. if (this._errorHandler != null) {
  75. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  76. this._errorHandler(res)
  77. }
  78. reject(res)
  79. }
  80. }),
  81. fail: (res => {
  82. if (this._errorHandler != null) {
  83. this._errorHandler(res)
  84. }
  85. wx.showToast({
  86. title: '网络异常请,稍后再试~',
  87. })
  88. reject(res)
  89. })
  90. })
  91. })
  92. }
  93. /**
  94. * 网络请求
  95. */
  96. static requestAll(url, data, header, method) {
  97. const _self = this
  98. wx.showLoading()
  99. return new Promise((resolve, reject) => {
  100. wx.request({
  101. url: url,
  102. data: data,
  103. header: header,
  104. method: method,
  105. success: (res => {
  106. wx.hideLoading()
  107. if (res.data.code === 1) {
  108. //200: 服务端业务处理正常结束
  109. resolve(res.data)
  110. } else {
  111. if (res.data.code === 0){
  112. wx.showToast({
  113. title: res.data.message,
  114. })
  115. }
  116. if (res.data.code === 901){
  117. console.log(res.data)
  118. }
  119. //其它错误,提示用户错误信息
  120. if (this._errorHandler != null) {
  121. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  122. this._errorHandler(res)
  123. }
  124. reject(res)
  125. }
  126. }),
  127. fail: (res => {
  128. if (this._errorHandler != null) {
  129. this._errorHandler(res)
  130. }
  131. wx.showToast({
  132. title: '网络异常请,稍后再试~',
  133. })
  134. reject(res)
  135. })
  136. })
  137. })
  138. }
  139. }
  140. export default request