ent-api.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {
  2. ajaxFollowEntAdd,
  3. ajaxFollowEntCancel,
  4. ajaxFollowEntInfo,
  5. ajaxMonitorGroup
  6. } from '../api/follow-ent'
  7. import MonitorApiBase from './base'
  8. export default class MonitorEntApi extends MonitorApiBase {
  9. constructor({ id }) {
  10. super()
  11. this.id = id
  12. }
  13. /**
  14. * 提供覆盖的ajax请求
  15. * @return {{}}
  16. */
  17. async ajaxGetState() {
  18. return ajaxFollowEntInfo({ entId: this.id }).then((res) => {
  19. const result = this.createModel()
  20. result.canFollow = res?.data?.isShow || false
  21. result.follow = res?.data?.followed || false
  22. result.followedGroup = res?.data?.followedGroup || ''
  23. if (res?.data?.info) {
  24. result.expands = res.data.info
  25. }
  26. return result
  27. })
  28. }
  29. async ajaxAdd() {
  30. return ajaxFollowEntAdd({ entId: this.id, groups: this.groupId }).then(
  31. (res) => {
  32. const result = {
  33. success: false,
  34. msg: '',
  35. data: {}
  36. }
  37. result.success = res?.error_code === 0 && res?.data?.status
  38. result.data = res?.data
  39. if (result.success) {
  40. this.model.expands.used += 1
  41. this.model.expands.surplus = Math.max(
  42. this.model.expands.surplus - 1,
  43. 0
  44. )
  45. }
  46. else {
  47. result.msg = res?.error_msg || '抱歉,操作失败'
  48. }
  49. return result
  50. }
  51. )
  52. }
  53. async ajaxRemove() {
  54. return ajaxFollowEntCancel({ entId: this.id }).then((res) => {
  55. const result = {
  56. success: false,
  57. msg: '',
  58. data: {}
  59. }
  60. result.success = res?.error_code === 0 && res?.data === 'success'
  61. result.data = res?.data
  62. if (result.success) {
  63. this.model.expands.surplus += 1
  64. this.model.expands.used = Math.max(this.model.expands.used - 1, 0)
  65. this.model.followedGroup = ''
  66. }
  67. else {
  68. result.msg = res?.error_msg || '抱歉,操作失败'
  69. }
  70. return result
  71. })
  72. }
  73. async ajaxGroup(params = {}) {
  74. const { type, name, groupId, callback } = params
  75. return ajaxMonitorGroup({ type, name, groupId }).then((res) => {
  76. const result = {
  77. success: false,
  78. msg: '',
  79. data: {}
  80. }
  81. result.success = res?.error_code === 0
  82. result.data = res?.data
  83. if (result.success) {
  84. // 非获取(新增、修改、删除)成功后再次获取分组列表
  85. if (type !== 'get') {
  86. this.ajaxGroup({ type: 'get' })
  87. }
  88. else {
  89. this.groupList = res?.data
  90. }
  91. callback && callback()
  92. }
  93. else {
  94. result.msg = res?.error_msg || '抱歉,操作失败'
  95. }
  96. return result
  97. })
  98. }
  99. }