base.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * 监控业务基础类
  3. * 1. 统一业务入口
  4. * 2. 统一接口返回值
  5. */
  6. class MonitorApiBase {
  7. constructor() {
  8. this.model = this.createModel()
  9. // 通用函数Hooks
  10. this.doFetch = this.runFetch.bind(this)
  11. this.doChange = this.runChange.bind(this)
  12. }
  13. /**
  14. * 获取监控状态并赋值
  15. */
  16. async runFetch() {
  17. let result = this.createModel()
  18. this.model = result = await this.ajaxGetState()
  19. return result
  20. }
  21. /**
  22. * 改变监控状态并返回结果
  23. * @param follow
  24. * @return {Promise<{}>}
  25. */
  26. async runChange(follow = this.model.follow) {
  27. let result = {
  28. // 操作是否成功
  29. success: false,
  30. // 接口返回值
  31. data: {}
  32. }
  33. const doAction = follow ? this.ajaxRemove.bind(this) : this.ajaxAdd.bind(this)
  34. result = await doAction()
  35. if (result.success) {
  36. this.model.follow = !follow
  37. }
  38. return result
  39. }
  40. createModel() {
  41. return {
  42. // 是否可以展示监控按钮
  43. canFollow: false,
  44. // 当前监控状态
  45. follow: false,
  46. // 可扩展字段
  47. expands: {
  48. // 剩余可用数量
  49. surplus: 0,
  50. // 已用数量
  51. used: 0
  52. }
  53. }
  54. }
  55. /**
  56. * 提供覆盖的ajax请求
  57. * @return {{}}
  58. */
  59. async ajaxGetState() {
  60. return {}
  61. }
  62. async ajaxAdd() {
  63. return {}
  64. }
  65. async ajaxRemove() {
  66. return {}
  67. }
  68. }
  69. export default MonitorApiBase