index.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { NScrollbar, NSpin, NTabPane, NTabs } from 'naive-ui';
  2. import { PropType, defineComponent, onMounted, reactive } 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 } 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. export default defineComponent({
  12. name: 'select-music',
  13. props: {
  14. type: {
  15. type: String,
  16. default: ''
  17. }
  18. },
  19. emits: ['add'],
  20. setup(props, { emit }) {
  21. console.log(props.type);
  22. const prepareStore = usePrepareStore();
  23. const state = reactive({
  24. loading: false,
  25. finshed: false, // 是否加载完
  26. pagination: {
  27. page: 1,
  28. rows: 20
  29. },
  30. searchGroup: {
  31. name: '',
  32. musicSheetCategoriesId: '',
  33. status: 1,
  34. versionFlag: false,
  35. subjectId: null
  36. },
  37. tableList: [] as any,
  38. show: false,
  39. item: {} as any,
  40. isShowAddDisabled: !prepareStore.getIsEditTrain
  41. });
  42. const getList = async () => {
  43. try {
  44. if (state.pagination.page === 1) {
  45. state.loading = true;
  46. }
  47. const { data } = await musicSheetPage({
  48. ...state.searchGroup,
  49. ...state.pagination,
  50. subjectId: prepareStore.getSubjectId
  51. });
  52. state.loading = false;
  53. const tempRows = data.rows || [];
  54. const temp: any = [];
  55. tempRows.forEach((row: any) => {
  56. temp.push({
  57. id: row.id,
  58. coverImg: row.musicSvg,
  59. type: 'MUSIC',
  60. title: row.musicSheetName,
  61. isCollect: false,
  62. isSelected: true,
  63. content: row.id,
  64. xmlFileUrl: row.xmlFileUrl
  65. });
  66. });
  67. state.tableList.push(...temp);
  68. state.finshed = data.pages <= data.current ? true : false;
  69. } catch {
  70. state.loading = false;
  71. }
  72. };
  73. // const onSearch = async (item: any) => {
  74. // state.pagination.page = 1;
  75. // state.tableList = [];
  76. // state.searchGroup = Object.assign(state.searchGroup, item);
  77. // getList();
  78. // };
  79. const throttledFnSearch = useDebounceFn(item => {
  80. state.pagination.page = 1;
  81. state.tableList = [];
  82. state.searchGroup = Object.assign(state.searchGroup, item);
  83. getList();
  84. }, 500);
  85. const throttledFn = useThrottleFn(() => {
  86. state.pagination.page = state.pagination.page + 1;
  87. getList();
  88. }, 500);
  89. onMounted(() => {
  90. if (props.type === 'homework') {
  91. state.isShowAddDisabled = false;
  92. }
  93. getList();
  94. });
  95. return () => (
  96. <div class={styles.selectMusic}>
  97. <NTabs
  98. animated
  99. defaultValue="shareResources"
  100. paneClass={styles.paneTitle}
  101. justifyContent="center"
  102. paneWrapperClass={styles.paneWrapperContainer}>
  103. <NTabPane name="shareResources" tab="选择曲目">
  104. <SearchGroup onSearch={(item: any) => throttledFnSearch(item)} />
  105. <NScrollbar
  106. class={styles.listContainer}
  107. onScroll={(e: any) => {
  108. const clientHeight = e.target?.clientHeight;
  109. const scrollTop = e.target?.scrollTop;
  110. const scrollHeight = e.target?.scrollHeight;
  111. // 是否到底,是否加载完
  112. if (
  113. clientHeight + scrollTop + 20 >= scrollHeight &&
  114. !state.finshed &&
  115. !state.loading
  116. ) {
  117. throttledFn();
  118. }
  119. }}>
  120. <NSpin show={state.loading} size={'small'}>
  121. <div
  122. class={[
  123. styles.listSection,
  124. !state.loading && state.tableList.length <= 0
  125. ? styles.emptySection
  126. : ''
  127. ]}>
  128. {state.tableList.length > 0 && (
  129. <div class={styles.list}>
  130. {state.tableList.map((item: any) => (
  131. <CardType
  132. isShowAdd
  133. isShowCollect={false}
  134. item={item}
  135. // isShowAddDisabled={state.isShowAddDisabled}
  136. onAdd={() => emit('add', item)}
  137. disabledMouseHover={false}
  138. onClick={() => {
  139. if (item.type === 'IMG') return;
  140. state.show = true;
  141. state.item = item;
  142. }}
  143. />
  144. ))}
  145. </div>
  146. )}
  147. {!state.loading && state.tableList.length <= 0 && (
  148. <TheEmpty />
  149. )}
  150. </div>
  151. </NSpin>
  152. </NScrollbar>
  153. </NTabPane>
  154. </NTabs>
  155. {/* 弹窗查看 */}
  156. <CardPreview v-model:show={state.show} item={state.item} />
  157. </div>
  158. );
  159. }
  160. });