diff --git a/country.go b/country.go index 4973ff2..621af45 100644 --- a/country.go +++ b/country.go @@ -10,6 +10,7 @@ package consts import ( _ "embed" "encoding/json" + "fmt" ) type Country struct { @@ -55,6 +56,31 @@ func (country Country) GetLanguage() string { return country.Language } +// GetNationalFlag 获取国家国旗链接 +func (country Country) GetNationalFlag(width uint, height uint) string { + supportSizeList := []string{ + "16x12", "20x15", "24x18", "28x21", + "32x24", "36x27", "40x30", "48x36", + "56x42", "60x45", "64x48", "72x54", + "80x60", "84x63", "96x72", "108x81", + "112x84", "120x90", "128x96", "144x108", + "160x120", "192x144", "224x168", "256x192", + } + size := fmt.Sprintf("%vx%v", width, height) + isSupportSize := false + for _, itemSize := range supportSizeList { + if itemSize == size { + isSupportSize = true + break + } + } + if !isSupportSize { + // 不支持 + return "" + } + return fmt.Sprintf("https://flagcdn.com/%vx%v/%v.png", width, height, country.ShortLower) +} + //go:embed country.json var countryJSON []byte