index.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. const { subjectId, ...res } = item;
  106. state.searchGroup = Object.assign(state.searchGroup, {
  107. ...res,
  108. musicalInstrumentId: subjectId
  109. });
  110. getList();
  111. }, 500);
  112. // 添加资源
  113. const onAdd = async (item: any) => {
  114. try {
  115. eventGlobal.emit('onPrepareAddItem', {
  116. materialId: item.id,
  117. coverImg: item.coverImg,
  118. type: item.type,
  119. title: item.title,
  120. isCollect: item.isCollect,
  121. isSelected: item.isSelected,
  122. content: item.content,
  123. removeFlag: false
  124. });
  125. } catch {
  126. //
  127. }
  128. };
  129. watch(
  130. () => prepareStore.coursewareList,
  131. () => {
  132. state.tableList.forEach((item: any) => {
  133. const index = prepareStore.getCoursewareList.findIndex(
  134. (course: any) => course.materialId === item.id
  135. );
  136. item.exist = index !== -1 ? true : false; // 是否存在
  137. });
  138. },
  139. {
  140. deep: true,
  141. immediate: true
  142. }
  143. );
  144. // 收藏
  145. const onCollect = async (item: any) => {
  146. try {
  147. await favorite({
  148. materialId: item.id,
  149. favoriteFlag: item.isCollect ? 0 : 1,
  150. type: item.type
  151. });
  152. item.isCollect = !item.isCollect;
  153. } catch {
  154. //
  155. }
  156. };
  157. onMounted(() => {
  158. getList();
  159. useResizeObserver(
  160. document.querySelector('.' + className) as HTMLElement,
  161. (entries: any) => {
  162. const entry = entries[0];
  163. const { height } = entry.contentRect;
  164. state.searchHeight = height + 'px';
  165. }
  166. );
  167. });
  168. return () => (
  169. <div>
  170. <div class={className}>
  171. {props.from === 'class' ? (
  172. <ClassSearchGroup
  173. type={props.type}
  174. subjectId={prepareStore.getSubjectId as any}
  175. onSearch={(item: any) => throttledFnSearch(item)}
  176. />
  177. ) : (
  178. <ResourceSearchGroup
  179. type={props.type}
  180. subjectId={prepareStore.getSubjectId as any}
  181. onSearch={(item: any) => throttledFnSearch(item)}
  182. />
  183. )}
  184. </div>
  185. <NScrollbar
  186. class={styles.listContainer}
  187. style={{
  188. 'max-height': `calc(85vh - var(--modal-lesson-tab-height) - ${state.searchHeight} - 12px) `
  189. }}
  190. onScroll={(e: any) => {
  191. const clientHeight = e.target?.clientHeight;
  192. const scrollTop = e.target?.scrollTop;
  193. const scrollHeight = e.target?.scrollHeight;
  194. // 是否到底,是否加载完
  195. if (
  196. clientHeight + scrollTop + 20 >= scrollHeight &&
  197. !state.finshed &&
  198. !state.loading
  199. ) {
  200. state.pagination.page = state.pagination.page + 1;
  201. getList();
  202. }
  203. }}>
  204. <NSpin show={state.loading} size={'small'}>
  205. <div
  206. style={{
  207. 'min-height': `calc(85vh - var(--modal-lesson-tab-height) - ${state.searchHeight} - 12px)`
  208. }}
  209. class={[
  210. styles.listSection,
  211. !state.loading && state.tableList.length <= 0
  212. ? styles.emptySection
  213. : ''
  214. ]}>
  215. {state.tableList.length > 0 && (
  216. <div class={styles.list}>
  217. {state.tableList.map((item: any) => (
  218. <div class={styles.itemWrap}>
  219. <div class={styles.itemWrapBox}>
  220. <CardType
  221. isShowAdd
  222. item={item}
  223. isShowCollect={true}
  224. isShowAddDisabled={!prepareStore.getIsEditResource}
  225. onAdd={(item: any) => onAdd(item)}
  226. disabledMouseHover={false}
  227. onCollect={(item: any) => onCollect(item)}
  228. onClick={() => {
  229. if (item.type === 'IMG') return;
  230. state.show = true;
  231. state.item = item;
  232. }}
  233. />
  234. </div>
  235. </div>
  236. ))}
  237. </div>
  238. )}
  239. {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
  240. </div>
  241. </NSpin>
  242. </NScrollbar>
  243. {/* 弹窗查看 */}
  244. <CardPreview
  245. size={props.from === 'class' ? 'large' : 'default'}
  246. v-model:show={state.show}
  247. item={state.item}
  248. />
  249. </div>
  250. );
  251. }
  252. });