index.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 { 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. temp.push({
  76. id: row.id,
  77. coverImg: row.musicSvg,
  78. type: 'MUSIC',
  79. title: row.musicSheetName,
  80. isCollect: false,
  81. isSelected: true,
  82. content: row.id,
  83. xmlFileUrl: row.xmlFileUrl
  84. });
  85. });
  86. state.tableList.push(...temp);
  87. state.finshed = data.pages <= data.current ? true : false;
  88. } catch {
  89. state.loading = false;
  90. }
  91. };
  92. const throttledFnSearch = useDebounceFn(item => {
  93. state.pagination.page = 1;
  94. state.tableList = [];
  95. state.searchGroup = Object.assign(state.searchGroup, item);
  96. getList();
  97. }, 500);
  98. const throttledFn = useThrottleFn(() => {
  99. state.pagination.page = state.pagination.page + 1;
  100. getList();
  101. }, 500);
  102. // 收藏
  103. const onCollect = async (item: any) => {
  104. try {
  105. await favorite({
  106. materialId: item.id,
  107. favoriteFlag: item.isCollect ? 0 : 1,
  108. type: item.type
  109. });
  110. item.isCollect = !item.isCollect;
  111. } catch {
  112. //
  113. }
  114. };
  115. onMounted(() => {
  116. useResizeObserver(
  117. document.querySelector('.' + className) as HTMLElement,
  118. (entries: any) => {
  119. const entry = entries[0];
  120. const { height } = entry.contentRect;
  121. state.searchHeight = height + 'px';
  122. }
  123. );
  124. if (props.type === 'homework') {
  125. state.isShowAddDisabled = false;
  126. }
  127. getList();
  128. });
  129. return () => (
  130. <div class={styles.selectMusic}>
  131. <div class={className}>
  132. <SearchGroup onSearch={(item: any) => throttledFnSearch(item)} />
  133. </div>
  134. <NScrollbar
  135. class={styles.listContainer}
  136. style={{
  137. 'max-height': `calc(85vh - var(--modal-lesson-tab-height) - ${state.searchHeight} - 12px) `
  138. }}
  139. onScroll={(e: any) => {
  140. const clientHeight = e.target?.clientHeight;
  141. const scrollTop = e.target?.scrollTop;
  142. const scrollHeight = e.target?.scrollHeight;
  143. // 是否到底,是否加载完
  144. if (
  145. clientHeight + scrollTop + 20 >= scrollHeight &&
  146. !state.finshed &&
  147. !state.loading
  148. ) {
  149. throttledFn();
  150. }
  151. }}>
  152. <NSpin show={state.loading} size={'small'}>
  153. <div
  154. style={{
  155. 'min-height': `calc(85vh - var(--modal-lesson-tab-height) - ${state.searchHeight} - 12px)`
  156. }}
  157. class={[
  158. styles.listSection,
  159. !state.loading && state.tableList.length <= 0
  160. ? styles.emptySection
  161. : ''
  162. ]}>
  163. {state.tableList.length > 0 && (
  164. <div class={styles.list}>
  165. {state.tableList.map((item: any) => (
  166. <CardType
  167. isShowAdd
  168. isShowCollect
  169. item={item}
  170. // isShowAddDisabled={state.isShowAddDisabled}
  171. onAdd={() => emit('add', item)}
  172. disabledMouseHover={false}
  173. onClick={() => {
  174. if (item.type === 'IMG') return;
  175. state.show = true;
  176. state.item = item;
  177. }}
  178. onCollect={(item: any) => onCollect(item)}
  179. />
  180. ))}
  181. </div>
  182. )}
  183. {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
  184. </div>
  185. </NSpin>
  186. </NScrollbar>
  187. {/* 弹窗查看 */}
  188. <CardPreview v-model:show={state.show} item={state.item} />
  189. </div>
  190. );
  191. }
  192. });