search-result.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 './result-all-search'
  10. import MusicList from '../list'
  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.search || '')
  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. if (activeTab.value !== 'all') {
  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. }
  41. const onComfirm = tags => {
  42. const data = Object.values(tags).flat().filter(Boolean).join(',')
  43. tagids.value = data
  44. const activeRef = activeTab.value === 'album' ? albumList : musicList
  45. ;(activeRef.value as any).onComfirm?.(tags)
  46. tagVisibility.value = false
  47. }
  48. const onConfirmSubject = (item: any) => {
  49. subject.value = item.id
  50. const activeRef = activeTab.value === 'album' ? albumList : musicList
  51. ;(activeRef.value as any).onComfirmSubject?.(item)
  52. }
  53. const albumList = ref(null)
  54. const musicList = ref(null)
  55. const changeTab = (val: any, searchStr: any) => {
  56. keyword.value = searchStr
  57. activeTab.value = val
  58. }
  59. onMounted(() => {
  60. mitter.on('changeTab', changeTab)
  61. mitter.on('search', onSearch)
  62. mitter.on('confirm', onComfirm)
  63. mitter.on('confirmSubject', onConfirmSubject)
  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. idAndName: keyword.value,
  78. albumTagIds: tagids.value,
  79. subjectIds: subject.value
  80. }}
  81. />
  82. )}
  83. {activeTab.value === 'album' && (
  84. <div class="mt12">
  85. <AlbumList
  86. hideSearch
  87. showLight
  88. lightText={keyword.value as any}
  89. ref={albumList}
  90. defauleParams={{
  91. search: keyword.value,
  92. albumTagIds: tagids.value,
  93. subjectIds: subject.value
  94. }}
  95. />
  96. </div>
  97. )}
  98. {activeTab.value === 'songe' && (
  99. <div class={[styles.musicGroup]}>
  100. <MusicList
  101. hideSearch
  102. showLight
  103. lightText={keyword.value as any}
  104. defauleParams={{
  105. idAndName: keyword.value,
  106. albumTagIds: tagids.value,
  107. subjectIds: subject.value
  108. }}
  109. ref={musicList}
  110. onItemClick={(item: any) => {
  111. router.push({
  112. path: '/music-detail',
  113. query: {
  114. id: item.id,
  115. albumId: route.params.id
  116. }
  117. })
  118. }}
  119. />
  120. </div>
  121. )}
  122. </div>
  123. )
  124. }
  125. }
  126. })