request.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import util, { isFunc } from './util.js'
  2. class request {
  3. //本地的
  4. // static BASE_URL = 'http://localhost:8014/'
  5. // pre环境的
  6. // static BASE_URL = 'https://oapi.shpr.top/'
  7. // 正式的
  8. static BASE_URL = 'https://vapi.hsayi.com/'
  9. // @todo需要修改正式的域名
  10. static HEAD = {
  11. "Content-Type": "application/json",
  12. "SIGN":'',
  13. "NONCE":'',
  14. "TIMESTAMP":'',
  15. }
  16. static constructor() {
  17. }
  18. /**
  19. * 设置统一的异常处理
  20. */
  21. static setErrorHandler(handler) {
  22. this._errorHandler = handler
  23. }
  24. /**
  25. * GET类型的网络请求
  26. */
  27. static getRequest(url, data ) {
  28. let headerSign = this.getSignHead();
  29. return this.requestAll(url, data, headerSign, 'GET')
  30. }
  31. /**
  32. * POST类型的网络请求
  33. */
  34. static postRequest(url, data, notUseLoading, catchErrorFunc) {
  35. let headerSign = this.getSignHead();
  36. return this.requestAll(url, data, headerSign, 'POST', notUseLoading, catchErrorFunc)
  37. }
  38. static upload(url, name, path, ortherData, header = this.HEAD){
  39. return new Promise((resolve, reject) => {
  40. wx.uploadFile({
  41. url: url,
  42. header: header,
  43. filePath: path,
  44. name: name,
  45. formData: ortherData,
  46. success: (res => {
  47. res.data = JSON.parse(res.data);
  48. if (res.data.code === 200) {
  49. //200: 服务端业务处理正常结束
  50. resolve(res.data)
  51. } else {
  52. wx.showToast({
  53. title: res.data.msg,
  54. })
  55. //其它错误,提示用户错误信息
  56. if (this._errorHandler != null) {
  57. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  58. this._errorHandler(res)
  59. }
  60. reject(res)
  61. }
  62. }),
  63. fail: (res => {
  64. if (this._errorHandler != null) {
  65. this._errorHandler(res)
  66. }
  67. wx.showToast({
  68. title: '网络异常请,稍后再试~',
  69. })
  70. reject(res)
  71. })
  72. })
  73. })
  74. }
  75. static getSignHead(){
  76. let timestamp = parseInt(Date.now()/1000);
  77. let nonce = Math.floor(Math.random() * 1000).toString()
  78. let signStr = timestamp+nonce+""+"U6Watb875eCiX4Lq";
  79. let sign = util.sha1(signStr).toString();
  80. let signHeadInfo = {
  81. "SIGN":sign,
  82. "NONCE":nonce,
  83. "TIMESTAMP":timestamp,
  84. "Content-Type": "application/json"
  85. }
  86. return signHeadInfo;
  87. }
  88. /**
  89. * 网络请求
  90. */
  91. static requestAll(url, data, header, method, notUseLoading, catchErrorFunc) {
  92. if (!notUseLoading) {
  93. getApp().showLoading()
  94. }
  95. return new Promise((resolve, reject) => {
  96. wx.request({
  97. url: url,
  98. data: data,
  99. header: header,
  100. method: method,
  101. success: (res => {
  102. if (res?.data.code === 200) {
  103. //200: 服务端业务处理正常结束
  104. resolve(res?.data)
  105. } else {
  106. if (catchErrorFunc) {
  107. if (isFunc(catchErrorFunc)) {
  108. catchErrorFunc(res?.data)
  109. }
  110. } else {
  111. wx.showToast({
  112. title: res?.data?.msg,
  113. icon: "none"
  114. })
  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. complete: _ => {
  134. getApp().hideLoading()
  135. }
  136. })
  137. })
  138. }
  139. }
  140. export default request