Files
zzw ce7aa18477
CI / changes (push) Successful in 12s
CI / ad (push) Successful in 1s
CI / admin (push) Successful in 0s
CI / bff (push) Failing after 25s
CI / chore (push) Successful in 1s
CI / express (push) Successful in 0s
CI / product (push) Successful in 27s
CI / sale (push) Successful in 0s
CI / user (push) Successful in 0s
CI / wecom (push) Successful in 25s
fix: wecom user agent
2026-08-31 15:22:44 +08:00

56 lines
1.5 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)
}