update utils

master
李光春 2 years ago
parent bf400e3ace
commit cd08e0aa17

@ -1,8 +1,6 @@
## v1.0.33 / 2021-12-25 ## v1.0.34 / 2021-12-25
- 优化 preg 方法 - update utils
- 优化 aes 方法
- 增加 base64 方法
## v1.0.31 / 2021-12-24 ## v1.0.31 / 2021-12-24

@ -1,5 +1,5 @@
package go_library package go_library
func Version() string { func Version() string {
return "v1.0.33" return "v1.0.34"
} }

@ -6,7 +6,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"github.com/bitly/go-simplejson" "github.com/bitly/go-simplejson"
params3 "github.com/dtapps/go-library/utils/params" "github.com/dtapps/go-library/utils/goparams"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -60,7 +60,7 @@ func sign(params Parameter, customerKey string) string {
query := bytes.NewBufferString(customerKey) query := bytes.NewBufferString(customerKey)
for _, k := range keys { for _, k := range keys {
query.WriteString(k) query.WriteString(k)
query.WriteString(params3.GetParamsString(params[k])) query.WriteString(goparams.GetParamsString(params[k]))
} }
// MD5加密 // MD5加密
h := md5.New() h := md5.New()

@ -1 +0,0 @@
app_test.go

@ -1 +0,0 @@
pinduoduo_test.go

@ -5,7 +5,7 @@ import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"github.com/bitly/go-simplejson" "github.com/bitly/go-simplejson"
params3 "github.com/dtapps/go-library/utils/params" "github.com/dtapps/go-library/utils/goparams"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -61,7 +61,7 @@ func sign(params Parameter, clientSecret string) string {
query := bytes.NewBufferString(clientSecret) query := bytes.NewBufferString(clientSecret)
for _, k := range keys { for _, k := range keys {
query.WriteString(k) query.WriteString(k)
query.WriteString(params3.GetParamsString(params[k])) query.WriteString(goparams.GetParamsString(params[k]))
} }
query.WriteString(clientSecret) query.WriteString(clientSecret)
// MD5加密 // MD5加密
@ -89,7 +89,7 @@ func (p Parameter) getRequestData() string {
args := url.Values{} args := url.Values{}
// 请求参数 // 请求参数
for key, val := range p { for key, val := range p {
args.Set(key, params3.GetParamsString(val)) args.Set(key, goparams.GetParamsString(val))
} }
return args.Encode() return args.Encode()
} }

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/dtapps/go-library/utils/request" "github.com/dtapps/go-library/utils/gorequest"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
) )
@ -49,7 +49,7 @@ func (app *App) request(url string, params map[string]interface{}, method string
return nil, result, err return nil, result, err
} }
req.Header.Add("Authorization", "WECHATPAY2-SHA256-RSA2048 "+authorization) 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("Content-Type", "application/json")
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("Accept-Language", "zh-CN") req.Header.Add("Accept-Language", "zh-CN")

@ -1,4 +1,4 @@
package decimal package godecimal
import ( import (
"fmt" "fmt"

@ -1,10 +1,9 @@
package decimal package godecimal
import ( import (
"fmt"
"testing" "testing"
) )
func TestName(t *testing.T) { func TestName(t *testing.T) {
fmt.Println(Decimal(2.3333)) t.Log(Decimal(2.3333))
} }

@ -1,6 +1,7 @@
package files package gofiles
import ( import (
"log"
"os" "os"
) )
@ -71,3 +72,30 @@ func CreateFiles(path string, perm int) (bool, error) {
return true, nil 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
}

@ -1,4 +1,4 @@
package header package goheader
import ( import (
"encoding/json" "encoding/json"

@ -5,13 +5,12 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/dtapps/go-library/utils/gorequest"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"github.com/dtapps/go-library/utils/request"
) )
type Response struct { type Response struct {
@ -36,7 +35,7 @@ func Get(url string, params map[string]interface{}) (httpResponse Response, err
req.URL.RawQuery = q.Encode() req.URL.RawQuery = q.Encode()
} }
// 设置请求头 // 设置请求头
req.Header.Set("User-Agent", request.GetRandomUserAgent()) req.Header.Set("User-Agent", gorequest.GetRandomUserAgent())
// 发送请求 // 发送请求
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { 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, _ := 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") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// 发送请求 // 发送请求
resp, err := client.Do(req) 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, _ := 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") req.Header.Set("Content-Type", "application/json")
// 创建 http 客户端 // 创建 http 客户端
client := &http.Client{} client := &http.Client{}

@ -1,14 +1,13 @@
package gohttp package gohttp
import ( import (
"fmt"
"testing" "testing"
) )
func TestName(t *testing.T) { func TestName(t *testing.T) {
get, err := Get("https://api.dtapp.net/", nil) get, err := Get("https://api.dtapp.net/", nil)
t.Logf("%+v\n", get)
if err != nil { if err != nil {
return return
} }
fmt.Printf("%#v\n", get)
} }

@ -1,4 +1,4 @@
package mail package gomail
import ( import (
"fmt" "fmt"

@ -1,8 +1,9 @@
package params package goparams
import ( import (
"encoding/json" "encoding/json"
"github.com/nilorg/sdk/convert" "github.com/nilorg/sdk/convert"
"log"
"net/url" "net/url"
) )
@ -50,7 +51,7 @@ func GetParamsString(src interface{}) string {
} }
data, err := json.Marshal(src) data, err := json.Marshal(src)
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
return string(data) return string(data)
} }

@ -1,4 +1,4 @@
package request package gorequest
import ( import (
"errors" "errors"

@ -1,10 +1,9 @@
package request package gorequest
import ( import (
"fmt"
"testing" "testing"
) )
func TestExternalIp(t *testing.T) { func TestExternalIp(t *testing.T) {
fmt.Println(ExternalIp()) t.Log(ExternalIp())
} }

@ -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
}

@ -1,4 +1,4 @@
package rsa package gorsa
import ( import (
"crypto/rand" "crypto/rand"

@ -1,4 +1,4 @@
package url package gourl
import ( import (
"io" "io"

@ -1,4 +1,4 @@
package url package gourl
import ( import (
"log" "log"

@ -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
}
Loading…
Cancel
Save