diff --git a/.gitea/workflows/miniapp-preview.yml b/.gitea/workflows/miniapp-preview.yml index 9836635..fb80c62 100644 --- a/.gitea/workflows/miniapp-preview.yml +++ b/.gitea/workflows/miniapp-preview.yml @@ -31,6 +31,7 @@ jobs: env: MINIAPP_BUILD_NUMBER: ${{ gitea.run_number }} MINIAPP_VERSION_OVERRIDE: ${{ vars.MINIAPP_VERSION_OVERRIDE }} + GITEA_SHA: ${{ gitea.sha }} run: | test -n "$MINIAPP_BUILD_NUMBER" VERSION="$(node -e 'const {resolveVersion}=require("./ci/resolve-version.cjs"); process.stdout.write(resolveVersion(process.cwd()))')" @@ -38,6 +39,25 @@ jobs: mkdir -p ci-artifacts printf '%s\n' "$VERSION" > ci-artifacts/resolved-version.txt echo "Resolved Mini Program version for build+upload: $VERSION" + + # 注入版本号到 utils/version.js + BUILD_TIME="$(date -u +%Y-%m-%dT%H:%M:%SZ)" + COMMIT_SHORT="${GITEA_SHA:0:7}" + cat > utils/version.js < bin/version.env < 关于 - 老黄历小程序 v1.0.1 + 老黄历小程序 v{{version}} + 构建 #{{buildNumber}} ({{commitSha}}) 提供农历、黄历、八字、许愿等功能 diff --git a/mini/pages/wish-tree/wish-tree.wxml b/mini/pages/wish-tree/wish-tree.wxml index f0d60f1..0a290a8 100644 --- a/mini/pages/wish-tree/wish-tree.wxml +++ b/mini/pages/wish-tree/wish-tree.wxml @@ -35,7 +35,7 @@ diff --git a/mini/utils/version.js b/mini/utils/version.js new file mode 100644 index 0000000..6bbf883 --- /dev/null +++ b/mini/utils/version.js @@ -0,0 +1,11 @@ +/** + * 版本号配置文件 + * 此文件由 CI 构建时自动生成,请勿手动修改 + */ + +module.exports = { + version: "1.0.1", // 会被 CI 替换为实际版本号 + buildNumber: "0", // 会被 CI 替换为构建编号 + buildTime: "2026-08-08T00:00:00Z", // 会被 CI 替换为构建时间 + commitSha: "unknown", // 会被 CI 替换为 Git commit SHA +}; diff --git a/server/cmd/main.go b/server/cmd/main.go index 6746854..cc10667 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -42,6 +42,9 @@ func main() { // API 路由 api := r.Group("/api") { + // 版本信息(无需认证) + api.GET("/version", handler.GetVersion) + // 用户相关 user := api.Group("/user") { diff --git a/server/internal/handler/version.go b/server/internal/handler/version.go new file mode 100644 index 0000000..2c4860f --- /dev/null +++ b/server/internal/handler/version.go @@ -0,0 +1,42 @@ +package handler + +import ( + "net/http" + "os" + + "github.com/gin-gonic/gin" +) + +// VersionInfo 版本信息 +type VersionInfo struct { + Version string `json:"version"` + BuildTime string `json:"buildTime"` + CommitSha string `json:"commitSha"` + GoVersion string `json:"goVersion"` +} + +// GetVersion 获取服务器版本信息 +func GetVersion(c *gin.Context) { + // 从环境变量读取版本信息(由 CI 构建时注入) + version := os.Getenv("APP_VERSION") + if version == "" { + version = "dev" + } + + buildTime := os.Getenv("APP_BUILD_TIME") + if buildTime == "" { + buildTime = "unknown" + } + + commitSha := os.Getenv("APP_COMMIT_SHA") + if commitSha == "" { + commitSha = "unknown" + } + + c.JSON(http.StatusOK, VersionInfo{ + Version: version, + BuildTime: buildTime, + CommitSha: commitSha, + GoVersion: "go1.22", // 可以通过 runtime.Version() 获取 + }) +}