index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { PropType, defineComponent, onMounted, reactive } from 'vue';
  2. import ResourceSearchGroup from './resource-search-group';
  3. import { NScrollbar, NSpin, useDialog, useMessage } from 'naive-ui';
  4. import styles from './index.module.less';
  5. import CardType from '/src/components/card-type';
  6. import { materialQueryPage } from '/src/views/natural-resources/api';
  7. import TheEmpty from '/src/components/TheEmpty';
  8. import { usePrepareStore } from '/src/store/modules/prepareLessons';
  9. import { saveCourseware } from '../../../api';
  10. const formatType = (type: string) => {
  11. if (type === 'shareResources') {
  12. return 2;
  13. } else if (type === 'myResources') {
  14. return 3;
  15. } else if (type === 'myCollect') {
  16. return 4;
  17. }
  18. };
  19. export default defineComponent({
  20. name: 'share-resources',
  21. props: {
  22. type: {
  23. type: String as PropType<'shareResources' | 'myResources' | 'myCollect'>,
  24. default: 'shareResources'
  25. }
  26. },
  27. setup(props) {
  28. const prepareStore = usePrepareStore();
  29. const message = useMessage();
  30. const dialog = useDialog();
  31. const state = reactive({
  32. loading: false,
  33. finshed: false, // 是否加载完
  34. pagination: {
  35. page: 1,
  36. rows: 20
  37. },
  38. searchGroup: {
  39. type: props.type === 'shareResources' ? 'MUSIC' : '', //
  40. keyword: '',
  41. bookVersionId: null,
  42. subjectId: null,
  43. sourceType: formatType(props.type),
  44. enableFlag: true
  45. },
  46. tableList: [] as any
  47. });
  48. // 查询列表
  49. const getList = async () => {
  50. try {
  51. if (state.pagination.page === 1) {
  52. state.loading = true;
  53. }
  54. const { data } = await materialQueryPage({
  55. ...state.searchGroup,
  56. ...state.pagination,
  57. subjectId: prepareStore.getSubjectId
  58. });
  59. state.loading = false;
  60. const tempRows = data.rows || [];
  61. const temp: any = [];
  62. tempRows.forEach((row: any) => {
  63. temp.push({
  64. id: row.id,
  65. coverImg: row.coverImg,
  66. type: row.type,
  67. title: row.name,
  68. isCollect: !!row.favoriteFlag,
  69. isSelected: row.sourceFrom === 'PLATFORM' ? true : false,
  70. content: row.content
  71. });
  72. });
  73. state.tableList.push(...temp);
  74. state.finshed = data.pages <= data.current ? true : false;
  75. } catch {
  76. state.loading = false;
  77. }
  78. };
  79. const onSearch = async (item: any) => {
  80. state.pagination.page = 1;
  81. state.tableList = [];
  82. state.searchGroup = Object.assign(state.searchGroup, item);
  83. getList();
  84. };
  85. // 添加资源
  86. const onAdd = async (item: any) => {
  87. dialog.warning({
  88. title: '提示',
  89. content: `是否添加"${item.title}"资源?`,
  90. positiveText: '确定',
  91. negativeText: '取消',
  92. onPositiveClick: async () => {
  93. try {
  94. console.log(item, 'any');
  95. const temp: any = [];
  96. prepareStore.getCoursewareList.forEach((item: any) => {
  97. temp.push({
  98. materialId: item.materialId,
  99. materialName: item.title,
  100. materialType: item.type,
  101. id: item.id
  102. });
  103. });
  104. // 保存课件
  105. await saveCourseware({
  106. coursewareDetailKnowledgeId: prepareStore.getSelectKey,
  107. lessonCoursewareId: prepareStore.getLessonCoursewareId,
  108. lessonCoursewareDetailId:
  109. prepareStore.getLessonCoursewareDetailId,
  110. materialList: [
  111. ...temp,
  112. {
  113. materialName: item.title,
  114. materialType: item.type,
  115. materialId: item.id
  116. }
  117. ]
  118. });
  119. message.success('添加成功');
  120. prepareStore.setIsAddResource(true);
  121. } catch {
  122. //
  123. }
  124. }
  125. });
  126. };
  127. onMounted(() => {
  128. getList();
  129. });
  130. return () => (
  131. <div>
  132. <ResourceSearchGroup
  133. type={props.type}
  134. onSearch={(item: any) => onSearch(item)}
  135. />
  136. <NScrollbar
  137. class={styles.listContainer}
  138. onScroll={(e: any) => {
  139. const clientHeight = e.target?.clientHeight;
  140. const scrollTop = e.target?.scrollTop;
  141. const scrollHeight = e.target?.scrollHeight;
  142. // 是否到底,是否加载完
  143. if (
  144. clientHeight + scrollTop + 20 >= scrollHeight &&
  145. !state.finshed &&
  146. !state.loading
  147. ) {
  148. state.pagination.page = state.pagination.page + 1;
  149. getList();
  150. }
  151. }}>
  152. <NSpin show={state.loading} size={'small'}>
  153. <div
  154. class={[
  155. styles.listSection,
  156. !state.loading && state.tableList.length <= 0
  157. ? styles.emptySection
  158. : ''
  159. ]}>
  160. {state.tableList.length > 0 && (
  161. <div class={styles.list}>
  162. {state.tableList.map((item: any) => (
  163. <CardType
  164. isShowAdd
  165. item={item}
  166. onAdd={(item: any) => onAdd(item)}
  167. />
  168. ))}
  169. </div>
  170. )}
  171. {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
  172. </div>
  173. </NSpin>
  174. </NScrollbar>
  175. </div>
  176. );
  177. }
  178. });