index.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import {
  2. PropType,
  3. defineComponent,
  4. onMounted,
  5. reactive,
  6. toRefs,
  7. watch
  8. } from 'vue';
  9. import ResourceSearchGroup from './resource-search-group';
  10. import { NScrollbar, NSpin, useDialog, useMessage } from 'naive-ui';
  11. import styles from './index.module.less';
  12. import CardType from '/src/components/card-type';
  13. import { favorite, materialQueryPage } from '/src/views/natural-resources/api';
  14. import TheEmpty from '/src/components/TheEmpty';
  15. import { usePrepareStore } from '/src/store/modules/prepareLessons';
  16. import { saveCourseware } from '../../../api';
  17. import { useDebounceFn, useResizeObserver } from '@vueuse/core';
  18. import CardPreview from '/src/components/card-preview';
  19. import { eventGlobal } from '/src/utils';
  20. const formatType = (type: string) => {
  21. if (type === 'shareResources') {
  22. return 2;
  23. } else if (type === 'myResources') {
  24. return 3;
  25. } else if (type === 'myCollect') {
  26. return 4;
  27. }
  28. };
  29. export default defineComponent({
  30. name: 'share-resources',
  31. props: {
  32. type: {
  33. type: String as PropType<'shareResources' | 'myResources' | 'myCollect'>,
  34. default: 'shareResources'
  35. }
  36. },
  37. setup(props) {
  38. const prepareStore = usePrepareStore();
  39. const message = useMessage();
  40. const { type } = toRefs(props);
  41. const className = 'resourceSearchGroup' + +new Date();
  42. const state = reactive({
  43. searchHeight: '0px',
  44. loading: false,
  45. finshed: false, // 是否加载完
  46. pagination: {
  47. page: 1,
  48. rows: 20
  49. },
  50. searchGroup: {
  51. type:
  52. type.value === 'shareResources' || type.value === 'myResources'
  53. ? 'MUSIC'
  54. : '', //
  55. name: '',
  56. bookVersionId: null,
  57. subjectId: prepareStore.getSubjectId,
  58. sourceType: formatType(type.value),
  59. enableFlag: true
  60. },
  61. tableList: [] as any,
  62. show: false,
  63. item: {} as any
  64. });
  65. // 查询列表
  66. const getList = async () => {
  67. try {
  68. if (state.pagination.page === 1) {
  69. state.loading = true;
  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.getCoursewareList.findIndex(
  81. (course: any) => course.materialId === row.id
  82. );
  83. temp.push({
  84. id: row.id,
  85. coverImg: row.coverImg,
  86. type: row.type,
  87. title: row.name,
  88. isCollect: !!row.favoriteFlag,
  89. isSelected: row.sourceFrom === 'PLATFORM' ? true : false,
  90. content: row.content,
  91. exist: index !== -1 ? true : false // 是否存在
  92. });
  93. });
  94. state.tableList.push(...temp);
  95. state.finshed = data.pages <= data.current ? true : false;
  96. } catch {
  97. state.loading = false;
  98. }
  99. };
  100. // const onSearch = async (item: any) => {
  101. // state.pagination.page = 1;
  102. // state.tableList = [];
  103. // state.searchGroup = Object.assign(state.searchGroup, item);
  104. // getList();
  105. // };
  106. const throttledFnSearch = useDebounceFn(item => {
  107. state.pagination.page = 1;
  108. state.tableList = [];
  109. state.searchGroup = Object.assign(state.searchGroup, item);
  110. getList();
  111. }, 500);
  112. // 添加资源
  113. const onAdd = async (item: any) => {
  114. // dialog.warning({
  115. // title: '提示',
  116. // content: `是否添加"${item.title}"资源?`,
  117. // positiveText: '确定',
  118. // negativeText: '取消',
  119. // onPositiveClick: async () => {
  120. try {
  121. // const temp: any = [];
  122. // prepareStore.getCoursewareList.forEach((item: any) => {
  123. // temp.push({
  124. // materialId: item.materialId,
  125. // materialName: item.title,
  126. // materialType: item.type,
  127. // id: item.id
  128. // });
  129. // });
  130. // // 保存课件
  131. // await saveCourseware({
  132. // coursewareDetailKnowledgeId: prepareStore.getSelectKey,
  133. // lessonCoursewareId: prepareStore.getLessonCoursewareId,
  134. // lessonCoursewareDetailId: prepareStore.getLessonCoursewareDetailId,
  135. // materialList: [
  136. // ...temp,
  137. // {
  138. // materialName: item.title,
  139. // materialType: item.type,
  140. // materialId: item.id
  141. // }
  142. // ],
  143. // subjectId: prepareStore.getSubjectId
  144. // });
  145. // message.success('添加成功');
  146. // prepareStore.setIsAddResource(true);
  147. eventGlobal.emit('onPrepareAddItem', {
  148. materialId: item.id,
  149. coverImg: item.coverImg,
  150. type: item.type,
  151. title: item.title,
  152. isCollect: item.isCollect,
  153. isSelected: item.isSelected,
  154. content: item.content,
  155. removeFlag: false
  156. });
  157. } catch {
  158. //
  159. }
  160. // }
  161. // });
  162. };
  163. watch(
  164. () => prepareStore.coursewareList,
  165. () => {
  166. state.tableList.forEach((item: any) => {
  167. const index = prepareStore.getCoursewareList.findIndex(
  168. (course: any) => course.materialId === item.id
  169. );
  170. item.exist = index !== -1 ? true : false; // 是否存在
  171. });
  172. },
  173. {
  174. deep: true,
  175. immediate: true
  176. }
  177. );
  178. // 收藏
  179. const onCollect = async (item: any) => {
  180. try {
  181. await favorite({
  182. materialId: item.id,
  183. favoriteFlag: item.isCollect ? 0 : 1,
  184. type: item.type
  185. });
  186. item.isCollect = !item.isCollect;
  187. } catch {
  188. //
  189. }
  190. };
  191. onMounted(() => {
  192. getList();
  193. useResizeObserver(
  194. document.querySelector('.' + className) as HTMLElement,
  195. (entries: any) => {
  196. const entry = entries[0];
  197. const { height } = entry.contentRect;
  198. // document.documentElement.style.setProperty(
  199. // '--modal-lesson-search-height',
  200. // height + 'px'
  201. // );
  202. state.searchHeight = height + 'px';
  203. }
  204. );
  205. });
  206. return () => (
  207. <div>
  208. <div class={className}>
  209. <ResourceSearchGroup
  210. type={props.type}
  211. subjectId={prepareStore.getSubjectId as any}
  212. onSearch={(item: any) => throttledFnSearch(item)}
  213. />
  214. </div>
  215. <NScrollbar
  216. class={styles.listContainer}
  217. style={{
  218. 'max-height': `calc(85vh - var(--modal-lesson-tab-height) - ${state.searchHeight} - 12px) `
  219. }}
  220. onScroll={(e: any) => {
  221. const clientHeight = e.target?.clientHeight;
  222. const scrollTop = e.target?.scrollTop;
  223. const scrollHeight = e.target?.scrollHeight;
  224. // 是否到底,是否加载完
  225. if (
  226. clientHeight + scrollTop + 20 >= scrollHeight &&
  227. !state.finshed &&
  228. !state.loading
  229. ) {
  230. state.pagination.page = state.pagination.page + 1;
  231. getList();
  232. }
  233. }}>
  234. <NSpin show={state.loading} size={'small'}>
  235. <div
  236. style={{
  237. 'min-height': `calc(85vh - var(--modal-lesson-tab-height) - ${state.searchHeight} - 12px)`
  238. }}
  239. class={[
  240. styles.listSection,
  241. !state.loading && state.tableList.length <= 0
  242. ? styles.emptySection
  243. : ''
  244. ]}>
  245. {state.tableList.length > 0 && (
  246. <div class={styles.list}>
  247. {state.tableList.map((item: any) => (
  248. <div class={styles.itemWrap}>
  249. <div class={styles.itemWrapBox}>
  250. <CardType
  251. isShowAdd
  252. item={item}
  253. isShowCollect={true}
  254. isShowAddDisabled={!prepareStore.getIsEditResource}
  255. onAdd={(item: any) => onAdd(item)}
  256. disabledMouseHover={false}
  257. onCollect={(item: any) => onCollect(item)}
  258. onClick={() => {
  259. if (item.type === 'IMG') return;
  260. state.show = true;
  261. state.item = item;
  262. }}
  263. />
  264. </div>
  265. </div>
  266. ))}
  267. </div>
  268. )}
  269. {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
  270. </div>
  271. </NSpin>
  272. </NScrollbar>
  273. {/* 弹窗查看 */}
  274. <CardPreview v-model:show={state.show} item={state.item} />
  275. </div>
  276. );
  277. }
  278. });