index.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import {
  2. PropType,
  3. defineComponent,
  4. onMounted,
  5. onUnmounted,
  6. reactive,
  7. toRefs,
  8. watch
  9. } from 'vue';
  10. import ResourceSearchGroup from './resource-search-group';
  11. import { NScrollbar, NSpin, useMessage } from 'naive-ui';
  12. import styles from './index.module.less';
  13. import CardType from '/src/components/card-type';
  14. import { favorite, materialQueryPage } from '/src/views/natural-resources/api';
  15. import TheEmpty from '/src/components/TheEmpty';
  16. import { usePrepareStore } from '/src/store/modules/prepareLessons';
  17. import { useDebounceFn, useResizeObserver } from '@vueuse/core';
  18. import CardPreview from '/src/components/card-preview';
  19. import { eventGlobal } from '/src/utils';
  20. import ClassSearchGroup from './class-search-group';
  21. import { useCatchStore } from '/src/store/modules/catchData';
  22. const formatType = (type: string) => {
  23. if (type === 'shareResources') {
  24. return 2;
  25. } else if (type === 'myResources') {
  26. return 3;
  27. } else if (type === 'myCollect') {
  28. return 4;
  29. } else if (type === 'relateResources') {
  30. return 5;
  31. }
  32. };
  33. export default defineComponent({
  34. name: 'share-resources',
  35. props: {
  36. type: {
  37. type: String as PropType<
  38. 'relateResources' | 'shareResources' | 'myResources' | 'myCollect'
  39. >,
  40. default: 'shareResources'
  41. },
  42. /** 从哪里使用 */
  43. from: {
  44. type: String,
  45. default: ''
  46. }
  47. },
  48. setup(props) {
  49. const prepareStore = usePrepareStore();
  50. const catchStore = useCatchStore();
  51. const message = useMessage();
  52. const { type } = toRefs(props);
  53. const className = 'resourceSearchGroup' + +new Date();
  54. const state = reactive({
  55. searchHeight: '0px',
  56. loading: false,
  57. finshed: false, // 是否加载完
  58. pagination: {
  59. page: 1,
  60. rows: 20
  61. },
  62. searchGroup: {
  63. type: 'MUSIC', //
  64. name: '',
  65. bookVersionId: null,
  66. subjectId: null,
  67. sourceType: formatType(type.value),
  68. musicalInstrumentId: null as any,
  69. enableFlag: true
  70. },
  71. tableList: [] as any,
  72. show: false,
  73. item: {} as any
  74. });
  75. // 查询列表
  76. const getList = async () => {
  77. try {
  78. if (state.pagination.page === 1) {
  79. state.loading = true;
  80. }
  81. const { data } = await materialQueryPage({
  82. ...state.searchGroup,
  83. ...state.pagination,
  84. lessonCoursewareKnowledgeId:
  85. props.type === 'relateResources' || props.type === 'shareResources'
  86. ? prepareStore.getSelectKey
  87. : null,
  88. relateLessonCoursewareKnowledgeMaterialIds: getIds()
  89. });
  90. state.loading = false;
  91. const tempRows = data.rows || [];
  92. const temp: any = [];
  93. tempRows.forEach((row: any) => {
  94. temp.push({
  95. id: row.id,
  96. coverImg: row.coverImg,
  97. type: row.type,
  98. title: row.name,
  99. isCollect: !!row.favoriteFlag,
  100. isSelected: row.sourceFrom === 'PLATFORM' ? true : false,
  101. refFlag: row.refFlag,
  102. containAccompaniment: row.containAccompaniment,
  103. content: row.content
  104. });
  105. });
  106. state.tableList.push(...temp);
  107. state.finshed = data.pages <= data.current ? true : false;
  108. } catch {
  109. state.loading = false;
  110. }
  111. };
  112. const throttledFnSearch = useDebounceFn(item => {
  113. state.pagination.page = 1;
  114. state.tableList = [];
  115. const { subjectId, ...res } = item;
  116. state.searchGroup = Object.assign(state.searchGroup, {
  117. ...res,
  118. musicalInstrumentId: subjectId,
  119. subjectId: null
  120. });
  121. getList();
  122. }, 500);
  123. // 添加资源
  124. const onAdd = async (item: any) => {
  125. try {
  126. eventGlobal.emit('onPrepareAddItem', {
  127. materialId: item.id,
  128. coverImg: item.coverImg,
  129. type: item.type,
  130. title: item.title,
  131. refFlag: item.refFlag,
  132. isCollect: item.isCollect,
  133. isSelected: item.isSelected,
  134. content: item.content,
  135. removeFlag: false
  136. });
  137. } catch {
  138. //
  139. }
  140. };
  141. // 收藏
  142. const onCollect = async (item: any) => {
  143. try {
  144. await favorite({
  145. materialId: item.id,
  146. favoriteFlag: item.isCollect ? 0 : 1,
  147. type: item.type
  148. });
  149. item.isCollect = !item.isCollect;
  150. } catch {
  151. //
  152. }
  153. };
  154. const getIds = () => {
  155. let noRepeatIds: any = [];
  156. if (props.type === 'relateResources') {
  157. const materialIds: any = [];
  158. prepareStore.getCoursewareList.forEach((course: any) => {
  159. course.list?.forEach((item: any) => {
  160. materialIds.push(item.materialId);
  161. });
  162. });
  163. noRepeatIds = Array(...new Set(materialIds));
  164. }
  165. return noRepeatIds.join(',');
  166. };
  167. const onUpdate = () => {
  168. state.pagination.page = 1;
  169. state.tableList = [];
  170. getList();
  171. };
  172. onMounted(async () => {
  173. // 加载的时候判断是否有资源数据
  174. // 获取声部
  175. await catchStore.getSubjects();
  176. useResizeObserver(
  177. document.querySelector('.' + className) as HTMLElement,
  178. (entries: any) => {
  179. const entry = entries[0];
  180. const { height } = entry.contentRect;
  181. state.searchHeight = height + 'px';
  182. }
  183. );
  184. getList();
  185. if (props.type === 'relateResources') {
  186. eventGlobal.on('onCoursewareUpdate', onUpdate);
  187. }
  188. });
  189. onUnmounted(() => {
  190. eventGlobal.off('onCoursewareUpdate', onUpdate);
  191. });
  192. // onMounted(async () => {
  193. // // 获取声部
  194. // await catchStore.getSubjects();
  195. // // 加载的时候判断是否有资源数据
  196. // let noRepeatIds: any = [];
  197. // if (props.type === 'relateResources') {
  198. // const materialIds: any = [];
  199. // prepareStore.getCoursewareList.forEach((course: any) => {
  200. // course.list?.forEach((item: any) => {
  201. // materialIds.push(item.materialId);
  202. // });
  203. // });
  204. // noRepeatIds = Array(...new Set(materialIds));
  205. // }
  206. // getList(noRepeatIds.join(','));
  207. // useResizeObserver(
  208. // document.querySelector('.' + className) as HTMLElement,
  209. // (entries: any) => {
  210. // const entry = entries[0];
  211. // const { height } = entry.contentRect;
  212. // state.searchHeight = height + 'px';
  213. // }
  214. // );
  215. // });
  216. return () => (
  217. <div>
  218. <div class={className}>
  219. {props.from === 'class' ? (
  220. <ClassSearchGroup
  221. type={props.type}
  222. subjectId={prepareStore.getSubjectId as any}
  223. onSearch={(item: any) => throttledFnSearch(item)}
  224. />
  225. ) : (
  226. <ResourceSearchGroup
  227. type={props.type}
  228. // subjectId={prepareStore.getSubjectId as any}
  229. onSearch={(item: any) => throttledFnSearch(item)}
  230. />
  231. )}
  232. </div>
  233. <NScrollbar
  234. class={[styles.listContainer, 'list_container']}
  235. style={{
  236. 'max-height': `calc(var(--listContainerHeight) - var(--modal-lesson-tab-height) - ${state.searchHeight}) `
  237. }}
  238. onScroll={(e: any) => {
  239. const clientHeight = e.target?.clientHeight;
  240. const scrollTop = e.target?.scrollTop;
  241. const scrollHeight = e.target?.scrollHeight;
  242. // 是否到底,是否加载完
  243. if (
  244. clientHeight + scrollTop + 20 >= scrollHeight &&
  245. !state.finshed &&
  246. !state.loading
  247. ) {
  248. state.pagination.page = state.pagination.page + 1;
  249. getList();
  250. }
  251. }}>
  252. <NSpin show={state.loading} size={'small'}>
  253. <div
  254. style={{
  255. 'min-height': `calc(var(--listContainerHeight) - var(--modal-lesson-tab-height) - ${state.searchHeight})`
  256. }}
  257. class={[
  258. styles.listSection,
  259. !state.loading && state.tableList.length <= 0
  260. ? styles.emptySection
  261. : ''
  262. ]}>
  263. {state.tableList.length > 0 && (
  264. <div class={styles.list}>
  265. {state.tableList.map((item: any) => (
  266. <div class={[styles.itemWrap, 'selresources_item_Wrap']}>
  267. <div class={styles.itemWrapBox}>
  268. <CardType
  269. isShowAdd={props.from === 'class' ? false : true}
  270. item={item}
  271. isShowCollect={true}
  272. // isShowAddDisabled={!prepareStore.getIsEditResource}
  273. onAdd={(item: any) => onAdd(item)}
  274. disabledMouseHover={false}
  275. onCollect={(item: any) => onCollect(item)}
  276. onClick={() => {
  277. if (item.type === 'IMG') return;
  278. state.show = true;
  279. state.item = item;
  280. }}
  281. />
  282. </div>
  283. </div>
  284. ))}
  285. </div>
  286. )}
  287. {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
  288. </div>
  289. </NSpin>
  290. </NScrollbar>
  291. {/* 弹窗查看 */}
  292. <CardPreview
  293. size={props.from === 'class' ? 'large' : 'default'}
  294. v-model:show={state.show}
  295. from={props.from}
  296. item={state.item}
  297. />
  298. </div>
  299. );
  300. }
  301. });