util.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package util
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/rand"
  7. "crypto/tls"
  8. "encoding/base64"
  9. "fmt"
  10. "github.com/astaxie/beego/logs"
  11. "io"
  12. "io/ioutil"
  13. "math"
  14. "net"
  15. "net/http"
  16. "time"
  17. "github.com/pkg/errors"
  18. "kpt.xdmy/pkg/log"
  19. )
  20. func Bool() {
  21. }
  22. func Zero(b bool) int {
  23. if b {
  24. return 1
  25. }
  26. return 0
  27. }
  28. func ZeroStr(b bool, s string) string {
  29. if b {
  30. return s
  31. }
  32. return ""
  33. }
  34. func ParseDate(d string) (res time.Time, err error) {
  35. if len(d) == 8 {
  36. d = d[:4] + "-" + d[4:6] + "-" + d[6:]
  37. }
  38. res, err = time.ParseInLocation("2006-01-02", d, time.Local)
  39. if err != nil {
  40. err = errors.Wrapf(err, "util ParseDate %s", d)
  41. log.Print(err.Error())
  42. }
  43. return
  44. }
  45. func ParseDateAndTime(d, t string) (res time.Time, e error) {
  46. if d == "00000000" {
  47. return time.Now(), nil
  48. }
  49. if len(d) == 8 {
  50. d = d[:4] + "-" + d[4:6] + "-" + d[6:]
  51. }
  52. if len(t) == 6 {
  53. t = t[:2] + ":" + t[2:4] + ":" + t[4:]
  54. }
  55. if t != "" {
  56. d = d + " " + t
  57. }
  58. if res, e = time.ParseInLocation("2006-01-02 15:04:05", d, time.Local); e != nil {
  59. e = errors.Wrapf(e, "util ParseDateAndTime %s %s", d, t)
  60. }
  61. return
  62. }
  63. func Unmarshal(p interface{}, res interface{}) (e error) {
  64. b := []byte{}
  65. switch p.(type) {
  66. case string:
  67. b = []byte(p.(string))
  68. case []byte:
  69. b = p.([]byte)
  70. default:
  71. if b, e = json.Marshal(p); e != nil {
  72. return errors.Wrapf(e, "util Unmarshal")
  73. }
  74. }
  75. fmt.Println(string(b))
  76. e = json.Unmarshal(b, res)
  77. // defer func(e error) {
  78. // if e != nil {
  79. // log.Error("json UnMarshal ", e)
  80. // }
  81. // }(e)
  82. return errors.Wrapf(e, "util Unmarshal")
  83. }
  84. func UnmarshalNoflag(p interface{}, res interface{}) (err error) {
  85. b := make([]byte, 0)
  86. switch p.(type) {
  87. case string:
  88. b = []byte(p.(string))
  89. case []byte:
  90. b = p.([]byte)
  91. default:
  92. b, err = json.Marshal(p)
  93. if err != nil {
  94. return
  95. }
  96. }
  97. err = json.Unmarshal(b, res)
  98. return
  99. }
  100. func Marshal(p interface{}) (b []byte, err error) {
  101. // json.MarshalIndent(p, "", " ")
  102. if b, err = json.Marshal(p); err != nil {
  103. log.Error("json Marshal error:", err, p)
  104. }
  105. return
  106. }
  107. func JsonMarshal(p interface{}) (r io.Reader, err error) {
  108. if p == nil {
  109. return
  110. }
  111. var b []byte
  112. switch p.(type) {
  113. case string:
  114. b = []byte(p.(string))
  115. case []byte:
  116. b = p.([]byte)
  117. default:
  118. b, err = json.Marshal(p)
  119. if err != nil {
  120. err = log.Error("failed to marshal", err, p)
  121. return
  122. }
  123. }
  124. r = bytes.NewBuffer(b)
  125. return
  126. }
  127. func NewMap(name string, value interface{}) map[string]interface{} {
  128. return map[string]interface{}{
  129. name: value,
  130. }
  131. }
  132. func Round(number float64, size int) float64 {
  133. return math.Round(number*100) / 100
  134. }
  135. func PostPush(url string, data []byte) ([]byte, error) {
  136. req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
  137. if err != nil {
  138. logs.Error(err)
  139. return nil, err
  140. }
  141. req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  142. tr := &http.Transport{
  143. TLSClientConfig: &tls.Config{
  144. InsecureSkipVerify: true, // 注意:在生产环境中不要使用此选项,因为它会跳过证书验证
  145. },
  146. }
  147. client := &http.Client{Transport: tr}
  148. //req.Header.Set("host", "quiplink.liugong.com:443")
  149. //req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
  150. //client := &http.Client{}
  151. resp, err := client.Do(req)
  152. if err != nil {
  153. logs.Error(err)
  154. return nil, err
  155. }
  156. defer resp.Body.Close()
  157. body, _ := ioutil.ReadAll(resp.Body)
  158. fmt.Println(string(body))
  159. return body, nil
  160. }
  161. func getHostIp() string {
  162. addrList, err := net.InterfaceAddrs()
  163. if err != nil {
  164. fmt.Println("get current host ip err: ", err)
  165. return ""
  166. }
  167. var ip string
  168. for _, address := range addrList {
  169. if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
  170. if ipNet.IP.To4() != nil {
  171. ip = ipNet.IP.String()
  172. break
  173. }
  174. }
  175. }
  176. return ip
  177. }
  178. // 加密
  179. func AesEncryptByECB(originalText string) string {
  180. key := make([]byte, 16)
  181. if _, err := io.ReadFull(rand.Reader, key); err != nil {
  182. panic(err)
  183. }
  184. // 创建一个AES块密码(AES-128)
  185. block, err := aes.NewCipher(key)
  186. if err != nil {
  187. panic(err)
  188. }
  189. // 使用AES块密码创建一个CBC模式的加密器
  190. cipherText := make([]byte, aes.BlockSize+len(originalText))
  191. iv := cipherText[:aes.BlockSize]
  192. if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  193. panic(err)
  194. }
  195. stream := cipher.NewCFBEncrypter(block, iv)
  196. stream.XORKeyStream(cipherText[aes.BlockSize:], []byte(originalText))
  197. return base64.StdEncoding.EncodeToString(cipherText)
  198. }