模型(一张卡 = 一个主体,账号和卡密都只是主体的钥匙): - app_users 泛化成主体:新增 subject_type(account/card)、display_name、remark; display_name 就是客服看到的主体标识(账号填用户名,卡密主体填卡密) - cards 承担「凭证 + 发卡记录」:新增 purpose(recharge/credential)、subject_id、applied_to_subject_id - card_types 新增 purpose,生成卡时复制到卡上(卡型决定是登录卡还是充值卡) - applications 新增 login_mode(account/card/both)、card_login_bind_device、card_login_allow_password - 迁移回填:display_name=username、subject_type=account、cards.purpose=recharge、login_mode=account 登录(单一权威流程,不再复制设备/IP/风控逻辑): - /login 增加 login_type(account 默认 / card / auto) 与 card_key - auto = 密码留空即按卡密处理,由服务端判定,壳不猜格式 - 卡密首登在同一事务里:建主体 → 按卡型发额度(余额或时长)→ 回填 cards.subject_id → 卡置 used; 之后每次登录只是查回同一主体,设备上限/IP/多开/风控与账号完全同一条链路 - 卡密不存在/作废/过期对外统一「卡密无效或已失效」,真实原因只进日志 充值(卡给卡充值): - /recharge 目标既可以是用户名也可以是卡密(填卡密即充到该卡密主体) - purpose=credential 的卡打充值接口直接拒绝(先判用途再判状态,文案才准确) - 充值卡置 used 并回填 applied_to_subject_id,财务能追溯充给了谁 后台显示(按反馈做得能一眼分清): - 用户列表首列改「主体标识」:账号显示用户名/邮箱,卡密主体直接显示卡密 + 「卡密」标签, 副行显示内部 username(card-N);接口同时返回 card_key 供搜索与客服核对 - 卡型创建新增「卡型用途」二选一,并附一句说明登录卡是什么 - 应用设置新增「登录方式」三选一 + 卡密登录两个开关(受设备上限约束、允许再设密码) 壳(七套皮肤通用): - 按 /info 的 login_mode 变形登录面板:card 模式只留「卡密」一个框、按钮「卡密登录」、 隐藏密码/记住账号/注册/忘记密码,保留卡密充值;both 模式单框「账号或卡密」,密码留空即卡密 实测:卡密首登激活主体(card-1/display_name=卡密/到期+30天)、同卡再登同主体、卡给卡充值余额 100→200、 登录卡打充值接口被拒、充值卡打登录接口被拒、both 模式两种凭证都能进、卡密模式下窗口只剩两个 tab。 壳资源 851,456 B,e2e 8 套全过。
537 lines
19 KiB
Go
537 lines
19 KiB
Go
package app
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"verification-platform-backend/internal/database"
|
|
"verification-platform-backend/internal/model"
|
|
"verification-platform-backend/internal/service"
|
|
"verification-platform-backend/internal/utils/contentstore"
|
|
"verification-platform-backend/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func SetupInfoRoutes(r *gin.RouterGroup) {
|
|
r.GET("/info", handleAppGetInfo)
|
|
r.GET("/check-update", handleAppCheckUpdate)
|
|
r.GET("/announcements", handleAppGetAnnouncements)
|
|
r.GET("/versions/manifest", handleAppGetVersionManifest)
|
|
}
|
|
|
|
// 允许客户端拉取清单的版本状态,草稿版本不对外暴露
|
|
var manifestAllowedStatus = []string{"published", "superseded"}
|
|
|
|
// buildVersionManifest 生成版本文件清单、清单指纹,以及两个完整性标记
|
|
//
|
|
// 返回的 files 按 path 排序,保证同一版本算出的 manifestHash 可复现
|
|
// complete 表示清单是否是目标版本的完整文件列表:
|
|
// - 全量版本:清单就是完整文件列表,客户端可据此判断哪些文件要下载、哪些要删除
|
|
// - 补丁版本:清单只记录补丁包内(新增/变更)的文件,客户端只能覆盖安装,不能据此删除文件
|
|
//
|
|
// stored 表示清单里的文件是否都已拆散落盘,未落盘时 url 为空
|
|
func buildVersionManifest(version *model.Version) (files []gin.H, manifestHash string, complete, stored bool) {
|
|
type manifestEntry struct {
|
|
Path string
|
|
Size int64
|
|
Hash string
|
|
Type string
|
|
Required bool
|
|
}
|
|
|
|
entries := make([]manifestEntry, 0, len(version.Files))
|
|
for i := range version.Files {
|
|
f := &version.Files[i]
|
|
entries = append(entries, manifestEntry{
|
|
Path: f.FilePath,
|
|
Size: f.FileSize,
|
|
Hash: f.FileHash,
|
|
Type: f.FileType,
|
|
Required: f.IsRequired,
|
|
})
|
|
}
|
|
|
|
sort.Slice(entries, func(i, j int) bool { return entries[i].Path < entries[j].Path })
|
|
|
|
hasher := sha256.New()
|
|
files = make([]gin.H, 0, len(entries))
|
|
stored = true
|
|
for _, e := range entries {
|
|
// 内容寻址下载地址,相同内容跨版本复用同一份。
|
|
// 尚未落盘时不给地址,客户端应回退整包更新
|
|
url := ""
|
|
if contentstore.Exists(e.Hash) {
|
|
url = contentstore.ContentFileURL(e.Hash)
|
|
} else if e.Hash != "" {
|
|
stored = false
|
|
}
|
|
files = append(files, gin.H{
|
|
"path": e.Path,
|
|
"size": e.Size,
|
|
"hash": e.Hash,
|
|
"type": e.Type,
|
|
"required": e.Required,
|
|
"url": url,
|
|
})
|
|
fmt.Fprintf(hasher, "%s|%d|%s|%s|%t\n", e.Path, e.Size, e.Hash, e.Type, e.Required)
|
|
}
|
|
|
|
return files, "sha256:" + hex.EncodeToString(hasher.Sum(nil)), version.UpdateType == "full", stored
|
|
}
|
|
|
|
// BuildManifest 导出清单计算口径,供 MCP 等内部调用复用,避免两套逻辑漂移
|
|
func BuildManifest(version *model.Version) (files []gin.H, manifestHash string, complete, stored bool) {
|
|
return buildVersionManifest(version)
|
|
}
|
|
|
|
// buildManifestURL 构造清单端点地址,客户端拿它去拉完整文件清单
|
|
func buildManifestURL(appKey string, versionID uint) string {
|
|
return fmt.Sprintf("/api/v1/app/%s/versions/manifest?version_id=%d", appKey, versionID)
|
|
}
|
|
|
|
// handleAppGetVersionManifest 返回指定版本的文件清单
|
|
//
|
|
// 客户端拿到清单后与本地文件哈希比对,只下载变化的文件,从而避免整包下载
|
|
func handleAppGetVersionManifest(c *gin.Context) {
|
|
appKey := c.Param("appKey")
|
|
|
|
var app model.Application
|
|
if err := database.DB.Where("app_key = ?", appKey).First(&app).Error; err != nil {
|
|
response.Error(c, 404, "应用不存在")
|
|
return
|
|
}
|
|
|
|
if service.GetApplicationDisabledStatus(app.ID) {
|
|
response.Error(c, 403, "该应用已被禁用")
|
|
return
|
|
}
|
|
|
|
var version model.Version
|
|
if versionID := strings.TrimSpace(c.Query("version_id")); versionID != "" {
|
|
if err := database.DB.
|
|
Where("application_id = ? AND publish_status IN ? AND id = ?", app.ID, manifestAllowedStatus, versionID).
|
|
Preload("Files").First(&version).Error; err != nil {
|
|
response.Error(c, 404, "版本不存在")
|
|
return
|
|
}
|
|
} else {
|
|
// 兼容 v/V 前缀,与 check-update 的版本号清洗规则保持一致
|
|
v := strings.TrimSpace(c.Query("version"))
|
|
v = strings.TrimPrefix(v, "v")
|
|
v = strings.TrimPrefix(v, "V")
|
|
if v == "" {
|
|
response.Error(c, 400, "缺少 version_id 或 version 参数")
|
|
return
|
|
}
|
|
// 同一版本号可能有多条记录(不同发布批次),取最新的
|
|
if err := database.DB.
|
|
Where("application_id = ? AND publish_status IN ? AND version IN ?",
|
|
app.ID, manifestAllowedStatus, []string{v, "v" + v, "V" + v}).
|
|
Order("id DESC").Preload("Files").First(&version).Error; err != nil {
|
|
response.Error(c, 404, "版本不存在")
|
|
return
|
|
}
|
|
}
|
|
|
|
files, manifestHash, complete, stored := buildVersionManifest(&version)
|
|
|
|
// 内容还没拆出来(历史版本、或上次拆包失败)时现场拆一次。
|
|
// 这一轮客户端拿不到地址会回退整包,拆完后下次请求就能走单文件更新
|
|
if !stored && version.FilePath != "" {
|
|
contentstore.ExtractOnDemand(version.FilePath)
|
|
}
|
|
|
|
response.Success(c, gin.H{
|
|
"version_id": version.ID,
|
|
"version": version.Version,
|
|
"update_type": version.UpdateType,
|
|
"complete": complete,
|
|
"manifest_hash": manifestHash,
|
|
"files": files,
|
|
})
|
|
}
|
|
|
|
// respondInstallerUpdate 直接下发最新版本的完整包信息
|
|
//
|
|
// 用于两种无法判断差量的场景:客户端没有上报版本号(安装器模式),
|
|
// 以及上报的版本号在服务器版本列表里找不到(版本记录被删、或客户端装的是未登记的包)。
|
|
// 这两种情况客户端本地状态未知,只能走整包覆盖安装。
|
|
func respondInstallerUpdate(c *gin.Context, app *model.Application, appKey string, latestVersion *model.Version) {
|
|
// 优先给全量包,没有全量包才退回上传的原始包
|
|
downloadURL := latestVersion.FullPackagePath
|
|
downloadSize := latestVersion.FullPackageSize
|
|
downloadHash := latestVersion.FullPackageHash
|
|
if downloadURL == "" {
|
|
downloadURL = latestVersion.FilePath
|
|
downloadSize = latestVersion.FileSize
|
|
downloadHash = latestVersion.FileHash
|
|
}
|
|
if latestVersion.CustomDownloadURL != "" {
|
|
downloadURL = latestVersion.CustomDownloadURL
|
|
}
|
|
|
|
// 清单供客户端按需下载文件,避免整包更新
|
|
_, manifestHash, _, _ := buildVersionManifest(latestVersion)
|
|
|
|
result := gin.H{
|
|
"has_update": true,
|
|
"app_name": app.Name,
|
|
"latest_version": latestVersion.Version,
|
|
"latest_version_id": latestVersion.ID,
|
|
"manifest_url": buildManifestURL(appKey, latestVersion.ID),
|
|
"manifest_hash": manifestHash,
|
|
"download_url": downloadURL,
|
|
"file_size": downloadSize,
|
|
"file_hash": downloadHash,
|
|
"entry_file": latestVersion.EntryFile,
|
|
"entry_files": latestVersion.EntryFiles,
|
|
"desktop_shortcut_files": latestVersion.DesktopShortcutFiles,
|
|
"update_notes": latestVersion.Description,
|
|
"update_strategy": latestVersion.UpdateStrategy,
|
|
"update_type": latestVersion.UpdateType,
|
|
"update_method": latestVersion.UpdateMethod,
|
|
"changelog": latestVersion.Changelog,
|
|
}
|
|
|
|
// 同时返回补丁包信息(如果有),供客户端增量更新使用
|
|
if latestVersion.FilePath != "" && latestVersion.FilePath != downloadURL {
|
|
result["patch_url"] = latestVersion.FilePath
|
|
result["patch_size"] = latestVersion.FileSize
|
|
result["patch_hash"] = latestVersion.FileHash
|
|
}
|
|
|
|
// 如果有完整包,也单独返回完整包信息
|
|
if latestVersion.FullPackagePath != "" {
|
|
result["full_package_url"] = latestVersion.FullPackagePath
|
|
result["full_package_size"] = latestVersion.FullPackageSize
|
|
result["full_package_hash"] = latestVersion.FullPackageHash
|
|
}
|
|
|
|
response.Success(c, result)
|
|
}
|
|
|
|
func handleAppGetInfo(c *gin.Context) {
|
|
appKey := c.Param("appKey")
|
|
var app model.Application
|
|
if err := database.DB.Where("app_key = ?", appKey).First(&app).Error; err != nil {
|
|
response.Error(c, 404, "应用不存在")
|
|
return
|
|
}
|
|
|
|
if service.GetApplicationDisabledStatus(app.ID) {
|
|
response.Error(c, 403, "该应用已被禁用")
|
|
return
|
|
}
|
|
|
|
response.Success(c, gin.H{
|
|
"id": app.ID,
|
|
"name": app.Name,
|
|
"description": app.Description,
|
|
"icon_url": app.IconURL,
|
|
"status": app.Status,
|
|
"billing_type": app.BillingType,
|
|
"login_policy": app.LoginPolicy,
|
|
"max_devices": app.MaxDevices,
|
|
"multi_open_mode": app.MultiOpenMode,
|
|
"max_instances": app.MaxInstances,
|
|
"enable_trial": app.EnableTrial,
|
|
"trial_balance": app.TrialBalance,
|
|
"heartbeat_interval": app.HeartbeatInterval,
|
|
"heartbeat_timeout": app.HeartbeatTimeout,
|
|
// —— 客户端界面开关:壳按这些字段决定显示哪些 tab / 哪些步骤 ——
|
|
"allow_register": app.AllowRegister,
|
|
"enable_login_verify": app.EnableLoginVerify,
|
|
"enable_register_verify": app.EnableRegisterVerify,
|
|
"verify_method": app.VerifyMethod,
|
|
"enable_password_reset": app.EnablePasswordReset,
|
|
"password_reset_method": app.PasswordResetMethod,
|
|
"enable_free_period": app.EnableFreePeriod,
|
|
"trial_days": app.TrialDays,
|
|
"bind_type": app.BindType,
|
|
"login_mode": app.LoginMode,
|
|
"card_login_bind_device": app.CardLoginBindDevice,
|
|
"card_login_allow_password": app.CardLoginAllowPassword,
|
|
})
|
|
}
|
|
|
|
func handleAppCheckUpdate(c *gin.Context) {
|
|
appKey := c.Param("appKey")
|
|
clientVersion := c.Query("version")
|
|
// 可选:客户端把自己本地的清单指纹报上来。
|
|
// 报了就能发现"版本号没变但文件被重新上传过"的情况;不报则退回按版本记录 ID 判定。
|
|
clientManifestHash := strings.TrimSpace(c.Query("manifest_hash"))
|
|
|
|
var app model.Application
|
|
if err := database.DB.Where("app_key = ?", appKey).First(&app).Error; err != nil {
|
|
response.Error(c, 404, "应用不存在")
|
|
return
|
|
}
|
|
|
|
if service.GetApplicationDisabledStatus(app.ID) {
|
|
response.Error(c, 403, "该应用已被禁用")
|
|
return
|
|
}
|
|
|
|
// 查询该应用的所有已发布版本,按ID降序(用于获取最新版本和下载地址)
|
|
var publishedVersions []model.Version
|
|
if err := database.DB.Where("application_id = ? AND publish_status = ?", app.ID, "published").
|
|
Order("id DESC").
|
|
Preload("Files").
|
|
Find(&publishedVersions).Error; err != nil {
|
|
response.Error(c, 500, "获取版本列表失败")
|
|
return
|
|
}
|
|
|
|
// 同时查询已发布和已失效的版本,用于匹配客户端版本号
|
|
var matchableVersions []model.Version
|
|
if clientVersion != "" {
|
|
if err := database.DB.Where("application_id = ? AND publish_status IN ?", app.ID, []string{"published", "superseded"}).
|
|
Order("id DESC").
|
|
Find(&matchableVersions).Error; err != nil {
|
|
response.Error(c, 500, "获取版本列表失败")
|
|
return
|
|
}
|
|
}
|
|
|
|
// 没有任何已发布版本
|
|
if len(publishedVersions) == 0 {
|
|
response.Success(c, gin.H{
|
|
"has_update": false,
|
|
"current_version": clientVersion,
|
|
"latest_version": "",
|
|
"download_url": "",
|
|
"update_notes": "",
|
|
"update_strategy": "",
|
|
"update_method": "",
|
|
"files": []interface{}{},
|
|
})
|
|
return
|
|
}
|
|
|
|
// 清理版本号:去除空格和前缀 v/V
|
|
cleanVersion := func(v string) string {
|
|
v = strings.TrimSpace(v)
|
|
v = strings.TrimPrefix(v, "v")
|
|
v = strings.TrimPrefix(v, "V")
|
|
return v
|
|
}
|
|
|
|
// 如果没有传入版本号,直接返回最新已发布版本(安装器模式,优先全量包)
|
|
if clientVersion == "" {
|
|
respondInstallerUpdate(c, &app, appKey, &publishedVersions[0])
|
|
return
|
|
}
|
|
|
|
clientVersionClean := cleanVersion(clientVersion)
|
|
|
|
// 找到客户端当前版本对应的版本记录(从已发布和已失效版本中匹配)
|
|
var clientVersionRecord *model.Version
|
|
for i := range matchableVersions {
|
|
if cleanVersion(matchableVersions[i].Version) == clientVersionClean {
|
|
clientVersionRecord = &matchableVersions[i]
|
|
break
|
|
}
|
|
}
|
|
|
|
// 客户端版本在服务器版本列表里找不到(版本记录被删、或客户端装的是未登记的包),
|
|
// 无法判断差量,按安装器模式下发最新版本的完整包,而不是报错
|
|
if clientVersionRecord == nil {
|
|
respondInstallerUpdate(c, &app, appKey, &publishedVersions[0])
|
|
return
|
|
}
|
|
|
|
// 判断客户端版本状态:检查是否有更高ID的强制更新版本(只看已发布版本)
|
|
isSuperseded := false
|
|
for i := range publishedVersions {
|
|
v := &publishedVersions[i]
|
|
// 存在ID更高(创建时间更晚)的强制更新版本
|
|
if v.ID > clientVersionRecord.ID && v.UpdateStrategy == "forced" {
|
|
isSuperseded = true
|
|
break
|
|
}
|
|
}
|
|
|
|
// 如果被取代(有强制更新),必须更新
|
|
// 找到最新的强制更新版本
|
|
var latestVersion *model.Version
|
|
var updateStrategy string
|
|
|
|
if isSuperseded {
|
|
// 找到ID最高的强制更新版本
|
|
for i := range publishedVersions {
|
|
v := &publishedVersions[i]
|
|
if v.ID > clientVersionRecord.ID && v.UpdateStrategy == "forced" {
|
|
if latestVersion == nil || v.ID > latestVersion.ID {
|
|
latestVersion = v
|
|
}
|
|
}
|
|
}
|
|
updateStrategy = "forced"
|
|
} else {
|
|
// 没有强制更新,检查是否有更高ID的版本(普通更新)
|
|
// 最新已发布版本就是ID最高的版本(已经是按ID降序排列,第一个就是最新)
|
|
latestVersion = &publishedVersions[0]
|
|
|
|
// 只有当最新版本ID大于客户端版本ID时才需要更新
|
|
if latestVersion.ID > clientVersionRecord.ID {
|
|
updateStrategy = latestVersion.UpdateStrategy // optional 或 forced
|
|
} else {
|
|
// 版本记录 ID 相同,但客户端报了清单指纹且与服务器不一致
|
|
// → 说明该版本号下的文件被重新上传过,仍然下发更新
|
|
_, latestManifestHash, _, _ := buildVersionManifest(latestVersion)
|
|
contentChanged := clientManifestHash != "" && latestManifestHash != "" &&
|
|
clientManifestHash != latestManifestHash
|
|
if !contentChanged {
|
|
// 已经是最新版本
|
|
response.Success(c, gin.H{
|
|
"has_update": false,
|
|
"current_version": clientVersion,
|
|
"latest_version": clientVersionRecord.Version,
|
|
"latest_version_id": latestVersion.ID,
|
|
"manifest_hash": latestManifestHash,
|
|
"reason": "version_record_id_unchanged",
|
|
"download_url": "",
|
|
"update_notes": "",
|
|
"update_strategy": "",
|
|
"update_method": "",
|
|
"files": []interface{}{},
|
|
})
|
|
return
|
|
}
|
|
// 文件内容变了:按最新版本下发
|
|
updateStrategy = latestVersion.UpdateStrategy
|
|
}
|
|
}
|
|
|
|
// 确定下载地址
|
|
var downloadURL string
|
|
var downloadSize int64
|
|
var downloadHash string
|
|
var fullPackageURL string
|
|
var fullPackageSize int64
|
|
var fullPackageHash string
|
|
var isPatchUpdate bool
|
|
|
|
// 保存完整包地址(如果有)
|
|
if latestVersion.FullPackagePath != "" {
|
|
fullPackageURL = latestVersion.FullPackagePath
|
|
fullPackageSize = latestVersion.FullPackageSize
|
|
fullPackageHash = latestVersion.FullPackageHash
|
|
}
|
|
|
|
// 补丁内容是「基础版本的完整包 → 目标版本」的差量,只有客户端当前版本
|
|
// 正好是该补丁的基础版本时才能安全应用;装在其他版本上会丢改动或产生残缺文件
|
|
canApplyPatch := latestVersion.UpdateType == "patch" &&
|
|
latestVersion.BaseVersionID != nil &&
|
|
*latestVersion.BaseVersionID == clientVersionRecord.ID &&
|
|
latestVersion.FilePath != ""
|
|
|
|
if canApplyPatch {
|
|
// download_url 返回补丁包地址,full_package_url 返回完整包地址
|
|
downloadURL = latestVersion.FilePath
|
|
downloadSize = latestVersion.FileSize
|
|
downloadHash = latestVersion.FileHash
|
|
isPatchUpdate = true
|
|
} else if fullPackageURL != "" {
|
|
// 客户端不在补丁基础版本上,或最新版本是全量包,下发完整包
|
|
downloadURL = fullPackageURL
|
|
downloadSize = fullPackageSize
|
|
downloadHash = fullPackageHash
|
|
} else {
|
|
downloadURL = latestVersion.FilePath
|
|
downloadSize = latestVersion.FileSize
|
|
downloadHash = latestVersion.FileHash
|
|
}
|
|
|
|
// 实际下发的包类型,客户端据此决定覆盖安装还是叠加补丁
|
|
deliverType := "full"
|
|
if isPatchUpdate {
|
|
deliverType = "patch"
|
|
}
|
|
|
|
// 自定义地址优先级最高(覆盖下载地址)
|
|
if latestVersion.CustomDownloadURL != "" {
|
|
downloadURL = latestVersion.CustomDownloadURL
|
|
}
|
|
|
|
// 清单供客户端按需下载文件,避免整包更新;补丁版本返回的是补丁内文件清单(非完整列表)
|
|
_, manifestHash, _, _ := buildVersionManifest(latestVersion)
|
|
|
|
// 构建返回结果
|
|
result := map[string]interface{}{
|
|
"has_update": true,
|
|
"app_name": app.Name,
|
|
"current_version": clientVersion,
|
|
"latest_version": latestVersion.Version,
|
|
"latest_version_id": latestVersion.ID,
|
|
"manifest_url": buildManifestURL(appKey, latestVersion.ID),
|
|
"manifest_hash": manifestHash,
|
|
"download_url": downloadURL,
|
|
"file_size": downloadSize,
|
|
"file_hash": downloadHash,
|
|
"entry_file": latestVersion.EntryFile,
|
|
"entry_files": latestVersion.EntryFiles,
|
|
"desktop_shortcut_files": latestVersion.DesktopShortcutFiles,
|
|
"update_notes": latestVersion.Description,
|
|
"update_strategy": updateStrategy,
|
|
"update_type": deliverType,
|
|
"update_method": latestVersion.UpdateMethod,
|
|
"changelog": latestVersion.Changelog,
|
|
}
|
|
|
|
// 如果是补丁更新,添加补丁信息
|
|
if isPatchUpdate {
|
|
result["is_patch"] = true
|
|
result["patch_url"] = downloadURL
|
|
result["patch_size"] = downloadSize
|
|
result["patch_hash"] = downloadHash
|
|
}
|
|
|
|
// 如果有完整包,添加完整包信息作为备选
|
|
if fullPackageURL != "" {
|
|
result["full_package_url"] = fullPackageURL
|
|
result["full_package_size"] = fullPackageSize
|
|
result["full_package_hash"] = fullPackageHash
|
|
}
|
|
|
|
response.Success(c, result)
|
|
}
|
|
|
|
func handleAppGetAnnouncements(c *gin.Context) {
|
|
appKey := c.Param("appKey")
|
|
var app model.Application
|
|
if err := database.DB.Where("app_key = ?", appKey).First(&app).Error; err != nil {
|
|
response.Error(c, 404, "应用不存在")
|
|
return
|
|
}
|
|
|
|
if service.GetApplicationDisabledStatus(app.ID) {
|
|
response.Error(c, 403, "该应用已被禁用")
|
|
return
|
|
}
|
|
|
|
var announcements []model.Announcement
|
|
if err := database.DB.Where("application_id = ? AND status = ?", app.ID, "active").Order("is_top DESC, created_at DESC").Find(&announcements).Error; err != nil {
|
|
response.Error(c, 500, "获取公告失败")
|
|
return
|
|
}
|
|
|
|
result := make([]gin.H, len(announcements))
|
|
for i, a := range announcements {
|
|
result[i] = gin.H{
|
|
"id": a.ID,
|
|
"title": a.Title,
|
|
"content": a.Content,
|
|
"type": a.Type,
|
|
"is_top": a.IsTop,
|
|
"created_at": a.CreatedAt,
|
|
}
|
|
}
|
|
|
|
response.Success(c, result)
|
|
}
|