SRequest.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. class Interceptor {
  2. handlerId = 1
  3. constructor() {
  4. this.id = Interceptor.id++
  5. this.handlers = []
  6. }
  7. use(success, fail) {
  8. this.handlers.push({
  9. id: this.handlerId,
  10. success,
  11. fail
  12. })
  13. return this.handlerId++
  14. }
  15. eject(handlerId) {
  16. for (let i = 0; i < this.handlers.length; i++) {
  17. if (this.handlers[i].id === handlerId) {
  18. return this.handlers.splice(i, 1)
  19. }
  20. }
  21. }
  22. go(config, isSuccess) {
  23. if(!isSuccess){
  24. return this.handlers[0].fail(config)
  25. }else{
  26. for (const v of this.handlers) {
  27. config = v.success(config)
  28. }
  29. return config
  30. }
  31. }
  32. }
  33. Interceptor.id = 1
  34. function isHttpSuccess(status){
  35. return status >= 200 && status < 300 || status === 304
  36. }
  37. class SRequest {
  38. config = {
  39. baseUrl:'',
  40. dataType: 'json',
  41. responseType: 'text',
  42. header: {},
  43. }
  44. constructor(config) {
  45. this.config = Object.assign(this.config, config)
  46. this.interceptors = {
  47. request: new Interceptor(),
  48. response: new Interceptor()
  49. }
  50. }
  51. request(config) {
  52. config = config ? Object.assign({}, this.config, config) : this.config
  53. config = this.interceptReq(config, true)
  54. let { url, method, data, header, dataType, responseType } = config
  55. if (!url.startsWith('https://')) {
  56. url = config.baseUrl + url
  57. }
  58. return new Promise((resolve, reject) => {
  59. wx.request({
  60. url,
  61. data,
  62. header: header || this.header,
  63. method,
  64. dataType,
  65. responseType,
  66. success: res => {
  67. if(isHttpSuccess(res.statusCode)){
  68. res = this.interceptRes(res, true)
  69. resolve(res.data)
  70. }else{
  71. res = this.interceptRes(res, false)
  72. reject(res)
  73. }
  74. },
  75. fail: res => {
  76. res = this.interceptRes(res, false)
  77. reject(res)
  78. },
  79. })
  80. })
  81. }
  82. // 遍历请求拦截器
  83. interceptReq(config, isSuccess) {
  84. return this.interceptors.request.go(config, isSuccess)
  85. }
  86. // 遍历响应拦截器
  87. interceptRes(response, isSuccess) {
  88. return this.interceptors.response.go(response, isSuccess)
  89. }
  90. get(url, config) {
  91. const options = { url, method: 'GET' }
  92. Object.assign(options, config || null)
  93. return this.request(options)
  94. }
  95. post(url, data, config) {
  96. const options = { url, method: 'POST' }
  97. Object.assign(options, data ? { data } : null, config || null)
  98. return this.request(options)
  99. }
  100. put(url, data, config) {
  101. const options = { url, method: 'PUT' }
  102. Object.assign(options, data ? { data } : null, config || null)
  103. return this.request(options)
  104. }
  105. delete(url, config) {
  106. const options = { url, method: 'DELETE' }
  107. Object.assign(options, config || null)
  108. return this.request(options)
  109. }
  110. }
  111. module.exports = SRequest