index.tsx 8.2 KB

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