index.tsx 3.4 KB

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