index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { defineComponent, onMounted, onUnmounted, ref } from 'vue'
  2. import { useLocalStorage } from '@vueuse/core'
  3. import AlbumList from '../album'
  4. import MusicList from '../list'
  5. import styles from './index.module.less'
  6. import { useRoute, useRouter } from 'vue-router'
  7. import { getRandomKey } from '../music'
  8. import { mitter } from './header'
  9. import { SubjectEnum, useSubjectId } from '@/helpers/hooks'
  10. import AllSearch from './all-search'
  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. keyword.value = val
  28. const indexOf = words.value.indexOf(val)
  29. if (indexOf > -1) {
  30. words.value.splice(indexOf, 1)
  31. }
  32. if (val) {
  33. words.value.unshift(val)
  34. words.value.length = Math.min(words.value.length, 5)
  35. }
  36. const activeRef = activeTab.value === 'album' ? albumList : musicList
  37. ;(activeRef.value as any).onSearch?.(val)
  38. }
  39. const onComfirm = tags => {
  40. const data = Object.values(tags).flat().filter(Boolean).join(',')
  41. tagids.value = data
  42. const activeRef = activeTab.value === 'album' ? albumList : musicList
  43. ;(activeRef.value as any).onComfirm?.(tags)
  44. tagVisibility.value = false
  45. }
  46. const onConfirmSubject = (item: any) => {
  47. subject.value = item.id
  48. const activeRef = activeTab.value === 'album' ? albumList : musicList
  49. ;(activeRef.value as any).onComfirmSubject?.(item)
  50. }
  51. const albumList = ref(null)
  52. const musicList = ref(null)
  53. const changeTab = (val: any) => {
  54. console.log(val, 'val')
  55. activeTab.value = val
  56. }
  57. onMounted(() => {
  58. mitter.on('changeTab', changeTab)
  59. mitter.on('search', onSearch)
  60. mitter.on('confirm', onComfirm)
  61. mitter.on('confirmSubject', onConfirmSubject)
  62. console.log(activeTab.value, 'activeTab.value')
  63. })
  64. onUnmounted(() => {
  65. mitter.off('changeTab', changeTab)
  66. mitter.off('search', onSearch)
  67. mitter.off('confirm', onComfirm)
  68. mitter.off('confirmSubject', onConfirmSubject)
  69. })
  70. return () => {
  71. return (
  72. <div class={styles.search}>
  73. {activeTab.value === 'all' && (
  74. <AllSearch
  75. defauleParams={{
  76. albumTagIds: tagids.value,
  77. subjectIds: subject.value
  78. }}
  79. />
  80. )}
  81. {activeTab.value === 'album' && (
  82. <AlbumList
  83. hideSearch
  84. ref={albumList}
  85. defauleParams={{
  86. // search: keyword.value,
  87. // tagids: tagids.value,
  88. albumTagIds: tagids.value,
  89. subjectIds: subject.value
  90. }}
  91. />
  92. )}
  93. {activeTab.value === 'songe' && (
  94. <MusicList
  95. hideSearch
  96. ref={musicList}
  97. onItemClick={(item: any) => {
  98. router.push({
  99. path: '/music-detail',
  100. query: {
  101. id: item.id,
  102. albumId: route.params.id
  103. }
  104. })
  105. }}
  106. defauleParams={{
  107. // search: keyword.value,
  108. // tagids: tagids.value,
  109. musicTagIds: tagids.value,
  110. subjectIds: subject.value
  111. }}
  112. />
  113. )}
  114. </div>
  115. )
  116. }
  117. }
  118. })