project-api.js 1.8 KB

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