import { defineStore } from 'pinia'; import { store } from '@/store'; import { getSubjectList, getSubjectList2, getCategories, api_musicalInstrumentList } from '@/api/user'; export const useCatchStore = defineStore('catch-store', { state: () => ({ bookVersionList: [] as any[], // 其它类型 musicTypeList: [] as any[], // 乐谱分类 subjectList: [] as any[], // 声部列表, musicInstrumentList: [] as any[], // 乐器列表, subjectInstruemnts: [] as any[] // 乐器列表, }), getters: { getBookVersion(): any[] { return this.bookVersionList; }, getMusicCategories(): any[] { return this.musicTypeList; }, getMusicInstruments(): any[] { return this.musicInstrumentList; }, getAllMusicCategories(): any[] { return [ { name: '全部', id: null }, ...this.musicTypeList ]; }, getSubjectList(): any[] { return this.subjectList; }, getSubjectAllList(): any[] { return [ { name: '全部', id: null }, ...this.subjectList ]; }, /** * 获取所有启用的声部 */ getEnableSubjects(): any[] { const temp: any[] = []; this.subjectList.forEach((subject: any) => { if (subject.enableFlag) { const { instruments, ...r } = subject; if (instruments && instruments.length > 0) { const tempChild: any[] = []; instruments?.forEach((instrument: any) => { if (instrument.enableFlag) { tempChild.push(instrument); } }); temp.push({ ...r, instruments: tempChild }); } } }); return temp; }, getSubjectInstruments(): any[] { return [ { name: '全部', id: null, label: '全部', value: null }, ...this.subjectInstruemnts ]; } }, actions: { setBookVersion(books: any[]) { this.bookVersionList = books; }, setMusicCategories(musics: any[]) { this.musicTypeList = musics; }, setSubjects(subjects: any[]) { this.subjectList = subjects; }, setSubjectInstruemnts(subjects: any[]) { this.subjectInstruemnts = subjects; }, setMusicInstruments(instruments: any[]) { this.musicInstrumentList = instruments; }, /** * 判断是否有声部数据,如不存在则获取声部列表 * @returns Promise */ async getSubjects() { try { // 判断是否存在声部数据 if (this.getSubjectList && this.getSubjectList.length > 0) { return Promise.resolve(); } const { data } = await getSubjectList2({ // enableFlag: true, delFlag: 0, page: 1, rows: 999 }); const tempSubjectList = data || []; const tempSubjectInstruments: any = []; tempSubjectList.forEach((item: any) => { item.value = item.id; item.label = item.name; if (item.instruments && item.instruments.length > 0) { item.instruments.forEach((child: any) => { child.label = child.name; child.value = child.id; }); } const tempSi = { value: item.id, label: item.name, id: item.id, name: item.name, instruments: [] as any }; if (item.instruments) { if (item.instruments.length == 1) { tempSi.value = item.instruments[0].id; tempSi.label = item.instruments[0].name; tempSi.id = item.id; tempSi.name = item.name; } else if (item.instruments.length > 1) { item.instruments.forEach((child: any) => { child.label = child.name; child.value = child.id; tempSi.instruments.push({ label: child.name, value: child.id, id: child.id, name: child.name }); }); } } tempSubjectInstruments.push(tempSi); }); this.setSubjects(tempSubjectList || []); this.setSubjectInstruemnts(tempSubjectInstruments || []); return Promise.resolve(); } catch (e) { return Promise.reject(e); } }, /** * 判断是否有教材分类数据,如不存在则获取教材分类列表 * @returns Promise */ async getMusicSheetCategory() { try { // 判断是否存在声部数据 if (this.getMusicCategories && this.getMusicCategories.length > 0) { return Promise.resolve(); } const { data } = await getCategories({ enable: true, page: 1, rows: 999 }); this.setMusicCategories(data.rows || []); return Promise.resolve(); } catch (e) { return Promise.reject(e); } }, /** * 获取乐器列表 * @returns Promise */ async getMusicInstrument() { try { // 判断是否存在声部数据 if (this.getMusicInstruments && this.getMusicInstruments.length > 0) { return Promise.resolve(); } const { data } = await api_musicalInstrumentList({ enableFlag: true }); console.log(data, 'data'); this.setMusicInstruments(data || []); return Promise.resolve(); } catch (e) { return Promise.reject(e); } } } }); // Need to be used outside the setup export function useCatchStoreWidthOut() { return useCatchStore(store); }