gitee.com/h79/goutils@v1.22.10/api/header.go (about)

     1  package api
     2  
     3  import (
     4  	"gitee.com/h79/goutils/common/stringutil"
     5  	"gitee.com/h79/goutils/common/timer"
     6  )
     7  
     8  const (
     9  	CSysVersion = "X-H79-SysVersion"
    10  	CDeviceId   = "X-H79-DeviceId"
    11  	CModel      = "X-H79-Model"
    12  	CWifi       = "X-H79-Wifi"
    13  	CChannel    = "X-H79-Channel"
    14  	CExtInfo    = "X-H79-ExtInfo"
    15  	CTerminal   = "terminal"
    16  	CAppId      = "appId"
    17  	CVersion    = "version"
    18  	CSeqId      = "seqId"
    19  	CSource     = "source"
    20  	CTimeAt     = "timeAt"
    21  	CToken      = "Authorization"
    22  	CSign       = "sign"
    23  	CTimeStamp  = "timestamp"
    24  	XReqTime    = "X-ReqTime"
    25  	XSeqId      = "X-SeqNo"
    26  	XSign       = "X-Sign"
    27  	XTimeStamp  = "X-Timestamp"
    28  )
    29  
    30  type Header struct {
    31  	Base
    32  	ReqHead
    33  	Token string
    34  	// 自定义属性
    35  	Custom interface{}
    36  }
    37  
    38  func NewHeader(req ReqHead) *Header {
    39  	h := &Header{
    40  		ReqHead: req,
    41  	}
    42  	h.RecvAt = timer.CurrentMS()
    43  	return h
    44  }
    45  
    46  func (h *Header) WriteTo(ctx Context) {
    47  	headerCtx := ctx.HeaderContext()
    48  
    49  	headerCtx.Set(CVersion, h.Version)
    50  	headerCtx.Set(CSource, h.Source)
    51  	headerCtx.Set(CSeqId, h.SeqId)
    52  
    53  	headerCtx.Set(CDeviceId, h.System.DeviceId)
    54  	headerCtx.Set(CSysVersion, h.System.SysVer)
    55  	headerCtx.Set(CModel, h.System.Model)
    56  	headerCtx.Set(CWifi, h.System.Wifi)
    57  
    58  	headerCtx.Set(CAppId, h.App.AppId)
    59  	headerCtx.Set(CTerminal, h.App.Terminal)
    60  	headerCtx.Set(CVersion, h.App.Version)
    61  	headerCtx.Set(CChannel, h.App.Channel)
    62  	headerCtx.Set(CExtInfo, h.App.ExtInfo)
    63  	headerCtx.Set(CToken, Bearer(h.Token))
    64  }
    65  
    66  func (h *Header) ReadFrom(ctx Context) {
    67  	headerCtx := ctx.HeaderContext()
    68  
    69  	time := headerCtx.Get(CTimeAt)
    70  	if time == "" {
    71  		time = headerCtx.Get(XReqTime)
    72  	}
    73  	h.ReqAt = stringutil.StringToInt64(time)
    74  
    75  	h.Version = headerCtx.Get(CVersion)
    76  	h.Source = headerCtx.Get(CSource)
    77  	h.SeqId = headerCtx.Get(CSeqId)
    78  	if h.SeqId == "" {
    79  		h.SeqId = headerCtx.Get(XSeqId)
    80  	}
    81  
    82  	h.System.DeviceId = headerCtx.Get(CDeviceId)
    83  	h.System.SysVer = headerCtx.Get(CSysVersion)
    84  	h.System.Model = headerCtx.Get(CModel)
    85  	h.System.Wifi = headerCtx.Get(CWifi)
    86  
    87  	h.App.Source = h.Source
    88  	h.App.AppId = headerCtx.Get(CAppId)
    89  	h.App.Terminal = headerCtx.Get(CTerminal)
    90  	h.App.Version = headerCtx.Get(CVersion)
    91  	h.App.Channel = headerCtx.Get(CChannel)
    92  	h.App.ExtInfo = headerCtx.Get(CExtInfo)
    93  
    94  	if tk, er := TokenReq(ctx); er == nil {
    95  		h.Token = tk
    96  	}
    97  }
    98  
    99  func (h *Header) HasApp() bool {
   100  	return h.App.HasValid()
   101  }
   102  
   103  func (h *Header) HasSystem() bool {
   104  	return h.System.HasValid()
   105  }
   106  
   107  func (h *Header) HasDeviceId() bool {
   108  	return len(h.System.DeviceId) > 0
   109  }
   110  
   111  func (h *Header) HasToken() bool {
   112  	return len(h.Token) > 0
   113  }