context.go 934 B

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. package ciphers
  3. import "context"
  4. // contextKey WechatPayCipher Context Key Type
  5. //
  6. // 使用强类型避免与其他 Context Key 冲突
  7. type contextKey string
  8. // String contextKey 的字符串描述,区分普通字符串
  9. func (c contextKey) String() string {
  10. return "WPCipherContext(" + string(c) + ")"
  11. }
  12. const (
  13. // 加密使用的微信支付平台证书序列号
  14. contextKeyEncryptSerial contextKey = "EncryptSerial"
  15. )
  16. // setEncryptSerial 往Context中写入用于加密的证书序列号,返回更新后的Context
  17. func setEncryptSerial(ctx context.Context, serial string) context.Context {
  18. return context.WithValue(ctx, contextKeyEncryptSerial, serial)
  19. }
  20. // getEncryptSerial 从Context中读取用于加密的证书序列号
  21. func getEncryptSerial(ctx context.Context) (string, bool) {
  22. serial, ok := ctx.Value(contextKeyEncryptSerial).(string)
  23. return serial, ok
  24. }