diff --git a/CHANGELOG.md b/CHANGELOG.md index 58849ce6..893b5769 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,6 @@ -## v1.0.33 / 2021-12-25 +## v1.0.34 / 2021-12-25 -- 优化 preg 方法 -- 优化 aes 方法 -- 增加 base64 方法 +- update utils ## v1.0.31 / 2021-12-24 diff --git a/library.go b/library.go index 4bf2fc72..0e532fbd 100644 --- a/library.go +++ b/library.go @@ -1,5 +1,5 @@ package go_library func Version() string { - return "v1.0.33" + return "v1.0.34" } diff --git a/service/kashangwl/kashangwl.go b/service/kashangwl/kashangwl.go index b1c3c1ab..1295f529 100644 --- a/service/kashangwl/kashangwl.go +++ b/service/kashangwl/kashangwl.go @@ -6,7 +6,7 @@ import ( "encoding/hex" "encoding/json" "github.com/bitly/go-simplejson" - params3 "github.com/dtapps/go-library/utils/params" + "github.com/dtapps/go-library/utils/goparams" "io" "io/ioutil" "net/http" @@ -60,7 +60,7 @@ func sign(params Parameter, customerKey string) string { query := bytes.NewBufferString(customerKey) for _, k := range keys { query.WriteString(k) - query.WriteString(params3.GetParamsString(params[k])) + query.WriteString(goparams.GetParamsString(params[k])) } // MD5加密 h := md5.New() diff --git a/service/movieapiv2/.gitignore b/service/movieapiv2/.gitignore deleted file mode 100644 index b59718f5..00000000 --- a/service/movieapiv2/.gitignore +++ /dev/null @@ -1 +0,0 @@ -app_test.go \ No newline at end of file diff --git a/service/pinduoduo/.gitignore b/service/pinduoduo/.gitignore deleted file mode 100644 index 0589bbb8..00000000 --- a/service/pinduoduo/.gitignore +++ /dev/null @@ -1 +0,0 @@ -pinduoduo_test.go \ No newline at end of file diff --git a/service/pinduoduo/pinduoduo.go b/service/pinduoduo/pinduoduo.go index aeca96f4..cf4596dd 100644 --- a/service/pinduoduo/pinduoduo.go +++ b/service/pinduoduo/pinduoduo.go @@ -5,7 +5,7 @@ import ( "crypto/md5" "encoding/hex" "github.com/bitly/go-simplejson" - params3 "github.com/dtapps/go-library/utils/params" + "github.com/dtapps/go-library/utils/goparams" "io" "io/ioutil" "net/http" @@ -61,7 +61,7 @@ func sign(params Parameter, clientSecret string) string { query := bytes.NewBufferString(clientSecret) for _, k := range keys { query.WriteString(k) - query.WriteString(params3.GetParamsString(params[k])) + query.WriteString(goparams.GetParamsString(params[k])) } query.WriteString(clientSecret) // MD5加密 @@ -89,7 +89,7 @@ func (p Parameter) getRequestData() string { args := url.Values{} // 请求参数 for key, val := range p { - args.Set(key, params3.GetParamsString(val)) + args.Set(key, goparams.GetParamsString(val)) } return args.Encode() } diff --git a/service/wechatpayapiv3/app.go b/service/wechatpayapiv3/app.go index 991995f2..a869512a 100644 --- a/service/wechatpayapiv3/app.go +++ b/service/wechatpayapiv3/app.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/dtapps/go-library/utils/request" + "github.com/dtapps/go-library/utils/gorequest" "io/ioutil" "net/http" ) @@ -49,7 +49,7 @@ func (app *App) request(url string, params map[string]interface{}, method string return nil, result, err } req.Header.Add("Authorization", "WECHATPAY2-SHA256-RSA2048 "+authorization) - req.Header.Add("User-Agent", request.GetRandomUserAgent()) + req.Header.Add("User-Agent", gorequest.GetRandomUserAgent()) req.Header.Add("Content-Type", "application/json") req.Header.Add("Accept", "application/json") req.Header.Add("Accept-Language", "zh-CN") diff --git a/utils/decimal/decimal.go b/utils/godecimal/godecimal.go similarity index 90% rename from utils/decimal/decimal.go rename to utils/godecimal/godecimal.go index 5fd0f590..63451eea 100644 --- a/utils/decimal/decimal.go +++ b/utils/godecimal/godecimal.go @@ -1,4 +1,4 @@ -package decimal +package godecimal import ( "fmt" diff --git a/utils/decimal/decimal_test.go b/utils/godecimal/godecimal_test.go similarity index 51% rename from utils/decimal/decimal_test.go rename to utils/godecimal/godecimal_test.go index 23f81351..ad5cda15 100644 --- a/utils/decimal/decimal_test.go +++ b/utils/godecimal/godecimal_test.go @@ -1,10 +1,9 @@ -package decimal +package godecimal import ( - "fmt" "testing" ) func TestName(t *testing.T) { - fmt.Println(Decimal(2.3333)) + t.Log(Decimal(2.3333)) } diff --git a/utils/files/files.go b/utils/gofiles/gofiles.go similarity index 70% rename from utils/files/files.go rename to utils/gofiles/gofiles.go index 877d2f08..b4e5332d 100644 --- a/utils/files/files.go +++ b/utils/gofiles/gofiles.go @@ -1,6 +1,7 @@ -package files +package gofiles import ( + "log" "os" ) @@ -71,3 +72,30 @@ func CreateFiles(path string, perm int) (bool, error) { return true, nil } } + +// DirExist 判断目录是否存在,存在返回 true +func DirExist(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} + +// CreateDir 创建目录 +func CreateDir(path string) error { + dirExist, err := DirExist(path) + if err != nil { + return err + } + if !dirExist { + err := os.Mkdir(path, os.ModePerm) + if err != nil { + log.Printf("创建[%s]目录失败: %s\n", path, err) + } + } + return err +} diff --git a/utils/header/header.go b/utils/goheader/goheader.go similarity index 97% rename from utils/header/header.go rename to utils/goheader/goheader.go index 9dba8720..a3a9dd6b 100644 --- a/utils/header/header.go +++ b/utils/goheader/goheader.go @@ -1,4 +1,4 @@ -package header +package goheader import ( "encoding/json" diff --git a/utils/gohttp/gohttp.go b/utils/gohttp/gohttp.go index fe21cfab..3e417ede 100644 --- a/utils/gohttp/gohttp.go +++ b/utils/gohttp/gohttp.go @@ -5,13 +5,12 @@ import ( "encoding/json" "errors" "fmt" + "github.com/dtapps/go-library/utils/gorequest" "io/ioutil" "net/http" "net/url" "strconv" "strings" - - "github.com/dtapps/go-library/utils/request" ) type Response struct { @@ -36,7 +35,7 @@ func Get(url string, params map[string]interface{}) (httpResponse Response, err req.URL.RawQuery = q.Encode() } // 设置请求头 - req.Header.Set("User-Agent", request.GetRandomUserAgent()) + req.Header.Set("User-Agent", gorequest.GetRandomUserAgent()) // 发送请求 resp, err := client.Do(req) if err != nil { @@ -71,7 +70,7 @@ func PostForm(targetUrl string, params map[string]interface{}) (httpResponse Res // 创建请求 req, _ := http.NewRequest(http.MethodPost, targetUrl, strings.NewReader(form.Encode())) // 设置请求头 - req.Header.Set("User-Agent", request.GetRandomUserAgent()) + req.Header.Set("User-Agent", gorequest.GetRandomUserAgent()) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") // 发送请求 resp, err := client.Do(req) @@ -98,7 +97,7 @@ func PostJson(targetUrl string, paramsStr []byte) (httpResponse Response, err er // 创建请求 req, _ := http.NewRequest(http.MethodPost, targetUrl, bytes.NewBuffer(paramsStr)) // 设置请求头 - req.Header.Set("User-Agent", request.GetRandomUserAgent()) + req.Header.Set("User-Agent", gorequest.GetRandomUserAgent()) req.Header.Set("Content-Type", "application/json") // 创建 http 客户端 client := &http.Client{} diff --git a/utils/gohttp/gohttp_test.go b/utils/gohttp/gohttp_test.go index 46e342f3..a8b6ce82 100644 --- a/utils/gohttp/gohttp_test.go +++ b/utils/gohttp/gohttp_test.go @@ -1,14 +1,13 @@ package gohttp import ( - "fmt" "testing" ) func TestName(t *testing.T) { get, err := Get("https://api.dtapp.net/", nil) + t.Logf("%+v\n", get) if err != nil { return } - fmt.Printf("%#v\n", get) } diff --git a/utils/mail/mail.go b/utils/gomail/gomail.go similarity index 98% rename from utils/mail/mail.go rename to utils/gomail/gomail.go index b89f0943..83345504 100644 --- a/utils/mail/mail.go +++ b/utils/gomail/gomail.go @@ -1,4 +1,4 @@ -package mail +package gomail import ( "fmt" diff --git a/utils/params/params.go b/utils/goparams/goparams.go similarity index 95% rename from utils/params/params.go rename to utils/goparams/goparams.go index 72cc368b..1ed46245 100644 --- a/utils/params/params.go +++ b/utils/goparams/goparams.go @@ -1,8 +1,9 @@ -package params +package goparams import ( "encoding/json" "github.com/nilorg/sdk/convert" + "log" "net/url" ) @@ -50,7 +51,7 @@ func GetParamsString(src interface{}) string { } data, err := json.Marshal(src) if err != nil { - panic(err) + log.Fatal(err) } return string(data) } diff --git a/utils/request/request.go b/utils/gorequest/gorequest.go similarity index 99% rename from utils/request/request.go rename to utils/gorequest/gorequest.go index 29fe348f..bb9946c7 100644 --- a/utils/request/request.go +++ b/utils/gorequest/gorequest.go @@ -1,4 +1,4 @@ -package request +package gorequest import ( "errors" diff --git a/utils/request/request_test.go b/utils/gorequest/gorequest_test.go similarity index 55% rename from utils/request/request_test.go rename to utils/gorequest/gorequest_test.go index 855a254c..0202896f 100644 --- a/utils/request/request_test.go +++ b/utils/gorequest/gorequest_test.go @@ -1,10 +1,9 @@ -package request +package gorequest import ( - "fmt" "testing" ) func TestExternalIp(t *testing.T) { - fmt.Println(ExternalIp()) + t.Log(ExternalIp()) } diff --git a/utils/goround/goround.go b/utils/goround/goround.go new file mode 100644 index 00000000..897d2bca --- /dev/null +++ b/utils/goround/goround.go @@ -0,0 +1,13 @@ +package goround + +import ( + "fmt" + "strconv" +) + +// FloatRound 截取小数位数 +func FloatRound(f float64, n int) float64 { + format := "%." + strconv.Itoa(n) + "f" + res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64) + return res +} diff --git a/utils/rsa/rsa.go b/utils/gorsa/gorsa.go similarity index 99% rename from utils/rsa/rsa.go rename to utils/gorsa/gorsa.go index 891be13f..1bdd8d64 100644 --- a/utils/rsa/rsa.go +++ b/utils/gorsa/gorsa.go @@ -1,4 +1,4 @@ -package rsa +package gorsa import ( "crypto/rand" diff --git a/utils/url/url.go b/utils/gourl/gourl.go similarity index 98% rename from utils/url/url.go rename to utils/gourl/gourl.go index a099e7be..2c2d2cf7 100644 --- a/utils/url/url.go +++ b/utils/gourl/gourl.go @@ -1,4 +1,4 @@ -package url +package gourl import ( "io" diff --git a/utils/url/url_test.go b/utils/gourl/gourl_test.go similarity index 93% rename from utils/url/url_test.go rename to utils/gourl/gourl_test.go index a467041a..33e3e331 100644 --- a/utils/url/url_test.go +++ b/utils/gourl/gourl_test.go @@ -1,4 +1,4 @@ -package url +package gourl import ( "log" diff --git a/utils/phpjson/phpjson.go b/utils/phpjson/phpjson.go new file mode 100644 index 00000000..16d8eafa --- /dev/null +++ b/utils/phpjson/phpjson.go @@ -0,0 +1,50 @@ +package phpjson + +import ( + "encoding/json" + "fmt" + "reflect" + "strings" +) + +func Encode(v interface{}) (string, error) { + bytes, err := json.Marshal(v) + if err != nil { + return "", err + } + return string(bytes), nil +} + +func MarshalToString(msg interface{}) (string, error) { + j, e := json.Marshal(msg) + if e != nil { + return "", e + } + return string(j), nil +} + +func Implode(list interface{}, seq string) string { + listValue := reflect.Indirect(reflect.ValueOf(list)) + if listValue.Kind() != reflect.Slice { + return "" + } + count := listValue.Len() + listStr := make([]string, 0, count) + for i := 0; i < count; i++ { + v := listValue.Index(i) + if str, err := getValue(v); err == nil { + listStr = append(listStr, str) + } + } + return strings.Join(listStr, seq) +} + +func getValue(value reflect.Value) (res string, err error) { + switch value.Kind() { + case reflect.Ptr: + res, err = getValue(value.Elem()) + default: + res = fmt.Sprint(value.Interface()) + } + return +}