mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-20 00:00:27 +08:00
refactor: oauth2登录重构
This commit is contained in:
@@ -3,15 +3,23 @@ package jsonx
|
||||
import (
|
||||
"encoding/json"
|
||||
"mayfly-go/pkg/global"
|
||||
"strings"
|
||||
|
||||
"github.com/buger/jsonparser"
|
||||
)
|
||||
|
||||
// json字符串转map
|
||||
func ToMap(jsonStr string) map[string]any {
|
||||
return ToMapByBytes([]byte(jsonStr))
|
||||
}
|
||||
|
||||
// json字节数组转map
|
||||
func ToMapByBytes(bytes []byte) map[string]any {
|
||||
var res map[string]any
|
||||
if jsonStr == "" {
|
||||
return res
|
||||
err := json.Unmarshal(bytes, &res)
|
||||
if err != nil {
|
||||
global.Log.Errorf("json字符串转map失败: %s", err.Error())
|
||||
}
|
||||
_ = json.Unmarshal([]byte(jsonStr), &res)
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -24,3 +32,45 @@ func ToStr(val any) string {
|
||||
return string(strBytes)
|
||||
}
|
||||
}
|
||||
|
||||
// 根据json字节数组获取对应字段路径的string类型值
|
||||
//
|
||||
// @param fieldPath字段路径。如user.username等
|
||||
func GetStringByBytes(bytes []byte, fieldPath string) (string, error) {
|
||||
return jsonparser.GetString(bytes, strings.Split(fieldPath, ".")...)
|
||||
}
|
||||
|
||||
// 根据json字符串获取对应字段路径的string类型值
|
||||
//
|
||||
// @param fieldPath字段路径。如user.username等
|
||||
func GetString(jsonStr string, fieldPath string) (string, error) {
|
||||
return GetStringByBytes([]byte(jsonStr), fieldPath)
|
||||
}
|
||||
|
||||
// 根据json字节数组获取对应字段路径的int类型值
|
||||
//
|
||||
// @param fieldPath字段路径。如user.age等
|
||||
func GetIntByBytes(bytes []byte, fieldPath string) (int64, error) {
|
||||
return jsonparser.GetInt(bytes, strings.Split(fieldPath, ".")...)
|
||||
}
|
||||
|
||||
// 根据json字符串获取对应字段路径的int类型值
|
||||
//
|
||||
// @param fieldPath字段路径。如user.age等
|
||||
func GetInt(jsonStr string, fieldPath string) (int64, error) {
|
||||
return GetIntByBytes([]byte(jsonStr), fieldPath)
|
||||
}
|
||||
|
||||
// 根据json字节数组获取对应字段路径的bool类型值
|
||||
//
|
||||
// @param fieldPath字段路径。如user.isDeleted等
|
||||
func GetBoolByBytes(bytes []byte, fieldPath string) (bool, error) {
|
||||
return jsonparser.GetBoolean(bytes, strings.Split(fieldPath, ".")...)
|
||||
}
|
||||
|
||||
// 根据json字符串获取对应字段路径的bool类型值
|
||||
//
|
||||
// @param fieldPath字段路径。如user.isDeleted等
|
||||
func GetBool(jsonStr string, fieldPath string) (bool, error) {
|
||||
return GetBoolByBytes([]byte(jsonStr), fieldPath)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user