index.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { defineComponent, onMounted, onUnmounted, ref } from 'vue'
  2. import { useLocalStorage } from '@vueuse/core'
  3. import AlbumList from '../album'
  4. import styles from './index.module.less'
  5. import { useRoute, useRouter } from 'vue-router'
  6. import { getRandomKey } from '../music'
  7. import { mitter } from './header'
  8. import { SubjectEnum, useSubjectId } from '@/helpers/hooks'
  9. import AllSearch from './all-search'
  10. import MusicSwiper from './music-swiper'
  11. export default defineComponent({
  12. name: 'MusicSearch',
  13. emits: ['confirm'],
  14. setup() {
  15. localStorage.setItem('behaviorId', getRandomKey())
  16. const route = useRoute()
  17. const router = useRouter()
  18. const keyword = ref(route.query.keyword || '')
  19. const tagids = ref(route.query.tagids || '')
  20. const subject = ref()
  21. const tagVisibility = ref(false)
  22. const words = useLocalStorage<string[]>('music-search', [])
  23. const activeTab = ref('all')
  24. const getSubject: any = useSubjectId(SubjectEnum.SEARCH)
  25. subject.value = getSubject.id
  26. const onSearch = val => {
  27. console.log(val, 'val')
  28. keyword.value = val
  29. const indexOf = words.value.indexOf(val)
  30. if (indexOf > -1) {
  31. words.value.splice(indexOf, 1)
  32. }
  33. if (val) {
  34. words.value.unshift(val)
  35. words.value.length = Math.min(words.value.length, 10)
  36. }
  37. const activeRef = activeTab.value === 'album' ? albumList : musicList
  38. ;(activeRef.value as any).onSearch?.(val)
  39. }
  40. const onComfirm = tags => {
  41. const data = Object.values(tags).flat().filter(Boolean).join(',')
  42. tagids.value = data
  43. const activeRef = activeTab.value === 'album' ? albumList : musicList
  44. ;(activeRef.value as any).onComfirm?.(tags)
  45. tagVisibility.value = false
  46. }
  47. const onConfirmSubject = (item: any) => {
  48. subject.value = item.id
  49. const activeRef = activeTab.value === 'album' ? albumList : musicList
  50. ;(activeRef.value as any).onComfirmSubject?.(item)
  51. }
  52. const albumList = ref(null)
  53. const musicList = ref(null)
  54. const changeTab = (val: any) => {
  55. console.log(val, 'val')
  56. activeTab.value = val
  57. }
  58. onMounted(() => {
  59. mitter.on('changeTab', changeTab)
  60. mitter.on('search', onSearch)
  61. mitter.on('confirm', onComfirm)
  62. mitter.on('confirmSubject', onConfirmSubject)
  63. console.log(activeTab.value, 'activeTab.value')
  64. })
  65. onUnmounted(() => {
  66. mitter.off('changeTab', changeTab)
  67. mitter.off('search', onSearch)
  68. mitter.off('confirm', onComfirm)
  69. mitter.off('confirmSubject', onConfirmSubject)
  70. })
  71. return () => {
  72. return (
  73. <div class={styles.search}>
  74. {activeTab.value === 'all' && (
  75. <AllSearch
  76. defauleParams={{
  77. albumTagIds: tagids.value,
  78. subjectIds: subject.value
  79. }}
  80. />
  81. )}
  82. {activeTab.value === 'album' && (
  83. <AlbumList
  84. hideSearch
  85. ref={albumList}
  86. defauleParams={{
  87. albumTagIds: tagids.value,
  88. subjectIds: subject.value
  89. }}
  90. />
  91. )}
  92. {activeTab.value === 'songe' && (
  93. <div class={styles.musicGroup}>
  94. <MusicSwiper
  95. defauleParams={{
  96. musicTagIds: tagids.value,
  97. subjectIds: subject.value
  98. }}
  99. />
  100. </div>
  101. )}
  102. </div>
  103. )
  104. }
  105. }
  106. })