|
|
@@ -14,6 +14,7 @@ import { getSession } from "@/lib/auth";
|
|
|
import type { ActionResult } from "./types";
|
|
|
import { ERROR_CODES } from "@/lib/utils/error-messages";
|
|
|
import { formatZodError } from "@/lib/utils/zod-i18n";
|
|
|
+import { getTranslations } from "next-intl/server";
|
|
|
|
|
|
// 获取用户数据
|
|
|
export async function getUsers(): Promise<UserDisplay[]> {
|
|
|
@@ -23,6 +24,9 @@ export async function getUsers(): Promise<UserDisplay[]> {
|
|
|
return [];
|
|
|
}
|
|
|
|
|
|
+ // Get translations for user-related texts
|
|
|
+ const t = await getTranslations("users");
|
|
|
+
|
|
|
// 普通用户只能看到自己的数据
|
|
|
let users;
|
|
|
if (session.user.role === "user") {
|
|
|
@@ -69,7 +73,7 @@ export async function getUsers(): Promise<UserDisplay[]> {
|
|
|
maskedKey: maskKey(key.key),
|
|
|
fullKey: canUserManageKey ? key.key : undefined,
|
|
|
canCopy: canUserManageKey,
|
|
|
- expiresAt: key.expiresAt ? key.expiresAt.toISOString().split("T")[0] : "永不过期",
|
|
|
+ expiresAt: key.expiresAt ? key.expiresAt.toISOString().split("T")[0] : t("neverExpires"),
|
|
|
status: key.isEnabled ? "enabled" : ("disabled" as const),
|
|
|
createdAt: key.createdAt,
|
|
|
createdAtFormatted: key.createdAt.toLocaleString("zh-CN", {
|
|
|
@@ -127,12 +131,15 @@ export async function addUser(data: {
|
|
|
dailyQuota?: number;
|
|
|
}): Promise<ActionResult> {
|
|
|
try {
|
|
|
+ // Get translations for error messages
|
|
|
+ const tError = await getTranslations("errors");
|
|
|
+
|
|
|
// 权限检查:只有管理员可以添加用户
|
|
|
const session = await getSession();
|
|
|
if (!session || session.user.role !== "admin") {
|
|
|
return {
|
|
|
ok: false,
|
|
|
- error: "无权限执行此操作",
|
|
|
+ error: tError("PERMISSION_DENIED"),
|
|
|
errorCode: ERROR_CODES.PERMISSION_DENIED,
|
|
|
};
|
|
|
}
|
|
|
@@ -178,7 +185,8 @@ export async function addUser(data: {
|
|
|
return { ok: true };
|
|
|
} catch (error) {
|
|
|
logger.error("添加用户失败:", error);
|
|
|
- const message = error instanceof Error ? error.message : "添加用户失败,请稍后重试";
|
|
|
+ const tError = await getTranslations("errors");
|
|
|
+ const message = error instanceof Error ? error.message : tError("CREATE_USER_FAILED");
|
|
|
return {
|
|
|
ok: false,
|
|
|
error: message,
|
|
|
@@ -199,11 +207,14 @@ export async function editUser(
|
|
|
}
|
|
|
): Promise<ActionResult> {
|
|
|
try {
|
|
|
+ // Get translations for error messages
|
|
|
+ const tError = await getTranslations("errors");
|
|
|
+
|
|
|
const session = await getSession();
|
|
|
if (!session) {
|
|
|
return {
|
|
|
ok: false,
|
|
|
- error: "未登录",
|
|
|
+ error: tError("UNAUTHORIZED"),
|
|
|
errorCode: ERROR_CODES.UNAUTHORIZED,
|
|
|
};
|
|
|
}
|
|
|
@@ -239,7 +250,7 @@ export async function editUser(
|
|
|
if (hasSensitiveFields) {
|
|
|
return {
|
|
|
ok: false,
|
|
|
- error: "普通用户不能修改账户限额和供应商分组",
|
|
|
+ error: tError("USER_CANNOT_MODIFY_SENSITIVE_FIELDS"),
|
|
|
errorCode: ERROR_CODES.PERMISSION_DENIED,
|
|
|
};
|
|
|
}
|
|
|
@@ -268,7 +279,7 @@ export async function editUser(
|
|
|
// 普通用户尝试修改他人信息
|
|
|
return {
|
|
|
ok: false,
|
|
|
- error: "无权限执行此操作",
|
|
|
+ error: tError("PERMISSION_DENIED"),
|
|
|
errorCode: ERROR_CODES.PERMISSION_DENIED,
|
|
|
};
|
|
|
}
|
|
|
@@ -277,7 +288,8 @@ export async function editUser(
|
|
|
return { ok: true };
|
|
|
} catch (error) {
|
|
|
logger.error("更新用户失败:", error);
|
|
|
- const message = error instanceof Error ? error.message : "更新用户失败,请稍后重试";
|
|
|
+ const tError = await getTranslations("errors");
|
|
|
+ const message = error instanceof Error ? error.message : tError("UPDATE_USER_FAILED");
|
|
|
return {
|
|
|
ok: false,
|
|
|
error: message,
|
|
|
@@ -289,9 +301,12 @@ export async function editUser(
|
|
|
// 删除用户
|
|
|
export async function removeUser(userId: number): Promise<ActionResult> {
|
|
|
try {
|
|
|
+ // Get translations for error messages
|
|
|
+ const tError = await getTranslations("errors");
|
|
|
+
|
|
|
const session = await getSession();
|
|
|
if (!session || session.user.role !== "admin") {
|
|
|
- return { ok: false, error: "无权限执行此操作" };
|
|
|
+ return { ok: false, error: tError("PERMISSION_DENIED") };
|
|
|
}
|
|
|
|
|
|
await deleteUser(userId);
|
|
|
@@ -299,7 +314,8 @@ export async function removeUser(userId: number): Promise<ActionResult> {
|
|
|
return { ok: true };
|
|
|
} catch (error) {
|
|
|
logger.error("删除用户失败:", error);
|
|
|
- const message = error instanceof Error ? error.message : "删除用户失败,请稍后重试";
|
|
|
+ const tError = await getTranslations("errors");
|
|
|
+ const message = error instanceof Error ? error.message : tError("DELETE_USER_FAILED");
|
|
|
return { ok: false, error: message };
|
|
|
}
|
|
|
}
|
|
|
@@ -314,19 +330,22 @@ export async function getUserLimitUsage(userId: number): Promise<
|
|
|
}>
|
|
|
> {
|
|
|
try {
|
|
|
+ // Get translations for error messages
|
|
|
+ const tError = await getTranslations("errors");
|
|
|
+
|
|
|
const session = await getSession();
|
|
|
if (!session) {
|
|
|
- return { ok: false, error: "未登录" };
|
|
|
+ return { ok: false, error: tError("UNAUTHORIZED") };
|
|
|
}
|
|
|
|
|
|
const user = await findUserById(userId);
|
|
|
if (!user) {
|
|
|
- return { ok: false, error: "用户不存在" };
|
|
|
+ return { ok: false, error: tError("USER_NOT_FOUND") };
|
|
|
}
|
|
|
|
|
|
// 权限检查:用户只能查看自己,管理员可以查看所有人
|
|
|
if (session.user.role !== "admin" && session.user.id !== userId) {
|
|
|
- return { ok: false, error: "无权限执行此操作" };
|
|
|
+ return { ok: false, error: tError("PERMISSION_DENIED") };
|
|
|
}
|
|
|
|
|
|
// 动态导入避免循环依赖
|
|
|
@@ -358,7 +377,8 @@ export async function getUserLimitUsage(userId: number): Promise<
|
|
|
};
|
|
|
} catch (error) {
|
|
|
logger.error("获取用户限额使用情况失败:", error);
|
|
|
- const message = error instanceof Error ? error.message : "获取用户限额使用情况失败";
|
|
|
+ const tError = await getTranslations("errors");
|
|
|
+ const message = error instanceof Error ? error.message : tError("GET_USER_QUOTA_FAILED");
|
|
|
return { ok: false, error: message };
|
|
|
}
|
|
|
}
|