tools.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. //获取客户端IP
  61. func GetClientIp(r *ghttp.Request) string {
  62. ip := r.Header.Get("X-Forwarded-For")
  63. if ip == "" {
  64. ip = r.GetClientIp()
  65. }
  66. return ip
  67. }
  68. //获取相差时间
  69. func GetHourDiffer(startTime, endTime string) int64 {
  70. var hour int64
  71. t1, err := time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
  72. t2, err := time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
  73. if err == nil && t1.Before(t2) {
  74. diff := t2.Unix() - t1.Unix() //
  75. hour = diff / 3600
  76. return hour
  77. } else {
  78. return hour
  79. }
  80. }
  81. //日期字符串转时间戳(秒)
  82. func StrToTimestamp(dateStr string) int64 {
  83. tm, err := gtime.StrToTime(dateStr)
  84. if err != nil {
  85. g.Log().Error(err)
  86. return 0
  87. }
  88. return tm.Timestamp()
  89. }
  90. //获取ip所属城市
  91. func GetCityByIp(ip string) string {
  92. if ip == "" {
  93. return ""
  94. }
  95. if ip == "[::1]" || ip == "127.0.0.1" {
  96. return "内网IP"
  97. }
  98. url := "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=" + ip
  99. bytes := ghttp.GetBytes(url)
  100. src := string(bytes)
  101. srcCharset := "GBK"
  102. tmp, _ := gcharset.ToUTF8(srcCharset, src)
  103. json, err := gjson.DecodeToJson(tmp)
  104. if err != nil {
  105. return ""
  106. }
  107. if json.GetInt("code") == 0 {
  108. city := json.GetString("city")
  109. return city
  110. } else {
  111. return ""
  112. }
  113. }