Refactor the backend

This commit is contained in:
jiangrui
2025-03-11 00:06:10 +08:00
parent a78ea7e5bd
commit 615149c83f
22 changed files with 338 additions and 84 deletions

View File

@@ -1,17 +1,22 @@
import { Request, Response } from "express";
import { ApiResponse } from "../core/ApiResponse";
interface ApiResponseData<T> {
data?: T;
message?: string;
}
export abstract class BaseController {
protected async handleRequest<T>(
req: Request,
res: Response,
action: () => Promise<T>
action: () => Promise<ApiResponseData<T>>
): Promise<void> {
try {
const result = await action();
res.json(ApiResponse.success(result));
} catch (error: any) {
res.status(500).json(ApiResponse.error(error?.message || "未知错误"));
res.json(ApiResponse.success(result.data, result.message));
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : "未知错误";
res.status(200).json(ApiResponse.error(errorMessage));
}
}
}

View File

@@ -1,24 +1,9 @@
import { Request, Response } from "express";
import { Cloud115Service } from "../services/Cloud115Service";
import { sendSuccess, sendError } from "../utils/response";
import UserSetting from "../models/UserSetting";
import { BaseController } from "./BaseController";
import { injectable, inject } from "inversify";
import { TYPES } from "../core/types";
const cloud115 = new Cloud115Service();
const setCookie = async (req: Request): Promise<void> => {
const userId = req.user?.userId;
const userSetting = await UserSetting.findOne({
where: { userId },
});
if (userSetting && userSetting.dataValues.cloud115Cookie) {
cloud115.setCookie(userSetting.dataValues.cloud115Cookie);
} else {
throw new Error("请先设置115网盘cookie");
}
};
@injectable()
export class Cloud115Controller extends BaseController {
constructor(@inject(TYPES.Cloud115Service) private cloud115Service: Cloud115Service) {
@@ -34,31 +19,23 @@ export class Cloud115Controller extends BaseController {
}
async getFolderList(req: Request, res: Response): Promise<void> {
try {
await this.handleRequest(req, res, async () => {
const { parentCid } = req.query;
await setCookie(req);
const result = await cloud115.getFolderList(parentCid as string);
sendSuccess(res, result);
} catch (error) {
sendError(res, { message: (error as Error).message || "获取目录列表失败" });
}
await this.cloud115Service.setCookie(req);
return await this.cloud115Service.getFolderList(parentCid as string);
});
}
async saveFile(req: Request, res: Response): Promise<void> {
try {
await this.handleRequest(req, res, async () => {
const { shareCode, receiveCode, fileId, folderId } = req.body;
await setCookie(req);
const result = await cloud115.saveSharedFile({
await this.cloud115Service.setCookie(req);
return await this.cloud115Service.saveSharedFile({
shareCode,
receiveCode,
fileId,
cid: folderId,
});
sendSuccess(res, result);
} catch (error) {
sendError(res, { message: (error as Error).message || "保存文件失败" });
}
});
}
}
export const Cloud115ServiceInstance = cloud115;

View File

@@ -3,7 +3,6 @@ import { injectable, inject } from "inversify";
import { TYPES } from "../core/types";
import { QuarkService } from "../services/QuarkService";
import { BaseController } from "./BaseController";
import { sendSuccess } from "../utils/response";
@injectable()
export class QuarkController extends BaseController {
@@ -15,11 +14,7 @@ export class QuarkController extends BaseController {
await this.handleRequest(req, res, async () => {
const { shareCode, receiveCode } = req.query;
await this.quarkService.setCookie(req);
const result = await this.quarkService.getShareInfo(
shareCode as string,
receiveCode as string
);
sendSuccess(res, result);
return await this.quarkService.getShareInfo(shareCode as string, receiveCode as string);
});
}
@@ -27,16 +22,14 @@ export class QuarkController extends BaseController {
await this.handleRequest(req, res, async () => {
const { parentCid } = req.query;
await this.quarkService.setCookie(req);
const result = await this.quarkService.getFolderList(parentCid as string);
sendSuccess(res, result);
return await this.quarkService.getFolderList(parentCid as string);
});
}
async saveFile(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
await this.quarkService.setCookie(req);
const result = await this.quarkService.saveSharedFile(req.body);
sendSuccess(res, result);
return await this.quarkService.saveSharedFile(req.body);
});
}
}

View File

@@ -3,7 +3,6 @@ import { injectable, inject } from "inversify";
import { TYPES } from "../core/types";
import { Searcher } from "../services/Searcher";
import { BaseController } from "./BaseController";
import { sendSuccess } from "../utils/response";
@injectable()
export class ResourceController extends BaseController {
@@ -14,12 +13,11 @@ export class ResourceController extends BaseController {
async search(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
const { keyword, channelId = "", lastMessageId = "" } = req.query;
const result = await this.searcher.searchAll(
return await this.searcher.searchAll(
keyword as string,
channelId as string,
lastMessageId as string
);
sendSuccess(res, result);
});
}
}

View File

@@ -12,7 +12,7 @@ export class SettingController extends BaseController {
async get(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
const userId = Number(req.user?.userId);
const userId = req.user?.userId;
const role = Number(req.user?.role);
return await this.settingService.getSettings(userId, role);
});
@@ -20,7 +20,7 @@ export class SettingController extends BaseController {
async save(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
const userId = Number(req.user?.userId);
const userId = req.user?.userId;
const role = Number(req.user?.role);
return await this.settingService.saveSettings(userId, role, req.body);
});

View File

@@ -12,8 +12,8 @@ export class UserController extends BaseController {
async register(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
const { username, password, code } = req.body;
return await this.userService.register(username, password, code);
const { username, password, registerCode } = req.body;
return await this.userService.register(username, password, registerCode);
});
}