index.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { NScrollbar, NSpin, NTabPane, NTabs } from 'naive-ui';
  2. import { defineComponent, onMounted, reactive, watch } from 'vue';
  3. import styles from './index.module.less';
  4. import CardType from '@/components/card-type';
  5. import SearchGroup from './search-group';
  6. import TheEmpty from '/src/components/TheEmpty';
  7. import { useDebounceFn, useThrottleFn, useResizeObserver } from '@vueuse/core';
  8. import { usePrepareStore } from '/src/store/modules/prepareLessons';
  9. import { musicSheetPage } from '../../../api';
  10. import CardPreview from '/src/components/card-preview';
  11. import { favorite, materialQueryPage } from '/src/views/natural-resources/api';
  12. const formatType = (type: string) => {
  13. if (type === 'shareResources') {
  14. return 2;
  15. } else if (type === 'myResources') {
  16. return 3;
  17. } else if (type === 'myCollect') {
  18. return 4;
  19. }
  20. };
  21. export default defineComponent({
  22. name: 'select-music',
  23. props: {
  24. type: {
  25. type: String,
  26. default: ''
  27. },
  28. from: {
  29. type: String,
  30. default: ''
  31. }
  32. },
  33. emits: ['add'],
  34. setup(props, { emit }) {
  35. const prepareStore = usePrepareStore();
  36. const state = reactive({
  37. searchHeight: '0px',
  38. loading: false,
  39. finshed: false, // 是否加载完
  40. pagination: {
  41. page: 1,
  42. rows: 20
  43. },
  44. searchGroup: {
  45. name: '',
  46. type: 'MUSIC', //
  47. musicSheetCategoriesId: '',
  48. musicalInstrumentId: '',
  49. sourceType: formatType(props.type),
  50. status: 1,
  51. versionFlag: false,
  52. subjectId: null
  53. },
  54. tableList: [] as any,
  55. show: false,
  56. item: {} as any,
  57. isShowAddDisabled: !prepareStore.getIsEditTrain
  58. });
  59. const className = 'musicSearchGroup' + +new Date();
  60. const getList = async () => {
  61. try {
  62. if (state.pagination.page === 1) {
  63. state.loading = true;
  64. }
  65. const { data } = await materialQueryPage({
  66. ...state.searchGroup,
  67. ...state.pagination
  68. });
  69. state.loading = false;
  70. if (data.current === 1 && state.tableList.length > 0) return;
  71. const tempRows = data.rows || [];
  72. const temp: any = [];
  73. tempRows.forEach((row: any) => {
  74. // const index = prepareStore.getTrainList.findIndex(
  75. // (course: any) => course.musicId === row.id
  76. // );
  77. temp.push({
  78. id: row.id,
  79. coverImg: row.coverImg || row.musicSvg,
  80. type: 'MUSIC',
  81. title: row.name,
  82. isCollect: !!row.favoriteFlag,
  83. isSelected: row.sourceFrom === 'PLATFORM' ? true : false,
  84. refFlag: row.refFlag,
  85. content: row.id,
  86. xmlFileUrl: row.xmlFileUrl,
  87. containAccompaniment: row.containAccompaniment
  88. // exist: index !== -1 ? true : false // 是否存在
  89. });
  90. });
  91. state.tableList.push(...temp);
  92. state.finshed = data.pages <= data.current ? true : false;
  93. } catch {
  94. state.loading = false;
  95. }
  96. };
  97. // watch(
  98. // () => prepareStore.trainList,
  99. // () => {
  100. // state.tableList.forEach((item: any) => {
  101. // const index = prepareStore.getTrainList.findIndex(
  102. // (course: any) => course.musicId === item.id
  103. // );
  104. // item.exist = index !== -1 ? true : false; // 是否存在
  105. // });
  106. // },
  107. // {
  108. // deep: true,
  109. // immediate: true
  110. // }
  111. // );
  112. const throttledFnSearch = useDebounceFn(item => {
  113. state.pagination.page = 1;
  114. state.tableList = [];
  115. state.searchGroup = Object.assign(state.searchGroup, item);
  116. getList();
  117. }, 500);
  118. const throttledFn = useThrottleFn(() => {
  119. state.pagination.page = state.pagination.page + 1;
  120. getList();
  121. }, 500);
  122. // 收藏
  123. const onCollect = async (item: any) => {
  124. try {
  125. await favorite({
  126. materialId: item.id,
  127. favoriteFlag: item.isCollect ? 0 : 1,
  128. type: item.type
  129. });
  130. item.isCollect = !item.isCollect;
  131. } catch {
  132. //
  133. }
  134. };
  135. onMounted(() => {
  136. useResizeObserver(
  137. document.querySelector('.' + className) as HTMLElement,
  138. (entries: any) => {
  139. const entry = entries[0];
  140. const { height } = entry.contentRect;
  141. state.searchHeight = height + 'px';
  142. }
  143. );
  144. if (props.type === 'homework') {
  145. state.isShowAddDisabled = false;
  146. }
  147. getList();
  148. });
  149. return () => (
  150. <div class={styles.selectMusic}>
  151. <div class={className}>
  152. <SearchGroup
  153. type={props.type}
  154. onSearch={(item: any) => throttledFnSearch(item)}
  155. />
  156. </div>
  157. <NScrollbar
  158. class={styles.listContainer}
  159. style={{
  160. 'max-height': `calc(85vh - var(--modal-lesson-tab-height) - ${state.searchHeight} - 12px) `
  161. }}
  162. onScroll={(e: any) => {
  163. const clientHeight = e.target?.clientHeight;
  164. const scrollTop = e.target?.scrollTop;
  165. const scrollHeight = e.target?.scrollHeight;
  166. // 是否到底,是否加载完
  167. if (
  168. clientHeight + scrollTop + 20 >= scrollHeight &&
  169. !state.finshed &&
  170. !state.loading
  171. ) {
  172. throttledFn();
  173. }
  174. }}>
  175. <NSpin show={state.loading} size={'small'}>
  176. <div
  177. style={{
  178. 'min-height': `calc(85vh - var(--modal-lesson-tab-height) - ${state.searchHeight} - 12px)`
  179. }}
  180. class={[
  181. styles.listSection,
  182. !state.loading && state.tableList.length <= 0
  183. ? styles.emptySection
  184. : ''
  185. ]}>
  186. {state.tableList.length > 0 && (
  187. <div class={styles.list}>
  188. {state.tableList.map((item: any) => (
  189. <CardType
  190. isShowAdd
  191. isShowCollect
  192. item={item}
  193. // isShowAddDisabled={state.isShowAddDisabled}
  194. onAdd={() => emit('add', item)}
  195. disabledMouseHover={false}
  196. onClick={() => {
  197. if (item.type === 'IMG') return;
  198. state.show = true;
  199. state.item = item;
  200. }}
  201. onCollect={(item: any) => onCollect(item)}
  202. />
  203. ))}
  204. </div>
  205. )}
  206. {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
  207. </div>
  208. </NSpin>
  209. </NScrollbar>
  210. {/* 弹窗查看 */}
  211. <CardPreview
  212. from={props.from}
  213. v-model:show={state.show}
  214. item={state.item}
  215. isDownload={false}
  216. />
  217. </div>
  218. );
  219. }
  220. });