request.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import {randomString, sha1} from "./util"
  2. class request {
  3. // static BASE_URL = 'http://127.0.0.1:8014/open/'
  4. static BASE_URL = 'https://kd.llzlovesh.top/open/'
  5. static COUPON_URL = this.BASE_URL
  6. static HEAD = {
  7. "Content-Type": "application/json",
  8. "defaultToken": 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MTY5MDg3NjAsImV4cCI6MjQ4MDgyMjM2MCwidXNlcklkIjoxMjM0NTY3ODksInZpc2l0b3IiOiJ2aXNpdG9yNjA2MDExZDg4NTQwYyIsImlzQWRtaW4iOjAsImFkbWluSWQiOjB9.7XrRxA3WYMBlqJWIXEw-71AxOxQvCc4VVVN6rlqn3uc',
  9. "token":'',
  10. }
  11. static constructor() {
  12. }
  13. /**
  14. * 设置统一的异常处理
  15. */
  16. static setErrorHandler(handler) {
  17. this._errorHandler = handler
  18. }
  19. /**
  20. * GET类型的网络请求
  21. */
  22. static getRequest(url, data, header = this.HEAD ) {
  23. const token = wx.getStorageSync('token')
  24. if (token) {
  25. header.token = token
  26. }else {
  27. header.token = header.defaultToken
  28. }
  29. return this.requestAll(url, data, header, 'GET')
  30. }
  31. /**
  32. * DELETE类型的网络请求
  33. */
  34. static deleteRequest(url, data, header = this.HEAD ) {
  35. const token = wx.getStorageSync('token')
  36. if (token) {
  37. header.token = token
  38. }else {
  39. header.token = header.defaultToken
  40. }
  41. return this.requestAll(url, data, header, 'DELETE')
  42. }
  43. /**
  44. * PUT类型的网络请求
  45. */
  46. static putRequest(url, data, header = this.HEAD ) {
  47. const token = wx.getStorageSync('token')
  48. if (token) {
  49. header.token = token
  50. }else {
  51. header.token = header.defaultToken
  52. }
  53. return this.requestAll(url, data, header, 'PUT')
  54. }
  55. /**
  56. * POST类型的网络请求
  57. */
  58. static postRequest(url, data, header = this.HEAD) {
  59. const token = wx.getStorageSync('token')
  60. if (token) {
  61. header.token = token
  62. }else {
  63. header.token = header.defaultToken
  64. }
  65. return this.requestAll(url, data, header, 'POST')
  66. }
  67. static upload(url, name, path, ortherData, header = this.HEAD){
  68. const token = wx.getStorageSync('token')
  69. if (token) {
  70. header.token = token
  71. }else {
  72. header.token = header.defaultToken
  73. }
  74. return new Promise((resolve, reject) => {
  75. wx.uploadFile({
  76. url: url,
  77. header: header,
  78. filePath: path,
  79. name: name,
  80. formData: ortherData,
  81. success: (res => {
  82. res.data = JSON.parse(res.data);
  83. if (res.data.code === 1 || res.data.code == 200) {
  84. //200: 服务端业务处理正常结束
  85. resolve(res.data)
  86. } else {
  87. if (res.data.code === 0){
  88. wx.showToast({
  89. title: res.data.message,
  90. })
  91. }
  92. if (res.data.code === 901) {
  93. _self.HEAD.token = ''
  94. }
  95. //其它错误,提示用户错误信息
  96. if (this._errorHandler != null) {
  97. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  98. this._errorHandler(res)
  99. }
  100. reject(res)
  101. }
  102. }),
  103. fail: (res => {
  104. if (this._errorHandler != null) {
  105. this._errorHandler(res)
  106. }
  107. wx.showToast({
  108. title: '网络异常请,稍后再试~',
  109. })
  110. reject(res)
  111. })
  112. })
  113. })
  114. }
  115. /**
  116. * 网络请求
  117. */
  118. static requestAll(url, data, header, method) {
  119. const _self = this
  120. const timestamp = parseInt(new Date().getTime()/1000)
  121. const nonce = randomString(16)
  122. const msg = ''
  123. const token = 'U6Watb875eCiX4Lq'
  124. const strArr = timestamp + nonce + msg + token
  125. const encodeStr = sha1(strArr)
  126. header['NONCE'] = nonce
  127. header['msg'] = msg
  128. header['TIMESTAMP'] = timestamp
  129. header['SIGN'] = encodeStr
  130. header['VERSION'] = '1.8.0'
  131. data['errMsg'] = data['errMsg'] ? 'modal' :'toast'
  132. wx.showLoading()
  133. return new Promise((resolve, reject) => {
  134. wx.request({
  135. url: url,
  136. data: data,
  137. header: header,
  138. method: method,
  139. success: (res => {
  140. wx.hideLoading()
  141. if (res.data.code === 1 || res.data.code == 200) {
  142. //200: 服务端业务处理正常结束
  143. resolve(res.data)
  144. } else {
  145. if (res.data.code === 401){
  146. if (data['errMsg'] == 'modal') {
  147. wx.showModal({
  148. title: '错误',
  149. content: res.data.msg,
  150. })
  151. }else {
  152. wx.showToast({
  153. title: res.data.msg,
  154. })
  155. }
  156. }
  157. if (res.data.code === 901) {
  158. _self.HEAD.token = ''
  159. // wx.setStorageSync('token', '')
  160. }
  161. //其它错误,提示用户错误信息
  162. if (this._errorHandler != null) {
  163. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  164. this._errorHandler(res)
  165. }
  166. reject(res)
  167. }
  168. }),
  169. fail: (res => {
  170. if (this._errorHandler != null) {
  171. this._errorHandler(res)
  172. }
  173. wx.showToast({
  174. title: '网络异常请,稍后再试~',
  175. })
  176. reject(res)
  177. })
  178. })
  179. })
  180. }
  181. }
  182. export default request