index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { PropType, defineComponent, onMounted, reactive } from 'vue';
  2. import styles from './index.module.less';
  3. import SearchGroupResources from './search-group-resources';
  4. import { NImage, NScrollbar, NSpin } from 'naive-ui';
  5. import TheEmpty from '/src/components/TheEmpty';
  6. import Pagination from '/src/components/pagination';
  7. import { useRouter } from 'vue-router';
  8. import { api_knowledgeWiki_page } from '/src/views/content-information/api';
  9. export default defineComponent({
  10. name: 'instrument-list',
  11. props: {
  12. categoryId: {
  13. type: String,
  14. default: ''
  15. },
  16. categoryChildList: {
  17. type: Array as PropType<any>,
  18. default: () => []
  19. },
  20. selectItems: {
  21. type: Array as PropType<any>,
  22. default: () => []
  23. }
  24. },
  25. emits: ['confirm'],
  26. setup(props, { emit }) {
  27. const router = useRouter();
  28. const state = reactive({
  29. searchWord: '',
  30. loading: false,
  31. pageTotal: 0,
  32. finshed: false, // 是否加载完
  33. pagination: {
  34. page: 1,
  35. rows: 18
  36. },
  37. searchGroup: {
  38. type: 'INSTRUMENT', //
  39. keyword: '',
  40. wikiCategoryId: props.categoryId
  41. },
  42. tableList: [] as any,
  43. teachingStatus: false,
  44. show: false,
  45. item: {} as any
  46. // selectIds: [] as any
  47. });
  48. const getList = async () => {
  49. state.loading = true;
  50. try {
  51. const { data } = await api_knowledgeWiki_page({
  52. ...state.pagination,
  53. ...state.searchGroup
  54. });
  55. const temp = data.rows || [];
  56. temp.forEach((item: any) => {
  57. if (
  58. item.knowledgeWikiCategories &&
  59. item.knowledgeWikiCategories.length
  60. ) {
  61. item.categories =
  62. item.knowledgeWikiCategories[0].knowledgeWikiCategoryTypeName;
  63. }
  64. });
  65. state.tableList.push(...temp);
  66. state.pageTotal = Number(data.total);
  67. state.finshed = data.pages <= data.current ? true : false;
  68. } catch {
  69. //
  70. }
  71. state.loading = false;
  72. };
  73. const onSearch = async (item: any) => {
  74. state.pagination.page = 1;
  75. state.searchGroup = Object.assign(state.searchGroup, item);
  76. state.tableList = [];
  77. getList();
  78. };
  79. // 更新
  80. const onSelect = (item: any) => {
  81. const ids = props.selectItems || [];
  82. const index = ids.findIndex((i: any) => i.id === item.id);
  83. if (index !== -1) {
  84. ids.splice(index, 1);
  85. } else {
  86. ids.push(item);
  87. }
  88. emit('confirm', ids);
  89. };
  90. onMounted(() => {
  91. getList();
  92. });
  93. return () => (
  94. <div class={styles.instrumentList}>
  95. <SearchGroupResources
  96. class={styles.searchGroups}
  97. categoryChildList={props.categoryChildList || []}
  98. onSearch={(item: any) => onSearch(item)}
  99. wikiCategoryId={props.categoryId}
  100. />
  101. <NScrollbar
  102. class={styles.listContainer}
  103. style={{
  104. 'max-height': `55vh`
  105. }}
  106. onScroll={(e: any) => {
  107. const clientHeight = e.target?.clientHeight;
  108. const scrollTop = e.target?.scrollTop;
  109. const scrollHeight = e.target?.scrollHeight;
  110. // 是否到底,是否加载完
  111. if (
  112. clientHeight + scrollTop + 20 >= scrollHeight &&
  113. !state.finshed &&
  114. !state.loading
  115. ) {
  116. state.pagination.page = state.pagination.page + 1;
  117. getList();
  118. }
  119. }}>
  120. <NSpin v-model:show={state.loading} style={{ 'min-height': '50vh' }}>
  121. <div class={styles.list}>
  122. {state.tableList.map((item: any) => (
  123. <div
  124. class={styles.itemWrap}
  125. onClick={() => {
  126. // router.push({
  127. // path: '/content-instruments-detail',
  128. // query: {
  129. // id: item.id,
  130. // name: item.name
  131. // }
  132. // });
  133. }}>
  134. <div
  135. class={styles.itemWrapBox}
  136. onClick={() => onSelect(item)}>
  137. <div class={styles.itemCard}>
  138. <div
  139. class={[
  140. styles.itemImgSection,
  141. props.selectItems.findIndex(
  142. (i: any) => i.id === item.id
  143. ) !== -1 && styles.itemImgSectionSelected
  144. ]}>
  145. <NImage
  146. src={item.avatar}
  147. class={styles.img}
  148. objectFit="cover"
  149. previewDisabled
  150. />
  151. <i class={[styles.iconCheck]}></i>
  152. </div>
  153. <div class={styles.itemTitle}>{item.name}</div>
  154. </div>
  155. </div>
  156. </div>
  157. ))}
  158. {!state.loading && state.tableList.length <= 0 && (
  159. <TheEmpty
  160. style={{ minHeight: '50vh' }}
  161. description="暂无乐器百科"
  162. />
  163. )}
  164. </div>
  165. </NSpin>
  166. </NScrollbar>
  167. </div>
  168. );
  169. }
  170. });