123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- import {
- PropType,
- defineComponent,
- onMounted,
- onUnmounted,
- reactive,
- 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 { useDebounceFn, useThrottleFn, useThrottle } from '@vueuse/core';
- import { saveCourseware } from '/src/views/prepare-lessons/api';
- import CardPreview from '/src/components/card-preview';
- import { eventGlobal } from '/src/utils';
- import Draggable from 'vuedraggable';
- const formatType = (type: string) => {
- if (type === 'shareResources') {
- return 2;
- } else if (type === 'myResources') {
- return 3;
- } else if (type === 'myCollect') {
- return 4;
- } else if (type === 'relateResources') {
- return 5;
- }
- };
- export default defineComponent({
- name: 'share-resources',
- props: {
- type: {
- type: String as PropType<
- 'shareResources' | 'myResources' | 'myCollect' | 'relateResources'
- >,
- default: 'relateResources'
- }
- },
- setup(props, { expose }) {
- const prepareStore = usePrepareStore();
- const message = useMessage();
- const dialog = useDialog();
- const state = reactive({
- loading: false,
- finshed: false, // 是否加载完
- pagination: {
- page: 1,
- rows: 20
- },
- searchGroup: {
- type: 'MUSIC', //
- name: '',
- bookVersionId: null,
- musicalInstrumentId: null,
- subjectId: null,
- audioPlayTypes: [], // 场景
- sourceType: formatType(props.type),
- enableFlag: true
- },
- tableList: [] as any,
- show: false,
- item: {} as any
- });
- const getList = async () => {
- try {
- // if (!prepareStore.getSubjectId) return;
- if (state.pagination.page === 1) {
- state.loading = true;
- }
- const { data } = await materialQueryPage({
- ...state.searchGroup,
- ...state.pagination,
- lessonCoursewareKnowledgeId:
- props.type === 'relateResources' || props.type === 'shareResources'
- ? prepareStore.getSelectKey
- : null,
- relateLessonCoursewareKnowledgeMaterialIds: getIds()
- // 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,
- refFlag: row.refFlag,
- isSelected: row.sourceFrom === 'PLATFORM' ? true : false,
- containAccompaniment: row.containAccompaniment,
- content: row.content,
- instrumentIds: row.instrumentIds,
- hasNotCheck: row.hasNotCheck,
- sourceForm: 'resource-item',
- audioPlayTypeArray: row.audioPlayTypes
- ? row.audioPlayTypes.split(',')
- : []
- // exist: index !== -1 ? true : false // 是否存在
- });
- });
- if (state.pagination.page === 1) {
- state.tableList = temp;
- } else {
- 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 = state.pagination.page + 1;
- state.pagination.page = 1;
- state.tableList = [];
- state.searchGroup = Object.assign(state.searchGroup, item);
- getList();
- }, 300);
- // 声部变化时
- watch(
- () => prepareStore.getInstrumentId,
- () => {
- onSearch(state.searchGroup);
- }
- );
- const throttledFn = useThrottleFn(() => {
- state.pagination.page = state.pagination.page + 1;
- getList();
- }, 500);
- // 添加资源
- const onAdd = async (item: any) => {
- try {
- eventGlobal.emit('onPrepareAddItem', {
- materialId: item.id,
- coverImg: item.coverImg,
- refFlag: item.refFlag,
- type: item.type,
- title: item.title,
- isCollect: item.isCollect,
- isSelected: item.isSelected,
- audioPlayTypeArray: item.audioPlayTypeArray,
- instrumentIds: item.instrumentIds,
- hasNotCheck: item.hasNotCheck,
- content: item.content,
- removeFlag: false
- });
- } catch {
- //
- }
- };
- // 收藏
- const onCollect = async (item: any) => {
- try {
- await favorite({
- materialId: item.id,
- favoriteFlag: item.isCollect ? 0 : 1,
- type: item.type
- });
- item.isCollect = !item.isCollect;
- } catch {
- //
- }
- };
- const getIds = () => {
- let noRepeatIds: any = [];
- if (props.type === 'relateResources') {
- const materialIds: any = [];
- prepareStore.getCoursewareList.forEach((course: any) => {
- course.list?.forEach((item: any) => {
- materialIds.push(item.materialId);
- });
- });
- noRepeatIds = Array(...new Set(materialIds));
- }
- return noRepeatIds.join(',');
- };
- const onUpdate = () => {
- state.pagination.page = 1;
- state.tableList = [];
- getList();
- };
- onMounted(() => {
- // 加载的时候判断是否有资源数据
- onUpdate();
- if (props.type === 'relateResources') {
- eventGlobal.on('onCoursewareUpdate', onUpdate);
- }
- });
- onUnmounted(() => {
- eventGlobal.off('onCoursewareUpdate', onUpdate);
- });
- const getParams = () => {
- return state.searchGroup;
- };
- expose({
- getParams
- });
- return () => (
- <div>
- <ResourceSearchGroup
- type={props.type}
- onSearch={(item: any) => {
- state.searchGroup = Object.assign(state.searchGroup, item);
- throttledFnSearch(item);
- }}
- />
- <NScrollbar
- class={[
- styles.listContainer,
- 'listContainerResource',
- state.searchGroup.type !== 'MUSIC' ||
- ['myResources', 'myCollect', 'relateResources'].includes(props.type)
- ? [styles.listNoMusic, 'listNoMusicResource']
- : ''
- ]}
- 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
- ) {
- throttledFn();
- }
- }}>
- <NSpin show={state.loading} size={'small'}>
- <div
- class={[
- styles.listSection,
- 'listSectionResource',
- !state.loading && state.tableList.length <= 0
- ? [styles.emptySection, 'emptySectionResource']
- : ''
- ]}>
- {state.tableList.length > 0 && (
- <Draggable
- v-model:modelValue={state.tableList}
- itemKey="id"
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- // @ts-ignore
- // group="description"
- group={{ name: 'description', pull: 'clone', put: false }}
- animation={200}
- sort={false}
- onMove={(evt: any) => {
- // console.log(evt, 'evt', evt.from !== evt.to);
- return evt.from !== evt.to;
- }}
- componentData={{
- draggable: 'row-nav',
- itemKey: 'id',
- tag: 'div',
- pull: 'clone',
- put: false,
- animation: 200,
- group: 'description'
- }}
- class={styles.list}>
- {{
- item: (element: any) => {
- const item = element.element;
- return (
- <div
- data-id={item.id}
- class={['itemWrap', 'itemBlock', 'row-nav']}>
- <div class={'itemWrapBox'}>
- <CardType
- isShowAdd
- item={item}
- isShowCollect={true}
- draggable={false}
- disabledMouseHover={false}
- onClick={() => {
- if (item.type === 'IMG') return;
- state.show = true;
- state.item = {
- instrumentId:
- state.searchGroup.musicalInstrumentId ||
- null,
- ...item
- };
- }}
- onCollect={(item: any) => onCollect(item)}
- // isShowAddDisabled={!prepareStore.getIsEditResource}
- onAdd={(item: any) => onAdd(item)}
- />
- </div>
- </div>
- // <div
- // data-id={item.id}
- // class={[
- // styles.itemWrap,
- // styles.itemBlock,
- // 'row-nav'
- // ]}>
- // <div class={styles.itemWrapBox}>
- // <CardType
- // class={[styles.itemContent]}
- // isShowCollect={false}
- // offShelf={item.removeFlag ? true : false}
- // // onOffShelf={() => onRemove(item)}
- // item={item}
- // disabledMouseHover={false}
- // onClick={() => {
- // if (item.type === 'IMG') return;
- // forms.show = true;
- // forms.item = item;
- // }}
- // />
- // <div class={styles.itemOperation}>
- // <img
- // src={iconDelete}
- // class={styles.iconDelete}
- // onClick={(e: MouseEvent) => {
- // e.stopPropagation();
- // onDelete(element.index, index);
- // }}
- // />
- // </div>
- // </div>
- // </div>
- );
- }
- }}
- </Draggable>
- // <div class={styles.list}>
- // {state.tableList.map((item: any) => (
- // <CardType
- // isShowAdd
- // item={item}
- // isShowCollect={true}
- // draggable
- // disabledMouseHover={false}
- // onClick={() => {
- // if (item.type === 'IMG') return;
- // state.show = true;
- // state.item = item;
- // }}
- // onCollect={(item: any) => onCollect(item)}
- // // isShowAddDisabled={!prepareStore.getIsEditResource}
- // onAdd={(item: any) => onAdd(item)}
- // />
- // ))}
- // </div>
- )}
- {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
- </div>
- </NSpin>
- </NScrollbar>
- {/* 弹窗查看 */}
- <CardPreview
- v-model:show={state.show}
- item={state.item}
- isDownload={false}
- />
- </div>
- );
- }
- });
|