index.tsx 6.9 KB

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