123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- import {
- PropType,
- defineComponent,
- onMounted,
- reactive,
- toRefs,
- watch
- } from 'vue';
- import ResourceSearchGroup from './resource-search-group';
- import { NScrollbar, NSpin, useDialog, useMessage } from 'naive-ui';
- import styles from './index.module.less';
- import CardType from '/src/components/card-type';
- import { favorite, materialQueryPage } from '/src/views/natural-resources/api';
- import TheEmpty from '/src/components/TheEmpty';
- import { usePrepareStore } from '/src/store/modules/prepareLessons';
- import { saveCourseware } from '../../../api';
- import { useDebounceFn, useResizeObserver } from '@vueuse/core';
- import CardPreview from '/src/components/card-preview';
- import { eventGlobal } from '/src/utils';
- const formatType = (type: string) => {
- if (type === 'shareResources') {
- return 2;
- } else if (type === 'myResources') {
- return 3;
- } else if (type === 'myCollect') {
- return 4;
- }
- };
- export default defineComponent({
- name: 'share-resources',
- props: {
- type: {
- type: String as PropType<'shareResources' | 'myResources' | 'myCollect'>,
- default: 'shareResources'
- }
- },
- setup(props) {
- const prepareStore = usePrepareStore();
- const message = useMessage();
- const { type } = toRefs(props);
- const className = 'resourceSearchGroup' + +new Date();
- const state = reactive({
- searchHeight: '0px',
- loading: false,
- finshed: false, // 是否加载完
- pagination: {
- page: 1,
- rows: 20
- },
- searchGroup: {
- type:
- type.value === 'shareResources' || type.value === 'myResources'
- ? 'MUSIC'
- : '', //
- name: '',
- bookVersionId: null,
- subjectId: prepareStore.getSubjectId,
- sourceType: formatType(type.value),
- enableFlag: true
- },
- tableList: [] as any,
- show: false,
- item: {} as any
- });
- // 查询列表
- const getList = async () => {
- try {
- if (state.pagination.page === 1) {
- state.loading = true;
- }
- const { data } = await materialQueryPage({
- ...state.searchGroup,
- ...state.pagination
- // subjectId: prepareStore.getSubjectId
- });
- state.loading = false;
- const tempRows = data.rows || [];
- const temp: any = [];
- tempRows.forEach((row: any) => {
- const index = prepareStore.getCoursewareList.findIndex(
- (course: any) => course.materialId === row.id
- );
- temp.push({
- id: row.id,
- coverImg: row.coverImg,
- type: row.type,
- title: row.name,
- isCollect: !!row.favoriteFlag,
- isSelected: row.sourceFrom === 'PLATFORM' ? true : false,
- content: row.content,
- exist: index !== -1 ? true : false // 是否存在
- });
- });
- state.tableList.push(...temp);
- state.finshed = data.pages <= data.current ? true : false;
- } catch {
- state.loading = false;
- }
- };
- // const onSearch = async (item: any) => {
- // state.pagination.page = 1;
- // state.tableList = [];
- // state.searchGroup = Object.assign(state.searchGroup, item);
- // getList();
- // };
- const throttledFnSearch = useDebounceFn(item => {
- state.pagination.page = 1;
- state.tableList = [];
- state.searchGroup = Object.assign(state.searchGroup, item);
- getList();
- }, 500);
- // 添加资源
- const onAdd = async (item: any) => {
- // dialog.warning({
- // title: '提示',
- // content: `是否添加"${item.title}"资源?`,
- // positiveText: '确定',
- // negativeText: '取消',
- // onPositiveClick: async () => {
- try {
- // const temp: any = [];
- // prepareStore.getCoursewareList.forEach((item: any) => {
- // temp.push({
- // materialId: item.materialId,
- // materialName: item.title,
- // materialType: item.type,
- // id: item.id
- // });
- // });
- // // 保存课件
- // await saveCourseware({
- // coursewareDetailKnowledgeId: prepareStore.getSelectKey,
- // lessonCoursewareId: prepareStore.getLessonCoursewareId,
- // lessonCoursewareDetailId: prepareStore.getLessonCoursewareDetailId,
- // materialList: [
- // ...temp,
- // {
- // materialName: item.title,
- // materialType: item.type,
- // materialId: item.id
- // }
- // ],
- // subjectId: prepareStore.getSubjectId
- // });
- // message.success('添加成功');
- // prepareStore.setIsAddResource(true);
- eventGlobal.emit('onPrepareAddItem', {
- materialId: item.id,
- coverImg: item.coverImg,
- type: item.type,
- title: item.title,
- isCollect: item.isCollect,
- isSelected: item.isSelected,
- content: item.content,
- removeFlag: false
- });
- } catch {
- //
- }
- // }
- // });
- };
- watch(
- () => prepareStore.coursewareList,
- () => {
- state.tableList.forEach((item: any) => {
- const index = prepareStore.getCoursewareList.findIndex(
- (course: any) => course.materialId === item.id
- );
- item.exist = index !== -1 ? true : false; // 是否存在
- });
- },
- {
- deep: true,
- immediate: true
- }
- );
- // 收藏
- const onCollect = async (item: any) => {
- try {
- await favorite({
- materialId: item.id,
- favoriteFlag: item.isCollect ? 0 : 1,
- type: item.type
- });
- item.isCollect = !item.isCollect;
- } catch {
- //
- }
- };
- onMounted(() => {
- getList();
- useResizeObserver(
- document.querySelector('.' + className) as HTMLElement,
- (entries: any) => {
- const entry = entries[0];
- const { height } = entry.contentRect;
- // document.documentElement.style.setProperty(
- // '--modal-lesson-search-height',
- // height + 'px'
- // );
- state.searchHeight = height + 'px';
- }
- );
- });
- return () => (
- <div>
- <div class={className}>
- <ResourceSearchGroup
- type={props.type}
- subjectId={prepareStore.getSubjectId as any}
- onSearch={(item: any) => throttledFnSearch(item)}
- />
- </div>
- <NScrollbar
- class={styles.listContainer}
- style={{
- 'max-height': `calc(85vh - var(--modal-lesson-tab-height) - ${state.searchHeight} - 12px) `
- }}
- onScroll={(e: any) => {
- const clientHeight = e.target?.clientHeight;
- const scrollTop = e.target?.scrollTop;
- const scrollHeight = e.target?.scrollHeight;
- // 是否到底,是否加载完
- if (
- clientHeight + scrollTop + 20 >= scrollHeight &&
- !state.finshed &&
- !state.loading
- ) {
- state.pagination.page = state.pagination.page + 1;
- getList();
- }
- }}>
- <NSpin show={state.loading} size={'small'}>
- <div
- style={{
- 'min-height': `calc(85vh - var(--modal-lesson-tab-height) - ${state.searchHeight} - 12px)`
- }}
- class={[
- styles.listSection,
- !state.loading && state.tableList.length <= 0
- ? styles.emptySection
- : ''
- ]}>
- {state.tableList.length > 0 && (
- <div class={styles.list}>
- {state.tableList.map((item: any) => (
- <div class={styles.itemWrap}>
- <div class={styles.itemWrapBox}>
- <CardType
- isShowAdd
- item={item}
- isShowCollect={true}
- isShowAddDisabled={!prepareStore.getIsEditResource}
- onAdd={(item: any) => onAdd(item)}
- disabledMouseHover={false}
- onCollect={(item: any) => onCollect(item)}
- onClick={() => {
- if (item.type === 'IMG') return;
- state.show = true;
- state.item = item;
- }}
- />
- </div>
- </div>
- ))}
- </div>
- )}
- {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
- </div>
- </NSpin>
- </NScrollbar>
- {/* 弹窗查看 */}
- <CardPreview v-model:show={state.show} item={state.item} />
- </div>
- );
- }
- });
|