project-api.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. msg: '',
  36. data: {}
  37. }
  38. result.success = res?.error_code === 0 && res?.data?.status
  39. result.data = res?.data
  40. if (res?.data?.followId) {
  41. this.fid = res.data.followId
  42. }
  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 ajaxFollowProjectCancel({ sid: this.id, fid: this.fid }).then(
  54. (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. } else {
  66. result.msg = res?.error_msg || '抱歉,操作失败'
  67. }
  68. return result
  69. }
  70. )
  71. }
  72. }