index.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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, useDialog, useMessage } from 'naive-ui';
  11. import styles from './index.module.less';
  12. import CardType from '/src/components/card-type';
  13. import { materialQueryPage } from '/src/views/natural-resources/api';
  14. import TheEmpty from '/src/components/TheEmpty';
  15. import { usePrepareStore } from '/src/store/modules/prepareLessons';
  16. import { saveCourseware } from '../../../api';
  17. import { useDebounceFn } from '@vueuse/core';
  18. import CardPreview from '/src/components/card-preview';
  19. import { eventGlobal } from '/src/utils';
  20. const formatType = (type: string) => {
  21. if (type === 'shareResources') {
  22. return 2;
  23. } else if (type === 'myResources') {
  24. return 3;
  25. } else if (type === 'myCollect') {
  26. return 4;
  27. }
  28. };
  29. export default defineComponent({
  30. name: 'share-resources',
  31. props: {
  32. type: {
  33. type: String as PropType<'shareResources' | 'myResources' | 'myCollect'>,
  34. default: 'shareResources'
  35. }
  36. },
  37. setup(props) {
  38. const prepareStore = usePrepareStore();
  39. const message = useMessage();
  40. const { type } = toRefs(props);
  41. const state = reactive({
  42. loading: false,
  43. finshed: false, // 是否加载完
  44. pagination: {
  45. page: 1,
  46. rows: 20
  47. },
  48. searchGroup: {
  49. type: type.value === 'shareResources' ? 'MUSIC' : '', //
  50. name: '',
  51. bookVersionId: null,
  52. subjectId: null,
  53. sourceType: formatType(type.value),
  54. enableFlag: true
  55. },
  56. tableList: [] as any,
  57. show: false,
  58. item: {} as any
  59. });
  60. // 查询列表
  61. const getList = async () => {
  62. try {
  63. if (state.pagination.page === 1) {
  64. state.loading = true;
  65. }
  66. const { data } = await materialQueryPage({
  67. ...state.searchGroup,
  68. ...state.pagination
  69. // subjectId: prepareStore.getSubjectId
  70. });
  71. state.loading = false;
  72. const tempRows = data.rows || [];
  73. const temp: any = [];
  74. tempRows.forEach((row: any) => {
  75. const index = prepareStore.getCoursewareList.findIndex(
  76. (course: any) => course.materialId === row.id
  77. );
  78. temp.push({
  79. id: row.id,
  80. coverImg: row.coverImg,
  81. type: row.type,
  82. title: row.name,
  83. isCollect: !!row.favoriteFlag,
  84. isSelected: row.sourceFrom === 'PLATFORM' ? true : false,
  85. content: row.content,
  86. exist: index !== -1 ? true : false // 是否存在
  87. });
  88. });
  89. state.tableList.push(...temp);
  90. state.finshed = data.pages <= data.current ? true : false;
  91. } catch {
  92. state.loading = false;
  93. }
  94. };
  95. // const onSearch = async (item: any) => {
  96. // state.pagination.page = 1;
  97. // state.tableList = [];
  98. // state.searchGroup = Object.assign(state.searchGroup, item);
  99. // getList();
  100. // };
  101. const throttledFnSearch = useDebounceFn(item => {
  102. state.pagination.page = 1;
  103. state.tableList = [];
  104. state.searchGroup = Object.assign(state.searchGroup, item);
  105. getList();
  106. }, 500);
  107. // 添加资源
  108. const onAdd = async (item: any) => {
  109. // dialog.warning({
  110. // title: '提示',
  111. // content: `是否添加"${item.title}"资源?`,
  112. // positiveText: '确定',
  113. // negativeText: '取消',
  114. // onPositiveClick: async () => {
  115. try {
  116. // const temp: any = [];
  117. // prepareStore.getCoursewareList.forEach((item: any) => {
  118. // temp.push({
  119. // materialId: item.materialId,
  120. // materialName: item.title,
  121. // materialType: item.type,
  122. // id: item.id
  123. // });
  124. // });
  125. // // 保存课件
  126. // await saveCourseware({
  127. // coursewareDetailKnowledgeId: prepareStore.getSelectKey,
  128. // lessonCoursewareId: prepareStore.getLessonCoursewareId,
  129. // lessonCoursewareDetailId: prepareStore.getLessonCoursewareDetailId,
  130. // materialList: [
  131. // ...temp,
  132. // {
  133. // materialName: item.title,
  134. // materialType: item.type,
  135. // materialId: item.id
  136. // }
  137. // ],
  138. // subjectId: prepareStore.getSubjectId
  139. // });
  140. // message.success('添加成功');
  141. // prepareStore.setIsAddResource(true);
  142. eventGlobal.emit('onPrepareAddItem', {
  143. materialId: item.id,
  144. coverImg: item.coverImg,
  145. type: item.type,
  146. title: item.title,
  147. isCollect: item.isCollect,
  148. isSelected: item.isSelected,
  149. content: item.content,
  150. removeFlag: false
  151. });
  152. } catch {
  153. //
  154. }
  155. // }
  156. // });
  157. };
  158. watch(
  159. () => prepareStore.coursewareList,
  160. () => {
  161. state.tableList.forEach((item: any) => {
  162. const index = prepareStore.getCoursewareList.findIndex(
  163. (course: any) => course.materialId === item.id
  164. );
  165. item.exist = index !== -1 ? true : false; // 是否存在
  166. });
  167. },
  168. {
  169. deep: true,
  170. immediate: true
  171. }
  172. );
  173. onMounted(() => {
  174. getList();
  175. });
  176. return () => (
  177. <div>
  178. <ResourceSearchGroup
  179. type={props.type}
  180. subjectId={prepareStore.getSubjectId}
  181. onSearch={(item: any) => throttledFnSearch(item)}
  182. />
  183. <NScrollbar
  184. class={styles.listContainer}
  185. onScroll={(e: any) => {
  186. const clientHeight = e.target?.clientHeight;
  187. const scrollTop = e.target?.scrollTop;
  188. const scrollHeight = e.target?.scrollHeight;
  189. // 是否到底,是否加载完
  190. if (
  191. clientHeight + scrollTop + 20 >= scrollHeight &&
  192. !state.finshed &&
  193. !state.loading
  194. ) {
  195. state.pagination.page = state.pagination.page + 1;
  196. getList();
  197. }
  198. }}>
  199. <NSpin show={state.loading} size={'small'}>
  200. <div
  201. class={[
  202. styles.listSection,
  203. !state.loading && state.tableList.length <= 0
  204. ? styles.emptySection
  205. : ''
  206. ]}>
  207. {state.tableList.length > 0 && (
  208. <div class={styles.list}>
  209. {state.tableList.map((item: any) => (
  210. <CardType
  211. isShowAdd
  212. item={item}
  213. isShowCollect={false}
  214. isShowAddDisabled={!prepareStore.getIsEditResource}
  215. onAdd={(item: any) => onAdd(item)}
  216. disabledMouseHover={false}
  217. onClick={() => {
  218. if (item.type === 'IMG') return;
  219. state.show = true;
  220. state.item = item;
  221. }}
  222. />
  223. ))}
  224. </div>
  225. )}
  226. {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
  227. </div>
  228. </NSpin>
  229. </NScrollbar>
  230. {/* 弹窗查看 */}
  231. <CardPreview v-model:show={state.show} item={state.item} />
  232. </div>
  233. );
  234. }
  235. });