You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.1 KiB
Go

package utils
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func GetClientIP(c *gin.Context) string {
realIP := c.GetHeader("X-Forwarded-For")
if realIP != "" {
ips := strings.Split(realIP, ",")
for _, ip := range ips {
ip = strings.TrimSpace(ip)
if ip != "" && ip != "127.0.0.1" {
return ip
}
}
}
realIP = c.GetHeader("X-Real-IP")
if realIP != "" && realIP != "127.0.0.1" {
return realIP
}
return c.ClientIP()
}
type ZiUtil struct{}
var Zi = &ZiUtil{}
func (z *ZiUtil) Exit(c *gin.Context, code int, message string, data interface{}) {
c.JSON(http.StatusOK, gin.H{
"code": code,
"message": message,
"data": data,
})
}
func (z *ZiUtil) Echo(c *gin.Context, data interface{}) {
Zi.Exit(c, 200, "ok", data)
}
func (z *ZiUtil) Error(c *gin.Context, message string, code ...int) {
errCode := 100001
if len(code) > 0 {
errCode = code[0]
}
Zi.Exit(c, errCode, message, gin.H{})
}
func (z *ZiUtil) Debug(c *gin.Context, data interface{}) {
Zi.Exit(c, 100000, "debug", data)
}
func (z *ZiUtil) Html(c *gin.Context, name string, data interface{}) {
c.HTML(200, name, data)
}