package utils import ( "github.com/gogf/gf/crypto/gaes" "github.com/gogf/gf/encoding/gbase64" "github.com/gogf/gf/encoding/gcharset" "github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/os/gtime" "net" "time" ) const AdminCbcPublicKey = "HqmP1KLMuz09Q0Bu" //字符串加密 func EncryptCBC(plainText, publicKey string) string { key := []byte(publicKey) b, e := gaes.EncryptCBC([]byte(plainText), key, key) if e != nil { g.Log().Error(e.Error()) return "" } return gbase64.EncodeToString(b) } //字符串解密 func DecryptCBC(plainText, publicKey string) string { key := []byte(publicKey) plainTextByte, e := gbase64.DecodeString(plainText) if e != nil { g.Log().Error(e.Error()) return "" } b, e := gaes.DecryptCBC(plainTextByte, key, key) if e != nil { g.Log().Error(e.Error()) return "" } return gbase64.EncodeToString(b) } //服务端ip func GetLocalIP() (ip string, err error) { addrs, err := net.InterfaceAddrs() if err != nil { return } for _, addr := range addrs { ipAddr, ok := addr.(*net.IPNet) if !ok { continue } if ipAddr.IP.IsLoopback() { continue } if !ipAddr.IP.IsGlobalUnicast() { continue } return ipAddr.IP.String(), nil } return } //获取相差时间 func GetHourDiffer(startTime, endTime string) int64 { var hour int64 t1, err := time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local) t2, err := time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local) if err == nil && t1.Before(t2) { diff := t2.Unix() - t1.Unix() // hour = diff / 3600 return hour } else { return hour } } //日期字符串转时间戳(秒) func StrToTimestamp(dateStr string) int64 { tm, err := gtime.StrToTime(dateStr) if err != nil { g.Log().Error(err) return 0 } return tm.Timestamp() } //获取ip所属城市 func GetCityByIp(ip string) string { if ip == "" { return "" } if ip == "[::1]" || ip == "127.0.0.1" { return "内网IP" } url := "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=" + ip bytes := ghttp.GetBytes(url) src := string(bytes) srcCharset := "GBK" tmp, _ := gcharset.ToUTF8(srcCharset, src) json, err := gjson.DecodeToJson(tmp) if err != nil { return "" } if json.GetInt("code") == 0 { city := json.GetString("city") return city } else { return "" } }