index.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { defineComponent, onMounted, reactive } from 'vue';
  2. import styles from './index.module.less';
  3. import CardType from '@/components/card-type';
  4. import Pagination from '@/components/pagination';
  5. import SearchGroupResources from './search-group-resources';
  6. import { favorite, materialQueryPage } from '../../api';
  7. import { NSpin } from 'naive-ui';
  8. import TheEmpty from '@/components/TheEmpty';
  9. import CardPreview from '@/components/card-preview';
  10. export default defineComponent({
  11. name: 'share-resources',
  12. setup() {
  13. const state = reactive({
  14. searchWord: '',
  15. loading: false,
  16. pageTotal: 0,
  17. pagination: {
  18. page: 1,
  19. rows: 20
  20. },
  21. searchGroup: {
  22. type: '', //
  23. keyword: '',
  24. bookVersionId: null,
  25. subjectId: null,
  26. sourceType: 4
  27. },
  28. tableList: [] as any,
  29. show: false,
  30. item: {} as any
  31. });
  32. const getList = async () => {
  33. try {
  34. state.loading = true;
  35. const { data } = await materialQueryPage({
  36. ...state.searchGroup,
  37. ...state.pagination
  38. });
  39. state.loading = false;
  40. state.pageTotal = Number(data.total);
  41. state.tableList = data.rows || [];
  42. } catch {
  43. state.loading = false;
  44. }
  45. };
  46. const onSearch = async (item: any) => {
  47. state.pagination.page = 1;
  48. state.searchGroup = Object.assign(state.searchGroup, item);
  49. getList();
  50. };
  51. // 收藏
  52. const onCollect = async (item: any) => {
  53. try {
  54. await favorite({
  55. materialId: item.id,
  56. favoriteFlag: item.isCollect ? 0 : 1,
  57. type: item.type
  58. });
  59. item.isCollect = !item.isCollect;
  60. onSearch();
  61. } catch {
  62. //
  63. }
  64. };
  65. onMounted(() => {
  66. getList();
  67. });
  68. return () => (
  69. <>
  70. <SearchGroupResources onSearch={(item: any) => onSearch(item)} />
  71. <NSpin v-model:show={state.loading}>
  72. <div class={styles.list}>
  73. {state.tableList.map((item: any) => {
  74. const tmpItem = {
  75. id: item.id,
  76. coverImg: item.coverImg,
  77. type: item.type,
  78. title: item.name,
  79. isCollect: !!item.favoriteFlag,
  80. isSelected: item.sourceFrom === 'PLATFORM' ? true : false
  81. };
  82. return (
  83. <CardType
  84. item={tmpItem}
  85. disabledMouseHover={false}
  86. onClick={(val: any) => {
  87. if (val.type === 'IMG') return;
  88. state.show = true;
  89. state.item = val;
  90. }}
  91. onCollect={(item: any) => onCollect(item)}
  92. />
  93. );
  94. })}
  95. {!state.loading && state.tableList.length <= 0 && (
  96. <TheEmpty description="暂无收藏资源" />
  97. )}
  98. </div>
  99. </NSpin>
  100. <Pagination
  101. v-model:page={state.pagination.page}
  102. v-model:pageSize={state.pagination.rows}
  103. v-model:pageTotal={state.pageTotal}
  104. onList={getList}
  105. />
  106. {/* 弹窗查看 */}
  107. <CardPreview v-model:show={state.show} item={state.item} />
  108. </>
  109. );
  110. }
  111. });