index.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. import { computed, 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. materialRemoveMusic,
  12. materialUpdateAll
  13. } from '../../api';
  14. import {
  15. NButton,
  16. NModal,
  17. NSpace,
  18. NSpin,
  19. useDialog,
  20. useMessage
  21. } from 'naive-ui';
  22. import TheEmpty from '/src/components/TheEmpty';
  23. import UploadModal, { formatUrlType } from './upload-modal';
  24. import CardPreview from '@/components/card-preview';
  25. import resourceDefault from '../../images/resource-default.png';
  26. import resourceChecked from '../../images/resource-checked.png';
  27. import MyResourcesGuide from '@/custom-plugins/guide-page/myResources-guide';
  28. import SaveModal from './save-modal';
  29. import deepClone from '/src/helpers/deep-clone';
  30. import UploadCover from './upload-cover';
  31. export default defineComponent({
  32. name: 'share-resources',
  33. setup() {
  34. const message = useMessage();
  35. const dialog = useDialog();
  36. const state = reactive({
  37. searchWord: '',
  38. loading: false,
  39. pageTotal: 0,
  40. pagination: {
  41. page: 1,
  42. rows: 20
  43. },
  44. searchGroup: {
  45. type: 'MUSIC', //
  46. name: '',
  47. bookVersionId: null,
  48. subjectId: null,
  49. sourceType: 3
  50. },
  51. tableList: [] as any,
  52. uploadStatus: false,
  53. saveStatus: false,
  54. show: false,
  55. item: {} as any,
  56. editStatus: false, // 是否编辑
  57. isAdd: false,
  58. editList: [] as any, // TOD
  59. editIds: [] as any, // 编辑的
  60. editOverIds: [] as any, // 确认修改的数据
  61. removeVisiable: false,
  62. removeContent: '是否删除该资源?',
  63. type: 'remove',
  64. removeItem: {} as any
  65. });
  66. const showGuide = ref(false);
  67. const getList = async () => {
  68. try {
  69. state.loading = true;
  70. const { data } = await materialQueryPage({
  71. ...state.searchGroup,
  72. ...state.pagination
  73. });
  74. state.loading = false;
  75. state.pageTotal = Number(data.total);
  76. const tempRows = data.rows || [];
  77. const temp: any = [];
  78. tempRows.forEach((row: any) => {
  79. temp.push({
  80. id: row.id,
  81. coverImg: row.coverImg,
  82. type: row.type,
  83. title: row.name,
  84. isCollect: !!row.favoriteFlag,
  85. isSelected: row.sourceFrom === 'PLATFORM' ? true : false,
  86. refFlag: row.refFlag,
  87. content: row.content,
  88. // subjectId: row.subjectIds,
  89. instrumentIds: row.instrumentIds,
  90. sourceFrom: row.sourceFrom,
  91. enableFlag: row.enableFlag ? 1 : 0,
  92. openFlag: row.openFlag
  93. });
  94. });
  95. state.tableList = temp || [];
  96. setTimeout(() => {
  97. showGuide.value = true;
  98. }, 500);
  99. } catch {
  100. state.loading = false;
  101. }
  102. };
  103. const isEmpty = computed(() => {
  104. const list = state.tableList || [];
  105. let num = 0;
  106. list.forEach((item: any) => {
  107. if (!item.delFlag) {
  108. num++;
  109. }
  110. });
  111. return num > 0 ? false : true;
  112. });
  113. // 收藏
  114. const onCollect = async (item: any) => {
  115. try {
  116. await favorite({
  117. materialId: item.id,
  118. favoriteFlag: item.isCollect ? 0 : 1,
  119. type: item.type
  120. });
  121. item.isCollect = !item.isCollect;
  122. } catch {
  123. //
  124. }
  125. };
  126. const onSearch = async (item: any) => {
  127. state.pagination.page = 1;
  128. const { subjectId, ...res } = item;
  129. state.searchGroup = Object.assign(state.searchGroup, {
  130. ...res,
  131. musicalInstrumentId: subjectId,
  132. subjectId: null
  133. });
  134. getList();
  135. };
  136. // 批量删除
  137. const onDelete = async () => {
  138. try {
  139. if (state.searchGroup.type === 'MUSIC') {
  140. await materialRemoveMusic(state.editIds);
  141. } else {
  142. await materialRemoveAll(state.editIds);
  143. }
  144. // message.success('删除成功');
  145. // state.pagination.page = 1;
  146. // getList();
  147. // state.editIds = [];
  148. } catch {
  149. //
  150. }
  151. };
  152. // 单个删除
  153. const onRemove = async () => {
  154. try {
  155. // 如果是乐谱类型则使用其它删除接口
  156. if (state.searchGroup.type === 'MUSIC') {
  157. await materialRemoveMusic([state.removeItem.id]);
  158. } else {
  159. await materialRemove({ ids: state.removeItem.id });
  160. }
  161. message.success('删除成功');
  162. onSearch(state.searchGroup);
  163. } catch {
  164. //
  165. }
  166. };
  167. const searchGroupResourcesRef = ref();
  168. onMounted(() => {
  169. getList();
  170. });
  171. return () => (
  172. <>
  173. <SearchGroupResources
  174. ref={searchGroupResourcesRef}
  175. onSearch={(item: any) => onSearch(item)}
  176. onUpload={() => {
  177. state.editList = [];
  178. // state.uploadStatus = true;
  179. state.saveStatus = true;
  180. }}
  181. onUpdate={() => {
  182. // 修改
  183. const list: any[] = [];
  184. state.tableList.forEach((item: any) => {
  185. if (state.editIds.indexOf(item.id) > -1 && item.delFlag !== 1) {
  186. list.push(item);
  187. }
  188. });
  189. state.editList = list || [];
  190. if (state.editList.length <= 0) {
  191. message.error('至少选择一条资源进行编辑');
  192. return;
  193. }
  194. state.uploadStatus = true;
  195. state.isAdd = false;
  196. }}
  197. onEditOver={async (status: boolean) => {
  198. state.editStatus = status;
  199. try {
  200. // 修改
  201. if (state.editOverIds.length > 0) {
  202. const body = [] as any;
  203. state.tableList.forEach((item: any) => {
  204. if (state.editOverIds.includes(item.id)) {
  205. body.push({
  206. instrumentIds: item.instrumentIds,
  207. openFlag: item.openFlag,
  208. coverImg: item.coverImg,
  209. name: item.title,
  210. type: item.type,
  211. enableFlag: 1,
  212. content: item.content,
  213. id: item.id || null,
  214. delFlag: item.delFlag
  215. });
  216. }
  217. });
  218. //
  219. if (body.length > 0) {
  220. if (state.searchGroup.type === 'MUSIC') {
  221. await materialRemoveMusic(state.editIds);
  222. } else {
  223. await materialUpdateAll(body);
  224. }
  225. }
  226. }
  227. message.success('修改成功');
  228. state.pagination.page = 1;
  229. getList();
  230. state.editIds = [];
  231. state.editOverIds = [];
  232. } catch {
  233. //
  234. }
  235. }}
  236. onCancel={status => {
  237. state.editStatus = status;
  238. state.pagination.page = 1;
  239. state.editIds = [];
  240. state.editOverIds = [];
  241. getList();
  242. }}
  243. onEdit={async (status: boolean) => {
  244. // 点击编辑
  245. state.editStatus = status;
  246. if (!state.editStatus) {
  247. state.editIds = [];
  248. state.editOverIds = [];
  249. }
  250. }}
  251. onSelectAll={(status: boolean) => {
  252. // 全选
  253. if (status) {
  254. const tempIds: any[] = [];
  255. state.tableList.forEach((item: any) => {
  256. tempIds.push(item.id);
  257. });
  258. state.editIds = tempIds;
  259. } else {
  260. state.editIds = [];
  261. }
  262. }}
  263. onDelete={() => {
  264. if (state.editIds.length <= 0) {
  265. message.error('至少选择一条资源进行删除');
  266. return;
  267. }
  268. state.type = 'delete';
  269. state.removeContent = '是否删除该资源?';
  270. state.removeVisiable = true;
  271. }}
  272. />
  273. <NSpin v-model:show={state.loading} style={{ 'min-height': '50vh' }}>
  274. <div class={styles.list}>
  275. {state.tableList.map(
  276. (item: any) =>
  277. item.delFlag !== 1 && (
  278. <div class={styles.itemWrap}>
  279. <div class={styles.itemWrapBox}>
  280. <CardType
  281. item={item}
  282. isDownload
  283. disabledMouseHover={false}
  284. offShelf={item.enableFlag ? false : true}
  285. onOffShelf={() => {
  286. state.type = 'remove';
  287. state.removeContent = '该资源已下架,是否删除?';
  288. state.removeVisiable = true;
  289. state.removeItem = item;
  290. }} // 下架
  291. onClick={(val: any) => {
  292. if (val.type === 'IMG' || !item.enableFlag) return;
  293. state.show = true;
  294. state.item = val;
  295. }}
  296. onCollect={(item: any) => onCollect(item)}
  297. />
  298. {/* 编辑模式 */}
  299. {state.editStatus && (
  300. <div
  301. class={[
  302. styles.itemBg,
  303. state.editIds.includes(item.id)
  304. ? styles.itemBgChecked
  305. : ''
  306. ]}
  307. onClick={() => {
  308. const index = state.editIds.indexOf(item.id);
  309. if (index > -1) {
  310. state.editIds.splice(index, 1);
  311. } else {
  312. state.editIds.push(item.id);
  313. }
  314. }}>
  315. <img
  316. src={
  317. state.editIds.includes(item.id)
  318. ? resourceChecked
  319. : resourceDefault
  320. }
  321. class={styles.resourceDefault}
  322. />
  323. </div>
  324. )}
  325. </div>
  326. </div>
  327. )
  328. )}
  329. {!state.loading && isEmpty.value && (
  330. <TheEmpty style={{ minHeight: '50vh' }} description="暂无资源" />
  331. )}
  332. </div>
  333. </NSpin>
  334. <Pagination
  335. disabled={state.editStatus}
  336. v-model:page={state.pagination.page}
  337. v-model:pageSize={state.pagination.rows}
  338. v-model:pageTotal={state.pageTotal}
  339. onList={getList}
  340. />
  341. {/* 弹窗查看 */}
  342. <CardPreview v-model:show={state.show} item={state.item} />
  343. <NModal
  344. v-model:show={state.uploadStatus}
  345. preset="card"
  346. showIcon={false}
  347. class={['modalTitle background', styles.attendClassModal]}
  348. title={state.editStatus ? '修改资源' : '上传资源'}
  349. blockScroll={false}>
  350. <UploadModal
  351. editStatus={state.editStatus}
  352. onClose={() => {
  353. state.uploadStatus = false;
  354. }}
  355. onConfirm={() => {
  356. state.editIds = [];
  357. state.editList = [];
  358. state.editOverIds = [];
  359. state.saveStatus = false;
  360. searchGroupResourcesRef.value?.resetStatus();
  361. onSearch(state.searchGroup);
  362. }}
  363. list={state.editList}
  364. showDelete={state.isAdd}
  365. onEditAll={(list: any) => {
  366. try {
  367. state.tableList.forEach((table: any) => {
  368. const item = list.find((item: any) => item.id === table.id);
  369. if (item) {
  370. table.openFlag = item.openFlag;
  371. table.title = item.name;
  372. table.instrumentIds = item.instrumentIds;
  373. table.content = item.content;
  374. table.coverImg = item.coverImg;
  375. if (!state.editOverIds.includes(table.id)) {
  376. state.editOverIds.push(table.id);
  377. }
  378. }
  379. });
  380. state.uploadStatus = false;
  381. } catch (e: any) {
  382. console.log(e);
  383. }
  384. }}
  385. />
  386. </NModal>
  387. <NModal
  388. v-model:show={state.saveStatus}
  389. preset="card"
  390. showIcon={false}
  391. class={['modalTitle background', styles.attendClassSaveModal]}
  392. title={'上传资源'}
  393. blockScroll={false}>
  394. <SaveModal
  395. onClose={() => (state.saveStatus = false)}
  396. onConfrim={(val: any) => {
  397. const list = val || [];
  398. const temp: any = [];
  399. list.forEach((item: any) => {
  400. temp.push({
  401. instrumentIds: null,
  402. openFlag: false,
  403. coverImg: item.coverImg,
  404. title: item.name || '',
  405. type: formatUrlType(item.content),
  406. enableFlag: 1,
  407. content: item.content,
  408. id: null
  409. });
  410. });
  411. state.editList = [...temp];
  412. state.uploadStatus = true;
  413. state.isAdd = true;
  414. state.editStatus = false;
  415. }}
  416. />
  417. </NModal>
  418. {showGuide.value ? <MyResourcesGuide></MyResourcesGuide> : null}
  419. <NModal
  420. v-model:show={state.removeVisiable}
  421. preset="card"
  422. class={['modalTitle', styles.removeVisiable]}
  423. title={'提示'}>
  424. <div class={styles.studentRemove}>
  425. <p>{state.removeContent}</p>
  426. <NSpace class={styles.btnGroupModal} justify="center">
  427. <NButton
  428. round
  429. type="primary"
  430. onClick={() => {
  431. if (state.type === 'remove') {
  432. onRemove();
  433. } else {
  434. state.tableList.forEach((item: any) => {
  435. if (state.editIds.includes(item.id)) {
  436. item.delFlag = 1;
  437. if (!state.editOverIds.includes(item.id)) {
  438. state.editOverIds.push(item.id);
  439. }
  440. }
  441. });
  442. }
  443. state.removeVisiable = false;
  444. }}>
  445. 确定
  446. </NButton>
  447. <NButton round onClick={() => (state.removeVisiable = false)}>
  448. 取消
  449. </NButton>
  450. </NSpace>
  451. </div>
  452. </NModal>
  453. </>
  454. );
  455. }
  456. });