diff --git a/src/components/article/edit-modal.tsx b/src/components/article/edit-modal.tsx index c658da7..4896d71 100644 --- a/src/components/article/edit-modal.tsx +++ b/src/components/article/edit-modal.tsx @@ -40,7 +40,6 @@ export default function ArticleEditModal(props: Props) { }).finally(() => { setState({loading: false}) }); - props.onClose?.() } useEffect(() => { if (props.id) { diff --git a/src/pages/live/index.tsx b/src/pages/live/index.tsx index 3b1df0c..ee20e2a 100644 --- a/src/pages/live/index.tsx +++ b/src/pages/live/index.tsx @@ -33,14 +33,14 @@ export default function LiveIndex() { const scrollDistance = rect.top - containerRect.top // 设置滚动高度 container.scrollTo({ - top: index == 0 ? 0 : container.scrollTop + scrollDistance - 10, + top: index == 0 ? 0 : container.scrollTop + scrollDistance - 10, behavior: 'smooth' }) } } - const activeToNext = () => { - const endToFirst = state.activeIndex >= videoData.length - 1 - const activeIndex = endToFirst ? 0 : state.activeIndex + 1 + const activeToNext = (index?: number) => { + const endToFirst = index != undefined && index > -1 ? false : state.activeIndex >= videoData.length - 1 + const activeIndex = index != undefined && index > -1 ? index : (endToFirst ? 0 : state.activeIndex + 1) setState(() => { return { activeIndex @@ -56,7 +56,7 @@ export default function LiveIndex() { const list = videoList || videoData || [] playState().then(ret => { setState({ - activeIndex: 0 //list.findIndex(v => v.id === ret.id) + activeIndex: list.findIndex(v => v.id === ret.id) }) }); } diff --git a/src/pages/news/components/button-news-download.tsx b/src/pages/news/components/button-news-download.tsx index 72f7d87..bbfa8ac 100644 --- a/src/pages/news/components/button-news-download.tsx +++ b/src/pages/news/components/button-news-download.tsx @@ -2,36 +2,86 @@ import {Button} from "antd"; import JSZip from "jszip" import {saveAs} from "file-saver"; import {useState} from "react"; + +import {getById} from "@/service/api/news.ts"; + import {showToast} from "@/components/message.ts"; +/** + * 批量获取新闻内容 + * @param ids + */ +function getAllNewsContent(ids: Id[]) { + return new Promise((resolve, reject) => { + const request = ids.map((id) => getById(id)) + Promise.all(request).then(res => { + resolve(res) + }).catch(err => { + reject(err) + }) + }) +} + +/** + * 获取新闻html + * @param news + */ +function getNewsHtml(news: NewsInfo) { + return ` + +${news.title} + + + +
+

${news.title}

+
+ ${news.media_name} + ${news.publish_time} +
+
${news.content}
+
+ +` +} + +/** + * 将新闻数据包装成html并打包下载 + * @param list + */ +async function downloadAsZip(list: NewsInfo[]) { + const zip = new JSZip(); + + list.forEach(news => { + zip.file(`${news.title}.html`, getNewsHtml(news)) + }) + const content = await zip.generateAsync({type: "blob"}); + saveAs(content, "news.zip"); + // .then(function (content) { + // + // }).finally(() => { + // setLoading(false) + // }); +} + export default function ButtonNewsDownload(props: { ids: Id[] }) { const [loading, setLoading] = useState(false) - const onDownloadClick = (ids: Id[]) => { + const onDownloadClick = async (ids: Id[]) => { if (props.ids.length === 0) { - showToast('请选择要推送的新闻', 'warning') + showToast('请选择要下载的新闻', 'warning') return } setLoading(true) - const zip = new JSZip(); - ids.forEach(id => { - zip.file(`${id}.html`, ` - -${id} - - -
-

title ${id}

-

content ${id}

-
- -`) - }) - zip.generateAsync({type: "blob"}).then(function (content) { - saveAs(content, "news.zip"); - }).finally(() => { + try { + const list = await getAllNewsContent(ids) + await downloadAsZip(list) + } catch (e) { + showToast('下载新闻失败,请重试!', 'error') + } finally { setLoading(false) - }); + } + } return ( diff --git a/src/service/api/article.ts b/src/service/api/article.ts index cf3b9ef..3baff95 100644 --- a/src/service/api/article.ts +++ b/src/service/api/article.ts @@ -21,7 +21,7 @@ export function getById(id: Id) { return post({url: '/article/detail/' + id}) } -export function save(title: string, content_group: BlockContent[][], id: number) { +export function save(title: string, content_group: BlockContent[][], id?: number) { return post<{ content: string }>(id && id > 0 ? '/article/modify' : '/article/create/new', { title, content_group,