user_util.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package utils
  2. import (
  3. "fmt"
  4. "go.uber.org/zap"
  5. "net"
  6. "net/http"
  7. "regexp"
  8. "sfbase/utils"
  9. "strings"
  10. )
  11. var (
  12. userAppIDEncrypt = &utils.SimpleEncrypt{"sfis20212120"}
  13. strReg = regexp.MustCompile("^[0-9a-zA-Z]+$")
  14. )
  15. func GetAppID(tn int64) (appID string) {
  16. for {
  17. randomstr := utils.GetLetterRandom(5)
  18. str := fmt.Sprintf("%s%d%s", randomstr[:2], tn, randomstr[2:])
  19. appID = userAppIDEncrypt.EncodeString(str)
  20. if strReg.MatchString(appID) {
  21. break
  22. }
  23. }
  24. appID = "sf" + appID
  25. return
  26. }
  27. func GetIp(req *http.Request) string {
  28. if req == nil {
  29. return ""
  30. }
  31. ip_for := req.Header.Get("x-forwarded-for")
  32. ip_client := req.Header.Get("http_client_ip")
  33. ip_addr := req.Header.Get("Remote_addr")
  34. un := "unknown"
  35. if (ip_for != un) && (len(strings.TrimSpace(ip_for)) > 0) {
  36. return ip_for
  37. }
  38. if (ip_client != un) && (len(strings.TrimSpace(ip_client)) > 0) {
  39. return ip_client
  40. }
  41. if (ip_addr != un) && (len(strings.TrimSpace(ip_addr)) > 0) {
  42. return ip_addr
  43. }
  44. ip, _, _ := net.SplitHostPort(req.RemoteAddr)
  45. return ip
  46. }
  47. func getUserProductError(appID string, productID int, err error) []zap.Field {
  48. arr := make([]zap.Field, 0)
  49. arr = append(arr, zap.Any("appID:", appID))
  50. arr = append(arr, zap.Any("productID:", productID))
  51. arr = append(arr, zap.Any("error:", err))
  52. return arr
  53. }
  54. func getUserProductInfo(appID string, productID int, key string, info interface{}) []zap.Field {
  55. arr := make([]zap.Field, 0)
  56. arr = append(arr, zap.Any("appID:", appID))
  57. arr = append(arr, zap.Any("productID:", productID))
  58. arr = append(arr, zap.Any(key, info))
  59. return arr
  60. }