request.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import util from './util.js'
  2. class request {
  3. //本地的
  4. // static BASE_URL = 'http://www.lx.com:81/'
  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) {
  35. let headerSign = this.getSignHead();
  36. return this.requestAll(url, data, headerSign, 'POST')
  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) {
  92. wx.showLoading()
  93. return new Promise((resolve, reject) => {
  94. wx.request({
  95. url: url,
  96. data: data,
  97. header: header,
  98. method: method,
  99. success: (res => {
  100. wx.hideLoading()
  101. if (res?.data.code === 200) {
  102. //200: 服务端业务处理正常结束
  103. resolve(res?.data)
  104. } else {
  105. wx.showToast({
  106. title: res?.data?.msg,
  107. })
  108. //其它错误,提示用户错误信息
  109. if (this._errorHandler != null) {
  110. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  111. this._errorHandler(res)
  112. }
  113. reject(res)
  114. }
  115. }),
  116. fail: (res => {
  117. if (this._errorHandler != null) {
  118. this._errorHandler(res)
  119. }
  120. wx.showToast({
  121. title: '网络异常请稍后',
  122. })
  123. reject(res)
  124. })
  125. })
  126. })
  127. }
  128. }
  129. export default request