tools.go 3.8 KB

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