Spaces:
Sleeping
Sleeping
File size: 10,169 Bytes
f0953a4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
import { defineStore } from "pinia";
import { cloud115Api } from "@/api/cloud115";
import { resourceApi } from "@/api/resource";
import { quarkApi } from "@/api/quark";
import type {
Resource,
ShareInfoResponse,
ShareInfo,
ResourceItem,
GetShareInfoParams,
SaveFileParams,
ShareFileInfoAndFolder,
} from "@/types";
import { ElMessage } from "element-plus";
interface StorageListObject {
list: Resource[];
lastUpdateTime?: string;
}
const lastResource = (
localStorage.getItem("last_resource_list")
? JSON.parse(localStorage.getItem("last_resource_list") as string)
: { list: [] }
) as StorageListObject;
// 定义云盘驱动配置类型
interface CloudDriveConfig {
name: string;
type: string;
regex: RegExp;
api: {
getShareInfo: (params: GetShareInfoParams) => Promise<ShareInfoResponse>;
saveFile: (params: SaveFileParams) => Promise<{ code: number; message?: string }>;
};
parseShareCode: (match: RegExpMatchArray) => GetShareInfoParams;
getSaveParams: (shareInfoAndFolder: ShareFileInfoAndFolder) => SaveFileParams;
}
// 云盘类型配置
export const CLOUD_DRIVES: CloudDriveConfig[] = [
{
name: "115网盘",
type: "pan115",
regex: /(?:115|anxia|115cdn)\.com\/s\/([^?]+)(?:\?password=([^&#]+))?/,
api: {
getShareInfo: (params: GetShareInfoParams) => cloud115Api.getShareInfo(params),
saveFile: async (params: SaveFileParams) => {
return await cloud115Api.saveFile(params);
},
},
parseShareCode: (match: RegExpMatchArray) => ({
shareCode: match[1],
receiveCode: match[2] || "",
}),
getSaveParams: (shareInfoAndFolder: ShareFileInfoAndFolder) => ({
shareCode: shareInfoAndFolder.shareCode || "",
receiveCode: shareInfoAndFolder.receiveCode || "",
fileId: shareInfoAndFolder.shareInfo.list[0].fileId,
folderId: shareInfoAndFolder.folderId,
fids: shareInfoAndFolder.shareInfo.list.map((item: { fileId?: string }) => item.fileId || ""),
}),
},
{
name: "夸克网盘",
type: "quark",
regex: /pan\.quark\.cn\/s\/([a-zA-Z0-9]+)/,
api: {
getShareInfo: (params) => quarkApi.getShareInfo(params),
saveFile: async (params: SaveFileParams) => {
return await quarkApi.saveFile(params);
},
},
parseShareCode: (match: RegExpMatchArray) => ({ shareCode: match[1] }),
getSaveParams: (shareInfoAndFolder: ShareFileInfoAndFolder) => ({
fids: shareInfoAndFolder.shareInfo.list.map((item: { fileId?: string }) => item.fileId || ""),
fidTokens: shareInfoAndFolder.shareInfo.list.map(
(item: { fileIdToken?: string }) => item.fileIdToken || ""
),
folderId: shareInfoAndFolder.folderId,
shareCode: shareInfoAndFolder.shareInfo.pwdId || "",
receiveCode: shareInfoAndFolder.shareInfo.stoken || "",
}),
},
];
export const useResourceStore = defineStore("resource", {
state: () => ({
tagColor: {
baiduPan: "primary",
weiyun: "info",
aliyun: "warning",
pan115: "danger",
quark: "success",
},
keyword: "",
resources: lastResource.list,
lastUpdateTime: lastResource.lastUpdateTime || "",
shareInfo: {} as ShareInfoResponse,
resourceSelect: [] as ShareInfo[],
loading: false,
backupPlan: false,
loadTree: false,
}),
actions: {
setLoadTree(loadTree: boolean) {
this.loadTree = loadTree;
},
// 搜索资源
async searchResources(keyword?: string, isLoadMore = false, channelId?: string): Promise<void> {
this.loading = true;
if (!isLoadMore) this.resources = [];
try {
let lastMessageId = "";
if (isLoadMore) {
const list = this.resources.find((x) => x.id === channelId)?.list || [];
lastMessageId = list[list.length - 1].messageId || "";
if (list[list.length - 1].isLastMessage) {
ElMessage.warning("没有更多了~");
return;
}
if (!lastMessageId) {
ElMessage.error("当次搜索源不支持加载更多");
return;
}
keyword = this.keyword;
}
let { data = [] } = await resourceApi.search(keyword || "", channelId, lastMessageId);
this.keyword = keyword || "";
data = data
.filter((item) => item.list.length > 0)
.map((x) => ({
...x,
list: x.list.map((item) => ({
...item,
isSupportSave: CLOUD_DRIVES.some((drive) => drive.regex.test(item.cloudLinks[0])),
})),
}));
console.log(data);
if (isLoadMore) {
const findedIndex = this.resources.findIndex((item) => item.id === data[0]?.id);
if (findedIndex !== -1) {
this.resources[findedIndex].list.push(...data[0].list);
}
if (data.length === 0) {
const list = this.resources.find((item) => item.id === channelId)?.list;
list && list[list.length - 1] && (list[list.length - 1]!.isLastMessage = true);
ElMessage.warning("没有更多了~");
}
} else {
this.resources = data.map((item, index) => ({ ...item, displayList: index === 0 }));
if (!keyword) {
// 获取当前时间字符串 用于存储到本地
this.lastUpdateTime = new Date().toLocaleString();
localStorage.setItem(
"last_resource_list",
JSON.stringify({ list: this.resources, lastUpdateTime: this.lastUpdateTime })
);
}
if (this.resources.length === 0) {
ElMessage.warning("未搜索到相关资源");
}
}
} catch (error) {
console.log(error);
this.handleError("搜索失败,请重试", null);
} finally {
this.loading = false;
}
},
// 设置选择资源
async setSelectedResource(resourceSelect: ShareInfo[]) {
this.resourceSelect = resourceSelect;
},
// 转存资源
async saveResource(resource: ResourceItem, folderId: string): Promise<void> {
const savePromises: Promise<void>[] = [];
CLOUD_DRIVES.forEach((drive) => {
if (resource.cloudLinks.some((link) => drive.regex.test(link))) {
savePromises.push(this.saveResourceToDrive(resource, folderId, drive));
}
});
await Promise.all(savePromises);
},
// 保存资源到网盘
async saveResourceToDrive(
resource: ResourceItem,
folderId: string,
drive: CloudDriveConfig
): Promise<void> {
const link = resource.cloudLinks.find((link) => drive.regex.test(link));
if (!link) return;
const match = link.match(drive.regex);
if (!match) throw new Error("链接解析失败");
const parsedCode = drive.parseShareCode(match);
const shareInfo = {
...this.shareInfo,
list: this.resourceSelect.filter((x) => x.isChecked),
};
console.log(shareInfo);
const params = drive.getSaveParams({
shareInfo,
...parsedCode,
folderId,
});
const result = await drive.api.saveFile(params);
if (result.code === 0) {
ElMessage.success(`${drive.name} 转存成功`);
} else {
ElMessage.error(result.message);
}
},
// 解析云盘链接
async parsingCloudLink(url: string): Promise<void> {
this.loading = true;
this.resources = [];
try {
const matchedDrive = CLOUD_DRIVES.find((drive) => drive.regex.test(url));
if (!matchedDrive) throw new Error("不支持的网盘链接");
const match = url.match(matchedDrive.regex);
if (!match) throw new Error("链接解析失败");
const parsedCode = matchedDrive.parseShareCode(match);
const shareInfo = await matchedDrive.api.getShareInfo(parsedCode);
if (shareInfo?.list?.length) {
this.resources = [
{
id: "",
channelInfo: {
name: "自定义搜索",
channelLogo: "",
channelId: "",
},
displayList: true,
list: [
{
id: "1",
title: shareInfo.list.map((item) => item.fileName).join(", "),
cloudLinks: [url],
cloudType: matchedDrive.type,
channel: matchedDrive.name,
pubDate: "",
isSupportSave: true,
},
],
},
];
} else {
throw new Error("解析失败,请检查链接是否正确");
}
} catch (error) {
this.handleError("解析失败,请重试", error);
} finally {
this.loading = false;
}
},
// 获取资源列表并选择
async getResourceListAndSelect(resource: ResourceItem): Promise<boolean> {
this.setSelectedResource([]);
const { cloudType } = resource;
const drive = CLOUD_DRIVES.find((x) => x.type === cloudType);
if (!drive) {
return false;
}
const link = resource.cloudLinks.find((link) => drive.regex.test(link));
if (!link) return false;
const match = link.match(drive.regex);
if (!match) throw new Error("链接解析失败");
const parsedCode = drive.parseShareCode(match);
this.setLoadTree(true);
let shareInfo = await drive.api.getShareInfo(parsedCode);
console.log(shareInfo);
this.setLoadTree(false);
if (shareInfo) {
shareInfo = {
...shareInfo,
...parsedCode,
};
this.shareInfo = shareInfo;
this.setSelectedResource(this.shareInfo.list.map((x) => ({ ...x, isChecked: true })));
return true;
} else {
ElMessage.error("获取资源信息失败,请先检查cookie!");
return false;
}
},
// 统一错误处理
handleError(message: string, error: unknown): void {
console.error(message, error);
ElMessage.error(error instanceof Error ? error.message : message);
},
},
});
|