123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import { defineComponent, onMounted, onUnmounted, ref } from 'vue'
- import { useLocalStorage } from '@vueuse/core'
- import AlbumList from '../album'
- import styles from './index.module.less'
- import { useRoute, useRouter } from 'vue-router'
- import { getRandomKey } from '../music'
- import { mitter } from './header'
- import { SubjectEnum, useSubjectId } from '@/helpers/hooks'
- import AllSearch from './all-search'
- import MusicSwiper from './music-swiper'
- export default defineComponent({
- name: 'MusicSearch',
- emits: ['confirm'],
- setup() {
- localStorage.setItem('behaviorId', getRandomKey())
- const route = useRoute()
- const router = useRouter()
- const keyword = ref(route.query.keyword || '')
- const tagids = ref(route.query.tagids || '')
- const subject = ref()
- const tagVisibility = ref(false)
- const words = useLocalStorage<string[]>('music-search', [])
- const activeTab = ref('all')
- const getSubject: any = useSubjectId(SubjectEnum.SEARCH)
- subject.value = getSubject.id
- const onSearch = val => {
- console.log(val, 'val')
- keyword.value = val
- const indexOf = words.value.indexOf(val)
- if (indexOf > -1) {
- words.value.splice(indexOf, 1)
- }
- if (val) {
- words.value.unshift(val)
- words.value.length = Math.min(words.value.length, 10)
- }
- const activeRef = activeTab.value === 'album' ? albumList : musicList
- ;(activeRef.value as any).onSearch?.(val)
- }
- const onComfirm = tags => {
- const data = Object.values(tags).flat().filter(Boolean).join(',')
- tagids.value = data
- const activeRef = activeTab.value === 'album' ? albumList : musicList
- ;(activeRef.value as any).onComfirm?.(tags)
- tagVisibility.value = false
- }
- const onConfirmSubject = (item: any) => {
- subject.value = item.id
- const activeRef = activeTab.value === 'album' ? albumList : musicList
- ;(activeRef.value as any).onComfirmSubject?.(item)
- }
- const albumList = ref(null)
- const musicList = ref(null)
- const changeTab = (val: any) => {
- console.log(val, 'val')
- activeTab.value = val
- }
- onMounted(() => {
- mitter.on('changeTab', changeTab)
- mitter.on('search', onSearch)
- mitter.on('confirm', onComfirm)
- mitter.on('confirmSubject', onConfirmSubject)
- console.log(activeTab.value, 'activeTab.value')
- })
- onUnmounted(() => {
- mitter.off('changeTab', changeTab)
- mitter.off('search', onSearch)
- mitter.off('confirm', onComfirm)
- mitter.off('confirmSubject', onConfirmSubject)
- })
- return () => {
- return (
- <div class={styles.search}>
- {activeTab.value === 'all' && (
- <AllSearch
- defauleParams={{
- albumTagIds: tagids.value,
- subjectIds: subject.value
- }}
- />
- )}
- {activeTab.value === 'album' && (
- <AlbumList
- hideSearch
- ref={albumList}
- defauleParams={{
- albumTagIds: tagids.value,
- subjectIds: subject.value
- }}
- />
- )}
- {activeTab.value === 'songe' && (
- <div class={styles.musicGroup}>
- <MusicSwiper
- defauleParams={{
- musicTagIds: tagids.value,
- subjectIds: subject.value
- }}
- />
- </div>
- )}
- </div>
- )
- }
- }
- })
|