index.tsx 6.5 KB

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