diff --git a/go.mod b/go.mod index 0339e13..0b50fc6 100644 --- a/go.mod +++ b/go.mod @@ -5,15 +5,16 @@ go 1.17 require ( github.com/Jeffail/gabs v1.4.0 github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 - github.com/go-ini/ini v1.66.6 + github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/go-ini/ini v1.67.0 github.com/pkg/errors v0.9.1 github.com/spaolacci/murmur3 v1.1.0 - github.com/tidwall/gjson v1.14.1 + github.com/tidwall/gjson v1.14.4 gopkg.in/yaml.v3 v3.0.1 ) require ( github.com/stretchr/testify v1.8.0 // indirect github.com/tidwall/match v1.1.1 // indirect - github.com/tidwall/pretty v1.2.0 // indirect + github.com/tidwall/pretty v1.2.1 // indirect ) diff --git a/go.sum b/go.sum index 0345e8d..5bfea82 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,12 @@ github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIg github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/go-ini/ini v1.66.6 h1:h6k2Bb0HWS/BXXHCXj4QHjxPmlIU4NK+7MuLp9SD+4k= github.com/go-ini/ini v1.66.6/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= +github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= +github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -20,10 +24,14 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo= github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= +github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/init.go b/init.go index 50a808a..9c79a13 100644 --- a/init.go +++ b/init.go @@ -34,6 +34,8 @@ var ( Project *project // 数组操作 Array *array + // JWT ... + JWT *ownJwt ) func init() { @@ -50,4 +52,5 @@ func init() { Calculate = &calculate{} Project = &project{} Array = &array{} + JWT = &ownJwt{} } diff --git a/jwt.go b/jwt.go new file mode 100644 index 0000000..5a03731 --- /dev/null +++ b/jwt.go @@ -0,0 +1,66 @@ +// Package util ... +// +// Description : go-jwt具体实现 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-02-11 20:09 +package util + +import ( + "errors" + + "github.com/dgrijalva/jwt-go" +) + +type ownJwt struct { +} + +type MyClaims struct { + jwt.StandardClaims +} + +// Generate 生成 token +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:13 2023/2/11 +func (j *ownJwt) Generate(signMethod jwt.SigningMethod, secret string, claims jwt.Claims) (string, error) { + if nil == signMethod { + signMethod = jwt.SigningMethodHS256 + } + var ( + token *jwt.Token + ) + + if nil == claims { + token = jwt.New(signMethod) + } else { + token = jwt.NewWithClaims(signMethod, claims) + } + return token.SignedString([]byte(secret)) +} + +// Validate 验证token +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:13 2023/2/11 +func (j *ownJwt) Validate(inputToken string, secret string, claims jwt.Claims) (jwt.Claims, error) { + + var ( + token *jwt.Token + err error + ) + + if token, err = jwt.ParseWithClaims(inputToken, claims, func(tokenInfo *jwt.Token) (interface{}, error) { + return []byte(secret), nil + }); nil != err { + return nil, errors.New("token parse fail : " + err.Error()) + } + if !token.Valid { + return nil, errors.New("token is valid : " + err.Error()) + } + + return token.Claims, nil +} diff --git a/time.go b/time.go index 69741bc..bd3ba82 100644 --- a/time.go +++ b/time.go @@ -26,7 +26,7 @@ type ownTime struct { // // Date : 1:34 上午 2021/10/7 func (ot *ownTime) GetCurrentFormatTime() string { - return time.Now().Format("2006-01-02 15:04:05") + return time.Now().In(time.Local).Format("2006-01-02 15:04:05") } // FormatUnixNano 格式化纳秒时间戳 @@ -46,8 +46,7 @@ func (ot *ownTime) FormatUnixNano(timestamp int64) string { // // Date : 11:55 2022/7/14 func (ot *ownTime) FormatUnixMilli(timestamp int64) string { - time.Now().UnixMilli() - return time.UnixMilli(timestamp).Format("2006-01-02 15:04:05.000") + return time.UnixMilli(timestamp).In(time.Local).Format("2006-01-02 15:04:05.000") } // FormatUnixSec ... @@ -56,7 +55,7 @@ func (ot *ownTime) FormatUnixMilli(timestamp int64) string { // // Date : 12:06 2022/7/14 func (ot *ownTime) FormatUnixSec(timestamp int64) string { - return time.UnixMilli(timestamp).Format("2006-01-02 15:04:05") + return time.Unix(timestamp, 0).In(time.Local).Format("2006-01-02 15:04:05") } // ParseISO8601Time 解析 2006-01-02T15:04:05+08:00 格式时间 -> 2006-01-02 15:04:05 @@ -75,5 +74,5 @@ func (ot *ownTime) ParseISO8601Time(parseTime string) string { return "" } - return formatTime.Format("2006-01-02 15:04:05") + return formatTime.In(time.Local).Format("2006-01-02 15:04:05") }