index.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. content: row.content
  103. });
  104. });
  105. state.tableList.push(...temp);
  106. state.finshed = data.pages <= data.current ? true : false;
  107. } catch {
  108. state.loading = false;
  109. }
  110. };
  111. const throttledFnSearch = useDebounceFn(item => {
  112. state.pagination.page = 1;
  113. state.tableList = [];
  114. const { subjectId, ...res } = item;
  115. state.searchGroup = Object.assign(state.searchGroup, {
  116. ...res,
  117. musicalInstrumentId: subjectId,
  118. subjectId: null
  119. });
  120. getList();
  121. }, 500);
  122. // 添加资源
  123. const onAdd = async (item: any) => {
  124. try {
  125. eventGlobal.emit('onPrepareAddItem', {
  126. materialId: item.id,
  127. coverImg: item.coverImg,
  128. type: item.type,
  129. title: item.title,
  130. refFlag: item.refFlag,
  131. isCollect: item.isCollect,
  132. isSelected: item.isSelected,
  133. content: item.content,
  134. removeFlag: false
  135. });
  136. } catch {
  137. //
  138. }
  139. };
  140. // 收藏
  141. const onCollect = async (item: any) => {
  142. try {
  143. await favorite({
  144. materialId: item.id,
  145. favoriteFlag: item.isCollect ? 0 : 1,
  146. type: item.type
  147. });
  148. item.isCollect = !item.isCollect;
  149. } catch {
  150. //
  151. }
  152. };
  153. const getIds = () => {
  154. let noRepeatIds: any = [];
  155. if (props.type === 'relateResources') {
  156. const materialIds: any = [];
  157. prepareStore.getCoursewareList.forEach((course: any) => {
  158. course.list?.forEach((item: any) => {
  159. materialIds.push(item.materialId);
  160. });
  161. });
  162. noRepeatIds = Array(...new Set(materialIds));
  163. }
  164. return noRepeatIds.join(',');
  165. };
  166. const onUpdate = () => {
  167. state.pagination.page = 1;
  168. state.tableList = [];
  169. getList();
  170. };
  171. onMounted(async () => {
  172. // 加载的时候判断是否有资源数据
  173. // 获取声部
  174. await catchStore.getSubjects();
  175. useResizeObserver(
  176. document.querySelector('.' + className) as HTMLElement,
  177. (entries: any) => {
  178. const entry = entries[0];
  179. const { height } = entry.contentRect;
  180. state.searchHeight = height + 'px';
  181. }
  182. );
  183. getList();
  184. eventGlobal.on('onCoursewareUpdate', onUpdate);
  185. });
  186. onUnmounted(() => {
  187. eventGlobal.off('onCoursewareUpdate', onUpdate);
  188. });
  189. // onMounted(async () => {
  190. // // 获取声部
  191. // await catchStore.getSubjects();
  192. // // 加载的时候判断是否有资源数据
  193. // let noRepeatIds: any = [];
  194. // if (props.type === 'relateResources') {
  195. // const materialIds: any = [];
  196. // prepareStore.getCoursewareList.forEach((course: any) => {
  197. // course.list?.forEach((item: any) => {
  198. // materialIds.push(item.materialId);
  199. // });
  200. // });
  201. // noRepeatIds = Array(...new Set(materialIds));
  202. // }
  203. // getList(noRepeatIds.join(','));
  204. // useResizeObserver(
  205. // document.querySelector('.' + className) as HTMLElement,
  206. // (entries: any) => {
  207. // const entry = entries[0];
  208. // const { height } = entry.contentRect;
  209. // state.searchHeight = height + 'px';
  210. // }
  211. // );
  212. // });
  213. return () => (
  214. <div>
  215. <div class={className}>
  216. {props.from === 'class' ? (
  217. <ClassSearchGroup
  218. type={props.type}
  219. subjectId={prepareStore.getSubjectId as any}
  220. onSearch={(item: any) => throttledFnSearch(item)}
  221. />
  222. ) : (
  223. <ResourceSearchGroup
  224. type={props.type}
  225. // subjectId={prepareStore.getSubjectId as any}
  226. onSearch={(item: any) => throttledFnSearch(item)}
  227. />
  228. )}
  229. </div>
  230. <NScrollbar
  231. class={[styles.listContainer, 'list_container']}
  232. style={{
  233. 'max-height': `calc(var(--listContainerHeight) - var(--modal-lesson-tab-height) - ${state.searchHeight}) `
  234. }}
  235. onScroll={(e: any) => {
  236. const clientHeight = e.target?.clientHeight;
  237. const scrollTop = e.target?.scrollTop;
  238. const scrollHeight = e.target?.scrollHeight;
  239. // 是否到底,是否加载完
  240. if (
  241. clientHeight + scrollTop + 20 >= scrollHeight &&
  242. !state.finshed &&
  243. !state.loading
  244. ) {
  245. state.pagination.page = state.pagination.page + 1;
  246. getList();
  247. }
  248. }}>
  249. <NSpin show={state.loading} size={'small'}>
  250. <div
  251. style={{
  252. 'min-height': `calc(var(--listContainerHeight) - var(--modal-lesson-tab-height) - ${state.searchHeight})`
  253. }}
  254. class={[
  255. styles.listSection,
  256. !state.loading && state.tableList.length <= 0
  257. ? styles.emptySection
  258. : ''
  259. ]}>
  260. {state.tableList.length > 0 && (
  261. <div class={styles.list}>
  262. {state.tableList.map((item: any) => (
  263. <div class={[styles.itemWrap, 'selresources_item_Wrap']}>
  264. <div class={styles.itemWrapBox}>
  265. <CardType
  266. isShowAdd={props.from === 'class' ? false : true}
  267. item={item}
  268. isShowCollect={true}
  269. // isShowAddDisabled={!prepareStore.getIsEditResource}
  270. onAdd={(item: any) => onAdd(item)}
  271. disabledMouseHover={false}
  272. onCollect={(item: any) => onCollect(item)}
  273. onClick={() => {
  274. if (item.type === 'IMG') return;
  275. state.show = true;
  276. state.item = item;
  277. }}
  278. />
  279. </div>
  280. </div>
  281. ))}
  282. </div>
  283. )}
  284. {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
  285. </div>
  286. </NSpin>
  287. </NScrollbar>
  288. {/* 弹窗查看 */}
  289. <CardPreview
  290. size={props.from === 'class' ? 'large' : 'default'}
  291. v-model:show={state.show}
  292. from={props.from}
  293. item={state.item}
  294. />
  295. </div>
  296. );
  297. }
  298. });