index.tsx 8.3 KB

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