request.js 4.2 KB

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