catchData.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { defineStore } from 'pinia';
  2. import { store } from '@/store';
  3. import { getSubjectList, getSubjectList2, getCategories } from '@/api/user';
  4. export const useCatchStore = defineStore('catch-store', {
  5. state: () => ({
  6. bookVersionList: [] as any[], // 其它类型
  7. musicTypeList: [] as any[], // 乐谱分类
  8. subjectList: [] as any[], // 声部列表,
  9. subjectInstruemnts: [] as any[] // 乐器列表,
  10. }),
  11. getters: {
  12. getBookVersion(): any[] {
  13. return this.bookVersionList;
  14. },
  15. getMusicCategories(): any[] {
  16. return this.musicTypeList;
  17. },
  18. getAllMusicCategories(): any[] {
  19. return [
  20. {
  21. name: '全部',
  22. id: null
  23. },
  24. ...this.musicTypeList
  25. ];
  26. },
  27. getSubjectList(): any[] {
  28. return this.subjectList;
  29. },
  30. getSubjectAllList(): any[] {
  31. return [
  32. {
  33. name: '全部',
  34. id: null
  35. },
  36. ...this.subjectList
  37. ];
  38. },
  39. getSubjectInstruments(): any[] {
  40. return [
  41. {
  42. name: '全部',
  43. id: null,
  44. label: '全部',
  45. value: null
  46. },
  47. ...this.subjectInstruemnts
  48. ];
  49. }
  50. },
  51. actions: {
  52. setBookVersion(books: any[]) {
  53. this.bookVersionList = books;
  54. },
  55. setMusicCategories(musics: any[]) {
  56. this.musicTypeList = musics;
  57. },
  58. setSubjects(subjects: any[]) {
  59. this.subjectList = subjects;
  60. },
  61. setSubjectInstruemnts(subjects: any[]) {
  62. this.subjectInstruemnts = subjects;
  63. },
  64. /**
  65. * 判断是否有声部数据,如不存在则获取声部列表
  66. * @returns Promise
  67. */
  68. async getSubjects() {
  69. try {
  70. // 判断是否存在声部数据
  71. if (this.getSubjectList && this.getSubjectList.length > 0) {
  72. return Promise.resolve();
  73. }
  74. const { data } = await getSubjectList2({
  75. enableFlag: true,
  76. delFlag: 0,
  77. page: 1,
  78. rows: 999
  79. });
  80. const tempSubjectList = data || [];
  81. const tempSubjectInstruments: any = [];
  82. tempSubjectList.forEach((item: any) => {
  83. item.value = item.id;
  84. item.label = item.name;
  85. if (item.instruments && item.instruments.length > 0) {
  86. item.instruments.forEach((child: any) => {
  87. child.label = child.name;
  88. child.value = child.id;
  89. });
  90. }
  91. const tempSi = {
  92. value: item.id,
  93. label: item.name,
  94. id: item.id,
  95. name: item.name,
  96. instruments: [] as any
  97. };
  98. if (item.instruments) {
  99. if (item.instruments.length == 1) {
  100. tempSi.value = item.instruments[0].id;
  101. tempSi.label = item.instruments[0].name;
  102. tempSi.id = item.id;
  103. tempSi.name = item.name;
  104. } else if (item.instruments.length > 1) {
  105. item.instruments.forEach((child: any) => {
  106. child.label = child.name;
  107. child.value = child.id;
  108. tempSi.instruments.push({
  109. label: child.name,
  110. value: child.id,
  111. id: child.id,
  112. name: child.name
  113. });
  114. });
  115. }
  116. }
  117. tempSubjectInstruments.push(tempSi);
  118. });
  119. this.setSubjects(tempSubjectList || []);
  120. this.setSubjectInstruemnts(tempSubjectInstruments || []);
  121. return Promise.resolve();
  122. } catch (e) {
  123. return Promise.reject(e);
  124. }
  125. },
  126. /**
  127. * 判断是否有教材分类数据,如不存在则获取教材分类列表
  128. * @returns Promise
  129. */
  130. async getMusicSheetCategory() {
  131. try {
  132. // 判断是否存在声部数据
  133. if (this.getMusicCategories && this.getMusicCategories.length > 0) {
  134. return Promise.resolve();
  135. }
  136. const { data } = await getCategories({
  137. enable: true,
  138. page: 1,
  139. rows: 999
  140. });
  141. this.setMusicCategories(data.rows || []);
  142. return Promise.resolve();
  143. } catch (e) {
  144. return Promise.reject(e);
  145. }
  146. }
  147. }
  148. });
  149. // Need to be used outside the setup
  150. export function useCatchStoreWidthOut() {
  151. return useCatchStore(store);
  152. }