base.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. this.doChangeGroup = this.ajaxChange.bind(this)
  18. this.doOnlyGroup = this.ajaxOnlyGroup.bind(this)
  19. }
  20. /**
  21. * 获取监控状态并赋值
  22. */
  23. async runFetch() {
  24. let result = this.createModel()
  25. this.model = result = await this.ajaxGetState()
  26. return result
  27. }
  28. /**
  29. * 改变监控状态并返回结果
  30. * @param follow
  31. * @return {Promise<{}>}
  32. */
  33. async runChange(follow = this.model.follow, params = {}) {
  34. const { groupId } = params
  35. this.groupId = groupId
  36. let result = {
  37. // 操作是否成功
  38. success: false,
  39. // 接口返回值
  40. data: {}
  41. }
  42. const doAction = follow
  43. ? this.ajaxRemove.bind(this)
  44. : this.ajaxAdd.bind(this)
  45. result = await doAction()
  46. if (result.success) {
  47. this.model.follow = !follow
  48. }
  49. return result
  50. }
  51. /**
  52. * 获取监控分组信息并赋值
  53. */
  54. async runFetchGroup(params = {}) {
  55. const result = await this.ajaxGroup(params)
  56. if (result.success) {
  57. this.groupList = result.data
  58. }
  59. return result
  60. }
  61. createModel() {
  62. return {
  63. // 是否可以展示监控按钮
  64. canFollow: false,
  65. // 当前监控状态
  66. follow: false,
  67. // 可扩展字段
  68. expands: {
  69. // 剩余可用数量
  70. surplus: 0,
  71. // 已用数量
  72. used: 0
  73. },
  74. // 分组名
  75. followedGroup: ''
  76. }
  77. }
  78. /**
  79. * 提供覆盖的ajax请求
  80. * @return {{}}
  81. */
  82. async ajaxGetState() {
  83. return {}
  84. }
  85. async ajaxAdd() {
  86. return {}
  87. }
  88. async ajaxRemove() {
  89. return {}
  90. }
  91. async ajaxGroup() {
  92. return {}
  93. }
  94. async ajaxChange() {
  95. return {}
  96. }
  97. async ajaxOnlyGroup() {
  98. return {}
  99. }
  100. }
  101. export default MonitorApiBase