project-api.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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({ 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.fid = ''
  64. this.model.expands.surplus += 1
  65. this.model.expands.used = Math.max(this.model.expands.used - 1, 0)
  66. } else {
  67. result.msg = res?.error_msg || '抱歉,操作失败'
  68. }
  69. return result
  70. }
  71. )
  72. }
  73. }