feat: 添加版本检查 API,支持从 Cloudflare Function 获取应用版本信息;更新相关配置文件
This commit is contained in:
101
functions/api/version.ts
Normal file
101
functions/api/version.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* Cloudflare Pages Function - 版本检查 API
|
||||
* 访问路径: /api/version
|
||||
*/
|
||||
|
||||
interface VersionInfo {
|
||||
version: string;
|
||||
forceUpdate: boolean;
|
||||
updateMessage: string;
|
||||
updateUrl: string;
|
||||
minSupportVersion: string;
|
||||
}
|
||||
|
||||
interface Env {
|
||||
// 可以在这里添加环境变量类型
|
||||
}
|
||||
|
||||
interface VersionConfig {
|
||||
ios: VersionInfo;
|
||||
android: VersionInfo;
|
||||
web: VersionInfo;
|
||||
}
|
||||
|
||||
// 版本配置 - 直接在这里管理版本信息
|
||||
const versionConfig: VersionConfig = {
|
||||
ios: {
|
||||
version: "0.0.1",
|
||||
forceUpdate: false,
|
||||
updateMessage: "修复了一些问题并优化了性能",
|
||||
updateUrl: "https://apps.apple.com/app/id123456789",
|
||||
minSupportVersion: "0.9.0",
|
||||
},
|
||||
android: {
|
||||
version: "0.0.1",
|
||||
forceUpdate: false,
|
||||
updateMessage: "修复了一些问题并优化了性能",
|
||||
updateUrl: "https://play.google.com/store/apps/details?id=riwa.ionic.app",
|
||||
minSupportVersion: "0.9.0",
|
||||
},
|
||||
web: {
|
||||
version: "0.0.1",
|
||||
forceUpdate: false,
|
||||
updateMessage: "修复了一些问题并优化了性能",
|
||||
updateUrl: "",
|
||||
minSupportVersion: "0.9.0",
|
||||
},
|
||||
};
|
||||
|
||||
export const onRequestGet: PagesFunction<Env> = async (context) => {
|
||||
try {
|
||||
const { searchParams } = new URL(context.request.url);
|
||||
const platform = searchParams.get("platform") || "web";
|
||||
const currentVersion = searchParams.get("currentVersion") || "0.0.1";
|
||||
|
||||
// 验证平台参数
|
||||
if (!["ios", "android", "web"].includes(platform)) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: "Invalid platform parameter" }),
|
||||
{
|
||||
status: 400,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// 获取对应平台的版本信息
|
||||
const versionInfo = versionConfig[platform as keyof VersionConfig];
|
||||
|
||||
// 返回版本信息
|
||||
return new Response(JSON.stringify(versionInfo), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Cache-Control": "public, max-age=300", // 缓存5分钟
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
},
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Version check error:", error);
|
||||
return new Response(
|
||||
JSON.stringify({ error: "Internal server error" }),
|
||||
{
|
||||
status: 500,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
},
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
// 支持 CORS 预检请求
|
||||
export const onRequestOptions: PagesFunction<Env> = async () => {
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "Content-Type",
|
||||
},
|
||||
});
|
||||
};
|
||||
17
functions/tsconfig.json
Normal file
17
functions/tsconfig.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"lib": ["ESNext"],
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"resolveJsonModule": true,
|
||||
"types": ["@cloudflare/workers-types"],
|
||||
"strict": true,
|
||||
"noEmit": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["**/*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user