解决部分

This commit is contained in:
eson
2023-07-20 15:21:03 +08:00
parent 3e1d4d2e4f
commit 8c7884bcc3
8 changed files with 210 additions and 174 deletions

View File

@@ -2,12 +2,7 @@ package {{.PkgName}}
import (
"net/http"
"errors"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
{{.ImportPackages}}
@@ -15,60 +10,16 @@ import (
func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
//
err error
//
userinfo *auth.BackendUserInfo
)
// JWT token,
claims, err := svcCtx.ParseJwtToken(r)
// JWT token出错,JSON响应并记录错误消息
if err != nil || claims == nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401, // 401,
Message: "unauthorized", //
})
logx.Info("unauthorized:", err.Error()) //
return
}
// token中获取对应的用户信息
userinfo, err = auth.GetBackendUserInfoFormMapClaims(claims)
// ,JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
{{if .HasRequest}}var req types.{{.RequestType}}
// 使httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
//
{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
{{if .HasResp}}resp{{end}} := l.{{.Call}}({{if .HasRequest}}&req, {{end}}userinfo)
// nil使httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
{{if .HasResp}}httpx.OkJsonCtx(r.Context(), w, resp){{else}}httpx.Ok(w){{end}}
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
basic.AfterLogic(w, r, resp)
}
}