index.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, NSpin } from 'naive-ui';
  5. import TheEmpty from '/src/components/TheEmpty';
  6. import Pagination from '/src/components/pagination';
  7. import { api_knowledgeWiki_page } from '../../../api';
  8. import { useRouter } from 'vue-router';
  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. },
  21. setup(props) {
  22. // 保存数据
  23. const operationCatch = (type: 'get'| 'set' = 'get', value: string = '') : any => {
  24. const sessionName = 'content-instrument-catch'
  25. if(type === "get") {
  26. const result = sessionStorage.getItem(sessionName)
  27. return result ? JSON.parse(result) : null
  28. } else if(type === 'set') {
  29. sessionStorage.setItem(sessionName, value)
  30. }
  31. }
  32. const router = useRouter();
  33. const catchData = operationCatch('get')
  34. const state = reactive({
  35. searchWord: '',
  36. loading: false,
  37. pageTotal: 0,
  38. pagination: catchData && catchData.pagination ? catchData.pagination : {
  39. page: 1,
  40. rows: 18
  41. },
  42. searchGroup: catchData && catchData.searchGroup ? catchData.searchGroup : {
  43. type: 'INSTRUMENT', //
  44. keyword: '',
  45. wikiCategoryId: props.categoryId
  46. },
  47. tableList: [] as any,
  48. teachingStatus: false,
  49. show: false,
  50. item: {} as any
  51. });
  52. const getList = async () => {
  53. state.loading = true;
  54. // 缓存
  55. operationCatch('set', JSON.stringify({
  56. pagination: state.pagination,
  57. searchGroup: state.searchGroup
  58. }))
  59. try {
  60. const { data } = await api_knowledgeWiki_page({
  61. ...state.pagination,
  62. ...state.searchGroup
  63. });
  64. const temp = data.rows || [];
  65. temp.forEach((item: any) => {
  66. if (
  67. item.knowledgeWikiCategories &&
  68. item.knowledgeWikiCategories.length
  69. ) {
  70. item.categories =
  71. item.knowledgeWikiCategories[0].knowledgeWikiCategoryTypeName;
  72. }
  73. });
  74. state.tableList = temp || [];
  75. state.pageTotal = Number(data.total);
  76. } catch {
  77. //
  78. }
  79. state.loading = false;
  80. };
  81. const onSearch = async (item: any) => {
  82. state.pagination.page = 1;
  83. state.searchGroup = Object.assign(state.searchGroup, item);
  84. getList();
  85. };
  86. onMounted(() => {
  87. getList();
  88. });
  89. return () => (
  90. <div class={styles.instrumentList}>
  91. <SearchGroupResources
  92. categoryChildList={props.categoryChildList || []}
  93. onSearch={(item: any) => onSearch(item)}
  94. wikiCategoryId={state.searchGroup.wikiCategoryId}
  95. defaultWikiCategoryId={props.categoryId}
  96. searchValue={state.searchGroup.keyword}
  97. />
  98. <NSpin v-model:show={state.loading} style={{ 'min-height': '50vh' }}>
  99. <div class={styles.list}>
  100. {state.tableList.map((item: any) => (
  101. <div
  102. class={styles.itemWrap}
  103. onClick={() => {
  104. router.push({
  105. path: '/content-instruments-detail',
  106. query: {
  107. id: item.id,
  108. name: item.name
  109. }
  110. });
  111. }}>
  112. <div class={styles.itemWrapBox}>
  113. <div class={styles.itemCard}>
  114. {item.categories ? (
  115. <span class={styles.itemTag}>{item.categories}</span>
  116. ) : (
  117. ''
  118. )}
  119. <div class={styles.itemImgSection}>
  120. <NImage
  121. src={
  122. item.avatar +
  123. '?imageMogr2/strip/format/jpg/size-limit/15k!'
  124. }
  125. class={styles.img}
  126. objectFit="cover"
  127. previewDisabled
  128. />
  129. </div>
  130. <div class={styles.itemTitle}>{item.name}</div>
  131. </div>
  132. </div>
  133. </div>
  134. ))}
  135. {!state.loading && state.tableList.length <= 0 && (
  136. <TheEmpty
  137. style={{ minHeight: '50vh' }}
  138. description="暂无乐器百科"
  139. />
  140. )}
  141. </div>
  142. </NSpin>
  143. <Pagination
  144. v-model:page={state.pagination.page}
  145. v-model:pageSize={state.pagination.rows}
  146. v-model:pageTotal={state.pageTotal}
  147. pageSizes={[18, 24, 30, 36]}
  148. onList={getList}
  149. />
  150. </div>
  151. );
  152. }
  153. });