diff --git a/helper/jwt.go b/helper/jwt.go index d9bcc8a2..785f6a7c 100644 --- a/helper/jwt.go +++ b/helper/jwt.go @@ -38,7 +38,7 @@ func JwtValidate(tokenString string) map[string]interface{} { // JwtMake 生成 JWT // subject options[0], audience options[1], issuer options[1] -func JwtMake(id int, data map[string]interface{}, timeout int64, options ...string) string { +func JwtMake(id int, data map[string]interface{}, timeout int64, options ...string) map[string]interface{} { now := time.Now().Unix() expiresAt := now + timeout uid := fmt.Sprintf("%d", id) @@ -73,7 +73,10 @@ func JwtMake(id int, data map[string]interface{}, timeout int64, options ...stri if err != nil { exception.New("生成令牌失败", 500).Ctx(err).Throw() } - return tokenString + return map[string]interface{}{ + "token": tokenString, + "expires_at": expiresAt, + } } // ProcessJwtMake xiang.helper.JwtMake 生成JWT diff --git a/helper/jwt_test.go b/helper/jwt_test.go index 17cd6e30..ae29d502 100644 --- a/helper/jwt_test.go +++ b/helper/jwt_test.go @@ -10,7 +10,8 @@ import ( func TestJwt(t *testing.T) { data := map[string]interface{}{"hello": "world", "id": 1} - tokenString := JwtMake(1, data, 1, "Unit Test", "Test", "UnitTest") + token := JwtMake(1, data, 1, "Unit Test", "Test", "UnitTest") + tokenString := token["token"].(string) res := JwtValidate(tokenString) assert.Equal(t, float64(1), res["id"]) assert.Equal(t, "world", res["hello"]) @@ -22,8 +23,8 @@ func TestProcessJwt(t *testing.T) { data := map[string]interface{}{"hello": "world", "id": 1} args := []interface{}{1, data, 1, "Unit Test", "Test", "UnitTest"} process := gou.NewProcess("xiang.helper.JwtMake", args...) - token := process.Run() - tokenString := token.(string) + token := process.Run().(map[string]interface{}) + tokenString := token["token"].(string) res := gou.NewProcess("xiang.helper.JwtValidate", tokenString).Run().(map[string]interface{}) assert.Equal(t, float64(1), res["id"]) assert.Equal(t, "world", res["hello"])