调整并优化新闻批量下载

This commit is contained in:
LittleBoy 2024-12-15 16:37:13 +08:00
parent 2525358eb9
commit b07f336bd5
4 changed files with 76 additions and 27 deletions

View File

@ -40,7 +40,6 @@ export default function ArticleEditModal(props: Props) {
}).finally(() => {
setState({loading: false})
});
props.onClose?.()
}
useEffect(() => {
if (props.id) {

View File

@ -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)
})
});
}

View File

@ -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<NewsInfo[]>((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 `<html>
<head>
<title>${news.title}</title>
<meta charset="UTF-8">
</head>
<body>
<div style="max-width: 80%;width:1000px;margin:30px auto;">
<h1>${news.title}</h1>
<div style="margin: 5px 0;font-size: 13px;">
<span style="color: #766DF4">${news.media_name}</span>
<span style="color: #999;margin-left: 10px;">${news.publish_time}</span>
</div>
<div>${news.content}</div>
</div>
</body>
</html>`
}
/**
* 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`, `<html>
<head>
<title>${id}</title>
</head>
<body>
<div style="max-width: 90%;width:1000px;margin:30px auto;">
<h1>title ${id}</h1>
<p>content ${id}</p>
</div>
</body>
</html>`)
})
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 (
<Button loading={loading} onClick={() => onDownloadClick(props.ids)}></Button>

View File

@ -21,7 +21,7 @@ export function getById(id: Id) {
return post<ArticleDetail>({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,