index.tsx 3.2 KB

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