ent-api.js 3.7 KB

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