index.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 } 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 { useDebounceFn, useResizeObserver } from '@vueuse/core';
  17. import CardPreview from '/src/components/card-preview';
  18. import { eventGlobal } from '/src/utils';
  19. import ClassSearchGroup from './class-search-group';
  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. from: {
  38. type: String,
  39. default: ''
  40. }
  41. },
  42. setup(props) {
  43. const prepareStore = usePrepareStore();
  44. const { type } = toRefs(props);
  45. const className = 'resourceSearchGroup' + +new Date();
  46. const state = reactive({
  47. searchHeight: '0px',
  48. loading: false,
  49. finshed: false, // 是否加载完
  50. pagination: {
  51. page: 1,
  52. rows: 20
  53. },
  54. searchGroup: {
  55. type: 'MUSIC', //
  56. name: '',
  57. bookVersionId: null,
  58. subjectId: prepareStore.getSubjectId,
  59. sourceType: formatType(type.value),
  60. enableFlag: true
  61. },
  62. tableList: [] as any,
  63. show: false,
  64. item: {} as any
  65. });
  66. // 查询列表
  67. const getList = async () => {
  68. try {
  69. if (state.pagination.page === 1) {
  70. state.loading = true;
  71. }
  72. const { data } = await materialQueryPage({
  73. ...state.searchGroup,
  74. ...state.pagination
  75. // subjectId: prepareStore.getSubjectId
  76. });
  77. state.loading = false;
  78. const tempRows = data.rows || [];
  79. const temp: any = [];
  80. tempRows.forEach((row: any) => {
  81. const index = prepareStore.getCoursewareList.findIndex(
  82. (course: any) => course.materialId === row.id
  83. );
  84. temp.push({
  85. id: row.id,
  86. coverImg: row.coverImg,
  87. type: row.type,
  88. title: row.name,
  89. isCollect: !!row.favoriteFlag,
  90. isSelected: row.sourceFrom === 'PLATFORM' ? true : false,
  91. content: row.content,
  92. exist: index !== -1 ? true : false // 是否存在
  93. });
  94. });
  95. state.tableList.push(...temp);
  96. state.finshed = data.pages <= data.current ? true : false;
  97. } catch {
  98. state.loading = false;
  99. }
  100. };
  101. const throttledFnSearch = useDebounceFn(item => {
  102. state.pagination.page = 1;
  103. state.tableList = [];
  104. state.searchGroup = Object.assign(state.searchGroup, item);
  105. getList();
  106. }, 500);
  107. // 添加资源
  108. const onAdd = async (item: any) => {
  109. try {
  110. eventGlobal.emit('onPrepareAddItem', {
  111. materialId: item.id,
  112. coverImg: item.coverImg,
  113. type: item.type,
  114. title: item.title,
  115. isCollect: item.isCollect,
  116. isSelected: item.isSelected,
  117. content: item.content,
  118. removeFlag: false
  119. });
  120. } catch {
  121. //
  122. }
  123. };
  124. watch(
  125. () => prepareStore.coursewareList,
  126. () => {
  127. state.tableList.forEach((item: any) => {
  128. const index = prepareStore.getCoursewareList.findIndex(
  129. (course: any) => course.materialId === item.id
  130. );
  131. item.exist = index !== -1 ? true : false; // 是否存在
  132. });
  133. },
  134. {
  135. deep: true,
  136. immediate: true
  137. }
  138. );
  139. // 收藏
  140. const onCollect = async (item: any) => {
  141. try {
  142. await favorite({
  143. materialId: item.id,
  144. favoriteFlag: item.isCollect ? 0 : 1,
  145. type: item.type
  146. });
  147. item.isCollect = !item.isCollect;
  148. } catch {
  149. //
  150. }
  151. };
  152. onMounted(() => {
  153. getList();
  154. useResizeObserver(
  155. document.querySelector('.' + className) as HTMLElement,
  156. (entries: any) => {
  157. const entry = entries[0];
  158. const { height } = entry.contentRect;
  159. state.searchHeight = height + 'px';
  160. }
  161. );
  162. });
  163. return () => (
  164. <div>
  165. <div class={className}>
  166. {props.from === 'class' ? (
  167. <ClassSearchGroup
  168. type={props.type}
  169. subjectId={prepareStore.getSubjectId as any}
  170. onSearch={(item: any) => throttledFnSearch(item)}
  171. />
  172. ) : (
  173. <ResourceSearchGroup
  174. type={props.type}
  175. subjectId={prepareStore.getSubjectId as any}
  176. onSearch={(item: any) => throttledFnSearch(item)}
  177. />
  178. )}
  179. </div>
  180. <NScrollbar
  181. class={styles.listContainer}
  182. style={{
  183. 'max-height': `calc(85vh - var(--modal-lesson-tab-height) - ${state.searchHeight} - 12px) `
  184. }}
  185. onScroll={(e: any) => {
  186. const clientHeight = e.target?.clientHeight;
  187. const scrollTop = e.target?.scrollTop;
  188. const scrollHeight = e.target?.scrollHeight;
  189. // 是否到底,是否加载完
  190. if (
  191. clientHeight + scrollTop + 20 >= scrollHeight &&
  192. !state.finshed &&
  193. !state.loading
  194. ) {
  195. state.pagination.page = state.pagination.page + 1;
  196. getList();
  197. }
  198. }}>
  199. <NSpin show={state.loading} size={'small'}>
  200. <div
  201. style={{
  202. 'min-height': `calc(85vh - var(--modal-lesson-tab-height) - ${state.searchHeight} - 12px)`
  203. }}
  204. class={[
  205. styles.listSection,
  206. !state.loading && state.tableList.length <= 0
  207. ? styles.emptySection
  208. : ''
  209. ]}>
  210. {state.tableList.length > 0 && (
  211. <div class={styles.list}>
  212. {state.tableList.map((item: any) => (
  213. <div class={styles.itemWrap}>
  214. <div class={styles.itemWrapBox}>
  215. <CardType
  216. isShowAdd
  217. item={item}
  218. isShowCollect={true}
  219. isShowAddDisabled={!prepareStore.getIsEditResource}
  220. onAdd={(item: any) => onAdd(item)}
  221. disabledMouseHover={false}
  222. onCollect={(item: any) => onCollect(item)}
  223. onClick={() => {
  224. if (item.type === 'IMG') return;
  225. state.show = true;
  226. state.item = item;
  227. }}
  228. />
  229. </div>
  230. </div>
  231. ))}
  232. </div>
  233. )}
  234. {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
  235. </div>
  236. </NSpin>
  237. </NScrollbar>
  238. {/* 弹窗查看 */}
  239. <CardPreview
  240. size={props.from === 'class' ? 'large' : 'default'}
  241. v-model:show={state.show}
  242. item={state.item}
  243. />
  244. </div>
  245. );
  246. }
  247. });