server.go 905 B

123456789101112131415161718192021222324252627282930313233
  1. package wechat
  2. // Server wechat
  3. type Server struct {
  4. AppID string `json:"appid"`
  5. AppSecret string `json:"appsecret"`
  6. Token string `json:"token"`
  7. EncodingAESKey string `json:"encodingaeskey"`
  8. }
  9. // NewServer new Server
  10. func NewServer(appID, appSecret, token, encodingAESKey string) *Server {
  11. key := "wechat:server:" + appID
  12. if v, ok := cache.Load(key); ok {
  13. return v.(*Server)
  14. }
  15. s := &Server{AppID: appID, AppSecret: appSecret, Token: token, EncodingAESKey: encodingAESKey}
  16. cache.Store(key, s)
  17. return s
  18. }
  19. // Echo notify echo GET
  20. func (ws Server) Echo(s FormSignature) (data string, err error) {
  21. if ok := makeSignature(ws.Token, s.Signature, s.TimeStamp, s.Nonce); ok {
  22. data = s.Echostr
  23. }
  24. return
  25. }
  26. // Validate 验证请求合法性
  27. func (ws Server) Validate(signature, timestamp, nonce string) bool {
  28. return makeSignature(ws.Token, signature, timestamp, nonce)
  29. }