123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- package util
- import (
- "bytes"
- "crypto/aes"
- "crypto/cipher"
- "crypto/rand"
- "crypto/tls"
- "encoding/base64"
- "fmt"
- "github.com/astaxie/beego/logs"
- "io"
- "io/ioutil"
- "math"
- "net"
- "net/http"
- "time"
- "github.com/pkg/errors"
- "kpt.xdmy/pkg/log"
- )
- func Bool() {
- }
- func Zero(b bool) int {
- if b {
- return 1
- }
- return 0
- }
- func ZeroStr(b bool, s string) string {
- if b {
- return s
- }
- return ""
- }
- func ParseDate(d string) (res time.Time, err error) {
- if len(d) == 8 {
- d = d[:4] + "-" + d[4:6] + "-" + d[6:]
- }
- res, err = time.ParseInLocation("2006-01-02", d, time.Local)
- if err != nil {
- err = errors.Wrapf(err, "util ParseDate %s", d)
- log.Print(err.Error())
- }
- return
- }
- func ParseDateAndTime(d, t string) (res time.Time, e error) {
- if d == "00000000" {
- return time.Now(), nil
- }
- if len(d) == 8 {
- d = d[:4] + "-" + d[4:6] + "-" + d[6:]
- }
- if len(t) == 6 {
- t = t[:2] + ":" + t[2:4] + ":" + t[4:]
- }
- if t != "" {
- d = d + " " + t
- }
- if res, e = time.ParseInLocation("2006-01-02 15:04:05", d, time.Local); e != nil {
- e = errors.Wrapf(e, "util ParseDateAndTime %s %s", d, t)
- }
- return
- }
- func Unmarshal(p interface{}, res interface{}) (e error) {
- b := []byte{}
- switch p.(type) {
- case string:
- b = []byte(p.(string))
- case []byte:
- b = p.([]byte)
- default:
- if b, e = json.Marshal(p); e != nil {
- return errors.Wrapf(e, "util Unmarshal")
- }
- }
- fmt.Println(string(b))
- e = json.Unmarshal(b, res)
- // defer func(e error) {
- // if e != nil {
- // log.Error("json UnMarshal ", e)
- // }
- // }(e)
- return errors.Wrapf(e, "util Unmarshal")
- }
- func UnmarshalNoflag(p interface{}, res interface{}) (err error) {
- b := make([]byte, 0)
- switch p.(type) {
- case string:
- b = []byte(p.(string))
- case []byte:
- b = p.([]byte)
- default:
- b, err = json.Marshal(p)
- if err != nil {
- return
- }
- }
- err = json.Unmarshal(b, res)
- return
- }
- func Marshal(p interface{}) (b []byte, err error) {
- // json.MarshalIndent(p, "", " ")
- if b, err = json.Marshal(p); err != nil {
- log.Error("json Marshal error:", err, p)
- }
- return
- }
- func JsonMarshal(p interface{}) (r io.Reader, err error) {
- if p == nil {
- return
- }
- var b []byte
- switch p.(type) {
- case string:
- b = []byte(p.(string))
- case []byte:
- b = p.([]byte)
- default:
- b, err = json.Marshal(p)
- if err != nil {
- err = log.Error("failed to marshal", err, p)
- return
- }
- }
- r = bytes.NewBuffer(b)
- return
- }
- func NewMap(name string, value interface{}) map[string]interface{} {
- return map[string]interface{}{
- name: value,
- }
- }
- func Round(number float64, size int) float64 {
- return math.Round(number*100) / 100
- }
- func PostPush(url string, data []byte) ([]byte, error) {
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
- if err != nil {
- logs.Error(err)
- return nil, err
- }
- req.Header.Set("Content-Type", "application/json;charset=UTF-8")
- tr := &http.Transport{
- TLSClientConfig: &tls.Config{
- InsecureSkipVerify: true, // 注意:在生产环境中不要使用此选项,因为它会跳过证书验证
- },
- }
- client := &http.Client{Transport: tr}
- //req.Header.Set("host", "quiplink.liugong.com:443")
- //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")
- //client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- logs.Error(err)
- return nil, err
- }
- defer resp.Body.Close()
- body, _ := ioutil.ReadAll(resp.Body)
- fmt.Println(string(body))
- return body, nil
- }
- func getHostIp() string {
- addrList, err := net.InterfaceAddrs()
- if err != nil {
- fmt.Println("get current host ip err: ", err)
- return ""
- }
- var ip string
- for _, address := range addrList {
- if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
- if ipNet.IP.To4() != nil {
- ip = ipNet.IP.String()
- break
- }
- }
- }
- return ip
- }
- // 加密
- func AesEncryptByECB(originalText string) string {
- key := make([]byte, 16)
- if _, err := io.ReadFull(rand.Reader, key); err != nil {
- panic(err)
- }
- // 创建一个AES块密码(AES-128)
- block, err := aes.NewCipher(key)
- if err != nil {
- panic(err)
- }
- // 使用AES块密码创建一个CBC模式的加密器
- cipherText := make([]byte, aes.BlockSize+len(originalText))
- iv := cipherText[:aes.BlockSize]
- if _, err := io.ReadFull(rand.Reader, iv); err != nil {
- panic(err)
- }
- stream := cipher.NewCFBEncrypter(block, iv)
- stream.XORKeyStream(cipherText[aes.BlockSize:], []byte(originalText))
- return base64.StdEncoding.EncodeToString(cipherText)
- }
|