catchData.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. /**
  49. * 获取所有启用的声部
  50. */
  51. getEnableSubjects(): any[] {
  52. const temp: any[] = [];
  53. this.subjectList.forEach((subject: any) => {
  54. if (subject.enableFlag) {
  55. const { instruments, ...r } = subject;
  56. if (instruments && instruments.length > 0) {
  57. const tempChild: any[] = [];
  58. instruments?.forEach((instrument: any) => {
  59. if (instrument.enableFlag) {
  60. tempChild.push(instrument);
  61. }
  62. });
  63. temp.push({ ...r, instruments: tempChild });
  64. }
  65. }
  66. });
  67. return temp;
  68. },
  69. getSubjectInstruments(): any[] {
  70. return [
  71. {
  72. name: '全部',
  73. id: null,
  74. label: '全部',
  75. value: null
  76. },
  77. ...this.subjectInstruemnts
  78. ];
  79. }
  80. },
  81. actions: {
  82. setBookVersion(books: any[]) {
  83. this.bookVersionList = books;
  84. },
  85. setMusicCategories(musics: any[]) {
  86. this.musicTypeList = musics;
  87. },
  88. setSubjects(subjects: any[]) {
  89. this.subjectList = subjects;
  90. },
  91. setSubjectInstruemnts(subjects: any[]) {
  92. this.subjectInstruemnts = subjects;
  93. },
  94. setMusicInstruments(instruments: any[]) {
  95. this.musicInstrumentList = instruments;
  96. },
  97. /**
  98. * 判断是否有声部数据,如不存在则获取声部列表
  99. * @returns Promise
  100. */
  101. async getSubjects() {
  102. try {
  103. // 判断是否存在声部数据
  104. if (this.getSubjectList && this.getSubjectList.length > 0) {
  105. return Promise.resolve();
  106. }
  107. const { data } = await getSubjectList2({
  108. // enableFlag: true,
  109. delFlag: 0,
  110. page: 1,
  111. rows: 999
  112. });
  113. const tempSubjectList = data || [];
  114. const tempSubjectInstruments: any = [];
  115. tempSubjectList.forEach((item: any) => {
  116. item.value = item.id;
  117. item.label = item.name;
  118. if (item.instruments && item.instruments.length > 0) {
  119. item.instruments.forEach((child: any) => {
  120. child.label = child.name;
  121. child.value = child.id;
  122. });
  123. }
  124. const tempSi = {
  125. value: item.id,
  126. label: item.name,
  127. id: item.id,
  128. name: item.name,
  129. instruments: [] as any
  130. };
  131. if (item.instruments) {
  132. if (item.instruments.length == 1) {
  133. tempSi.value = item.instruments[0].id;
  134. tempSi.label = item.instruments[0].name;
  135. tempSi.id = item.id;
  136. tempSi.name = item.name;
  137. } else if (item.instruments.length > 1) {
  138. item.instruments.forEach((child: any) => {
  139. child.label = child.name;
  140. child.value = child.id;
  141. tempSi.instruments.push({
  142. label: child.name,
  143. value: child.id,
  144. id: child.id,
  145. name: child.name
  146. });
  147. });
  148. }
  149. }
  150. tempSubjectInstruments.push(tempSi);
  151. });
  152. this.setSubjects(tempSubjectList || []);
  153. this.setSubjectInstruemnts(tempSubjectInstruments || []);
  154. return Promise.resolve();
  155. } catch (e) {
  156. return Promise.reject(e);
  157. }
  158. },
  159. /**
  160. * 判断是否有教材分类数据,如不存在则获取教材分类列表
  161. * @returns Promise
  162. */
  163. async getMusicSheetCategory() {
  164. try {
  165. // 判断是否存在声部数据
  166. if (this.getMusicCategories && this.getMusicCategories.length > 0) {
  167. return Promise.resolve();
  168. }
  169. const { data } = await getCategories({
  170. enable: true,
  171. page: 1,
  172. rows: 999
  173. });
  174. this.setMusicCategories(data.rows || []);
  175. return Promise.resolve();
  176. } catch (e) {
  177. return Promise.reject(e);
  178. }
  179. },
  180. /**
  181. * 获取乐器列表
  182. * @returns Promise
  183. */
  184. async getMusicInstrument() {
  185. try {
  186. // 判断是否存在声部数据
  187. if (this.getMusicInstruments && this.getMusicInstruments.length > 0) {
  188. return Promise.resolve();
  189. }
  190. const { data } = await api_musicalInstrumentList({
  191. enableFlag: true
  192. });
  193. console.log(data, 'data');
  194. this.setMusicInstruments(data || []);
  195. return Promise.resolve();
  196. } catch (e) {
  197. return Promise.reject(e);
  198. }
  199. }
  200. }
  201. });
  202. // Need to be used outside the setup
  203. export function useCatchStoreWidthOut() {
  204. return useCatchStore(store);
  205. }