utils.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * @desc:工具
  3. * @company:云南奇讯科技有限公司
  4. * @Author: yixiaohu
  5. * @Date: 2022/3/4 22:16
  6. */
  7. package libUtils
  8. import (
  9. "context"
  10. "fmt"
  11. "github.com/gogf/gf/v2/crypto/gmd5"
  12. "github.com/gogf/gf/v2/encoding/gcharset"
  13. "github.com/gogf/gf/v2/encoding/gjson"
  14. "github.com/gogf/gf/v2/encoding/gurl"
  15. "github.com/gogf/gf/v2/frame/g"
  16. "github.com/gogf/gf/v2/net/ghttp"
  17. "net"
  18. "os"
  19. "path"
  20. "strings"
  21. )
  22. // EncryptPassword 密码加密
  23. func EncryptPassword(password, salt string) string {
  24. return gmd5.MustEncryptString(gmd5.MustEncryptString(password) + gmd5.MustEncryptString(salt))
  25. }
  26. // GetDomain 获取当前请求接口域名
  27. func GetDomain(ctx context.Context) string {
  28. r := g.RequestFromCtx(ctx)
  29. pathInfo, err := gurl.ParseURL(r.GetUrl(), -1)
  30. if err != nil {
  31. g.Log().Error(ctx, err)
  32. return ""
  33. }
  34. return fmt.Sprintf("%s://%s:%s/", pathInfo["scheme"], pathInfo["host"], pathInfo["port"])
  35. }
  36. // GetClientIp 获取客户端IP
  37. func GetClientIp(ctx context.Context) string {
  38. return g.RequestFromCtx(ctx).GetClientIp()
  39. }
  40. // GetUserAgent 获取user-agent
  41. func GetUserAgent(ctx context.Context) string {
  42. return ghttp.RequestFromCtx(ctx).Header.Get("User-Agent")
  43. }
  44. // GetLocalIP 服务端ip
  45. func GetLocalIP() (ip string, err error) {
  46. var addrs []net.Addr
  47. addrs, err = net.InterfaceAddrs()
  48. if err != nil {
  49. return
  50. }
  51. for _, addr := range addrs {
  52. ipAddr, ok := addr.(*net.IPNet)
  53. if !ok {
  54. continue
  55. }
  56. if ipAddr.IP.IsLoopback() {
  57. continue
  58. }
  59. if !ipAddr.IP.IsGlobalUnicast() {
  60. continue
  61. }
  62. return ipAddr.IP.String(), nil
  63. }
  64. return
  65. }
  66. // GetCityByIp 获取ip所属城市
  67. func GetCityByIp(ip string) string {
  68. if ip == "" {
  69. return ""
  70. }
  71. if ip == "[::1]" || ip == "127.0.0.1" {
  72. return "内网IP"
  73. }
  74. url := "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=" + ip
  75. bytes := g.Client().GetBytes(context.TODO(), url)
  76. src := string(bytes)
  77. srcCharset := "GBK"
  78. tmp, _ := gcharset.ToUTF8(srcCharset, src)
  79. json, err := gjson.DecodeToJson(tmp)
  80. if err != nil {
  81. return ""
  82. }
  83. if json.Get("code").Int() == 0 {
  84. city := fmt.Sprintf("%s %s", json.Get("pro").String(), json.Get("city").String())
  85. return city
  86. } else {
  87. return ""
  88. }
  89. }
  90. // WriteToFile 写入文件
  91. func WriteToFile(fileName string, content string) error {
  92. f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
  93. if err != nil {
  94. return err
  95. }
  96. n, _ := f.Seek(0, os.SEEK_END)
  97. _, err = f.WriteAt([]byte(content), n)
  98. //defer f.Close()
  99. return err
  100. }
  101. // FileIsExisted 文件或文件夹是否存在
  102. func FileIsExisted(filename string) bool {
  103. existed := true
  104. if _, err := os.Stat(filename); os.IsNotExist(err) {
  105. existed = false
  106. }
  107. return existed
  108. }
  109. // ParseFilePath 解析路径获取文件名称及后缀
  110. func ParseFilePath(pathStr string) (fileName string, fileType string) {
  111. fileNameWithSuffix := path.Base(pathStr)
  112. fileType = path.Ext(fileNameWithSuffix)
  113. fileName = strings.TrimSuffix(fileNameWithSuffix, fileType)
  114. return
  115. }