package utils import ( "fmt" "go.uber.org/zap" "net" "net/http" "regexp" "sfbase/utils" "strings" ) var ( userAppIDEncrypt = &utils.SimpleEncrypt{"sfis20212120"} strReg = regexp.MustCompile("^[0-9a-zA-Z]+$") ) func GetAppID(tn int64) (appID string) { for { randomstr := utils.GetLetterRandom(5) str := fmt.Sprintf("%s%d%s", randomstr[:2], tn, randomstr[2:]) appID = userAppIDEncrypt.EncodeString(str) if strReg.MatchString(appID) { break } } appID = "sf" + appID return } func GetIp(req *http.Request) string { if req == nil { return "" } ip_for := req.Header.Get("x-forwarded-for") ip_client := req.Header.Get("http_client_ip") ip_addr := req.Header.Get("Remote_addr") un := "unknown" if (ip_for != un) && (len(strings.TrimSpace(ip_for)) > 0) { return ip_for } if (ip_client != un) && (len(strings.TrimSpace(ip_client)) > 0) { return ip_client } if (ip_addr != un) && (len(strings.TrimSpace(ip_addr)) > 0) { return ip_addr } ip, _, _ := net.SplitHostPort(req.RemoteAddr) return ip } func getUserProductError(appID string, productID int, err error) []zap.Field { arr := make([]zap.Field, 0) arr = append(arr, zap.Any("appID:", appID)) arr = append(arr, zap.Any("productID:", productID)) arr = append(arr, zap.Any("error:", err)) return arr } func getUserProductInfo(appID string, productID int, key string, info interface{}) []zap.Field { arr := make([]zap.Field, 0) arr = append(arr, zap.Any("appID:", appID)) arr = append(arr, zap.Any("productID:", productID)) arr = append(arr, zap.Any(key, info)) return arr }