index.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import {
  2. PropType,
  3. defineComponent,
  4. nextTick,
  5. onMounted,
  6. reactive,
  7. ref,
  8. toRef
  9. } from 'vue';
  10. import styles from './index.module.less';
  11. import {
  12. NButton,
  13. NForm,
  14. NFormItem,
  15. NImage,
  16. NPopselect,
  17. NSpace
  18. } from 'naive-ui';
  19. import { resourceTypeArray } from '/src/utils/searchArray';
  20. import { useCatchStore } from '/src/store/modules/catchData';
  21. import { useThrottleFn } from '@vueuse/core';
  22. import TheSearch from '/src/components/TheSearch';
  23. import isCollaose from '/src/views/natural-resources/images/isCollaose.png';
  24. import { audioPlayType } from '/src/utils/contants';
  25. export default defineComponent({
  26. name: 'resource-search-group',
  27. props: {
  28. type: {
  29. type: String as PropType<
  30. 'relateResources' | 'shareResources' | 'myResources' | 'myCollect'
  31. >,
  32. default: 'shareResources'
  33. },
  34. subjectId: {
  35. type: String,
  36. default: ''
  37. },
  38. // 从哪里来的
  39. from: {
  40. type: String,
  41. default: ''
  42. }
  43. },
  44. emits: ['search'],
  45. setup(props, { emit }) {
  46. const subjectId = toRef(props.subjectId);
  47. const catchStore = useCatchStore();
  48. const forms = reactive({
  49. type: 'MUSIC', //
  50. name: '',
  51. audioPlayTypes:
  52. props.type === 'myResources' && props.from === 'class' ? 'PLAY' : '',
  53. subjectId: subjectId.value as any,
  54. bookVersionId: null
  55. });
  56. const resourceType = ref([] as any);
  57. const audioPlayTypeList = ref([] as any);
  58. const onSearch = (type?: string) => {
  59. emit(
  60. 'search',
  61. {
  62. ...forms,
  63. subjectId: forms.audioPlayTypes !== 'SING' ? forms.subjectId : null,
  64. audioPlayTypes: forms.audioPlayTypes
  65. ? forms.audioPlayTypes === 'PLAY_SING'
  66. ? ['PLAY', 'SING']
  67. : [forms.audioPlayTypes]
  68. : []
  69. },
  70. type
  71. );
  72. };
  73. const throttleFn = useThrottleFn(() => onSearch(), 500);
  74. const selectChildObj = (item: any) => {
  75. const obj: any = {};
  76. item?.forEach((child: any) => {
  77. if (child.id === forms.subjectId) {
  78. obj.selected = true;
  79. obj.name = child.name;
  80. }
  81. });
  82. return obj;
  83. };
  84. /** 默认选中第一个声部 */
  85. const formatFirstSubject = () => {
  86. const tempSubjects = catchStore.getSubjectInstrumentOnly;
  87. if (tempSubjects.length > 0) {
  88. const firstSubject = tempSubjects[0];
  89. if (firstSubject.instruments && firstSubject.instruments.length > 1) {
  90. forms.subjectId = firstSubject.instruments[0]?.value;
  91. } else {
  92. forms.subjectId = firstSubject.value;
  93. }
  94. }
  95. };
  96. onMounted(async () => {
  97. // 场景
  98. const tempAudio = Object.keys(audioPlayType).map(key => {
  99. return {
  100. value: key,
  101. name: audioPlayType[key]
  102. };
  103. });
  104. audioPlayTypeList.value = [{ name: '全部', value: '' }, ...tempAudio];
  105. resourceTypeArray.forEach((item: any) => {
  106. resourceType.value.push(item);
  107. });
  108. // 获取教材分类列表
  109. await catchStore.getMusicSheetCategory();
  110. // 获取声部
  111. await catchStore.getSubjects();
  112. if (!props.subjectId) {
  113. formatFirstSubject();
  114. }
  115. catchStore.getSubjectInstruments.forEach((item: any) => {
  116. if (item.instruments?.length > 0) {
  117. item.instruments.forEach((child: any) => {
  118. if (child.id == props.subjectId) {
  119. forms.subjectId = child.value;
  120. }
  121. });
  122. } else {
  123. if (item.id == props.subjectId) {
  124. forms.subjectId = item.value;
  125. }
  126. }
  127. });
  128. onSearch('timer');
  129. });
  130. return () => (
  131. <div class={styles.searchGroup}>
  132. <NForm labelAlign="left" labelPlacement="left">
  133. {props.type !== 'myResources' && (
  134. <NFormItem label="场景:">
  135. <NSpace
  136. class={[
  137. styles.spaceSection,
  138. styles.spaceSection2,
  139. 'spaceSectionBox'
  140. ]}>
  141. {audioPlayTypeList.value.map((subject: any) => (
  142. <span
  143. class={[
  144. styles.textBtn,
  145. forms.audioPlayTypes === subject.value &&
  146. styles.textBtnActive
  147. ]}
  148. onClick={() => {
  149. forms.audioPlayTypes = subject.value;
  150. // if (subject.value === 'SING') {
  151. // forms.subjectId = null;
  152. // } else {
  153. // formatFirstSubject();
  154. // }
  155. onSearch();
  156. }}>
  157. {subject.name}
  158. </span>
  159. ))}
  160. </NSpace>
  161. </NFormItem>
  162. )}
  163. {forms.audioPlayTypes !== 'SING' && (
  164. <NFormItem label="乐器:">
  165. <NSpace
  166. class={[
  167. styles.spaceSection,
  168. styles.spaceSection2,
  169. 'spaceSectionBox'
  170. ]}>
  171. {catchStore.getSubjectInstrumentOnly.map((subject: any) =>
  172. subject.instruments && subject.instruments.length > 1 ? (
  173. <NPopselect
  174. options={subject.instruments}
  175. trigger="hover"
  176. scrollable
  177. v-model:value={forms.subjectId}
  178. onUpdate:value={() => {
  179. onSearch();
  180. }}
  181. key={subject.value}
  182. class={[styles.popSelect]}>
  183. <span
  184. class={[
  185. styles.textBtn,
  186. selectChildObj(subject.instruments).selected &&
  187. styles.textBtnActive
  188. ]}>
  189. {selectChildObj(subject.instruments).name ||
  190. subject.name}
  191. <i class={styles.iconArrow}></i>
  192. </span>
  193. </NPopselect>
  194. ) : (
  195. <span
  196. class={[
  197. styles.textBtn,
  198. forms.subjectId === subject.value &&
  199. styles.textBtnActive
  200. ]}
  201. onClick={() => {
  202. forms.subjectId = subject.value;
  203. onSearch();
  204. }}>
  205. {subject.name}
  206. </span>
  207. )
  208. )}
  209. </NSpace>
  210. </NFormItem>
  211. )}
  212. <TheSearch
  213. class={styles.inputSearch}
  214. round
  215. onSearch={(val: string) => {
  216. forms.name = val;
  217. throttleFn();
  218. }}
  219. />
  220. </NForm>
  221. </div>
  222. );
  223. }
  224. });