gitee.com/h79/goutils@v1.22.10/api/structs.go (about) 1 package api 2 3 import ( 4 "gitee.com/h79/goutils/common/app" 5 "gitee.com/h79/goutils/common/config" 6 "gitee.com/h79/goutils/common/option" 7 "gitee.com/h79/goutils/common/result" 8 "gitee.com/h79/goutils/common/stringutil" 9 "gitee.com/h79/goutils/common/timer" 10 "gitee.com/h79/goutils/request" 11 "time" 12 ) 13 14 type Base struct { 15 ClientIP string 16 System app.System 17 App app.Info 18 } 19 20 var _ request.Head = (*ReqHead)(nil) 21 var _ request.HeadV2 = (*ReqHead)(nil) 22 23 type ReqHead struct { 24 Version string `form:"version" binding:"-" json:"version"` 25 Source string `form:"source" binding:"-" json:"source"` 26 SeqId string `form:"seqId" binding:"-" json:"seqId"` 27 ReqAt int64 `form:"timeAt" binding:"-" json:"timeAt"` //请求者的时间(客户端时间) 28 RecvAt int64 `json:"-"` //收到请求时间(服务端时间) 29 TimeOut time.Duration `json:"-"` 30 } 31 32 // GetSeqId request.Head interface 33 func (h *ReqHead) GetSeqId() string { 34 return h.SeqId 35 } 36 37 // GetSource request.Head interface 38 func (h *ReqHead) GetSource() string { 39 return h.Source 40 } 41 42 // GetVersion request.Head interface 43 func (h *ReqHead) GetVersion() string { 44 return h.Version 45 } 46 47 // SetSource request.HeadV2 interface 48 func (h *ReqHead) SetSource(source string) { 49 h.Source = source 50 } 51 52 // GetTimeOut request.HeadV2 interface 53 func (h *ReqHead) GetTimeOut() time.Duration { 54 return h.TimeOut 55 } 56 57 // SetTimeOut request.HeadV2 interface 58 func (h *ReqHead) SetTimeOut(duration time.Duration) { 59 h.TimeOut = duration 60 } 61 62 // GetRecvAt request.HeadV2 interface 63 func (h *ReqHead) GetRecvAt() int64 { 64 return h.RecvAt 65 } 66 67 // GetReqAt request.HeadV2 interface 68 func (h *ReqHead) GetReqAt() int64 { 69 return h.ReqAt 70 } 71 72 type Request struct { 73 ReqHead 74 Data string `json:"data,omitempty"` //json format string 75 } 76 77 func NewRequest() Request { 78 config.SeqId++ 79 return Request{ 80 ReqHead: ReqHead{ 81 Version: config.Version, 82 Source: config.Source, 83 SeqId: stringutil.Int64ToString(config.SeqId), 84 }, 85 } 86 } 87 88 type ResHead struct { 89 result.Result 90 Version string `json:"version"` //版本,请求者版本 91 Source string `json:"source,omitempty"` //请求源,可以请求者填写 92 SeqId string `json:"seqId,omitempty"` //请求序号,由请求者定义,服务器原路返回 93 TimeAt int64 `json:"timeAt,omitempty"` //服务收到请求的时间(ms) 94 DiffAt int64 `json:"diffAt,omitempty"` //客户端与服务端时间差值(ms) 95 SpendAt int64 `json:"spendAt,omitempty"` //从服务收到请求到响应完成,所花的时长(ms) 96 ServerAt int64 `json:"serverAt,omitempty"` //服务器时间,豪秒,用于检验对时(ms) 97 LogId string `json:"logId,omitempty"` 98 } 99 100 func NewResHead(res result.Result) ResHead { 101 return ResHead{ 102 Result: res, 103 Version: config.Version, 104 Source: config.Source, 105 ServerAt: timer.CurrentMS(), 106 } 107 } 108 109 type Response struct { 110 ResHead 111 EventType int32 `json:"eventType,omitempty"` //回应事件类型(业务自己定义,比如tcp,websocket) 112 Total int64 `json:"total,omitempty"` 113 Page int `json:"page,omitempty"` 114 Size int `json:"size,omitempty"` 115 Data interface{} `json:"data,omitempty"` //业务具体数据 116 } 117 118 func NewResponse(head request.HeadV2, result result.Result, data interface{}, opts ...option.Option) Response { 119 resHead := NewResHead(result) 120 resHead.Source = head.GetSource() 121 resHead.SeqId = head.GetSeqId() 122 resHead.TimeAt = head.GetRecvAt() 123 resHead.DiffAt = resHead.TimeAt - head.GetReqAt() 124 resHead.SpendAt = timer.CurrentMS() - head.GetRecvAt() 125 res := Response{ResHead: resHead, Data: data} 126 if page, ok := PageExist(opts...); ok { 127 res.Total = page.Total 128 res.Page = page.Page 129 res.Size = page.Size 130 } 131 if et, ok := EventTypeExist(opts...); ok { 132 res.EventType = et 133 } 134 return res 135 } 136 137 func NewResponseWithError(head request.HeadV2, err error, data interface{}, opts ...option.Option) Response { 138 return NewResponse(head, result.WithErr(err), data) 139 } 140 141 const ( 142 TypePage = iota + 1000 143 TypeEventType 144 ) 145 146 type PageOption struct { 147 Total int64 148 Page int 149 Size int 150 } 151 152 func WithPageOption(total int64) PageOption { 153 return PageOption{Total: total} 154 } 155 156 func (t PageOption) String() string { 157 return "page:response" 158 } 159 func (t PageOption) Type() int { return TypePage } 160 func (t PageOption) Value() interface{} { return t } 161 162 func PageExist(opts ...option.Option) (PageOption, bool) { 163 if r, ok := option.Exist(TypePage, opts...); ok { 164 return r.Value().(PageOption), true 165 } 166 return PageOption{}, false 167 } 168 169 type EventType int32 170 171 func WithEventType(e int32) EventType { 172 return EventType(e) 173 } 174 175 func (t EventType) String() string { 176 return "eventType:response" 177 } 178 func (t EventType) Type() int { return TypeEventType } 179 func (t EventType) Value() interface{} { return t } 180 181 func EventTypeExist(opts ...option.Option) (int32, bool) { 182 if r, ok := option.Exist(TypeEventType, opts...); ok { 183 return int32(r.Value().(EventType)), true 184 } 185 return 0, false 186 }