github.com/wfusion/gofusion@v1.1.14/http/response.go (about)

     1  package http
     2  
     3  import (
     4  	"net/http"
     5  	"reflect"
     6  
     7  	"github.com/gin-gonic/gin"
     8  	"github.com/spf13/cast"
     9  
    10  	"github.com/wfusion/gofusion/common/constraint"
    11  	"github.com/wfusion/gofusion/common/utils"
    12  	"github.com/wfusion/gofusion/i18n"
    13  
    14  	fusCtx "github.com/wfusion/gofusion/context"
    15  )
    16  
    17  type Response struct {
    18  	Code    int    `json:"code"`
    19  	Message string `json:"message"`
    20  	Data    any    `json:"data"`
    21  
    22  	// pagination
    23  	Page  *int `json:"page,omitempty"`
    24  	Count *int `json:"count,omitempty"`
    25  
    26  	// Trace
    27  	TraceID string `json:"traceid"`
    28  }
    29  
    30  type Embed struct {
    31  }
    32  
    33  var (
    34  	embedType    = reflect.TypeOf(Embed{})
    35  	responseType = reflect.TypeOf(Response{})
    36  )
    37  
    38  func RspError(c *gin.Context, data any, page, count int, msg string, err error, opts ...utils.OptionExtender) {
    39  	var code int
    40  	switch e := err.(type) {
    41  	case Errcode:
    42  		code, msg = int(e), e.Error()
    43  	case *bizErr:
    44  		code, msg = int(e.code), e.Error()
    45  	default:
    46  		code, msg = int(errParam), e.Error()
    47  	}
    48  
    49  	r, _ := Use(opts...).(*router)
    50  	rspError(c, r.appName, code, data, page, count, msg)
    51  
    52  	go metricsCode(r.ctx, r.appName, c.Request.URL.Path, c.Request.Method, r.parseHeaderMetrics(c),
    53  		cast.ToInt(code), c.Writer.Status(), c.Writer.Size(), c.Request.ContentLength)
    54  }
    55  
    56  func RspSuccess(c *gin.Context, data any, page, count int, msg string, opts ...utils.OptionExtender) {
    57  	r, _ := Use(opts...).(*router)
    58  	rspSuccess(c, r.successCode, data, page, count, msg)
    59  
    60  	go metricsCode(r.ctx, r.appName, c.Request.URL.Path, c.Request.Method, r.parseHeaderMetrics(c),
    61  		r.successCode, c.Writer.Status(), c.Writer.Size(), c.Request.ContentLength)
    62  }
    63  
    64  func rspSuccess(c *gin.Context, code int, data any, page, count int, msg string) {
    65  	status := c.Writer.Status()
    66  	if status <= 0 {
    67  		status = http.StatusOK
    68  	}
    69  	if msg == "" {
    70  		msg = "ok"
    71  	}
    72  	var (
    73  		pagePtr  *int
    74  		countPtr *int
    75  	)
    76  	if page > 0 {
    77  		pagePtr = utils.AnyPtr(page)
    78  	}
    79  	if count >= 0 {
    80  		countPtr = utils.AnyPtr(count)
    81  	}
    82  
    83  	c.PureJSON(status, &Response{
    84  		Code:    code,
    85  		Message: msg,
    86  		Data:    data,
    87  		Page:    pagePtr,
    88  		Count:   countPtr,
    89  		TraceID: c.GetString(fusCtx.KeyTraceID),
    90  	})
    91  }
    92  
    93  func rspError[T constraint.Integer](c *gin.Context, appName string, code T, data any, page, count int, msg string) {
    94  	status := c.Writer.Status()
    95  	if status <= 0 {
    96  		status = http.StatusBadRequest
    97  	}
    98  
    99  	if msg == "" {
   100  		msg = Localizable(AppName(appName)).Localize(Errcode(code), i18n.Langs(langs(c)))
   101  	}
   102  	var (
   103  		pagePtr  *int
   104  		countPtr *int
   105  	)
   106  	if page > 0 {
   107  		pagePtr = utils.AnyPtr(page)
   108  	}
   109  	if count >= 0 {
   110  		countPtr = utils.AnyPtr(count)
   111  	}
   112  
   113  	c.PureJSON(status, &Response{
   114  		Code:    cast.ToInt(code),
   115  		Message: msg,
   116  		Data:    data,
   117  		Page:    pagePtr,
   118  		Count:   countPtr,
   119  		TraceID: c.GetString(fusCtx.KeyTraceID),
   120  	})
   121  }
   122  
   123  func embedResponse(c *gin.Context, data any, err error) {
   124  	status := c.Writer.Status()
   125  	if status == 0 {
   126  		if err != nil {
   127  			status = http.StatusOK
   128  		} else {
   129  			status = http.StatusBadRequest
   130  		}
   131  	}
   132  
   133  	c.PureJSON(status, data)
   134  }
   135  
   136  func langs(c *gin.Context) (langs []string) {
   137  	if c == nil {
   138  		return
   139  	}
   140  	langs = c.Request.Header.Values("Accept-Language")
   141  	if lang := c.GetString("lang"); utils.IsStrNotBlank(lang) {
   142  		langs = append(langs, lang)
   143  	}
   144  	if lang := c.GetString(fusCtx.KeyLangs); utils.IsStrNotBlank(lang) {
   145  		langs = append(langs, lang)
   146  	}
   147  	return langs
   148  }