- update json、string、random

master
李光春 2 years ago
parent b506aab9ef
commit d48e235d44

@ -1,4 +1,10 @@
## v1.0.27 / 2021-12-18
## v1.0.28 / 2021-12-18
- 优化随机数方法
- 优化字符串方法
- 优化json方法
## v1.0.27 / 2021-12-17
- 优化时间方法

@ -11,5 +11,5 @@
## Install 安装
```Importing
go get -v -u github.com/dtapps/go-library@v1.0.27
go get -v -u github.com/dtapps/go-library@v1.0.28
```

@ -8,7 +8,7 @@ import (
"fmt"
"github.com/dtapps/go-library/service/dingtalk/config"
"github.com/dtapps/go-library/service/dingtalk/message"
utilsJson "github.com/dtapps/go-library/utils/json"
"github.com/dtapps/go-library/utils/gojson"
"io/ioutil"
"net/http"
"strings"
@ -20,17 +20,17 @@ type DingBot struct {
AccessToken string
}
type response struct {
type Response struct {
Errcode int64 `json:"errcode"`
Errmsg string `json:"errmsg"`
}
func (bot *DingBot) Send(msg message.Message) (response, error) {
func (bot *DingBot) Send(msg message.Message) (Response, error) {
timestamp := time.Now().UnixNano() / 1e6
var response response
var response Response
signStr := sign(timestamp, bot.Secret)
dingUrl := fmt.Sprintf("%s?access_token=%s&timestamp=%d&sign=%s", config.Api, bot.AccessToken, timestamp, signStr)
toString, err := utilsJson.MarshalToString(msg)
toString, err := gojson.MarshalToString(msg)
if err != nil {
return response, err
}

@ -5,7 +5,7 @@ import (
"fmt"
"github.com/dtapps/go-library/service/qywechat/config"
"github.com/dtapps/go-library/service/qywechat/message"
utilsJson "github.com/dtapps/go-library/utils/json"
"github.com/dtapps/go-library/utils/gojson"
"io/ioutil"
"net/http"
"strings"
@ -15,7 +15,7 @@ type QyBot struct {
Key string
}
type response struct {
type Response struct {
Errcode int64 `json:"errcode"`
Errmsg string `json:"errmsg"`
Type string `json:"type"`
@ -23,10 +23,10 @@ type response struct {
CreatedAt string `json:"created_at"`
}
func (bot *QyBot) Send(msg message.Message) (response, error) {
var response response
func (bot *QyBot) Send(msg message.Message) (Response, error) {
var response Response
qyUrl := fmt.Sprintf("%s?key=%s", config.Api, bot.Key)
toString, err := utilsJson.MarshalToString(msg)
toString, err := gojson.MarshalToString(msg)
if err != nil {
return response, err
}

@ -2,7 +2,7 @@ package wechatpayapiv3
import (
"fmt"
"github.com/dtapps/go-library/utils/random"
"github.com/dtapps/go-library/utils/gorandom"
"io/ioutil"
"os"
"time"
@ -28,7 +28,7 @@ func (app *App) GetJsApi(param GetJsApi) (params GetJsApiResult, err error) {
// sign params
timeStamp := time.Now().Unix()
nonce := random.Alphanumeric(32)
nonce := gorandom.Alphanumeric(32)
params.AppId = app.AppId
params.TimeStamp = fmt.Sprintf("%v", timeStamp)

@ -13,7 +13,7 @@ import (
"encoding/pem"
"errors"
"fmt"
"github.com/dtapps/go-library/utils/random"
"github.com/dtapps/go-library/utils/gorandom"
"io/ioutil"
"net/url"
"os"
@ -70,7 +70,7 @@ func (app *App) authorization(method string, paramMap map[string]interface{}, ra
}
canonicalUrl := urlPart.RequestURI()
timestamp := time.Now().Unix()
nonce := random.Alphanumeric(32)
nonce := gorandom.Alphanumeric(32)
message := fmt.Sprintf("%s\n%s\n%d\n%s\n%s\n", method, canonicalUrl, timestamp, nonce, body)
open, err := os.Open(app.MchPrivateKey) // 商户私有证书路径或者从数据库读取
if err != nil {

@ -0,0 +1,41 @@
package gojson
import "encoding/json"
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 JsonDecode(data string) (map[string]interface{}, error) {
var dat map[string]interface{}
err := json.Unmarshal([]byte(data), &dat)
return dat, err
}
func JsonDecodeNoError(data string) map[string]interface{} {
var dat map[string]interface{}
_ = json.Unmarshal([]byte(data), &dat)
return dat
}
func JsonEncode(data interface{}) (string, error) {
jsons, err := json.Marshal(data)
return string(jsons), err
}
func JsonEncodeNoError(data interface{}) string {
jsons, _ := json.Marshal(data)
return string(jsons)
}

@ -1,4 +1,4 @@
package random
package gorandom
import (
"math/rand"

@ -1,4 +1,4 @@
package random
package gorandom
import (
"fmt"

@ -0,0 +1,114 @@
package gostring
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"strconv"
"strings"
"unicode/utf8"
)
// ToFloat64 string到float64
func ToFloat64(s string) float64 {
i, _ := strconv.ParseFloat(s, 64)
return i
}
// ToInt string到int
func ToInt(s string) int {
i, _ := strconv.Atoi(s)
return i
}
// ToInt64 string到int64
func ToInt64(s string) int64 {
i, _ := strconv.ParseInt(s, 10, 64)
return i
}
// Replace 字符串替换
func Replace(str, old, new string) string {
return strings.Replace(str, old, new, -1)
}
func HmacSha256Hex(key, strToSign string) string {
hasHer := hmac.New(sha256.New, []byte(key))
hasHer.Write([]byte(strToSign))
return hex.EncodeToString(hasHer.Sum(nil))
}
// Space 去除空格
func Space(str string) string {
return strings.Replace(str, " ", "", -1)
}
// LineBreak 去除换行符
func LineBreak(str string) string {
return strings.Replace(str, "\n", "", -1)
}
// SpaceAndLineBreak 去除空格和去除换行符
func SpaceAndLineBreak(str string) string {
return LineBreak(Space(str))
}
// TrimLastChar 删除字符串中的最后一个
func TrimLastChar(s string) string {
r, size := utf8.DecodeLastRuneInString(s)
if r == utf8.RuneError && (size == 0 || size == 1) {
size = 0
}
return s[:len(s)-size]
}
// Split 字符串分隔
func Split(s string, sep string) []string {
return strings.Split(s, sep)
}
func NumericalToString(value interface{}) (string, bool) {
var val string
switch value.(type) {
default:
return "0", false
case int:
intVal, _ := value.(int)
val = strconv.FormatInt(int64(intVal), 10)
case int8:
intVal, _ := value.(int8)
val = strconv.FormatInt(int64(intVal), 10)
case int16:
intVal, _ := value.(int16)
val = strconv.FormatInt(int64(intVal), 10)
case int32:
intVal, _ := value.(int32)
val = strconv.FormatInt(int64(intVal), 10)
case int64:
intVal, _ := value.(int64)
val = strconv.FormatInt(int64(intVal), 10)
case uint:
intVal, _ := value.(uint)
val = strconv.FormatUint(uint64(intVal), 10)
case uint8:
intVal, _ := value.(uint8)
val = strconv.FormatUint(uint64(intVal), 10)
case uint16:
intVal, _ := value.(uint16)
val = strconv.FormatUint(uint64(intVal), 10)
case uint32:
intVal, _ := value.(uint32)
val = strconv.FormatUint(uint64(intVal), 10)
case uint64:
intVal, _ := value.(uint64)
val = strconv.FormatUint(uint64(intVal), 10)
case float32:
floatVal, _ := value.(float32)
val = strconv.FormatFloat(float64(floatVal), 'f', -1, 32)
case float64:
floatVal, _ := value.(float64)
val = strconv.FormatFloat(float64(floatVal), 'f', -1, 64)
}
return val, true
}

@ -0,0 +1,38 @@
package gostring
import (
"fmt"
"log"
"strings"
"testing"
)
func TestName(t *testing.T) {
str := "iPhone 11 Pro Max<iPhone12,5>"
fmt.Printf("%d\n", strings.LastIndex(str, "<"))
fmt.Printf("%d\n", strings.LastIndex(str, "("))
fmt.Printf("%d\n", strings.LastIndex("iPad (6th generation, WiFi)<iPad7,5>", "<"))
fmt.Printf("%d\n", strings.LastIndex("iPad (6th generation, WiFi)<iPad7,5>", "("))
s := str[0:17]
fmt.Printf("%s\n", s)
str = "iPad (6th generation, WiFi)<iPad7,5>"
s = str[0:5]
fmt.Printf("%s\n", s)
fmt.Printf(strings.TrimSpace(s))
}
func TestToInt64(t *testing.T) {
log.Println(ToInt64("120"))
log.Println(ToInt64("120.9"))
}
func TestString(t *testing.T) {
str := "wx6566ef69e8738ad9"
fmt.Println(strings.Contains(str, "wx"))
myString := "www.5lmh.com"
if strings.HasPrefix(myString, "www") {
fmt.Println("Hello to you too")
} else {
fmt.Println("Goodbye")
}
}

@ -1,19 +0,0 @@
package json
import "encoding/json"
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
}

@ -1,22 +0,0 @@
package string
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"strings"
)
func HmacSha256Hex(key, strToSign string) string {
hasher := hmac.New(sha256.New, []byte(key))
hasher.Write([]byte(strToSign))
return hex.EncodeToString(hasher.Sum(nil))
}
func ToLower(str string) string {
return strings.ToLower(str)
}
func ToUpper(str string) string {
return strings.ToUpper(str)
}
Loading…
Cancel
Save