server.go 977 B

12345678910111213141516171819202122232425262728293031323334353637
  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. func keyServer(appid string) string {
  10. return "wechat:server:" + appid
  11. }
  12. // NewServer new Server
  13. func NewServer(appID, appSecret, token, encodingAESKey string) *Server {
  14. key := keyServer(appID)
  15. if v, ok := memcache.Load(key); ok {
  16. return v.(*Server)
  17. }
  18. s := &Server{AppID: appID, AppSecret: appSecret, Token: token, EncodingAESKey: encodingAESKey}
  19. memcache.Store(key, s)
  20. return s
  21. }
  22. // Echo notify echo GET
  23. func (ws Server) Echo(s FormSignature) (data string, err error) {
  24. if ok := makeSignature(ws.Token, s.Signature, s.TimeStamp, s.Nonce); ok {
  25. data = s.Echostr
  26. }
  27. return
  28. }
  29. // Validate 验证请求合法性
  30. func (ws Server) Validate(signature, timestamp, nonce string) bool {
  31. return makeSignature(ws.Token, signature, timestamp, nonce)
  32. }