activity.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Activity, StudyTour } from '../core/libs/models'
  2. import { getActivityStatusButtonText } from '../core/utils/common'
  3. import dayjs from 'dayjs'
  4. import { useUserStore } from '../store'
  5. import { storeToRefs } from 'pinia'
  6. /**
  7. * 游学活动
  8. */
  9. export const useActivity = (options: globalThis.Ref<Partial<Activity> | Partial<StudyTour>>) => {
  10. const userStore = useUserStore()
  11. const { userInfo } = storeToRefs(userStore)
  12. const applyStartAt = computed(
  13. () => options.value?.applyStartTime || options.value?.planApplyStartTime,
  14. )
  15. const applyEndAt = computed(() => options.value?.applyEndTime || options.value?.planApplyEndTime)
  16. const startAt = computed(() => options.value?.activityStartTime || options.value?.studyStartTime)
  17. const endAt = computed(() => options.value?.activityEndTime || options.value?.studyEndTime)
  18. const listItemButtonText = ref(getActivityStatusButtonText(applyStartAt.value, applyEndAt.value))
  19. const tooltipShow = ref(true)
  20. const difference = computed(() =>
  21. options.value?.needPointsType === '1'
  22. ? options.value?.needPointsCount - userInfo.value?.level?.point
  23. : 0,
  24. )
  25. const getActivityStatus = () => {
  26. const now = new Date()
  27. if (dayjs(now).isBefore(dayjs(applyStartAt.value))) {
  28. return 'waiting'
  29. } else if (
  30. dayjs(now).isAfter(dayjs(applyStartAt.value)) &&
  31. dayjs(now).isBefore(dayjs(applyEndAt.value))
  32. ) {
  33. return 'registering'
  34. } else if (
  35. dayjs(now).isAfter(dayjs(applyEndAt.value)) &&
  36. dayjs(now).isBefore(dayjs(startAt.value))
  37. ) {
  38. return 'closed'
  39. } else if (
  40. dayjs(now).isAfter(dayjs(startAt.value)) &&
  41. dayjs(now).isBefore(dayjs(endAt.value))
  42. ) {
  43. return 'running'
  44. } else {
  45. return 'overdue'
  46. }
  47. }
  48. // 报名未开始、报名中、报名已结束、活动进行中、活动已结束
  49. const getActivityStatusText = () =>
  50. ({
  51. waiting: '报名未开始',
  52. registering: '报名中',
  53. closed: '报名已结束',
  54. running: '活动进行中',
  55. overdue: '活动已结束',
  56. })[getActivityStatus()]
  57. const status = ref(getActivityStatus())
  58. const statusText = ref(getActivityStatusText())
  59. const detailButtonText = ref()
  60. const refresh = () => {
  61. listItemButtonText.value = getActivityStatusButtonText(applyStartAt.value, applyEndAt.value)
  62. status.value = getActivityStatus()
  63. statusText.value = getActivityStatusText()
  64. }
  65. watch(
  66. () => options.value,
  67. () => {
  68. console.log(1111)
  69. refresh()
  70. },
  71. )
  72. return {
  73. getActivityStatusText: () => getActivityStatusText(),
  74. status,
  75. listItemButtonText,
  76. statusText,
  77. tooltipShow,
  78. difference,
  79. refresh,
  80. startAt,
  81. endAt,
  82. }
  83. }