diff --git a/auto/api/v1/web_core.go b/auto/api/v1/web_core.go index 613977fe..8b61e383 100644 --- a/auto/api/v1/web_core.go +++ b/auto/api/v1/web_core.go @@ -7,9 +7,11 @@ import ( "github.com/alimy/mir/v3" "github.com/gin-gonic/gin" + "github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr" ) type LoginReq struct { + BaseInfo `json:"-"` AgentInfo AgentInfo `json:"agent_info"` Name string `json:"name"` Passwd string `json:"passwd"` @@ -20,6 +22,10 @@ type AgentInfo struct { UserAgent string `json:"user_agent"` } +type BaseInfo struct { + User *dbr.User +} + type LoginResp struct { UserInfo ServerInfo ServerInfo `json:"server_info"` @@ -105,8 +111,8 @@ func RegisterWebCoreServant(e *gin.Engine, s WebCore, b WebCoreBinding, r WebCor r.RenderArticles(c, s.Articles()) } - router.Handle("HEAD", "/articles/:category/", h) router.Handle("GET", "/articles/:category/", h) + router.Handle("HEAD", "/articles/:category/", h) } router.Handle("GET", "/index/", func(c *gin.Context) { diff --git a/go.mod b/go.mod index 6c7efd3b..50fb6bea 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/Masterminds/semver/v3 v3.1.1 github.com/afocus/captcha v0.0.0-20191010092841-4bd1f21c8868 github.com/alimy/cfg v0.3.0 - github.com/alimy/mir/v3 v3.0.0-beta.1 + github.com/alimy/mir/v3 v3.0.0-alpha.8 github.com/aliyun/aliyun-oss-go-sdk v2.2.2+incompatible github.com/allegro/bigcache/v3 v3.0.2 github.com/bytedance/sonic v1.5.0 diff --git a/go.sum b/go.sum index d6798787..7debc8ba 100644 --- a/go.sum +++ b/go.sum @@ -147,8 +147,8 @@ github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:C github.com/alexflint/go-filemutex v1.1.0/go.mod h1:7P4iRhttt/nUvUOrYIhcpMzv2G6CY9UnI16Z+UJqRyk= github.com/alimy/cfg v0.3.0 h1:9xgA0QWVCPSq9fFNRcYahVCAX22IL9ts2wrTQPfAStY= github.com/alimy/cfg v0.3.0/go.mod h1:rOxbasTH2srl6StAjNF5Vyi8bfrdkl3fLGmOYtSw81c= -github.com/alimy/mir/v3 v3.0.0-beta.1 h1:ZSbclyf9XRw1nMrndLnyCvDUqHvWvBx2Z1XSsHllvoQ= -github.com/alimy/mir/v3 v3.0.0-beta.1/go.mod h1:ybhT2ijOiDn0lLwWzIY6vXdv+uzZrctS7VFfczcXBWU= +github.com/alimy/mir/v3 v3.0.0-alpha.8 h1:Zz94oefYkYXA/IuXrjRT7LrQ6LdsZi5z9+IWmXuPdUw= +github.com/alimy/mir/v3 v3.0.0-alpha.8/go.mod h1:ybhT2ijOiDn0lLwWzIY6vXdv+uzZrctS7VFfczcXBWU= github.com/aliyun/aliyun-oss-go-sdk v2.2.2+incompatible h1:9gWa46nstkJ9miBReJcN8Gq34cBFbzSpQZVVT9N09TM= github.com/aliyun/aliyun-oss-go-sdk v2.2.2+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= diff --git a/internal/servants/base/base.go b/internal/servants/base/base.go index 6c204e7e..d2c30329 100644 --- a/internal/servants/base/base.go +++ b/internal/servants/base/base.go @@ -6,7 +6,6 @@ package base import ( "net/http" - "os" "github.com/alimy/mir/v3" "github.com/gin-gonic/gin" @@ -26,7 +25,13 @@ type BaseRender struct { // TODO } -func (BaseServant) userFrom(c *gin.Context) (*core.User, bool) { +type JsonResp struct { + Code int `json:"code"` + Msg string `json:"msg,omitempty"` + Data any `json:",omitempty"` +} + +func (BaseServant) UserFrom(c *gin.Context) (*core.User, bool) { if u, exists := c.Get("USER"); exists { user, ok := u.(*core.User) return user, ok @@ -45,33 +50,15 @@ func BindAny(c *gin.Context, obj any) mir.Error { func RenderAny(c *gin.Context, data any, err mir.Error) { if err == nil { - hostname, _ := os.Hostname() - if data == nil { - data = gin.H{ - "code": 0, - "msg": "success", - "tracehost": hostname, - } - } else { - data = gin.H{ - "code": 0, - "msg": "success", - "data": data, - "tracehost": hostname, - } - } - c.JSON(http.StatusOK, data) + c.JSON(http.StatusOK, &JsonResp{ + Code: 0, + Msg: "success", + Data: data, + }) } else { - // TODO: error process logic - resp := gin.H{"code": err.StatusCode(), "msg": err.Error()} - // xerr := &xerror.Error{} - // if errors.As(err, xerr) { - // resp["code"], resp["msg"] = xerr.Code(), xerr.Msg() - // details := xerr.Details() - // if len(details) > 0 { - // resp["details"] = details - // } - // } - c.JSON(http.StatusInternalServerError, resp) + c.JSON(http.StatusInternalServerError, &JsonResp{ + Code: err.StatusCode(), + Msg: err.Error(), + }) } } diff --git a/mirc/README.md b/mirc/README.md index c592aa65..81c88517 100644 --- a/mirc/README.md +++ b/mirc/README.md @@ -3,7 +3,7 @@ |目录|系列API|备注| | ----- | ----- | ----- | -|v1|/|Web v1版本RESTful API相关定义文件| +|web|/|Web v1版本RESTful API相关定义文件| |admin|m|Admin后台运维系列相关RESTful API相关定义文件| |space|x|SpaceX系列相关RESTful API相关定义文件| |localoss|s| LocalOSS OBS系列RESTful API相关定义文件| diff --git a/mirc/web/README.md b/mirc/web/README.md new file mode 100644 index 00000000..f8690712 --- /dev/null +++ b/mirc/web/README.md @@ -0,0 +1,4 @@ +### Web系列RESTful API +本目录包含Web系列RESTful API相关定义文件 + +* v1 - v1版本API diff --git a/mirc/web/v1/base.go b/mirc/web/v1/base.go new file mode 100644 index 00000000..ee279a39 --- /dev/null +++ b/mirc/web/v1/base.go @@ -0,0 +1,9 @@ +package v1 + +import ( + "github.com/rocboss/paopao-ce/internal/core" +) + +type BaseInfo struct { + User *core.User +} diff --git a/mirc/web/v1/web_core.go b/mirc/web/v1/web_core.go index 83193986..f87338d7 100644 --- a/mirc/web/v1/web_core.go +++ b/mirc/web/v1/web_core.go @@ -23,6 +23,7 @@ type UserInfo struct { } type LoginReq struct { + BaseInfo `json:"-"` AgentInfo AgentInfo `json:"agent_info"` Name string `json:"name"` Passwd string `json:"passwd"`