client-api.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import MonitorApiBase from './base'
  2. import {
  3. ajaxFollowClientInfo,
  4. ajaxFollowClientChange
  5. } from "../api/follow-client";
  6. export default class MonitorClientApi extends MonitorApiBase {
  7. constructor({ id }) {
  8. super()
  9. this.id = id
  10. }
  11. /**
  12. * 提供覆盖的ajax请求
  13. * @return {{}}
  14. */
  15. async ajaxGetState() {
  16. return ajaxFollowClientInfo({ name_list: [this.id] }).then((res) => {
  17. const result = this.createModel()
  18. result.canFollow = true
  19. result.follow = false
  20. if (res?.data?.follow) {
  21. if (res.data.follow[this.id]) {
  22. result.follow = true
  23. }
  24. }
  25. if (res?.data?.count) {
  26. result.expands = {
  27. used: res?.data?.count?.use || 0,
  28. surplus: res?.data?.count?.surplus || 0
  29. }
  30. }
  31. return result
  32. })
  33. }
  34. async ajaxAdd() {
  35. return ajaxFollowClientChange({ name: this.id, b: false }).then((res) => {
  36. const result = {
  37. success: false,
  38. msg: '',
  39. data: {}
  40. }
  41. result.success = res?.error_code === 0 && res?.data?.status
  42. result.data = res?.data
  43. if (result.success) {
  44. this.model.expands.used += 1
  45. this.model.expands.surplus = Math.max(this.model.expands.surplus - 1, 0)
  46. } else {
  47. result.msg = res?.error_msg || '抱歉,操作失败'
  48. }
  49. return result
  50. })
  51. }
  52. async ajaxRemove() {
  53. return ajaxFollowClientChange({ name: this.id, b: true }).then(
  54. (res) => {
  55. const result = {
  56. success: false,
  57. msg: '',
  58. data: {}
  59. }
  60. result.success = res?.error_code === 0 && res?.data?.status
  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. } else {
  66. result.msg = res?.error_msg || '抱歉,操作失败'
  67. }
  68. return result
  69. }
  70. )
  71. }
  72. }