index.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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, materialRemove } from '../../api';
  7. import { NSpin, useDialog, useMessage } 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 message = useMessage();
  14. const dialog = useDialog();
  15. const state = reactive({
  16. searchWord: '',
  17. loading: false,
  18. pageTotal: 0,
  19. pagination: {
  20. page: 1,
  21. rows: 20
  22. },
  23. searchGroup: {
  24. type: '', //
  25. keyword: '',
  26. bookVersionId: null,
  27. subjectId: null,
  28. sourceType: 4
  29. },
  30. tableList: [] as any,
  31. show: false,
  32. item: {} as any
  33. });
  34. const getList = async () => {
  35. try {
  36. state.loading = true;
  37. const { data } = await materialQueryPage({
  38. ...state.searchGroup,
  39. ...state.pagination
  40. });
  41. state.loading = false;
  42. state.pageTotal = Number(data.total);
  43. const tempRows = data.rows || [];
  44. const temp: any = [];
  45. tempRows.forEach((row: any) => {
  46. temp.push({
  47. id: row.id,
  48. coverImg: row.coverImg,
  49. type: row.type,
  50. title: row.name,
  51. isCollect: !!row.favoriteFlag,
  52. isSelected: row.sourceFrom === 'PLATFORM' ? true : false,
  53. content: row.content,
  54. subjectId: row.subjectIds,
  55. enableFlag: row.enableFlag ? 1 : 0,
  56. openFlag: row.openFlag
  57. });
  58. });
  59. state.tableList = temp || [];
  60. } catch {
  61. state.loading = false;
  62. }
  63. };
  64. const onSearch = async (item: any) => {
  65. state.pagination.page = 1;
  66. state.searchGroup = Object.assign(state.searchGroup, item);
  67. getList();
  68. };
  69. // 收藏
  70. const onCollect = async (item: any) => {
  71. try {
  72. await favorite({
  73. materialId: item.id,
  74. favoriteFlag: item.isCollect ? 0 : 1,
  75. type: item.type
  76. });
  77. item.isCollect = !item.isCollect;
  78. onSearch(state.searchGroup);
  79. } catch {
  80. //
  81. }
  82. };
  83. // 单个删除
  84. const onRemove = async (item: any) => {
  85. try {
  86. dialog.warning({
  87. title: '提示',
  88. content: '该资源已下架,是否删除?',
  89. positiveText: '确定',
  90. negativeText: '取消',
  91. onPositiveClick: async () => {
  92. await materialRemove({ id: item.id });
  93. message.success('删除成功');
  94. onSearch(state.searchGroup);
  95. }
  96. });
  97. } catch {
  98. //
  99. }
  100. };
  101. onMounted(() => {
  102. getList();
  103. });
  104. return () => (
  105. <>
  106. <SearchGroupResources onSearch={(item: any) => onSearch(item)} />
  107. <NSpin v-model:show={state.loading} style={{ 'min-height': '50vh' }}>
  108. <div class={styles.list}>
  109. {state.tableList.map((item: any) => (
  110. <CardType
  111. item={item}
  112. offShelf={item.enableFlag ? false : true}
  113. onOffShelf={() => onRemove(item)}
  114. disabledMouseHover={false}
  115. onClick={(val: any) => {
  116. if (val.type === 'IMG' || !item.enableFlag) return;
  117. state.show = true;
  118. state.item = val;
  119. }}
  120. onCollect={(item: any) => onCollect(item)}
  121. />
  122. ))}
  123. {!state.loading && state.tableList.length <= 0 && (
  124. <TheEmpty description="暂无收藏资源" />
  125. )}
  126. </div>
  127. </NSpin>
  128. <Pagination
  129. v-model:page={state.pagination.page}
  130. v-model:pageSize={state.pagination.rows}
  131. v-model:pageTotal={state.pageTotal}
  132. onList={getList}
  133. />
  134. {/* 弹窗查看 */}
  135. <CardPreview v-model:show={state.show} item={state.item} />
  136. </>
  137. );
  138. }
  139. });