request.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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){
  52. return new Promise((resolve, reject) => {
  53. wx.uploadFile({
  54. url: url,
  55. header: header,
  56. filePath: path,
  57. name: name,
  58. formData: ortherData,
  59. success: (res => {
  60. res.data = JSON.parse(res.data);
  61. if (res.data.code === 1) {
  62. //200: 服务端业务处理正常结束
  63. resolve(res.data)
  64. } else {
  65. if (res.data.code === 0){
  66. wx.showToast({
  67. title: res.data.message,
  68. })
  69. }
  70. //其它错误,提示用户错误信息
  71. if (this._errorHandler != null) {
  72. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  73. this._errorHandler(res)
  74. }
  75. reject(res)
  76. }
  77. }),
  78. fail: (res => {
  79. if (this._errorHandler != null) {
  80. this._errorHandler(res)
  81. }
  82. wx.showToast({
  83. title: '网络异常请,稍后再试~',
  84. })
  85. reject(res)
  86. })
  87. })
  88. })
  89. }
  90. /**
  91. * 网络请求
  92. */
  93. static requestAll(url, data, header, method) {
  94. const _self = this
  95. wx.showLoading()
  96. return new Promise((resolve, reject) => {
  97. wx.request({
  98. url: url,
  99. data: data,
  100. header: header,
  101. method: method,
  102. success: (res => {
  103. wx.hideLoading()
  104. if (res.data.code === 1) {
  105. //200: 服务端业务处理正常结束
  106. resolve(res.data)
  107. } else {
  108. if (res.data.code === 0){
  109. wx.showToast({
  110. title: res.data.message,
  111. })
  112. }
  113. if (res.data.code === 901){
  114. console.log(res.data)
  115. }
  116. //其它错误,提示用户错误信息
  117. if (this._errorHandler != null) {
  118. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  119. this._errorHandler(res)
  120. }
  121. reject(res)
  122. }
  123. }),
  124. fail: (res => {
  125. if (this._errorHandler != null) {
  126. this._errorHandler(res)
  127. }
  128. wx.showToast({
  129. title: '网络异常请,稍后再试~',
  130. })
  131. reject(res)
  132. })
  133. })
  134. })
  135. }
  136. }
  137. export default request