tools.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package utils
  2. import (
  3. "github.com/gogf/gf/crypto/gaes"
  4. "github.com/gogf/gf/encoding/gbase64"
  5. "github.com/gogf/gf/encoding/gcharset"
  6. "github.com/gogf/gf/encoding/gjson"
  7. "github.com/gogf/gf/frame/g"
  8. "github.com/gogf/gf/net/ghttp"
  9. "github.com/gogf/gf/os/gtime"
  10. "net"
  11. "time"
  12. )
  13. const AdminCbcPublicKey = "HqmP1KLMuz09Q0Bu"
  14. //字符串加密
  15. func EncryptCBC(plainText, publicKey string) string {
  16. key := []byte(publicKey)
  17. b, e := gaes.EncryptCBC([]byte(plainText), key, key)
  18. if e != nil {
  19. g.Log().Error(e.Error())
  20. return ""
  21. }
  22. return gbase64.EncodeToString(b)
  23. }
  24. //字符串解密
  25. func DecryptCBC(plainText, publicKey string) string {
  26. key := []byte(publicKey)
  27. plainTextByte, e := gbase64.DecodeString(plainText)
  28. if e != nil {
  29. g.Log().Error(e.Error())
  30. return ""
  31. }
  32. b, e := gaes.DecryptCBC(plainTextByte, key, key)
  33. if e != nil {
  34. g.Log().Error(e.Error())
  35. return ""
  36. }
  37. return gbase64.EncodeToString(b)
  38. }
  39. //服务端ip
  40. func GetLocalIP() (ip string, err error) {
  41. addrs, err := net.InterfaceAddrs()
  42. if err != nil {
  43. return
  44. }
  45. for _, addr := range addrs {
  46. ipAddr, ok := addr.(*net.IPNet)
  47. if !ok {
  48. continue
  49. }
  50. if ipAddr.IP.IsLoopback() {
  51. continue
  52. }
  53. if !ipAddr.IP.IsGlobalUnicast() {
  54. continue
  55. }
  56. return ipAddr.IP.String(), nil
  57. }
  58. return
  59. }
  60. //获取相差时间
  61. func GetHourDiffer(startTime, endTime string) int64 {
  62. var hour int64
  63. t1, err := time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
  64. t2, err := time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
  65. if err == nil && t1.Before(t2) {
  66. diff := t2.Unix() - t1.Unix() //
  67. hour = diff / 3600
  68. return hour
  69. } else {
  70. return hour
  71. }
  72. }
  73. //日期字符串转时间戳(秒)
  74. func StrToTimestamp(dateStr string) int64 {
  75. tm, err := gtime.StrToTime(dateStr)
  76. if err != nil {
  77. g.Log().Error(err)
  78. return 0
  79. }
  80. return tm.Timestamp()
  81. }
  82. //获取ip所属城市
  83. func GetCityByIp(ip string) string {
  84. if ip == "" {
  85. return ""
  86. }
  87. if ip == "[::1]" || ip == "127.0.0.1" {
  88. return "内网IP"
  89. }
  90. url := "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=" + ip
  91. bytes := ghttp.GetBytes(url)
  92. src := string(bytes)
  93. srcCharset := "GBK"
  94. tmp, _ := gcharset.ToUTF8(srcCharset, src)
  95. json, err := gjson.DecodeToJson(tmp)
  96. if err != nil {
  97. return ""
  98. }
  99. if json.GetInt("code") == 0 {
  100. city := json.GetString("city")
  101. return city
  102. } else {
  103. return ""
  104. }
  105. }