catchData.ts 5.0 KB

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