mirror of
https://github.com/ZSCGR/CloudFlare-ImgBed.git
synced 2026-08-13 04:03:42 +08:00
50 lines
1.8 KiB
JavaScript
50 lines
1.8 KiB
JavaScript
/**
|
|
* 用户端页面配置 API
|
|
* 负责读取页面配置并转换为前端可直接使用的用户配置对象
|
|
*/
|
|
import { fetchPageConfig } from "../utils/sysConfig.js";
|
|
|
|
export async function onRequest(context) {
|
|
const { env } = context;
|
|
const PageConfig = await fetchPageConfig(env);
|
|
const userConfigList = PageConfig.config;
|
|
const userConfig = {};
|
|
|
|
for (const config of userConfigList) {
|
|
if (config.value !== undefined && config.value !== null && config.value !== '') {
|
|
// 将config解析为JSON对象,若解析失败则返回原始字符串
|
|
try {
|
|
userConfig[config.id] = JSON.parse(config.value);
|
|
} catch (error) {
|
|
userConfig[config.id] = config.value;
|
|
}
|
|
} else if (config.type === 'boolean' && config.default !== undefined) {
|
|
// 布尔类型使用默认值
|
|
userConfig[config.id] = config.default;
|
|
}
|
|
}
|
|
|
|
if (Number.isFinite(Number(PageConfig.announcementRefreshAt))) {
|
|
userConfig.announcementRefreshAt = Number(PageConfig.announcementRefreshAt);
|
|
}
|
|
|
|
// 检查 USER_CONFIG 是否为空或未定义
|
|
if (!userConfig) {
|
|
return new Response(JSON.stringify({}), { status: 200 });
|
|
}
|
|
|
|
try {
|
|
// 尝试解析 USER_CONFIG 为 JSON
|
|
const parsedConfig = userConfig;
|
|
// 检查解析后的结果是否为对象
|
|
if (typeof parsedConfig === 'object' && parsedConfig !== null) {
|
|
return new Response(JSON.stringify(parsedConfig), { status: 200 });
|
|
} else {
|
|
return new Response(JSON.stringify({}), { status: 200 });
|
|
}
|
|
} catch (error) {
|
|
// 捕捉解析错误并返回空对象
|
|
return new Response(JSON.stringify({}), { status: 200 });
|
|
}
|
|
}
|