github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/server/rsp/response.go (about) 1 package rsp 2 3 import ( 4 "github.com/isyscore/isc-gobase/isc" 5 "net/http" 6 7 "github.com/gin-gonic/gin" 8 ) 9 10 type ResponseBase struct { 11 Code int `json:"code"` 12 Message string `json:"message"` 13 } 14 15 type DataResponse[T any] struct { 16 ResponseBase 17 Data T `json:"data"` 18 } 19 20 type DataArrayResponse[T any] struct { 21 ResponseBase 22 Data []T `json:"data"` 23 } 24 25 type PagedData[T any] struct { 26 Total int64 `json:"total"` 27 Size int64 `json:"size"` 28 Current int64 `json:"current"` 29 Pages int64 `json:"pages"` 30 IsSearchCount bool `json:"isSearchCount"` 31 Records []T `json:"records"` 32 } 33 34 type PagedResponse[T any] struct { 35 ResponseBase 36 Data PagedData[T] `json:"data"` 37 } 38 39 func Success(ctx *gin.Context, object any) { 40 ctx.JSON(http.StatusOK, object) 41 } 42 43 func SuccessOfStandard(ctx *gin.Context, v any) { 44 ctx.Header("isc-biz-code", "0") 45 ctx.Header("isc-biz-message", "success") 46 ctx.JSON(http.StatusOK, map[string]any{ 47 "code": 0, 48 "message": "success", 49 "data": v, 50 }) 51 } 52 53 func FailedOfStandard(ctx *gin.Context, code int, message string) { 54 ctx.Header("isc-biz-code", isc.ToString(code)) 55 ctx.Header("isc-biz-message", message) 56 ctx.JSON(http.StatusOK, map[string]any{ 57 "code": code, 58 "message": message, 59 "data": nil, 60 }) 61 } 62 63 func FailedWithDataOfStandard(ctx *gin.Context, code string, message string, v any) { 64 ctx.Header("isc-biz-code", isc.ToString(code)) 65 ctx.Header("isc-biz-message", message) 66 ctx.JSON(http.StatusOK, map[string]any{ 67 "code": code, 68 "message": message, 69 "data": v, 70 }) 71 }