一物一码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

52 lines
1.3 KiB

  1. package utils
  2. import (
  3. "applet/app/md"
  4. "crypto/sha1"
  5. "encoding/hex"
  6. "fmt"
  7. "sort"
  8. "strings"
  9. )
  10. // CheckSignature 微信公众号签名检查
  11. func CheckSignature(signature, timestamp, nonce, token string) bool {
  12. arr := []string{timestamp, nonce, token}
  13. // 字典序排序
  14. sort.Strings(arr)
  15. n := len(timestamp) + len(nonce) + len(token)
  16. var b strings.Builder
  17. b.Grow(n)
  18. for i := 0; i < len(arr); i++ {
  19. b.WriteString(arr[i])
  20. }
  21. return Sha1(b.String()) == signature
  22. }
  23. // 进行Sha1编码
  24. func Sha1(str string) string {
  25. h := sha1.New()
  26. h.Write([]byte(str))
  27. return hex.EncodeToString(h.Sum(nil))
  28. }
  29. func GetWechatToken(appid, secret string) (string, error) {
  30. get, err := CurlGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret, nil)
  31. return string(get), err
  32. }
  33. func GetWechatSelfMenu(token string) (string, error) {
  34. get, err := CurlGet("https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token="+token, nil)
  35. return string(get), err
  36. }
  37. func SetWechatSelfMenu(token string, args md.WechatReq) (string, error) {
  38. str := SerializeStr(args)
  39. str = strings.ReplaceAll(str, "\\u0026", "&")
  40. fmt.Println(str)
  41. get, err := CurlPost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+token, str, nil)
  42. return string(get), err
  43. }