fusenapi/server/product-template/internal/handler/savebasemaphandler.go

62 lines
1.7 KiB
Go
Raw Normal View History

2023-06-21 08:35:43 +00:00
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
2023-06-26 10:22:59 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-06-25 06:11:47 +00:00
"fusenapi/server/product-template/internal/logic"
"fusenapi/server/product-template/internal/svc"
2023-06-21 08:35:43 +00:00
)
func SaveBaseMapHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2023-06-26 10:22:59 +00:00
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
}
2023-06-21 08:35:43 +00:00
l := logic.NewSaveBaseMapLogic(r.Context(), svcCtx)
2023-06-26 10:27:38 +00:00
resp := l.SaveBaseMap(r, userinfo)
2023-06-21 08:35:43 +00:00
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}