feat: manage global app release policies

This commit is contained in:
richjohnrambo
2026-09-12 17:08:10 +08:00
parent 2fb5102add
commit 6e65935e3c
8 changed files with 220 additions and 74 deletions
@@ -2,10 +2,13 @@ package sysin
import (
"context"
"net/url"
"strconv"
"strings"
"hotgo/internal/consts"
"hotgo/internal/library/dict"
"hotgo/internal/model/input/form"
"strings"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gtime"
@@ -13,7 +16,6 @@ import (
type ImAppReleaseListInp struct {
form.PageReq
TenantId int64 `json:"tenantId" dc:"租户ID"`
Platform string `json:"platform" dc:"平台"`
Channel string `json:"channel" dc:"渠道"`
}
@@ -28,8 +30,6 @@ func (in *ImAppReleaseListInp) Filter(ctx context.Context) (err error) {
}
type ImAppReleaseModel struct {
TenantId int64 `json:"tenantId" dc:"租户ID"`
TenantName string `json:"tenantName" dc:"租户名称"`
Id int64 `json:"id" dc:"ID"`
Platform string `json:"platform" dc:"平台"`
Channel string `json:"channel" dc:"渠道"`
@@ -47,7 +47,6 @@ type ImAppReleaseModel struct {
}
type ImAppReleaseEditInp struct {
TenantId int64 `json:"tenantId" dc:"租户ID"`
Id int64 `json:"id" dc:"ID"`
Platform string `json:"platform" v:"required#平台不能为空" dc:"平台"`
Channel string `json:"channel" v:"required#渠道不能为空" dc:"渠道"`
@@ -82,11 +81,73 @@ func (in *ImAppReleaseEditInp) Filter(ctx context.Context) (err error) {
if in.MinSupportedVersion == "" || len([]rune(in.MinSupportedVersion)) > 64 {
return gerror.New("最低支持版本不能为空且不能超过64个字符")
}
latest, err := parseAppVersion(in.LatestVersion)
if err != nil {
return gerror.New("最新版本号格式不正确,请使用数字点分版本号")
}
minimum, err := parseAppVersion(in.MinSupportedVersion)
if err != nil {
return gerror.New("最低支持版本号格式不正确,请使用数字点分版本号")
}
if compareAppVersion(latest, minimum) < 0 {
return gerror.New("最新版本不能低于最低支持版本")
}
if !dict.HasOptionKey(consts.ImAppUpdateTypeOptions, in.UpdateType) {
return gerror.New("更新类型不合法")
}
if in.RemindAfterSeconds < 0 {
return gerror.New("再次提醒时间不能小于0")
}
if in.Platform == consts.ImAppPlatformIos && in.UpdateType != consts.ImAppUpdateTypeNone {
storeURL, parseErr := url.Parse(in.StoreUrl)
if parseErr != nil || storeURL.Scheme != "https" || !strings.EqualFold(storeURL.Hostname(), "apps.apple.com") {
return gerror.New("iOS更新策略需要配置 apps.apple.com 的 HTTPS 商店地址")
}
in.PackageUrl = ""
in.Checksum = ""
}
return nil
}
func parseAppVersion(raw string) ([]uint64, error) {
raw = strings.TrimPrefix(strings.ToLower(strings.TrimSpace(raw)), "v")
parts := strings.Split(raw, ".")
if len(parts) == 0 || len(parts) > 8 {
return nil, gerror.New("版本号格式不正确")
}
result := make([]uint64, len(parts))
for i, part := range parts {
if part == "" {
return nil, gerror.New("版本号格式不正确")
}
value, err := strconv.ParseUint(part, 10, 32)
if err != nil {
return nil, err
}
result[i] = value
}
return result, nil
}
func compareAppVersion(left, right []uint64) int {
length := len(left)
if len(right) > length {
length = len(right)
}
for i := 0; i < length; i++ {
var l, r uint64
if i < len(left) {
l = left[i]
}
if i < len(right) {
r = right[i]
}
if l < r {
return -1
}
if l > r {
return 1
}
}
return 0
}
@@ -0,0 +1,39 @@
package sysin
import "testing"
func TestImAppReleaseEditFilter(t *testing.T) {
tests := []struct {
name string
input ImAppReleaseEditInp
wantErr bool
}{
{
name: "valid ios release",
input: ImAppReleaseEditInp{Platform: "ios", Channel: "stable", LatestVersion: "1.10.0", MinSupportedVersion: "1.9.9", UpdateType: "recommended", StoreUrl: "https://apps.apple.com/app/id123"},
},
{
name: "minimum newer than latest",
input: ImAppReleaseEditInp{Platform: "android", Channel: "stable", LatestVersion: "1.2.0", MinSupportedVersion: "2.0.0", UpdateType: "force"},
wantErr: true,
},
{
name: "invalid version",
input: ImAppReleaseEditInp{Platform: "android", Channel: "stable", LatestVersion: "1.2-beta", MinSupportedVersion: "1.0.0", UpdateType: "recommended"},
wantErr: true,
},
{
name: "ios store url required",
input: ImAppReleaseEditInp{Platform: "ios", Channel: "stable", LatestVersion: "2.0.0", MinSupportedVersion: "1.0.0", UpdateType: "force", StoreUrl: "https://example.com/app"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.input.Filter(nil)
if (err != nil) != tt.wantErr {
t.Fatalf("Filter() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}