request.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import util 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) {
  35. let headerSign = this.getSignHead();
  36. return this.requestAll(url, data, headerSign, 'POST', notUseLoading)
  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) {
  92. if (!notUseLoading) {
  93. wx.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. wx.hideLoading()
  103. if (res?.data.code === 200) {
  104. //200: 服务端业务处理正常结束
  105. resolve(res?.data)
  106. } else {
  107. wx.showToast({
  108. title: res?.data?.msg,
  109. icon: "none"
  110. })
  111. //其它错误,提示用户错误信息
  112. if (this._errorHandler != null) {
  113. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  114. this._errorHandler(res)
  115. }
  116. reject(res)
  117. }
  118. }),
  119. fail: (res => {
  120. if (this._errorHandler != null) {
  121. this._errorHandler(res)
  122. }
  123. wx.showToast({
  124. title: '网络异常请稍后',
  125. })
  126. reject(res)
  127. })
  128. })
  129. })
  130. }
  131. }
  132. export default request