Refactor the backend

This commit is contained in:
jiangrui
2025-03-11 17:42:59 +08:00
parent 615149c83f
commit 37c25a9307
23 changed files with 347 additions and 323 deletions

View File

@@ -43,27 +43,6 @@ declare module 'vue' {
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
SearchBar: typeof import('./src/components/SearchBar.vue')['default']
VanBackTop: typeof import('vant/es')['BackTop']
VanButton: typeof import('vant/es')['Button']
VanCell: typeof import('vant/es')['Cell']
VanCellGroup: typeof import('vant/es')['CellGroup']
VanCheckbox: typeof import('vant/es')['Checkbox']
VanCheckboxGroup: typeof import('vant/es')['CheckboxGroup']
VanEmpty: typeof import('vant/es')['Empty']
VanField: typeof import('vant/es')['Field']
VanForm: typeof import('vant/es')['Form']
VanIcon: typeof import('vant/es')['Icon']
VanImage: typeof import('vant/es')['Image']
VanLoading: typeof import('vant/es')['Loading']
VanOverlay: typeof import('vant/es')['Overlay']
VanPopup: typeof import('vant/es')['Popup']
VanSearch: typeof import('vant/es')['Search']
VanSwitch: typeof import('vant/es')['Switch']
VanTab: typeof import('vant/es')['Tab']
VanTabbar: typeof import('vant/es')['Tabbar']
VanTabbarItem: typeof import('vant/es')['TabbarItem']
VanTabs: typeof import('vant/es')['Tabs']
VanTag: typeof import('vant/es')['Tag']
}
export interface ComponentCustomProperties {
vLoading: typeof import('element-plus/es')['ElLoadingDirective']

View File

@@ -1,10 +1,10 @@
import request from "@/utils/request";
import type { ShareInfoResponse, Folder, Save115FileParams } from "@/types";
import type { ShareInfoResponse, Folder, SaveFileParams, GetShareInfoParams } from "@/types";
export const cloud115Api = {
async getShareInfo(shareCode: string, receiveCode = "") {
async getShareInfo(params: GetShareInfoParams) {
const { data } = await request.get<ShareInfoResponse>("/api/cloud115/share-info", {
params: { shareCode, receiveCode },
params,
});
return data as ShareInfoResponse;
},
@@ -16,7 +16,7 @@ export const cloud115Api = {
return res;
},
async saveFile(params: Save115FileParams) {
async saveFile(params: SaveFileParams) {
const res = await request.post("/api/cloud115/save", params);
return res;
},

View File

@@ -1,10 +1,10 @@
import request from "@/utils/request";
import type { ShareInfoResponse, Folder, SaveQuarkFileParams } from "@/types";
import type { ShareInfoResponse, Folder, SaveFileParams, GetShareInfoParams } from "@/types";
export const quarkApi = {
async getShareInfo(shareCode: string, receiveCode = "") {
async getShareInfo(params: GetShareInfoParams) {
const { data } = await request.get<ShareInfoResponse>("/api/quark/share-info", {
params: { shareCode, receiveCode },
params,
});
return data as ShareInfoResponse;
},
@@ -16,7 +16,7 @@ export const quarkApi = {
return data;
},
async saveFile(params: SaveQuarkFileParams) {
async saveFile(params: SaveFileParams) {
return await request.post("/api/quark/save", params);
},
};

View File

@@ -5,10 +5,11 @@ import { quarkApi } from "@/api/quark";
import type {
Resource,
ShareInfoResponse,
Save115FileParams,
SaveQuarkFileParams,
ShareInfo,
ResourceItem,
GetShareInfoParams,
SaveFileParams,
ShareFileInfoAndFolder,
} from "@/types";
import { ElMessage } from "element-plus";
@@ -24,46 +25,40 @@ const lastResource = (
) as StorageListObject;
// 定义云盘驱动配置类型
interface CloudDriveConfig<
T extends Record<string, string>,
P extends Save115FileParams | SaveQuarkFileParams,
> {
interface CloudDriveConfig {
name: string;
type: string;
regex: RegExp;
api: {
getShareInfo: (parsedCode: T) => Promise<ShareInfoResponse>;
saveFile: (params: P) => Promise<{ code: number; message?: string }>;
getShareInfo: (params: GetShareInfoParams) => Promise<ShareInfoResponse>;
saveFile: (params: SaveFileParams) => Promise<{ code: number; message?: string }>;
};
parseShareCode: (match: RegExpMatchArray) => T;
getSaveParams: (shareInfo: ShareInfoResponse, folderId: string) => P;
parseShareCode: (match: RegExpMatchArray) => GetShareInfoParams;
getSaveParams: (shareInfoAndFolder: ShareFileInfoAndFolder) => SaveFileParams;
}
// 云盘类型配置
export const CLOUD_DRIVES: [
CloudDriveConfig<{ shareCode: string; receiveCode: string }, Save115FileParams>,
CloudDriveConfig<{ shareCode: string }, SaveQuarkFileParams>,
] = [
export const CLOUD_DRIVES: CloudDriveConfig[] = [
{
name: "115网盘",
type: "pan115",
regex: /(?:115|anxia|115cdn)\.com\/s\/([^?]+)(?:\?password=([^&#]+))?/,
api: {
getShareInfo: (parsedCode: { shareCode: string; receiveCode: string }) =>
cloud115Api.getShareInfo(parsedCode.shareCode, parsedCode.receiveCode),
saveFile: async (params: Save115FileParams) => {
return await cloud115Api.saveFile(params as Save115FileParams);
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: (shareInfo: ShareInfoResponse, folderId: string) => ({
shareCode: shareInfo.shareCode || "",
receiveCode: shareInfo.receiveCode || "",
fileId: shareInfo.list[0].fileId,
folderId,
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 || ""),
}),
},
{
@@ -71,23 +66,20 @@ export const CLOUD_DRIVES: [
type: "quark",
regex: /pan\.quark\.cn\/s\/([a-zA-Z0-9]+)/,
api: {
getShareInfo: (parsedCode: { shareCode: string }) =>
quarkApi.getShareInfo(parsedCode.shareCode),
saveFile: async (params: SaveQuarkFileParams) => {
return await quarkApi.saveFile(params as SaveQuarkFileParams);
getShareInfo: (params) => quarkApi.getShareInfo(params),
saveFile: async (params: SaveFileParams) => {
return await quarkApi.saveFile(params);
},
},
parseShareCode: (match: RegExpMatchArray) => ({ shareCode: match[1] }),
getSaveParams: (shareInfo: ShareInfoResponse, folderId: string) => ({
fid_list: shareInfo.list.map((item: { fileId?: string }) => item.fileId || ""),
fid_token_list: shareInfo.list.map(
getSaveParams: (shareInfoAndFolder: ShareFileInfoAndFolder) => ({
fids: shareInfoAndFolder.shareInfo.list.map((item: { fileId?: string }) => item.fileId || ""),
fidTokens: shareInfoAndFolder.shareInfo.list.map(
(item: { fileIdToken?: string }) => item.fileIdToken || ""
),
to_pdir_fid: folderId,
pwd_id: shareInfo.shareCode || "",
stoken: shareInfo.stoken || "",
pdir_fid: "0",
scene: "link",
folderId: shareInfoAndFolder.folderId,
shareCode: shareInfoAndFolder.shareInfo.pwdId || "",
receiveCode: shareInfoAndFolder.shareInfo.stoken || "",
}),
},
];
@@ -189,39 +181,32 @@ export const useResourceStore = defineStore("resource", {
async saveResourceToDrive(
resource: ResourceItem,
folderId: string,
drive:
| CloudDriveConfig<{ shareCode: string; receiveCode: string }, Save115FileParams>
| CloudDriveConfig<{ shareCode: string }, SaveQuarkFileParams>
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);
if (this.is115Drive(drive)) {
const params = drive.getSaveParams(shareInfo, folderId);
const result = await drive.api.saveFile(params);
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);
}
if (result.code === 0) {
ElMessage.success(`${drive.name} 转存成功`);
} else {
const params = drive.getSaveParams(shareInfo, folderId);
const result = await drive.api.saveFile(params);
if (result.code === 0) {
ElMessage.success(`${drive.name} 转存成功`);
} else {
ElMessage.error(result.message);
}
ElMessage.error(result.message);
}
},
@@ -237,18 +222,7 @@ export const useResourceStore = defineStore("resource", {
if (!match) throw new Error("链接解析失败");
const parsedCode = matchedDrive.parseShareCode(match);
let shareInfo = this.is115Drive(matchedDrive)
? await matchedDrive.api.getShareInfo(
parsedCode as { shareCode: string; receiveCode: string }
)
: await matchedDrive.api.getShareInfo(parsedCode as { shareCode: string });
if (Array.isArray(shareInfo)) {
shareInfo = {
list: shareInfo,
};
}
const shareInfo = await matchedDrive.api.getShareInfo(parsedCode);
if (shareInfo?.list?.length) {
this.resources = [
{
@@ -296,30 +270,14 @@ export const useResourceStore = defineStore("resource", {
if (!match) throw new Error("链接解析失败");
const parsedCode = drive.parseShareCode(match);
let shareInfo = {} as ShareInfoResponse;
this.setLoadTree(true);
if (this.is115Drive(drive)) {
shareInfo = await drive.api.getShareInfo(
parsedCode as { shareCode: string; receiveCode: string }
);
} else {
shareInfo = this.is115Drive(drive)
? await drive.api.getShareInfo(parsedCode as { shareCode: string; receiveCode: string })
: await drive.api.getShareInfo(parsedCode as { shareCode: string });
}
let shareInfo = await drive.api.getShareInfo(parsedCode);
this.setLoadTree(false);
if (shareInfo) {
if (Array.isArray(shareInfo)) {
shareInfo = {
list: shareInfo,
...parsedCode,
};
} else {
shareInfo = {
...shareInfo,
...parsedCode,
};
}
shareInfo = {
...shareInfo,
...parsedCode,
};
this.shareInfo = shareInfo;
this.setSelectedResource(this.shareInfo.list.map((x) => ({ ...x, isChecked: true })));
return true;
@@ -334,13 +292,5 @@ export const useResourceStore = defineStore("resource", {
console.error(message, error);
ElMessage.error(error instanceof Error ? error.message : message);
},
is115Drive(
drive:
| CloudDriveConfig<{ shareCode: string; receiveCode: string }, Save115FileParams>
| CloudDriveConfig<{ shareCode: string }, SaveQuarkFileParams>
): drive is CloudDriveConfig<{ shareCode: string; receiveCode: string }, Save115FileParams> {
return drive.type === "pan115";
},
},
});

View File

@@ -33,13 +33,25 @@ export interface ShareInfo {
isChecked?: boolean;
}
export interface ShareInfoItem {
fileId: string;
fileName: string;
fileSize?: number;
fileIdToken?: string;
}
export interface ShareInfoResponse {
list: ShareInfo[];
list: ShareInfoItem[];
fileSize?: number;
pwdId?: string;
stoken?: string;
shareCode?: string;
}
export interface ShareFileInfoAndFolder {
shareInfo: ShareInfoResponse;
folderId: string;
shareCode: string;
receiveCode?: string;
fileSize?: number;
}
export interface Folder {
@@ -49,10 +61,16 @@ export interface Folder {
}
export interface SaveFileParams {
shareCode: string; // 分享code
receiveCode?: string; // 分享文件的密码
folderId: string; // 文件夹id
fids: string[]; // 存储文件id
fidTokens?: string[]; // 存储文件token
}
export interface GetShareInfoParams {
shareCode: string;
receiveCode: string;
fileId: string;
folderId: string;
receiveCode?: string;
}
export interface ApiResponse<T = unknown> {