import vuex from "vuex" import axios from "axios" import {ElMessage} from "element-plus" import qs from "qs" import moment from 'moment' const BaseUrl = "http://downloader.lionwebsite.xyz/" const GalleryManageUrl = BaseUrl + "GalleryManage" const VideoManageUrl = BaseUrl + "VideoManage" const actions = { updateGalleryTasks(context, type){ axios.get(GalleryManageUrl, { params:{ type, AuthCode: state.AuthCode } }).then((res) => { if(res.data.result === "success") context.commit("_updateGalleryTasks", {tasks:JSON.parse(res.data.data), type}) else if(type === 'undone') { context.dispatch("updateGalleryTasks", "all").then() clearInterval(state.refreshTimer) state.refreshTimer = 0 } }) }, updateVideoTasks(context, type){ axios.get(VideoManageUrl, { params:{ type, AuthCode: state.AuthCode } }).then(res => { if(res.data.result === "success") context.commit("_updateVideoTasks", {tasks:JSON.parse(res.data.data), type}) else if(type === 'undone') { clearInterval(state.refreshTimer) state.refreshTimer = 0 context.dispatch("updateVideoTasks", "all").then() } }) }, postGalleryTask(context, data){ axios.post(GalleryManageUrl, qs.stringify({ AuthCode: state.AuthCode, link: data.link, targetResolution: data.targetResolution })).then((res) => { if(res.data.result === "success") { ElMessage("提交成功") context.commit("_setChosenGallery", {gallery: false, resolution:data.targetResolution}) if(state.refreshTimer === 0) state.refreshTimer = setInterval(() => { context.dispatch("updateGalleryTasks", "undone").then() }, 20000) } else{ if(res.data.data) ElMessage(res.data.data) else ElMessage("提交失败") } }) }, postVideoTask(context, data){ axios.post(VideoManageUrl, qs.stringify({ AuthCode: state.AuthCode, link: data.link, targetResolution: data.targetResolution })).then((res) => { if(res.data.result === "success") { ElMessage("提交成功") context.commit("_setChosenVideo", {video: false, resolution: data.targetResolution}) if(state.refreshTimer === 0) state.refreshTimer = setInterval(() => { context.dispatch("updateVideoTasks", "undone").then() }, 20000) } else{ if(res.data.data) ElMessage(res.data.data) else ElMessage("提交失败") } }) }, queryGalleryTask(context, link){ axios.get(GalleryManageUrl, { params:{ param: link, type:'link', AuthCode: state.AuthCode } }).then((res) => { if(res.data.result === 'success') context.commit("_setChosenGallery", {gallery: JSON.parse(res.data.data)}) else ElMessage("查询失败") }) }, queryVideoTask(context, link){ axios.get(VideoManageUrl, { params:{ param: link, type: "link", AuthCode: state.AuthCode } }).then((res) => { if(res.data.result === 'success') context.commit("_setChosenVideo", {video: JSON.parse(res.data.data)}) else ElMessage("查询失败") }) }, updateGallery(context, link){ axios.post(GalleryManageUrl + "/update", qs.stringify({AuthCode: state.AuthCode, link})) .then((res) => { if(res.data.result === 'success' && state.refreshTimer === 0){ setTimeout(() => { context.dispatch("updateGalleryTasks", "all").then() }, 5000) state.refreshTimer = setInterval(() => { context.dispatch("updateGalleryTasks", "undone").then() }, 20000) } ElMessage(res.data.data) }) }, validate(context, AuthCode){ axios.post(BaseUrl + "validate?AuthCode=" + AuthCode).then((res)=>{ if(res.data.result === 'success'){ state.userId = parseInt(res.data.data) context.commit("_authed", AuthCode) context.dispatch("loadMaskDomain").then() context.dispatch("loadWeekUsedAmount").then() context.dispatch("updateGalleryTasks", "all").then() context.dispatch("updateVideoTasks", "all").then() } else context.commit("_unAuthed") }) }, loadMaskDomain(context){ axios.get(BaseUrl + "maskDomain").then((res) => { if(res.data.result === "success") context.commit("_setMaskDomain", JSON.parse(res.data.data)) }) }, loadWeekUsedAmount(context){ axios.get(GalleryManageUrl + "/weekUsedAmount", { params: { AuthCode: state.AuthCode } }).then((res) => { if(res.data.result === "success"){ context.state.weekUsed = JSON.parse(res.data.data) ElMessage("查询用量成功") }else ElMessage("查询用量失败") }) }, collectGallery(context, gid){ axios.post(GalleryManageUrl + "/collect?" +qs.stringify( { gid, id:state.userId })).then((res) => { ElMessage(res.data.data) if(res.data.result === 'success') context.commit("_collectGallery", gid) }) }, disCollectGallery(context, gid){ axios.post(GalleryManageUrl + "/disCollect?" + qs.stringify( { gid, id:state.userId })).then((res) => { ElMessage(res.data.data) if(res.data.result === 'success') context.commit("_disCollectGallery", gid) }) }, updateTag(context, data){ axios.post(GalleryManageUrl + "/tag?" + qs.stringify(data)).then((res) => { ElMessage(res.data.data) if(res.data.result === 'success') context.commit("_updateTag", data) }) }, deleteGallery(context, gid){ axios.delete(GalleryManageUrl, { params:{ AuthCode:state.AuthCode, gid }}).then((res) => { if(res.data.result === "success"){ ElMessage("删除成功") context.commit("_deleteGallery", gid) } else ElMessage(res.data.data) }) }, deleteVideo(context, id){ axios.delete(VideoManageUrl, { params:{ AuthCode:state.AuthCode, id } }).then((res) => { if(res.data.result === "success"){ ElMessage("删除成功") context.commit("_deleteVideo", id) } else{ ElMessage(res.data.data) } }) }, alterAuthCode(context, AuthCode){ axios.put(BaseUrl + "AuthCode?" + qs.stringify({'AuthCode': state.AuthCode, 'newAuthCode': AuthCode})) .then((res) => { if(res.data.result === 'success') { ElMessage("修改成功") if(localStorage.getItem("auth") === state.AuthCode) localStorage.setItem("auth", AuthCode) context.state.AuthCode = AuthCode } else ElMessage(res.data.data) }) } } const mutations = { _collectGallery(state, gid){ state.totalGalleryTask.forEach((gallery) => { if(!gallery.isCollect && gallery.gid === gid){ gallery.isCollect = true state.collectGallery.push(gallery) } }) }, _disCollectGallery(state, gid){ state.collectGallery.splice(0) state.totalGalleryTask.forEach((gallery) => { if(gallery.isCollect && gallery.gid === gid) gallery.isCollect = false else if(gallery.isCollect) state.collectGallery.push(gallery) }) }, _updateTag(state, data){ state.totalGalleryTask.forEach((gallery) => { if(gallery.gid === data.gid){ gallery.tag = data.tag } }) }, _updateGalleryTasks(state, data){ let {tasks, type} = data if(type === 'all') { state.totalGalleryTask.splice(0) state.collectGallery.slice(0) tasks.forEach((task) => { //处理名字 task.shortName = getShortname(task.name) //处理进度相关 switch (task.status) { case "已提交": task.progress = "已提交" break; case "下载中": task.progress = (Math.round((task.proceeding / task.pages) * 100)).toString() + "%" break; case "下载完成": task.progress = "下载完成" let tempLink let url = new URL(task.link) state.maskDomain.forEach((mask) => { if (url.host === mask['raw']) tempLink = task.link.replace(mask['raw'], mask['mask']) }) task.download = GalleryManageUrl + "/file/" + encodeURI(task.name) + "?link=" + tempLink + "&AuthCode=" + state.AuthCode break; } //处理时间戳 task.createTimeDisplay = moment(task.createTime * 1000).format("YYYY-MM-DD HH:mm:ss") //处理标签 if (task.tag === undefined) { task.tag = "" } //处理是否收藏 if(task.collector !== undefined){ let collector = task.collector.split(",") task.collector = undefined collector.forEach((id) => { if(parseInt(id) === state.userId) { task.isCollect = true state.collectGallery.push(task) } }) if(task.isCollect === undefined) task.isCollect = false } state.totalGalleryTask.push(task) }) } else { let tempArray = Array.from(state.totalGalleryTask) state.totalGalleryTask.splice(0) let preDeleteIndex tempArray.forEach((task) => { preDeleteIndex = -1 if(task.status !== "下载完成") tasks.forEach((newTask, index) => { if(newTask.name === task.name){ preDeleteIndex = index task.status = newTask.status task.proceeding = newTask.proceeding if(task.proceeding === 0) task.progress = task.status else task.progress = (Math.round((task.proceeding / task.pages) * 100)).toString() + "%" } }) if(preDeleteIndex !== -1) delete tasks[preDeleteIndex] state.totalGalleryTask.push(task) }) } switch (state.sortType) { case "name": state.totalGalleryTask = state.totalGalleryTask.sort((before, after) => { return before.name > after.name ? 1: -1 }) break case "shortName": state.totalGalleryTask = state.totalGalleryTask.sort((before, after) => { return before.shortName > after.shortName ? 1: -1 }) break case "createTime": state.totalGalleryTask = state.totalGalleryTask.sort((before, after) => { return before.createTime - after.createTime }) } if(state.isAuth && !state.loadComplete){ state.loadComplete = true ElMessage("加载完成") } }, _updateVideoTasks(state, data){ let {tasks, type} = data if(type === 'all') { state.totalVideoTask.splice(0) tasks.forEach((task) => { task.progress = task.status if (task.status === "下载完成") { let tempLink let url = new URL(task.link) state.maskDomain.forEach((mask) => { if (url.host === mask['raw']) tempLink = task.link.replace(mask['raw'], mask['mask']) }) task.download = VideoManageUrl + "/file/" + encodeURI(task.name) + "?link=" + tempLink + "&AuthCode=" + state.AuthCode task.createTimeDisplay = moment(task.createTime * 1000).format("YYYY-MM-DD HH:mm:ss") } state.totalVideoTask.push(task) }) } else{ let tempArray = Array.from(state.totalVideoTask) state.totalVideoTask.splice(0) let preDeleteIndex tempArray.forEach((task) => { preDeleteIndex = -1 if(task.status !== "下载完成") tasks.forEach((newTask, index) => { if(newTask.name === task.name){ task.progress = task.status preDeleteIndex = index } }) if(preDeleteIndex !== -1) delete tasks[preDeleteIndex] state.totalVideoTask.push(task) }) } state.totalVideoTask = state.totalVideoTask.sort((before, after) => { if(state.sortType === 'name') return before.name > after.name ? 1: -1 else return before.createTime - after.createTime }) }, _changePage(state, targetPage){ state.page = targetPage }, _authed(state, AuthCode){ state.AuthCode = AuthCode state.isAuth = true ElMessage("验证成功,加载中") }, _unAuthed(state){ state.isAuth = false state.AuthCode = "" ElMessage("授权码错误") localStorage.removeItem("auth") }, _searchLocalByLink(state, link){ let tasks let i = 0 let found = false if(state.showType === "gallery") tasks = state.totalGalleryTask else tasks = state.totalVideoTask for (i = 0; i < tasks.length; i++) if (tasks[i].link === link) { state.page = Math.floor(i / state.length) + 1 found = true break } if(!found) ElMessage("未找到此任务") else ElMessage("已跳转到该任务所在页数") }, _searchLocalByKeyword(state, keyword){ state.searchTask.splice(0) if(keyword.trim() !== '') { state.page = 1 let tasks if (state.showType === "video") tasks = state.totalVideoTask else tasks = state.totalGalleryTask tasks.forEach((task) => { if (task.name.includes(keyword)) state.searchTask.push(task) }) if (state.searchTask.length === 0) { ElMessage("未找到该关键字的任务") } } }, _searchLocalByTag(state, tags) { state.searchTask.splice(0) state.page = 1 let tagAmount = tags.length let hitAmount let tasks = state.totalGalleryTask if (tags[0].trim() !== '') { tasks.forEach((task) => { hitAmount = 0 tags.forEach((tag) => { if (task.tag.includes(tag)) hitAmount++ }) if (hitAmount === tagAmount) state.searchTask.push(task) }) if (state.searchTask.length === 0) ElMessage("未找到符合这些tag的任务") } }, _deleteGallery(state, gid){ state.totalGalleryTask.forEach((item, index, arr) => { if(item.gid === gid){ arr.splice(index, 1) } }) }, _deleteVideo(state, id){ state.totalVideoTask.forEach((item, index, arr) => { if(item.id === id) arr.splice(index, 1) }) }, _setChosenGallery(state,data){ if(data.gallery === false) { state.chosenGallery.shortName = getShortname(state.chosenGallery.name) state.chosenGallery.resolution = data.resolution state.chosenGallery.fileSize = "等待下载完成后再查看" state.chosenGallery.createTimeDisplay = "等待下载完成后再查看" state.chosenGallery.progress = "已提交" state.chosenGallery.tag = "" state.totalGalleryTask.push(state.chosenGallery) } state.chosenGallery = data.gallery }, _setChosenVideo(state,data){ if(data.video === false) { state.chosenVideo.resolution = data.resolution state.chosenVideo.fileSize = "下载完成后再查看" state.chosenVideo.duration = "下载完成后再查看" state.chosenVideo.createTimeDisplay = "下载完成后再查看" state.chosenVideo.progress = "已提交" state.totalVideoTask.push(state.chosenVideo) } state.chosenVideo = data.video }, _setShowType(state, showType){ state.showType = showType }, _setSortType(state, sortType){ state.sortType = sortType switch (sortType) { case "name": state.totalGalleryTask = state.totalGalleryTask.sort((before, after) => { return before.name > after.name ? 1: -1 }) state.totalVideoTask = state.totalVideoTask.sort((before, after) => { return before.name > after.name ? 1: -1 }) break case "shortName": state.totalGalleryTask = state.totalGalleryTask.sort((before, after) => { return before.shortName > after.shortName ? 1: -1 }) break case "createTime": state.totalGalleryTask = state.totalGalleryTask.sort((before, after) => { return before.createTime - after.createTime }) state.totalVideoTask = state.totalVideoTask.sort((before, after) => { return before.createTime - after.createTime }) } }, _setShowNameType(state, type){ if(type === "shortName") state.length = state.shortLength else state.length = state.defaultLength }, _setMaskDomain(state, maskDomain){ state.maskDomain = maskDomain }, _changeThumbnailGallery(state, gallery){ state.thumbnailGallery = gallery state.thumbnailGallery.url = GalleryManageUrl + "/thumbnail/" + encodeURIComponent(gallery.name) + ".webp?AuthCode="+state.AuthCode } } const state = { totalGalleryTask:[], //存放本子数据的数组 chosenGallery:false, //准备下载的本子 thumbnailGallery:{}, collectGallery:[], //收藏的本子 totalVideoTask:[], //存放视频数据的数组 chosenVideo:false, //准备下载的视频 page:1, //当前页数 length:10, //每页能有多少个链接 defaultLength:7, //默认个数 shortLength:10, //简洁个数 userId:-1, //用户id isAuth:false, //是否授权 AuthCode:'', //授权码 loadComplete:false, //是否加载完成 refreshTimer:0, //更新计时器id isInclude:false, //是否搜索到任务 searchTask:[], //搜索到的任务 showType:'gallery', //展示类型 sortType:'shortName', //排序类型 weekUsed:{}, //每周用量 maskDomain:[] //伪装域名 } const getters = { galleryTasks(state){ if(state.showType === 'collect') return state.collectGallery.slice((state.page-1)*state.length, state.page*state.length) if(state.searchTask.length !== 0) return state.searchTask.slice((state.page-1)*state.length, state.page*state.length) if(state.totalGalleryTask.length !== 0) return state.totalGalleryTask.slice((state.page-1)*state.length, state.page*state.length) return null }, videoTasks(state){ if(state.searchTask.length !== 0) return state.searchTask.slice((state.page-1)*state.length, state.page*state.length) if(state.totalVideoTask.length !== 0) return state.totalVideoTask.slice((state.page-1)*state.length, state.page*state.length) return null }, min(){ return 1 }, max(state){ let max = 0 let tasks if(state.showType === 'collect') tasks = state.collectGallery else if(state.searchTask.length !== 0) tasks = state.searchTask else if(state.showType === "gallery") if(state.totalGalleryTask.length !== 0) tasks = state.totalGalleryTask else if(state.showType === "video") if(state.totalVideoTask.length) tasks = state.totalVideoTask if(!tasks) return 1 max = Math.floor(tasks.length/state.length) if(tasks.length % state.length !== 0) max += 1 if(max === 0) return 1 return max } } export default new vuex.Store({ actions, mutations, state, getters }) function getShortname(name){ if (name.includes("[")) { let lastIndex = name.lastIndexOf("[") name = name.substring(0, lastIndex) while (name.includes("[") && name.includes("]")) { let start = name.indexOf("[") let end = name.indexOf("]") + 1 let temp = name.substring(start, end) name = name.replace(temp, "") } while (name.includes("(") && name.includes(")")) { let start = name.indexOf("(") let end = name.indexOf(")") + 1 let temp = name.substring(start, end) name = name.replace(temp, "") } return name.trim() } else { return name } }