server.go 1023 B

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