package statusz import ( "git.noahlan.cn/noahlan/ntool-biz/core/i18n" "git.noahlan.cn/noahlan/ntool-biz/core/nstatus" "git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code" "git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg" "github.com/zeromicro/go-zero/rest/httpx" "net/http" ) // ResponseHandler API执行结果处理,统一返回值类型与结构 func ResponseHandler(req *http.Request, w http.ResponseWriter, trans bool, resp any, err error) { if err == nil { r := nstatus.NewResultWithData(code.StatusOK, msg.Success, resp) if trans { r.Msg = i18n.Trans(req.Context(), r.Msg) } httpx.WriteJson(w, code.StatusOK, r) return } result := nstatus.ConvertErr(err) if result == nil { // 不可能发生此情况,这里处理是以防万一 result = nstatus.NewResult(code.StatusFailed, msg.Failed) } result.Data = resp if trans { result.Msg = i18n.Trans(req.Context(), result.Msg) } c := result.Code // 判断code,如果不是http-status,全部使用500 if result.Code > http.StatusNetworkAuthenticationRequired { c = http.StatusInternalServerError } httpx.WriteJson(w, c, result) }