video-flow-b/lib/server-config.ts

201 lines
5.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 服务端配置工具函数
*/
import { baseUrl } from '@/lib/env';
// 注意:这里不使用 @/api/request 中的 post 函数,因为它会将请求发送到远程服务器
// 我们需要直接调用本地的 Next.js API 路由
/**
* 本地API请求函数 - 直接调用Next.js API路由
*/
const localPost = async <T>(url: string, data: any): Promise<T> => {
try {
// 使用统一配置中的 BASE_URL
const isAbsolute = /^https?:\/\//i.test(url);
const normalizedBase = baseUrl.replace(/\/$/, '');
const normalizedPath = url.startsWith('/') ? url : `/${url}`;
const fullUrl = isAbsolute ? url : `${normalizedBase}${normalizedPath}`;
const response = await fetch(fullUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Local API request failed:', error);
throw error;
}
};
/**
* SSO配置接口
*/
export interface SSOConfig {
show: boolean;
provider: string;
displayName: string;
description: string;
}
/**
* 视频修改配置接口
*/
export interface VideoModificationConfig {
show: boolean;
}
/**
* 获取SSO配置
* @returns Promise<SSOConfig | null>
*/
export const getSSOConfig = async (): Promise<SSOConfig | null> => {
try {
console.log('🔍 开始获取SSO配置...');
const res = await localPost<any>(`/api/server-setting/find_by_code`, { code: 'sso_config' });
console.log('📋 SSO API响应:', res);
if (!res || res.code !== 0 || !res.successful || !res.data) {
console.warn('❌ Failed to fetch SSO config:', res);
return null;
}
// 新的数据格式data直接包含id, code, value等字段
const { value } = res.data;
console.log('📝 SSO配置原始value:', value);
console.log('📝 value类型:', typeof value, 'value长度:', value?.length);
if (typeof value !== 'string' || value.length === 0) {
console.warn('❌ Invalid SSO config format:', value);
return null;
}
try {
const config: SSOConfig = JSON.parse(value);
console.log('✅ SSO配置解析成功:', config);
return config;
} catch (parseError) {
console.error('❌ Failed to parse SSO config:', parseError);
console.error('❌ 原始value:', JSON.stringify(value));
return null;
}
} catch (error) {
console.error('❌ Error fetching SSO config:', error);
return null;
}
};
/**
* 检查是否启用Google登录
* @returns Promise<boolean>
*/
export const isGoogleLoginEnabled = async (): Promise<boolean> => {
try {
console.log('🔍 检查Google登录是否启用...');
const config = await getSSOConfig();
console.log('📋 获得的配置:', config);
if (!config) {
console.log('❌ 没有获得配置返回false');
return false;
}
const isEnabled = config?.show === true;
console.log('🔍 配置检查:', {
show: config?.show,
provider: config?.provider,
isEnabled,
finalResult: isEnabled
});
// 简化逻辑只检查show字段因为sso_config专门用于Google登录
return isEnabled;
} catch (error) {
console.error('❌ Error checking Google login status:', error);
return false; // 出错时默认不显示
}
};
/**
* 获取视频修改配置
* @returns Promise<VideoModificationConfig | null>
*/
export const getVideoModificationConfig = async (): Promise<VideoModificationConfig | null> => {
try {
console.log('🔍 开始获取视频修改配置...');
const res = await localPost<any>(`/api/server-setting/find_by_code`, { code: 'video_modification' });
console.log('📋 视频修改配置API响应:', res);
if (!res || res.code !== 0 || !res.successful || !res.data) {
console.warn('❌ Failed to fetch video modification config:', res);
return null;
}
// 新的数据格式data直接包含id, code, value等字段
const { value } = res.data;
console.log('📝 视频修改配置原始value:', value);
console.log('📝 value类型:', typeof value, 'value长度:', value?.length);
if (typeof value !== 'string' || value.length === 0) {
console.warn('❌ Invalid video modification config format:', value);
return null;
}
try {
const config: VideoModificationConfig = JSON.parse(value);
console.log('✅ 视频修改配置解析成功:', config);
return config;
} catch (parseError) {
console.error('❌ Failed to parse video modification config:', parseError);
console.error('❌ 原始value:', JSON.stringify(value));
return null;
}
} catch (error) {
console.error('❌ Error fetching video modification config:', error);
return null;
}
};
/**
* 检查是否启用视频修改功能
* @returns Promise<boolean>
*/
export const isVideoModificationEnabled = async (): Promise<boolean> => {
try {
console.log('🔍 检查视频修改功能是否启用...');
const config = await getVideoModificationConfig();
console.log('📋 获得的视频修改配置:', config);
if (!config) {
console.log('❌ 没有获得视频修改配置返回false');
return false;
}
const isEnabled = config?.show === true;
console.log('🔍 视频修改配置检查:', {
show: config?.show,
isEnabled,
finalResult: isEnabled
});
// 简化逻辑只检查show字段
return isEnabled;
} catch (error) {
console.error('❌ Error checking video modification status:', error);
return false; // 出错时默认不显示
}
};