import vuex from "vuex" import axios from "axios" import {ElMessage} from "element-plus" import qs from "qs" const BaseUrl = "http://downloader.lionwebsite.xyz/" const GalleryManageUrl = BaseUrl + "GalleryManage" const actions = { updateGalleryTasks(context, type){ axios.get(GalleryManageUrl, { params:{ type, AuthCode: state.AuthCode } }).then((res) => { if(res.data.result === "success") { let tasks = JSON.parse(res.data.data) if (type === "all" && state.galleryRefreshTimer === 0) { //判断是否有未下载完成的本子以及定时更新是否开启 for (let i = tasks.length - 1; i > tasks.length - 11; i--) //从后往前遍历十个本子,查看是否有未下载完成的本子 if (tasks[i].status !== "下载完成") { state.galleryRefreshTimer = setInterval(() => { context.dispatch("updateGalleryTasks", "undone").then() }, 20000) break } } context.commit("_updateGalleryTasks", {tasks, type}) } else if(type === 'undone') { context.dispatch("updateGalleryTasks", "all").then() clearInterval(state.galleryRefreshTimer) state.galleryRefreshTimer = 0 } }) }, 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.galleryRefreshTimer === 0) state.galleryRefreshTimer = setInterval(() => { context.dispatch("updateGalleryTasks", "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("查询失败") }) }, queryOnlineLinks(context, gid){ // 如果本地有缓存,则直接返回,没有再请求 if(context.state.onlineLinks[gid] !== undefined){ context.commit("_setOnlineLinks", gid) } else { axios.get(GalleryManageUrl + "/onlineLinks", { params: { AuthCode: state.AuthCode, gid, } }).then((res) => { if (res.data.result === "success") context.commit("_addAndSetOnlineLinks", {gid, links: res.data.data}) else ElMessage(res.data.data) }) } }, updateGallery(context, link){ axios.post(GalleryManageUrl + "/update", qs.stringify({AuthCode: state.AuthCode, link})) .then((res) => { if(res.data.result === 'success' && state.galleryRefreshTimer === 0){ setTimeout(() => { context.dispatch("updateGalleryTasks", "all").then() }, 5000) state.galleryRefreshTimer = 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'){ context.commit("_authed", {AuthCode, ...JSON.parse(res.data.data)}) //初始化 context.dispatch("loadWeekUsedAmount").then() context.dispatch("updateGalleryTasks", "all").then(() => confirmCurrentTask(context.state)) //获取图片合适长度 let screenWidth = window.screen.width * 0.9 - 40 let width let i for(i=3; i>0; i--) if(screenWidth / i <= 350 || screenWidth / i >= 100){ width = screenWidth / i break } width = width - (width % 10) state.imageWidth = width + 'px' state.imagePadding = ((screenWidth - i * width) / i) + 'px' } else context.commit("_unAuthed") }) }, 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) }) }, updateGalleryTag(context, data){ axios.post(GalleryManageUrl + "/tag?" + qs.stringify(data)).then((res) => { ElMessage(res.data.data) if(res.data.result === 'success') context.commit("_updateGalleryTag", 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) }) }, 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){ let tasks = state.totalGalleryTask for(let i=0; i< tasks.length; i++){ if(!tasks[i].isCollect && tasks[i].gid === gid){ tasks[i].isCollect = true state.collectGallery.push(tasks[i]) } } }, _disCollectGallery(state, gid){ let index for(let i=0; i < state.collectGallery.length; i++){ if(state.collectGallery[i].gid === gid){ index = i break } } state.collectGallery[index].isCollect = false state.collectGallery.splice(index, 1) }, _updateGalleryTag(state, data){ for(let i=0; i < state.totalGalleryTask.length; i++){ if(state.totalGalleryTask[i].gid === data.gid){ state.totalGalleryTask[i].tag = data.tag break } } }, _updateGalleryTasks(state, data){ let {tasks, type} = data if(type === 'all') { state.totalGalleryTask.splice(0) state.collectGallery.slice(0) state.downloadGallery.splice(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 tempLink = task.link.replace("element-plus.org", "exhentai.org").replace("element.org", "e-hentai.org") task.download = GalleryManageUrl + "/file/" + encodeURI(task.name) + "?link=" + tempLink + "&AuthCode=" + state.AuthCode break; } //处理时间戳 task.createTimeDisplay = new Date(task.createTime * 1000).toLocaleString("zh") //处理标签 if (!'tag' in task) { task.tag = "" } //处理是否收藏 if('collector' in task){ let collector = task.collector.split(",") delete task.collector for(let i=0; i { preDeleteIndex = -1 if(task.status !== "下载完成") for(let i=0; i < tasks.length; i++) if(tasks[i].name === task.name) { preDeleteIndex = i task.status = tasks[i].status task.proceeding = tasks[i].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("加载完成") } }, _changePage(state, targetPage){ state.page = targetPage }, _authed(state, data){ state.AuthCode = data.AuthCode state.userId = data.userId state.username = data.username state.isAuth = true ElMessage("验证成功,加载中") }, _unAuthed(state){ state.isAuth = false state.AuthCode = "" state.userId = -1 state.username = "" ElMessage("授权码错误") localStorage.removeItem("auth") }, _searchLocalByLink(state, link){ let tasks = state.currentTasks let i = 0 let gid = null let name = null gid = link.split("/")[4] if(gid === null) for (i = 0; i < tasks.length; i++) { if (tasks[i].link === link) { state.page = Math.floor(i / state.length) + 1 name = tasks[i].name break } } else for (i = 0; i < tasks.length; i++) if (tasks[i].gid === gid) { state.page = Math.floor(i / state.length) + 1 name = state.sortType === "shortName" ? tasks[i].shortName: tasks[i].name break } if(!name) ElMessage("未找到此任务") else ElMessage("已跳转到该任务所在页数,任务名:" + name) }, _searchLocalByKeyword(state, keyword){ state.searchTask.splice(0) if(keyword.trim() !== '') { state.page = 1 let tasks = state.currentTasks tasks.forEach((task) => { if (task.name.includes(keyword)) state.searchTask.push(task) }) if (state.searchTask.length === 0) ElMessage("未找到该关键字的任务") else state.currentTasks = state.searchTask } }, _searchLocalByTag(state, tags) { state.searchTask.splice(0) state.page = 1 let tagAmount = tags.length let hitAmount let tasks = state.currentTasks if (tags[0].trim() !== '') { tasks.forEach((task) => { if('tag' in 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的任务") else state.currentTasks = state.searchTask } }, _deleteGallery(state, gid){ let tasks = [state.totalGalleryTask, state.downloadGallery, state.collectGallery] deleteTask(tasks, 'gid', gid) }, _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.downloadGallery.push(state.chosenGallery) } state.chosenGallery = data.gallery }, _setCategory(state, category){ state.category = category confirmCurrentTask(state) sortTasks(state) }, _setSortType(state, sortType){ state.sortType = sortType sortTasks(state) }, _setShowNameType(state, type){ if(type === "shortName") state.length = state.shortLength else state.length = state.defaultLength }, _addAndSetOnlineLinks(state, data){ let links = JSON.parse(data.links) state.onlineLinks[data.gid] = [] state.currentLinks.splice(0) state.currentGid = data.gid links.forEach((link) => { state.onlineLinks[data.gid].push('http://downloader.lionwebsite.xyz/GalleryManage/onlineImage/' + link + '?gid=' + state.currentGid + '&AuthCode=' + state.AuthCode) state.currentLinks.push('http://downloader.lionwebsite.xyz/GalleryManage/onlineImage/' + link + '?gid=' + state.currentGid + '&AuthCode=' + state.AuthCode) }) state.isOnlineReading = true }, _setOnlineLinks(state, gid){ state.currentLinks.splice(0) state.onlineLinks[gid].forEach((link) => { state.currentLinks.push(link) }) state.currentGid = gid state.isOnlineReading = true }, _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: [], //收藏的本子 downloadGallery: [], //下载的本子 onlineLinks: {}, //在线本子链接 isOnlineReading: false, //是否在线看 currentGid: "", //当前GID currentLinks: [], //当前本子链接 imageWidth: "", //图片宽度 imagePadding: "", //图片padding page: 1, //当前页数 length: 10, //每页能有多少个链接 defaultLength: 7, //默认个数 shortLength: 10, //简洁个数 userId: -1, //用户id username: "", //用户名 isAuth: false, //是否授权 AuthCode: '', //授权码 loadComplete: false, //是否加载完成 galleryRefreshTimer: 0, //本子更新计时器id isInclude: false, //是否搜索到任务 searchTask: [], //搜索到的任务 category: 'myDownload', //分类 sortType:'shortName', //排序类型 currentTasks: [], //当前任务 weekUsed: {}, //每周用量 } const getters = { currentTasks(state){ return state.currentTasks.slice((state.page-1)*state.length, state.page*state.length) }, min(){ return 1 }, max(state){ let max = 0 let tasks = state.currentTasks if(!tasks) return 1 max = Math.floor(tasks.length/state.length) if(tasks.length % state.length !== 0 || max === 0) max += 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) temp = name.replace(temp, "") if(temp.trim() === ""){ name = name.replace("[", "").replace("]", "") break } else name = temp } while (name.includes("(") && name.includes(")")) { let start = name.indexOf("(") let end = name.indexOf(")") + 1 let temp = name.substring(start, end) temp = name.replace(temp, "") if(temp.trim() === ""){ name = name.replace("(", "").replace(")", "") break } else name = temp } return name.trim() } else { return name } } function confirmCurrentTask(state){ switch (state.category){ case 'myDownload': state.currentTasks = state.downloadGallery break case 'myCollect': state.currentTasks = state.collectGallery break case 'total': state.currentTasks = state.totalGalleryTask break } } function sortTasks(state){ switch (state.sortType) { case "name": state.currentTasks = state.currentTasks.sort((before, after) => { return before.name > after.name ? 1: -1 }) break case "shortName": state.currentTasks = state.currentTasks.sort((before, after) => { return before.shortName > after.shortName ? 1: -1 }) break case "createTime": state.currentTasks = state.currentTasks.sort((before, after) => { return before.createTime - after.createTime }) } } function deleteTask(tasks, key, value){ for(let j=0; j < tasks.length; j++) for(let i=0; i