|
|
@@ -3,6 +3,29 @@
|
|
|
* 统一管理后端 API 调用
|
|
|
*/
|
|
|
|
|
|
+/**
|
|
|
+ * 获取 API Key(从环境变量读取)
|
|
|
+ */
|
|
|
+const getApiKey = (): string => {
|
|
|
+ return import.meta.env.VITE_API_CLIENT_KEY || '';
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取统一的请求头(包含 API Key)
|
|
|
+ */
|
|
|
+const getApiHeaders = (): Record<string, string> => {
|
|
|
+ const headers: Record<string, string> = {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ };
|
|
|
+
|
|
|
+ const apiKey = getApiKey();
|
|
|
+ if (apiKey) {
|
|
|
+ headers['X-Client-Key'] = apiKey;
|
|
|
+ }
|
|
|
+
|
|
|
+ return headers;
|
|
|
+};
|
|
|
+
|
|
|
/**
|
|
|
* 获取后端 API 基础 URL
|
|
|
*/
|
|
|
@@ -59,9 +82,7 @@ export const createUser = async (username: string = ''): Promise<CreateUserRespo
|
|
|
try {
|
|
|
const response = await fetch(url, {
|
|
|
method: 'POST',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify({ username }),
|
|
|
});
|
|
|
|
|
|
@@ -128,9 +149,7 @@ export const getUserInfo = async (user_id: string): Promise<GetUserResponse> =>
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/users/${user_id}`, {
|
|
|
method: 'GET',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
});
|
|
|
|
|
|
if (!response.ok) {
|
|
|
@@ -163,9 +182,7 @@ export const updateUsername = async (user_id: string, username: string): Promise
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/users/${user_id}/username`, {
|
|
|
method: 'PUT',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify({ username }),
|
|
|
});
|
|
|
|
|
|
@@ -191,9 +208,7 @@ export const updateBackgroundImageId = async (
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/users/${user_id}/background-image-id`, {
|
|
|
method: 'PUT',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify({ background_image_id }),
|
|
|
});
|
|
|
|
|
|
@@ -216,9 +231,7 @@ export const updateBotName = async (user_id: string, bot_name: string): Promise<
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/users/${user_id}/bot-name`, {
|
|
|
method: 'PUT',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify({ bot_name }),
|
|
|
});
|
|
|
|
|
|
@@ -260,9 +273,7 @@ export const getUserUIState = async (user_id: string): Promise<GetUserUIStateRes
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/users/${user_id}/ui-state`, {
|
|
|
method: 'GET',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
});
|
|
|
|
|
|
if (!response.ok) {
|
|
|
@@ -288,9 +299,7 @@ export const updateUserUIState = async (
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/users/${user_id}/ui-state`, {
|
|
|
method: 'PUT',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify(payload),
|
|
|
});
|
|
|
|
|
|
@@ -335,9 +344,7 @@ export const registerWithPhone = async (
|
|
|
}
|
|
|
const response = await fetch(`${apiBaseUrl}/api/auth/register/phone`, {
|
|
|
method: 'POST',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify(body),
|
|
|
});
|
|
|
|
|
|
@@ -370,9 +377,7 @@ export const registerWithEmail = async (email: string, username: string = '', us
|
|
|
}
|
|
|
const response = await fetch(`${apiBaseUrl}/api/auth/register/email`, {
|
|
|
method: 'POST',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify(body),
|
|
|
});
|
|
|
|
|
|
@@ -401,9 +406,7 @@ export const loginWithWechat = async (openid: string, username: string = ''): Pr
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/auth/login/wechat`, {
|
|
|
method: 'POST',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify({ openid, username }),
|
|
|
});
|
|
|
|
|
|
@@ -432,9 +435,7 @@ export const loginWithQQ = async (openid: string, username: string = ''): Promis
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/auth/login/qq`, {
|
|
|
method: 'POST',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify({ openid, username }),
|
|
|
});
|
|
|
|
|
|
@@ -464,9 +465,7 @@ export const loginWithPhone = async (phone: string, area_code?: string): Promise
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/auth/login/phone`, {
|
|
|
method: 'POST',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify({ phone, area_code }),
|
|
|
});
|
|
|
|
|
|
@@ -495,9 +494,7 @@ export const loginWithEmail = async (email: string): Promise<LoginEmailResponse>
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/auth/login/email`, {
|
|
|
method: 'POST',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify({ email }),
|
|
|
});
|
|
|
|
|
|
@@ -529,9 +526,7 @@ export const sendCode = async (request: SendCodeRequest): Promise<SendCodeRespon
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/auth/send-code`, {
|
|
|
method: 'POST',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify(request),
|
|
|
});
|
|
|
|
|
|
@@ -563,9 +558,7 @@ export const verifyCode = async (request: VerifyCodeRequest): Promise<VerifyCode
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/auth/verify-code`, {
|
|
|
method: 'POST',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
body: JSON.stringify(request),
|
|
|
});
|
|
|
|
|
|
@@ -619,9 +612,7 @@ export const getAvatarGifs = async (): Promise<AvatarGifConfigResponse> => {
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/avatar-gifs`, {
|
|
|
method: 'GET',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
});
|
|
|
|
|
|
if (!response.ok) {
|
|
|
@@ -651,9 +642,7 @@ export const getShareImages = async (): Promise<GetShareImagesResponse> => {
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/share-images`, {
|
|
|
method: 'GET',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
});
|
|
|
|
|
|
if (!response.ok) {
|
|
|
@@ -681,9 +670,7 @@ export const claimShareReward = async (user_id: string): Promise<ClaimShareRewar
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/users/${user_id}/claim-share-reward`, {
|
|
|
method: 'POST',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
});
|
|
|
|
|
|
if (!response.ok) {
|
|
|
@@ -733,9 +720,7 @@ export const getBackgroundImages = async (): Promise<GetBackgroundImagesResponse
|
|
|
const apiBaseUrl = getApiBaseUrl();
|
|
|
const response = await fetch(`${apiBaseUrl}/api/background-images`, {
|
|
|
method: 'GET',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
+ headers: getApiHeaders(),
|
|
|
});
|
|
|
|
|
|
if (!response.ok) {
|