diff --git a/ChangeLog.md b/ChangeLog.md index 8c76022..b764dbe 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -2,6 +2,7 @@ Change Log 更新说明 ------------------------------ ## 2019-09-12 v1.2.4 1. gf升级为V2.0.0 +2. 优化resp目录 ## 2019-09-12 v1.2.3 1. gf升级为V1.8.3 diff --git a/example/boot/router.go b/example/boot/router.go index 9529960..ffb0ef5 100644 --- a/example/boot/router.go +++ b/example/boot/router.go @@ -5,7 +5,6 @@ import ( "github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/os/glog" "gtoken/gtoken" - "gtoken/utils/resp" ) /* @@ -16,10 +15,10 @@ func bindRouter() { s := g.Server() // 调试路由 s.BindHandler("/hello", func(r *ghttp.Request) { - r.Response.WriteJson(resp.Succ("hello")) + r.Response.WriteJson(gtoken.Succ("hello")) }) s.BindHandler("/system/user", func(r *ghttp.Request) { - r.Response.WriteJson(resp.Succ("system user")) + r.Response.WriteJson(gtoken.Succ("system user")) }) loginFunc := Login @@ -70,7 +69,7 @@ func Login(r *ghttp.Request) (string, interface{}) { passwd := r.GetPostString("passwd") if username == "" || passwd == "" { - r.Response.WriteJson(resp.Fail("账号或密码错误.")) + r.Response.WriteJson(gtoken.Fail("账号或密码错误.")) r.ExitAll() } diff --git a/example/test/api_test.go b/example/test/api_test.go index 6282b86..a5f6ecd 100644 --- a/example/test/api_test.go +++ b/example/test/api_test.go @@ -4,7 +4,7 @@ import ( "encoding/json" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ghttp" - "gtoken/utils/resp" + "gtoken/gtoken" "testing" ) @@ -27,7 +27,7 @@ func TestHello(t *testing.T) { content := string(r.ReadAll()) t.Log(content) - var respData resp.Resp + var respData gtoken.Resp err := json.Unmarshal([]byte(content), &respData) if err != nil { t.Error(err) @@ -49,7 +49,7 @@ func TestSystemUser(t *testing.T) { content := string(r.ReadAll()) t.Log(content) - var respData resp.Resp + var respData gtoken.Resp err := json.Unmarshal([]byte(content), &respData) if err != nil { t.Error(err) @@ -137,11 +137,11 @@ func TestLogout(t *testing.T) { Username = "flyfox" } -func Post(t *testing.T, urlPath string, data ...interface{}) resp.Resp { +func Post(t *testing.T, urlPath string, data ...interface{}) gtoken.Resp { client := ghttp.NewClient() client.SetHeader("Authorization", "Bearer "+getToken(t)) content := client.RequestContent("POST", TestURL+urlPath, data...) - var respData resp.Resp + var respData gtoken.Resp err := json.Unmarshal([]byte(content), &respData) if err != nil { t.Error(err) @@ -161,7 +161,7 @@ func getToken(t *testing.T) string { content := string(r.ReadAll()) - var respData resp.Resp + var respData gtoken.Resp err := json.Unmarshal([]byte(content), &respData) if err != nil { t.Error(err) diff --git a/gtoken/gtoken.go b/gtoken/gtoken.go index c6a42e3..11cc785 100644 --- a/gtoken/gtoken.go +++ b/gtoken/gtoken.go @@ -11,7 +11,6 @@ import ( "github.com/gogf/gf/text/gstr" "github.com/gogf/gf/util/gconv" "github.com/gogf/gf/util/grand" - "gtoken/utils/resp" "strings" ) @@ -40,7 +39,7 @@ type GfToken struct { // return userKey 用户标识 如果userKey为空,结束执行 LoginBeforeFunc func(r *ghttp.Request) (string, interface{}) // 登录返回方法 - LoginAfterFunc func(r *ghttp.Request, respData resp.Resp) + LoginAfterFunc func(r *ghttp.Request, respData Resp) // 登出地址 LogoutPath string @@ -48,7 +47,7 @@ type GfToken struct { // return true 继续执行,否则结束执行 LogoutBeforeFunc func(r *ghttp.Request) bool // 登出返回方法 - LogoutAfterFunc func(r *ghttp.Request, respData resp.Resp) + LogoutAfterFunc func(r *ghttp.Request, respData Resp) // 拦截地址 AuthPaths g.SliceStr @@ -56,7 +55,7 @@ type GfToken struct { // return true 继续执行,否则结束执行 AuthBeforeFunc func(r *ghttp.Request) bool // 认证返回方法 - AuthAfterFunc func(r *ghttp.Request, respData resp.Resp) + AuthAfterFunc func(r *ghttp.Request, respData Resp) } // Init 初始化 @@ -86,11 +85,11 @@ func (m *GfToken) Init() bool { } if m.LoginAfterFunc == nil { - m.LoginAfterFunc = func(r *ghttp.Request, respData resp.Resp) { + m.LoginAfterFunc = func(r *ghttp.Request, respData Resp) { if !respData.Success() { r.Response.WriteJson(respData) } else { - r.Response.WriteJson(resp.Succ(g.Map{ + r.Response.WriteJson(Succ(g.Map{ "token": respData.GetString("token"), })) } @@ -104,9 +103,9 @@ func (m *GfToken) Init() bool { } if m.LogoutAfterFunc == nil { - m.LogoutAfterFunc = func(r *ghttp.Request, respData resp.Resp) { + m.LogoutAfterFunc = func(r *ghttp.Request, respData Resp) { if respData.Success() { - r.Response.WriteJson(resp.Succ("logout success")) + r.Response.WriteJson(Succ("logout success")) } else { r.Response.WriteJson(respData) } @@ -119,7 +118,7 @@ func (m *GfToken) Init() bool { } } if m.AuthAfterFunc == nil { - m.AuthAfterFunc = func(r *ghttp.Request, respData resp.Resp) { + m.AuthAfterFunc = func(r *ghttp.Request, respData Resp) { if !respData.Success() { r.Response.WriteJson(respData) r.ExitAll() @@ -178,7 +177,7 @@ func (m *GfToken) Stop() bool { } // GetTokenData 通过token获取对象 -func (m *GfToken) GetTokenData(r *ghttp.Request) resp.Resp { +func (m *GfToken) GetTokenData(r *ghttp.Request) Resp { respData := m.getRequestToken(r) if respData.Success() { // 验证token @@ -228,31 +227,31 @@ func (m *GfToken) authHook(r *ghttp.Request) { } // getRequestToken 返回请求Token -func (m *GfToken) getRequestToken(r *ghttp.Request) resp.Resp { +func (m *GfToken) getRequestToken(r *ghttp.Request) Resp { authHeader := r.Header.Get("Authorization") if authHeader != "" { parts := strings.SplitN(authHeader, " ", 2) if !(len(parts) == 2 && parts[0] == "Bearer") { glog.Warning("[GToken]authHeader:" + authHeader + " get token key fail") - return resp.Unauthorized("get token key fail", "") + return Unauthorized("get token key fail", "") } else if parts[1] == "" { glog.Warning("[GToken]authHeader:" + authHeader + " get token fail") - return resp.Unauthorized("get token fail", "") + return Unauthorized("get token fail", "") } - return resp.Succ(parts[1]) + return Succ(parts[1]) } authHeader = r.GetPostString("token") if authHeader == "" { - return resp.Unauthorized("query token fail", "") + return Unauthorized("query token fail", "") } - return resp.Succ(authHeader) + return Succ(authHeader) } // genToken 生成Token -func (m *GfToken) genToken(userKey string, data interface{}) resp.Resp { +func (m *GfToken) genToken(userKey string, data interface{}) Resp { token := m.EncryptToken(userKey) if !token.Success() { return token @@ -276,9 +275,9 @@ func (m *GfToken) genToken(userKey string, data interface{}) resp.Resp { } // validToken 验证Token -func (m *GfToken) validToken(token string) resp.Resp { +func (m *GfToken) validToken(token string) Resp { if token == "" { - return resp.Unauthorized("valid token empty", "") + return Unauthorized("valid token empty", "") } decryptToken := m.DecryptToken(token) @@ -298,7 +297,7 @@ func (m *GfToken) validToken(token string) resp.Resp { if uuid != userCache["uuid"] { glog.Error("[GToken]user auth error, decryptToken:" + decryptToken.Json() + " cacheValue:" + gconv.String(userCache)) - return resp.Unauthorized("user auth error", "") + return Unauthorized("user auth error", "") } nowTime := gtime.Now().Millisecond() @@ -312,11 +311,11 @@ func (m *GfToken) validToken(token string) resp.Resp { return m.setCache(cacheKey, userCache) } - return resp.Succ(userCache) + return Succ(userCache) } // removeToken 删除Token -func (m *GfToken) removeToken(token string) resp.Resp { +func (m *GfToken) removeToken(token string) Resp { decryptToken := m.DecryptToken(token) if !decryptToken.Success() { return decryptToken @@ -327,25 +326,25 @@ func (m *GfToken) removeToken(token string) resp.Resp { } // EncryptToken token加密方法 -func (m *GfToken) EncryptToken(userKey string) resp.Resp { +func (m *GfToken) EncryptToken(userKey string) Resp { if userKey == "" { - return resp.Fail("encrypt userKey empty") + return Fail("encrypt userKey empty") } uuid, err := gmd5.Encrypt(grand.Str(10)) if err != nil { glog.Error("[GToken]uuid error", err) - return resp.Error("uuid error") + return Error("uuid error") } tokenStr := userKey + m.TokenDelimiter + uuid token, err := gaes.Encrypt([]byte(tokenStr), m.EncryptKey) if err != nil { glog.Error("[GToken]encrypt error", err) - return resp.Error("encrypt error") + return Error("encrypt error") } - return resp.Succ(g.Map{ + return Succ(g.Map{ "userKey": userKey, "uuid": uuid, "token": gbase64.Encode(token), @@ -353,28 +352,28 @@ func (m *GfToken) EncryptToken(userKey string) resp.Resp { } // DecryptToken token解密方法 -func (m *GfToken) DecryptToken(token string) resp.Resp { +func (m *GfToken) DecryptToken(token string) Resp { if token == "" { - return resp.Fail("decrypt token empty") + return Fail("decrypt token empty") } token64, err := gbase64.Decode([]byte(token)) if err != nil { glog.Error("[GToken]decode error", err) - return resp.Error("decode error") + return Error("decode error") } decryptToken, err2 := gaes.Decrypt([]byte(token64), m.EncryptKey) if err2 != nil { glog.Error("[GToken]decrypt error", err2) - return resp.Error("decrypt error") + return Error("decrypt error") } tokenArray := gstr.Split(string(decryptToken), m.TokenDelimiter) if len(tokenArray) < 2 { glog.Error("[GToken]token len error") - return resp.Error("token len error") + return Error("token len error") } - return resp.Succ(g.Map{ + return Succ(g.Map{ "userKey": tokenArray[0], "uuid": tokenArray[1], }) diff --git a/gtoken/gtoken_cache.go b/gtoken/gtoken_cache.go index 6f4ba7d..0f6b361 100644 --- a/gtoken/gtoken_cache.go +++ b/gtoken/gtoken_cache.go @@ -6,11 +6,10 @@ import ( "github.com/gogf/gf/os/gcache" "github.com/gogf/gf/os/glog" "github.com/gogf/gf/util/gconv" - "gtoken/utils/resp" ) // setCache 设置缓存 -func (m *GfToken) setCache(cacheKey string, userCache g.Map) resp.Resp { +func (m *GfToken) setCache(cacheKey string, userCache g.Map) Resp { switch m.CacheMode { case CacheModeCache: gcache.Set(cacheKey, userCache, m.Timeout) @@ -18,54 +17,54 @@ func (m *GfToken) setCache(cacheKey string, userCache g.Map) resp.Resp { cacheValueJson, err1 := gjson.Encode(userCache) if err1 != nil { glog.Error("[GToken]cache json encode error", err1) - return resp.Error("cache json encode error") + return Error("cache json encode error") } _, err := g.Redis().Do("SETEX", cacheKey, m.Timeout, cacheValueJson) if err != nil { glog.Error("[GToken]cache set error", err) - return resp.Error("cache set error") + return Error("cache set error") } default: - return resp.Error("cache model error") + return Error("cache model error") } - return resp.Succ(userCache) + return Succ(userCache) } // getCache 获取缓存 -func (m *GfToken) getCache(cacheKey string) resp.Resp { +func (m *GfToken) getCache(cacheKey string) Resp { var userCache g.Map switch m.CacheMode { case CacheModeCache: userCacheValue := gcache.Get(cacheKey) if userCacheValue == nil { - return resp.Unauthorized("login timeout or not login", "") + return Unauthorized("login timeout or not login", "") } userCache = gconv.Map(userCacheValue) case CacheModeRedis: userCacheJson, err := g.Redis().Do("GET", cacheKey) if err != nil { glog.Error("[GToken]cache get error", err) - return resp.Error("cache get error") + return Error("cache get error") } if userCacheJson == nil { - return resp.Unauthorized("login timeout or not login", "") + return Unauthorized("login timeout or not login", "") } err = gjson.DecodeTo(userCacheJson, &userCache) if err != nil { glog.Error("[GToken]cache get json error", err) - return resp.Error("cache get json error") + return Error("cache get json error") } default: - return resp.Error("cache model error") + return Error("cache model error") } - return resp.Succ(userCache) + return Succ(userCache) } // removeCache 删除缓存 -func (m *GfToken) removeCache(cacheKey string) resp.Resp { +func (m *GfToken) removeCache(cacheKey string) Resp { switch m.CacheMode { case CacheModeCache: gcache.Remove(cacheKey) @@ -74,11 +73,11 @@ func (m *GfToken) removeCache(cacheKey string) resp.Resp { _, err = g.Redis().Do("DEL", cacheKey) if err != nil { glog.Error("[GToken]cache remove error", err) - return resp.Error("cache remove error") + return Error("cache remove error") } default: - return resp.Error("cache model error") + return Error("cache model error") } - return resp.Succ("") + return Succ("") } diff --git a/utils/resp/resp.go b/gtoken/gtoken_resp.go similarity index 99% rename from utils/resp/resp.go rename to gtoken/gtoken_resp.go index 1db20b6..79e11af 100644 --- a/utils/resp/resp.go +++ b/gtoken/gtoken_resp.go @@ -1,4 +1,4 @@ -package resp +package gtoken import ( "encoding/json"