2024-06-30 17:42:27 +08:00
|
|
|
|
package responsex
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type options struct {
|
2026-09-19 01:31:34 +08:00
|
|
|
|
logger Logger
|
|
|
|
|
|
ignoreLog bool
|
|
|
|
|
|
|
|
|
|
|
|
traceFunc func(ctx context.Context) string
|
|
|
|
|
|
|
|
|
|
|
|
defaultSuccessCode int
|
|
|
|
|
|
defaultErrorCode int
|
|
|
|
|
|
defaultSuccessMessage string
|
|
|
|
|
|
successMsgSet bool
|
2025-12-07 01:51:01 +08:00
|
|
|
|
|
2026-07-22 01:17:07 +08:00
|
|
|
|
code int
|
|
|
|
|
|
data any
|
|
|
|
|
|
message string
|
|
|
|
|
|
pagination *Pagination
|
|
|
|
|
|
httpStatusCode int
|
|
|
|
|
|
headers map[string]string
|
2024-06-30 17:42:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var op *options = nil
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
op = defaultOptions()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func InitOptions(ops ...Option) {
|
|
|
|
|
|
for _, o := range ops {
|
|
|
|
|
|
o(op)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func defaultOptions() *options {
|
|
|
|
|
|
return &options{
|
2026-09-19 01:31:34 +08:00
|
|
|
|
logger: &defaultLogger{},
|
|
|
|
|
|
|
|
|
|
|
|
traceFunc: func(ctx context.Context) string {
|
|
|
|
|
|
val := ctx.Value("trace_id")
|
|
|
|
|
|
v, ok := val.(string)
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
|
|
|
|
|
return ""
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
defaultSuccessCode: 200,
|
|
|
|
|
|
defaultErrorCode: 400,
|
|
|
|
|
|
defaultSuccessMessage: "请求成功",
|
2025-12-07 01:51:01 +08:00
|
|
|
|
|
|
|
|
|
|
code: 200,
|
|
|
|
|
|
data: nil, // 默认为nil
|
|
|
|
|
|
message: "请求成功",
|
2024-06-30 17:42:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Option func(*options)
|
|
|
|
|
|
|
2025-12-07 01:51:01 +08:00
|
|
|
|
func SetCode(code int) Option {
|
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
o.code = code
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func SetData(data any) Option {
|
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
o.data = data
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func SetMessage(message string) Option {
|
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
o.message = message
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-22 01:17:07 +08:00
|
|
|
|
func SetPage(page *Pagination) Option {
|
2025-12-07 01:51:01 +08:00
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
o.pagination = page
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-10 17:32:12 +08:00
|
|
|
|
// 设置默认的成功code
|
|
|
|
|
|
func SetDefaultSuccessCode(code int) Option {
|
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
o.defaultSuccessCode = code
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-19 01:31:34 +08:00
|
|
|
|
// 设置默认的成功文案(多语言场景见 SuccessMessageCtx)
|
|
|
|
|
|
// 传回内置默认文案 "请求成功" 即恢复为"未显式配置",交还给多语言解析
|
|
|
|
|
|
func SetDefaultSuccessMessage(message string) Option {
|
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
o.defaultSuccessMessage = message
|
|
|
|
|
|
o.successMsgSet = message != defaultSuccessMessage
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-10 17:32:12 +08:00
|
|
|
|
// 设置默认的失败code
|
|
|
|
|
|
func SetDefaultErrorCode(code int) Option {
|
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
o.defaultErrorCode = code
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-30 17:42:27 +08:00
|
|
|
|
// 是否需要打印日志
|
|
|
|
|
|
func SetIgnoreLog() Option {
|
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
o.ignoreLog = true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置日志打印器
|
|
|
|
|
|
func SetLogger(logger Logger) Option {
|
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
o.logger = logger
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-19 01:31:34 +08:00
|
|
|
|
func SetTraceFunc(f func(ctx context.Context) string) Option {
|
2024-06-30 17:42:27 +08:00
|
|
|
|
return func(o *options) {
|
2026-09-19 01:31:34 +08:00
|
|
|
|
o.traceFunc = f
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置trace_id的ctx key(默认已用标准key CtxTraceIDKey,一般无需调用)
|
|
|
|
|
|
func SetTraceKey(traceKey any) Option {
|
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
o.traceFunc = func(ctx context.Context) string {
|
|
|
|
|
|
return ctx.Value(traceKey).(string)
|
|
|
|
|
|
}
|
2024-06-30 17:42:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-22 01:17:07 +08:00
|
|
|
|
// 设置HTTP状态码
|
|
|
|
|
|
func SetHttpStatusCode(code int) Option {
|
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
o.httpStatusCode = code
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置单个响应头
|
|
|
|
|
|
func SetHeader(key, value string) Option {
|
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
if o.headers == nil {
|
|
|
|
|
|
o.headers = make(map[string]string)
|
|
|
|
|
|
}
|
|
|
|
|
|
o.headers[key] = value
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 批量设置响应头
|
|
|
|
|
|
func SetHeaders(headers map[string]string) Option {
|
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
|
o.headers = headers
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-06-30 17:42:27 +08:00
|
|
|
|
|
|
|
|
|
|
type Logger interface {
|
|
|
|
|
|
Info(ctx context.Context, args ...interface{})
|
|
|
|
|
|
Infof(ctx context.Context, format string, args ...interface{})
|
|
|
|
|
|
Error(ctx context.Context, args ...interface{})
|
|
|
|
|
|
Errorf(ctx context.Context, format string, args ...interface{})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type defaultLogger struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (d defaultLogger) Info(ctx context.Context, args ...interface{}) {
|
|
|
|
|
|
fmt.Println(args...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *defaultLogger) Infof(ctx context.Context, format string, args ...interface{}) {
|
|
|
|
|
|
fmt.Printf(format, args...)
|
|
|
|
|
|
}
|
|
|
|
|
|
func (d defaultLogger) Error(ctx context.Context, args ...interface{}) {
|
|
|
|
|
|
fmt.Println(args...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *defaultLogger) Errorf(ctx context.Context, format string, args ...interface{}) {
|
|
|
|
|
|
fmt.Printf(format, args...)
|
|
|
|
|
|
}
|