index.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { defineComponent, onMounted, reactive, ref } from 'vue';
  2. import styles from './index.module.less';
  3. import CardType from '/src/components/card-type';
  4. import Pagination from '/src/components/pagination';
  5. import SearchGroupResources from './search-group-resources';
  6. import {
  7. favorite,
  8. materialQueryPage,
  9. materialRemove,
  10. materialRemoveAll
  11. } from '../../api';
  12. import { NModal, NSpin, useDialog, useMessage } from 'naive-ui';
  13. import TheEmpty from '/src/components/TheEmpty';
  14. import UploadModal from './upload-modal';
  15. import CardPreview from '@/components/card-preview';
  16. import resourceDefault from '../../images/resource-default.svg';
  17. import resourceChecked from '../../images/resource-checked.svg';
  18. import MyResourcesGuide from '@/custom-plugins/guide-page/myResources-guide';
  19. export default defineComponent({
  20. name: 'share-resources',
  21. setup() {
  22. const message = useMessage();
  23. const dialog = useDialog();
  24. const state = reactive({
  25. searchWord: '',
  26. loading: false,
  27. pageTotal: 0,
  28. pagination: {
  29. page: 1,
  30. rows: 20
  31. },
  32. searchGroup: {
  33. type: '', //
  34. name: '',
  35. bookVersionId: null,
  36. subjectId: null,
  37. sourceType: 3
  38. },
  39. tableList: [] as any,
  40. uploadStatus: false,
  41. show: false,
  42. item: {} as any,
  43. editStatus: false, // 是否编辑
  44. editList: [] as any, // TOD
  45. editIds: [] as any // 编辑的
  46. });
  47. const showGuide = ref(false);
  48. const getList = async () => {
  49. try {
  50. state.loading = true;
  51. const { data } = await materialQueryPage({
  52. ...state.searchGroup,
  53. ...state.pagination
  54. });
  55. state.loading = false;
  56. state.pageTotal = Number(data.total);
  57. const tempRows = data.rows || [];
  58. const temp: any = [];
  59. tempRows.forEach((row: any) => {
  60. temp.push({
  61. id: row.id,
  62. coverImg: row.coverImg,
  63. type: row.type,
  64. title: row.name,
  65. isCollect: !!row.favoriteFlag,
  66. isSelected: row.sourceFrom === 'PLATFORM' ? true : false,
  67. content: row.content,
  68. subjectId: row.subjectIds,
  69. enableFlag: row.enableFlag ? 1 : 0,
  70. openFlag: row.openFlag
  71. });
  72. });
  73. state.tableList = temp || [];
  74. setTimeout(() => {
  75. showGuide.value = true;
  76. }, 500);
  77. } catch {
  78. state.loading = false;
  79. }
  80. };
  81. // 收藏
  82. const onCollect = async (item: any) => {
  83. try {
  84. await favorite({
  85. materialId: item.id,
  86. favoriteFlag: item.isCollect ? 0 : 1,
  87. type: item.type
  88. });
  89. item.isCollect = !item.isCollect;
  90. } catch {
  91. //
  92. }
  93. };
  94. const onSearch = async (item: any) => {
  95. state.pagination.page = 1;
  96. state.searchGroup = Object.assign(state.searchGroup, item);
  97. getList();
  98. };
  99. // 批量删除
  100. const onDelete = async () => {
  101. try {
  102. if (state.editIds.length <= 0) {
  103. message.error('至少选择一条资源进行删除');
  104. return;
  105. }
  106. dialog.warning({
  107. title: '提示',
  108. content: '你确定删除该资源?',
  109. positiveText: '确定',
  110. negativeText: '取消',
  111. onPositiveClick: async () => {
  112. await materialRemoveAll(state.editIds);
  113. message.success('删除成功');
  114. onSearch(state.searchGroup);
  115. state.editIds = [];
  116. }
  117. });
  118. } catch {
  119. //
  120. }
  121. };
  122. // 单个删除
  123. const onRemove = async (item: any) => {
  124. try {
  125. dialog.warning({
  126. title: '提示',
  127. content: '该资源已下架,是否删除?',
  128. positiveText: '确定',
  129. negativeText: '取消',
  130. onPositiveClick: async () => {
  131. await materialRemove({ id: item.id });
  132. message.success('删除成功');
  133. onSearch(state.searchGroup);
  134. }
  135. });
  136. } catch {
  137. //
  138. }
  139. };
  140. onMounted(() => {
  141. getList();
  142. });
  143. return () => (
  144. <>
  145. <SearchGroupResources
  146. onSearch={(item: any) => onSearch(item)}
  147. onUpload={() => {
  148. state.editList = [];
  149. state.uploadStatus = true;
  150. }}
  151. onUpdate={() => {
  152. // 修改
  153. const list: any[] = [];
  154. state.tableList.forEach((item: any) => {
  155. if (state.editIds.indexOf(item.id) > -1) {
  156. list.push(item);
  157. }
  158. });
  159. state.editList = list || [];
  160. if (state.editList.length <= 0) {
  161. message.error('至少选择一条资源进行编辑');
  162. return;
  163. }
  164. state.uploadStatus = true;
  165. }}
  166. onEdit={(status: boolean) => {
  167. // 点击编辑
  168. state.editStatus = status;
  169. if (!state.editStatus) {
  170. state.editIds = [];
  171. }
  172. }}
  173. onSelectAll={(status: boolean) => {
  174. // 全选
  175. if (status) {
  176. const tempIds: any[] = [];
  177. state.tableList.forEach((item: any) => {
  178. tempIds.push(item.id);
  179. });
  180. state.editIds = tempIds;
  181. } else {
  182. state.editIds = [];
  183. }
  184. }}
  185. onDelete={onDelete}
  186. />
  187. <NSpin v-model:show={state.loading} style={{ 'min-height': '50vh' }}>
  188. <div class={styles.list}>
  189. {state.tableList.map((item: any) => (
  190. <div class={styles.itemSection}>
  191. <CardType
  192. item={item}
  193. disabledMouseHover={false}
  194. offShelf={item.enableFlag ? false : true}
  195. onOffShelf={() => onRemove(item)}
  196. onClick={(val: any) => {
  197. if (val.type === 'IMG' || !item.enableFlag) return;
  198. state.show = true;
  199. state.item = val;
  200. }}
  201. onCollect={(item: any) => onCollect(item)}
  202. />
  203. {/* 编辑模式 */}
  204. {state.editStatus && (
  205. <div
  206. class={[
  207. styles.itemBg,
  208. state.editIds.includes(item.id)
  209. ? styles.itemBgChecked
  210. : ''
  211. ]}
  212. onClick={() => {
  213. const index = state.editIds.indexOf(item.id);
  214. if (index > -1) {
  215. state.editIds.splice(index, 1);
  216. } else {
  217. state.editIds.push(item.id);
  218. }
  219. }}>
  220. <img
  221. src={
  222. state.editIds.includes(item.id)
  223. ? resourceChecked
  224. : resourceDefault
  225. }
  226. class={styles.resourceDefault}
  227. />
  228. </div>
  229. )}
  230. </div>
  231. ))}
  232. {!state.loading && state.tableList.length <= 0 && (
  233. <TheEmpty description="暂无资源" />
  234. )}
  235. </div>
  236. </NSpin>
  237. <Pagination
  238. v-model:page={state.pagination.page}
  239. v-model:pageSize={state.pagination.rows}
  240. v-model:pageTotal={state.pageTotal}
  241. onList={getList}
  242. />
  243. {/* 弹窗查看 */}
  244. <CardPreview v-model:show={state.show} item={state.item} />
  245. <NModal
  246. v-model:show={state.uploadStatus}
  247. preset="card"
  248. showIcon={false}
  249. class={['modalTitle background', styles.attendClassModal]}
  250. title={state.editStatus ? '修改资源' : '上传资源'}
  251. blockScroll={false}>
  252. <UploadModal
  253. onClose={() => (state.uploadStatus = false)}
  254. onConfirm={() => {
  255. state.editIds = [];
  256. state.editList = [];
  257. onSearch(state.searchGroup);
  258. }}
  259. list={state.editList}
  260. />
  261. </NModal>
  262. {showGuide.value ? <MyResourcesGuide></MyResourcesGuide> : null}
  263. </>
  264. );
  265. }
  266. });