index.tsx 7.9 KB

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