index.tsx 6.6 KB

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