Files
lone-services/bff/internal/response/response.go
T
gjs 462d142a4c
CI / changes (push) Successful in 37s
CI / ad (push) Successful in 30s
CI / admin (push) Successful in 22s
CI / bff (push) Successful in 39s
CI / chore (push) Successful in 20s
CI / equipment (push) Successful in 19s
CI / express (push) Successful in 17s
CI / product (push) Successful in 16s
CI / record (push) Successful in 17s
CI / sale (push) Successful in 16s
CI / task (push) Successful in 16s
CI / user (push) Successful in 17s
CI / wecom (push) Successful in 18s
回调
2026-09-05 13:41:13 +08:00

63 lines
1.7 KiB
Go

package response
import (
"fmt"
"lone-services/pkg/utils"
"net/http"
jsoniter "github.com/json-iterator/go"
)
type BaseController struct{}
func (b *BaseController) Fail(w http.ResponseWriter) {
b.Out(w, nil, Fail)
}
func (b *BaseController) Error(w http.ResponseWriter, cm Error) {
b.Out(w, nil, cm)
}
func (b *BaseController) Out(w http.ResponseWriter, data any, cm Error) {
out := NewError(cm.GetCode(), cm.GetMsg()).WithData(data)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_ = jsoniter.NewEncoder(w).Encode(out)
}
func (b *BaseController) OutPut(w http.ResponseWriter, code int32, data any, msg string) {
if code == utils.Ok.Code && !utils.GetConfigBool("encrypt.debug") {
jsonData, err := jsoniter.Marshal(data)
if err != nil {
fmt.Printf("response encrypt: json.Marshal failed: %v\n", err)
msg = utils.ErrorEncryptAesError.Msg
code = utils.ErrorEncryptAesError.Code
} else {
key := utils.GetConfigString("encrypt.encrypt_key")
res, enErr := utils.Crypto{}.Encrypt(jsonData)
if enErr != nil {
fmt.Printf("response encrypt failed: keyLen=%d keyEmpty=%v errCode=%d errMsg=%s\n",
len(key), key == "", enErr.GetCode(), enErr.GetMsg())
msg = utils.ErrorEncryptAesError.Msg
code = utils.ErrorEncryptAesError.Code
} else {
data = res
}
}
}
out := NewError(int(code), msg).WithData(data)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_ = jsoniter.NewEncoder(w).Encode(out)
}
func (b *BaseController) OutPutBack(w http.ResponseWriter, back responseDataBack) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_ = jsoniter.NewEncoder(w).Encode(back)
}