index.tsx 6.8 KB

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