ip.go 842 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package ip
  2. import (
  3. game "bet24.com/servers/micros/game/proto"
  4. "fmt"
  5. "strings"
  6. )
  7. // 根据IP获取国家地区地理位置
  8. func GetCountryAndRegion(ipAddress string, isWrap bool) string {
  9. switch ipAddress {
  10. case "":
  11. return "未知"
  12. case "::1":
  13. return fmt.Sprintf("%s%s", ipAddress, "(未知)")
  14. }
  15. // 获取IP地理位置
  16. country, region := game.GetCountryAndRegion(ipAddress)
  17. //log.Debug("GetCountryAndRegion ipAddress=%s isWrap=%v country=%s region=%s", ipAddress, isWrap, country, region)
  18. var build strings.Builder
  19. build.WriteString(ipAddress)
  20. if isWrap {
  21. build.WriteString("<br/>")
  22. }
  23. build.WriteString("(")
  24. build.WriteString(country)
  25. region = strings.ReplaceAll(region, "未知", "")
  26. if len(region) > 0 {
  27. build.WriteString("|")
  28. build.WriteString(region)
  29. }
  30. build.WriteString(")")
  31. return build.String()
  32. }