index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { NScrollbar, NSpin, NTabPane, NTabs } from 'naive-ui';
  2. import { 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 { useThrottleFn } from '@vueuse/core';
  8. import { usePrepareStore } from '/src/store/modules/prepareLessons';
  9. import { musicSheetPage } from '../../api';
  10. export default defineComponent({
  11. name: 'select-music',
  12. emits: ['select', 'add'],
  13. setup(props, { emit }) {
  14. const prepareStore = usePrepareStore();
  15. const state = reactive({
  16. loading: false,
  17. finshed: false, // 是否加载完
  18. pagination: {
  19. page: 1,
  20. rows: 20
  21. },
  22. searchGroup: {
  23. keyword: '',
  24. musicSheetCategoriesId: '',
  25. status: 1,
  26. versionFlag: false,
  27. subjectId: null
  28. },
  29. tableList: [] as any
  30. });
  31. const getList = async () => {
  32. try {
  33. if (state.pagination.page === 1) {
  34. state.loading = true;
  35. }
  36. const { data } = await musicSheetPage({
  37. ...state.searchGroup,
  38. ...state.pagination,
  39. subjectId: prepareStore.getSubjectId
  40. });
  41. state.loading = false;
  42. const tempRows = data.rows || [];
  43. const temp: any = [];
  44. tempRows.forEach((row: any) => {
  45. temp.push({
  46. id: row.id,
  47. coverImg: row.titleImg,
  48. type: 'MUSIC',
  49. title: row.musicSheetName,
  50. isCollect: false,
  51. isSelected: true,
  52. content: row.id,
  53. xmlFileUrl: row.xmlFileUrl
  54. });
  55. });
  56. state.tableList.push(...temp);
  57. state.finshed = data.pages <= data.current ? true : false;
  58. } catch {
  59. state.loading = false;
  60. }
  61. };
  62. const onSearch = async (item: any) => {
  63. state.pagination.page = 1;
  64. state.tableList = [];
  65. state.searchGroup = Object.assign(state.searchGroup, item);
  66. getList();
  67. };
  68. const throttledFn = useThrottleFn(() => {
  69. state.pagination.page = state.pagination.page + 1;
  70. getList();
  71. }, 500);
  72. onMounted(() => {
  73. getList();
  74. });
  75. return () => (
  76. <div class={styles.selectMusic}>
  77. <NTabs
  78. animated
  79. defaultValue="shareResources"
  80. paneClass={styles.paneTitle}
  81. justifyContent="center"
  82. paneWrapperClass={styles.paneWrapperContainer}>
  83. <NTabPane name="shareResources" tab="选择曲目">
  84. <SearchGroup onSearch={(item: any) => onSearch(item)} />
  85. <NScrollbar
  86. class={styles.listContainer}
  87. onScroll={(e: any) => {
  88. const clientHeight = e.target?.clientHeight;
  89. const scrollTop = e.target?.scrollTop;
  90. const scrollHeight = e.target?.scrollHeight;
  91. // 是否到底,是否加载完
  92. if (
  93. clientHeight + scrollTop + 20 >= scrollHeight &&
  94. !state.finshed &&
  95. !state.loading
  96. ) {
  97. throttledFn();
  98. }
  99. }}>
  100. <NSpin show={state.loading} size={'small'}>
  101. <div
  102. class={[
  103. styles.listSection,
  104. !state.loading && state.tableList.length <= 0
  105. ? styles.emptySection
  106. : ''
  107. ]}>
  108. {state.tableList.length > 0 && (
  109. <div class={styles.list}>
  110. {state.tableList.map((item: any) => (
  111. <CardType
  112. isShowAdd
  113. item={item}
  114. onAdd={() => emit('add', item)}
  115. />
  116. ))}
  117. </div>
  118. )}
  119. {!state.loading && state.tableList.length <= 0 && (
  120. <TheEmpty />
  121. )}
  122. </div>
  123. </NSpin>
  124. </NScrollbar>
  125. </NTabPane>
  126. </NTabs>
  127. </div>
  128. );
  129. }
  130. });