base.js 1023 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. class SearchListApiBase {
  2. constructor(config = {}) {
  3. this.list = []
  4. this.loading = false
  5. this.finished = false
  6. this.total = -1
  7. this._getParams = config.getParams || (() => {})
  8. // 函数 Hooks
  9. this.doQuery = this.runQuery.bind(this)
  10. }
  11. /**
  12. * 统一查询 API 入口
  13. * @param params - 请求参数
  14. * @returns {Promise<{success: boolean, total: number, list: []}>}
  15. */
  16. async runQuery(params) {
  17. this.loading = true
  18. this.finished = false
  19. this.list = []
  20. const query = Object.assign({}, params, this._getParams(params))
  21. const result = await this.ajaxQuery(query)
  22. if (result.success) {
  23. this.list = result.list
  24. this.total = result.total
  25. } else {
  26. this.list = []
  27. this.total = -1
  28. }
  29. this.finished = true
  30. this.loading = false
  31. return result
  32. }
  33. // 需要覆写的API处理逻辑
  34. async ajaxQuery(params) {
  35. return {
  36. success: true,
  37. list: [],
  38. total: -1
  39. }
  40. }
  41. }
  42. export default SearchListApiBase