ent-api.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import MonitorApiBase from './base'
  2. import {
  3. ajaxFollowEntInfo,
  4. ajaxFollowEntAdd,
  5. ajaxFollowEntCancel
  6. } from '../api/follow-ent'
  7. export default class MonitorEntApi extends MonitorApiBase {
  8. constructor({ id }) {
  9. super()
  10. this.id = id
  11. }
  12. /**
  13. * 提供覆盖的ajax请求
  14. * @return {{}}
  15. */
  16. async ajaxGetState() {
  17. return ajaxFollowEntInfo({ entId: this.id }).then((res) => {
  18. const result = this.createModel()
  19. result.canFollow = res?.data?.isShow || false
  20. result.follow = res?.data?.followed || false
  21. if (res?.data?.info) {
  22. result.expands = res.data.info
  23. }
  24. return result
  25. })
  26. }
  27. async ajaxAdd() {
  28. return ajaxFollowEntAdd({ entId: this.id }).then((res) => {
  29. const result = {
  30. success: false,
  31. msg: '',
  32. data: {}
  33. }
  34. result.success = res?.error_code === 0 && res?.data?.status
  35. result.data = res?.data
  36. if (result.success) {
  37. this.model.expands.used += 1
  38. this.model.expands.surplus = Math.max(this.model.expands.surplus - 1, 0)
  39. } else {
  40. result.msg = res?.error_msg || '抱歉,操作失败'
  41. }
  42. return result
  43. })
  44. }
  45. async ajaxRemove() {
  46. return ajaxFollowEntCancel({ entId: this.id }).then(
  47. (res) => {
  48. const result = {
  49. success: false,
  50. msg: '',
  51. data: {}
  52. }
  53. result.success = res?.error_code === 0 && res?.data === 'success'
  54. result.data = res?.data
  55. if (result.success) {
  56. this.model.expands.surplus += 1
  57. this.model.expands.used = Math.max(this.model.expands.used - 1, 0)
  58. } else {
  59. result.msg = res?.error_msg || '抱歉,操作失败'
  60. }
  61. return result
  62. }
  63. )
  64. }
  65. }