tools.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package utils
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. "github.com/gogf/gf/crypto/gaes"
  7. "github.com/gogf/gf/encoding/gbase64"
  8. "github.com/gogf/gf/encoding/gcharset"
  9. "github.com/gogf/gf/encoding/gjson"
  10. "github.com/gogf/gf/encoding/gurl"
  11. "github.com/gogf/gf/errors/gerror"
  12. "github.com/gogf/gf/frame/g"
  13. "github.com/gogf/gf/net/ghttp"
  14. "github.com/gogf/gf/os/gtime"
  15. "github.com/gogf/gf/text/gstr"
  16. )
  17. //字符串加密
  18. func EncryptCBC(plainText, publicKey string) string {
  19. key := []byte(publicKey)
  20. b, e := gaes.EncryptCBC([]byte(plainText), key, key)
  21. if e != nil {
  22. g.Log().Error(e.Error())
  23. return ""
  24. }
  25. return gbase64.EncodeToString(b)
  26. }
  27. //字符串解密
  28. func DecryptCBC(plainText, publicKey string) string {
  29. key := []byte(publicKey)
  30. plainTextByte, e := gbase64.DecodeString(plainText)
  31. if e != nil {
  32. g.Log().Error(e.Error())
  33. return ""
  34. }
  35. b, e := gaes.DecryptCBC(plainTextByte, key, key)
  36. if e != nil {
  37. g.Log().Error(e.Error())
  38. return ""
  39. }
  40. return gbase64.EncodeToString(b)
  41. }
  42. //服务端ip
  43. func GetLocalIP() (ip string, err error) {
  44. addrs, err := net.InterfaceAddrs()
  45. if err != nil {
  46. return
  47. }
  48. for _, addr := range addrs {
  49. ipAddr, ok := addr.(*net.IPNet)
  50. if !ok {
  51. continue
  52. }
  53. if ipAddr.IP.IsLoopback() {
  54. continue
  55. }
  56. if !ipAddr.IP.IsGlobalUnicast() {
  57. continue
  58. }
  59. return ipAddr.IP.String(), nil
  60. }
  61. return
  62. }
  63. //获取客户端IP
  64. func GetClientIp(r *ghttp.Request) string {
  65. ip := r.Header.Get("X-Forwarded-For")
  66. if ip == "" {
  67. ip = r.GetClientIp()
  68. }
  69. return ip
  70. }
  71. //获取相差时间
  72. func GetHourDiffer(startTime, endTime string) int64 {
  73. var hour int64
  74. t1, err := time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
  75. t2, err := time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
  76. if err == nil && t1.Before(t2) {
  77. diff := t2.Unix() - t1.Unix() //
  78. hour = diff / 3600
  79. return hour
  80. } else {
  81. return hour
  82. }
  83. }
  84. //日期字符串转时间戳(秒)
  85. func StrToTimestamp(dateStr string) int64 {
  86. tm, err := gtime.StrToTime(dateStr)
  87. if err != nil {
  88. g.Log().Error(err)
  89. return 0
  90. }
  91. return tm.Timestamp()
  92. }
  93. //时间戳转 yyyy-MM-dd HH:mm:ss
  94. func TimeStampToDateTime(timeStamp int64) string {
  95. tm := gtime.NewFromTimeStamp(timeStamp)
  96. return tm.Format("Y-m-d H:i:s")
  97. }
  98. //时间戳转 yyyy-MM-dd
  99. func TimeStampToDate(timeStamp int64) string {
  100. tm := gtime.NewFromTimeStamp(timeStamp)
  101. return tm.Format("Y-m-d")
  102. }
  103. //获取ip所属城市
  104. func GetCityByIp(ip string) string {
  105. if ip == "" {
  106. return ""
  107. }
  108. if ip == "[::1]" || ip == "127.0.0.1" {
  109. return "内网IP"
  110. }
  111. url := "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=" + ip
  112. bytes := ghttp.GetBytes(url)
  113. src := string(bytes)
  114. srcCharset := "GBK"
  115. tmp, _ := gcharset.ToUTF8(srcCharset, src)
  116. json, err := gjson.DecodeToJson(tmp)
  117. if err != nil {
  118. return ""
  119. }
  120. if json.GetInt("code") == 0 {
  121. city := json.GetString("city")
  122. return city
  123. } else {
  124. return ""
  125. }
  126. }
  127. //获取附件真实路径
  128. func GetRealFilesUrl(r *ghttp.Request, path string) (realPath string, err error) {
  129. if gstr.ContainsI(path, "http") {
  130. realPath = path
  131. return
  132. }
  133. realPath, err = GetDomain(r)
  134. if err != nil {
  135. return
  136. }
  137. realPath = realPath + path
  138. return
  139. }
  140. //获取当前请求接口域名
  141. func GetDomain(r *ghttp.Request) (string, error) {
  142. pathInfo, err := gurl.ParseURL(r.GetUrl(), -1)
  143. if err != nil {
  144. g.Log().Error(err)
  145. err = gerror.New("解析附件路径失败")
  146. return "", err
  147. }
  148. return fmt.Sprintf("%s://%s:%s/", pathInfo["scheme"], pathInfo["host"], pathInfo["port"]), nil
  149. }
  150. //获取附件相对路径
  151. func GetFilesPath(fileUrl string) (path string, err error) {
  152. if !gstr.ContainsI(fileUrl, "http") {
  153. path = fileUrl
  154. return
  155. }
  156. pathInfo, err := gurl.ParseURL(fileUrl, 32)
  157. if err != nil {
  158. g.Log().Error(err)
  159. err = gerror.New("解析附件路径失败")
  160. return
  161. }
  162. path = gstr.TrimLeft(pathInfo["path"], "/")
  163. return
  164. }