2026-05-25 19:36:27 +08:00

364 lines
10 KiB
Go
Raw 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.

// Package appconfig 读取后台管理维护的 App 运行时配置。
package appconfig
import (
"context"
"database/sql"
"errors"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
)
const h5LinkGroup = "h5-links"
const listH5LinksSQL = "SELECT `key`, COALESCE(description, ''), COALESCE(value, ''), updated_at_ms FROM admin_app_configs WHERE `group` = ? ORDER BY `key` ASC"
const listExploreTabsSQL = `
SELECT id, app_code, tab, h5_url, enabled, sort_order, updated_at_ms
FROM admin_app_explore_tabs
WHERE app_code = ? AND enabled = TRUE
ORDER BY sort_order ASC, id DESC`
const listAppBannersSQL = `
SELECT id, app_code, cover_url, room_small_image_url, banner_type, display_scope, param, platform, sort_order, region_id, country_code, description, starts_at_ms, ends_at_ms, updated_at_ms
FROM admin_app_banners
WHERE app_code = ? AND status = 'active' AND display_scope = ?
AND (? = '' OR platform = ?)
AND (region_id = 0 OR region_id = ?)
AND (country_code = '' OR country_code = ?)
AND (starts_at_ms = 0 OR starts_at_ms <= ?)
AND (ends_at_ms = 0 OR ends_at_ms > ?)
ORDER BY sort_order ASC, id DESC`
const latestAppVersionSQL = `
SELECT id, app_code, platform, version, build_number, force_update, download_url, COALESCE(description, ''), updated_at_ms
FROM admin_app_versions
WHERE app_code = ? AND platform = ?
ORDER BY build_number DESC, id DESC
LIMIT 1`
// H5Link 是 gateway 下发给 App 的 H5 入口配置。
type H5Link struct {
Key string `json:"key"`
Label string `json:"label"`
URL string `json:"url"`
UpdatedAtMs int64 `json:"updated_at_ms"`
}
// ExploreTab 是后台 APP配置/Explore配置 中启用的 H5 tab。
type ExploreTab struct {
ID uint `json:"id"`
AppCode string `json:"app_code"`
Tab string `json:"tab"`
H5URL string `json:"h5_url"`
Enabled bool `json:"enabled"`
SortOrder int `json:"sort_order"`
UpdatedAtMs int64 `json:"updated_at_ms"`
}
// BannerQuery 是 App banner 的公开筛选条件。
type BannerQuery struct {
AppCode string
DisplayScope string
Platform string
RegionID int64
Country string
}
// Banner 是 gateway 下发给 App 指定显示范围的 banner 配置。
type Banner struct {
ID uint `json:"id"`
CoverURL string `json:"cover_url"`
RoomSmallImageURL string `json:"room_small_image_url,omitempty"`
BannerType string `json:"type"`
DisplayScope string `json:"display_scope"`
Param string `json:"param"`
Platform string `json:"platform"`
SortOrder int `json:"sort_order"`
RegionID int64 `json:"region_id,omitempty"`
CountryCode string `json:"country_code,omitempty"`
Description string `json:"description,omitempty"`
StartsAtMs int64 `json:"starts_at_ms,omitempty"`
EndsAtMs int64 `json:"ends_at_ms,omitempty"`
UpdatedAtMs int64 `json:"updated_at_ms"`
}
// VersionQuery 是 App 检查更新的公开筛选条件。
type VersionQuery struct {
AppCode string
Platform string
CurrentBuildNumber int64
}
// Version 是后台 APP配置/版本管理 中当前平台的最新 App 版本。
type Version struct {
ID uint `json:"id"`
AppCode string `json:"app_code"`
Platform string `json:"platform"`
Version string `json:"version"`
BuildNumber int64 `json:"build_number"`
ForceUpdate bool `json:"force_update"`
DownloadURL string `json:"download_url"`
Description string `json:"description"`
UpdatedAtMs int64 `json:"updated_at_ms"`
}
// Reader 是 HTTP 层读取 H5 配置的最小依赖。
type Reader interface {
ListH5Links(ctx context.Context) ([]H5Link, error)
ListExploreTabs(ctx context.Context, appCode string) ([]ExploreTab, error)
ListBanners(ctx context.Context, query BannerQuery) ([]Banner, error)
LatestVersion(ctx context.Context, query VersionQuery) (Version, error)
}
// MySQLReader 从 hyapp_admin 读取后台 App 配置。
type MySQLReader struct {
db *sql.DB
}
// OpenMySQLReader 创建后台配置只读连接池。
func OpenMySQLReader(dsn string) (*MySQLReader, error) {
db, err := sql.Open("mysql", strings.TrimSpace(dsn))
if err != nil {
return nil, err
}
return &MySQLReader{db: db}, nil
}
// Close 关闭后台配置连接池。
func (r *MySQLReader) Close() error {
if r == nil || r.db == nil {
return nil
}
return r.db.Close()
}
// ListH5Links 返回后台动态维护的 H5 入口集合。
func (r *MySQLReader) ListH5Links(ctx context.Context) ([]H5Link, error) {
if r == nil || r.db == nil {
return nil, errors.New("app config reader is not configured")
}
rows, err := r.db.QueryContext(ctx, listH5LinksSQL, h5LinkGroup)
if err != nil {
return nil, err
}
defer rows.Close()
items := make([]H5Link, 0)
for rows.Next() {
var item H5Link
if err := rows.Scan(&item.Key, &item.Label, &item.URL, &item.UpdatedAtMs); err != nil {
return nil, err
}
item.Key = strings.TrimSpace(item.Key)
item.Label = strings.TrimSpace(item.Label)
if item.Label == "" {
item.Label = item.Key
}
item.URL = strings.TrimSpace(item.URL)
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
// ListExploreTabs 返回当前 App 启用的 Explore H5 tabs后台关闭的 tab 不下发给客户端。
func (r *MySQLReader) ListExploreTabs(ctx context.Context, appCode string) ([]ExploreTab, error) {
if r == nil || r.db == nil {
return nil, errors.New("app config reader is not configured")
}
rows, err := r.db.QueryContext(ctx, listExploreTabsSQL, normalizeAppCode(appCode))
if err != nil {
return nil, err
}
defer rows.Close()
items := make([]ExploreTab, 0)
for rows.Next() {
var item ExploreTab
if err := rows.Scan(&item.ID, &item.AppCode, &item.Tab, &item.H5URL, &item.Enabled, &item.SortOrder, &item.UpdatedAtMs); err != nil {
return nil, err
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
// ListBanners 返回当前 App、显示范围、平台和地域可见的 banner。
func (r *MySQLReader) ListBanners(ctx context.Context, query BannerQuery) ([]Banner, error) {
if r == nil || r.db == nil {
return nil, errors.New("app config reader is not configured")
}
appCode := normalizeAppCode(query.AppCode)
displayScope := NormalizeBannerDisplayScope(query.DisplayScope)
if displayScope == "" {
return []Banner{}, nil
}
platform := normalizePlatform(query.Platform)
country := normalizeCountry(query.Country)
nowMs := time.Now().UTC().UnixMilli()
rows, err := r.db.QueryContext(ctx, listAppBannersSQL, appCode, displayScope, platform, platform, query.RegionID, country, nowMs, nowMs)
if err != nil {
return nil, err
}
defer rows.Close()
items := make([]Banner, 0)
for rows.Next() {
var item Banner
var appCode string
var updatedAtMS int64
if err := rows.Scan(
&item.ID,
&appCode,
&item.CoverURL,
&item.RoomSmallImageURL,
&item.BannerType,
&item.DisplayScope,
&item.Param,
&item.Platform,
&item.SortOrder,
&item.RegionID,
&item.CountryCode,
&item.Description,
&item.StartsAtMs,
&item.EndsAtMs,
&updatedAtMS,
); err != nil {
return nil, err
}
item.UpdatedAtMs = updatedAtMS
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
// LatestVersion 返回指定 App 和平台的最高 build_number 版本;未配置时返回空版本,方便 App 端按无更新处理。
func (r *MySQLReader) LatestVersion(ctx context.Context, query VersionQuery) (Version, error) {
if r == nil || r.db == nil {
return Version{}, errors.New("app config reader is not configured")
}
var item Version
err := r.db.QueryRowContext(ctx, latestAppVersionSQL, normalizeAppCode(query.AppCode), normalizePlatform(query.Platform)).Scan(
&item.ID,
&item.AppCode,
&item.Platform,
&item.Version,
&item.BuildNumber,
&item.ForceUpdate,
&item.DownloadURL,
&item.Description,
&item.UpdatedAtMs,
)
if errors.Is(err, sql.ErrNoRows) {
return Version{AppCode: normalizeAppCode(query.AppCode), Platform: normalizePlatform(query.Platform)}, nil
}
if err != nil {
return Version{}, err
}
return item, nil
}
// StaticReader 给测试和临时环境提供内存版 H5 配置读取。
type StaticReader struct {
Links []H5Link
ExploreTabs []ExploreTab
Banners []Banner
Version Version
Err error
}
// ListH5Links 返回预置 H5 配置。
func (r StaticReader) ListH5Links(context.Context) ([]H5Link, error) {
if r.Err != nil {
return nil, r.Err
}
out := make([]H5Link, len(r.Links))
copy(out, r.Links)
return out, nil
}
// ListExploreTabs 返回预置 Explore tab 配置。
func (r StaticReader) ListExploreTabs(context.Context, string) ([]ExploreTab, error) {
if r.Err != nil {
return nil, r.Err
}
out := make([]ExploreTab, len(r.ExploreTabs))
copy(out, r.ExploreTabs)
return out, nil
}
// ListBanners 返回预置 App banner 配置。
func (r StaticReader) ListBanners(context.Context, BannerQuery) ([]Banner, error) {
if r.Err != nil {
return nil, r.Err
}
out := make([]Banner, len(r.Banners))
copy(out, r.Banners)
return out, nil
}
// LatestVersion 返回预置版本配置。
func (r StaticReader) LatestVersion(_ context.Context, query VersionQuery) (Version, error) {
if r.Err != nil {
return Version{}, r.Err
}
item := r.Version
if item.AppCode == "" {
item.AppCode = normalizeAppCode(query.AppCode)
}
if item.Platform == "" {
item.Platform = normalizePlatform(query.Platform)
}
return item, nil
}
func normalizeAppCode(value string) string {
value = strings.ToLower(strings.TrimSpace(value))
if value == "" {
return "lalu"
}
return value
}
func normalizePlatform(value string) string {
return strings.ToLower(strings.TrimSpace(value))
}
func normalizeCountry(value string) string {
return strings.ToUpper(strings.TrimSpace(value))
}
// NormalizeBannerDisplayScope 统一 App 和后台约定的显示范围枚举。
func NormalizeBannerDisplayScope(value string) string {
value = strings.ToLower(strings.TrimSpace(value))
switch value {
case "":
return "home"
case "home", "首页":
return "home"
case "room", "房间内":
return "room"
case "recharge", "充值页":
return "recharge"
default:
return ""
}
}