base.js 1.9 KB

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