wechat.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/http"
  7. "hongze/hongze_cygx/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type WxAccessToken struct {
  13. AccessToken string `json:"access_token"`
  14. ExpiresIn int `json:"expires_in"`
  15. RefreshToken string `json:"refresh_token"`
  16. Openid string `json:"openid"`
  17. Unionid string `json:"unionid"`
  18. Scope string `json:"scope"`
  19. Errcode int `json:"errcode"`
  20. Errmsg string `json:"errmsg"`
  21. }
  22. func WxGetUserOpenIdByCode(code string) (item *WxAccessToken, err error) {
  23. if code == "" {
  24. err = errors.New("code is empty")
  25. return nil, err
  26. }
  27. requestUrl := `https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code`
  28. requestUrl = fmt.Sprintf(requestUrl, utils.WxAppId, utils.WxAppSecret, code)
  29. result, err := http.Get(requestUrl)
  30. if err != nil {
  31. return nil, err
  32. }
  33. err = json.Unmarshal(result, &item)
  34. return
  35. }
  36. //小助手
  37. func WxGetUserOpenIdByCodeXzs(code string) (item *WxAccessToken, err error) {
  38. if code == "" {
  39. err = errors.New("code is empty")
  40. return nil, err
  41. }
  42. requestUrl := `https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code`
  43. requestUrl = fmt.Sprintf(requestUrl, utils.WxPublicIdXzs, utils.WxPublicSecretXzs, code)
  44. result, err := http.Get(requestUrl)
  45. if err != nil {
  46. return nil, err
  47. }
  48. err = json.Unmarshal(result, &item)
  49. return
  50. }
  51. type WxToken struct {
  52. AccessToken string `json:"access_token"`
  53. ExpiresIn int `json:"expires_in"`
  54. Errcode int `json:"errcode"`
  55. Errmsg string `json:"errmsg"`
  56. }
  57. func WxGetToken() (item *WxToken, err error) {
  58. requestUrl := `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s`
  59. requestUrl = fmt.Sprintf(requestUrl, utils.WxAppId, utils.WxAppSecret)
  60. result, err := http.Get(requestUrl)
  61. if err != nil {
  62. utils.FileLog.Info("WxGetToken Result:%s ", string(result))
  63. return nil, err
  64. }
  65. err = json.Unmarshal(result, &item)
  66. return
  67. }
  68. type WxUserInfo struct {
  69. Openid string `json:"openid"`
  70. Nickname string `json:"nickname"`
  71. Sex int `json:"sex"`
  72. Language string `json:"language"`
  73. City string `json:"city"`
  74. Province string `json:"province"`
  75. Country string `json:"country"`
  76. Headimgurl string `json:"headimgurl"`
  77. SubscribeTime int `json:"subscribe_time"`
  78. Unionid string `json:"unionid"`
  79. Remark string `json:"remark"`
  80. Groupid int `json:"groupid"`
  81. SubscribeScene string `json:"subscribe_scene"`
  82. Errcode int `json:"errcode"`
  83. Errmsg string `json:"errmsg"`
  84. SessionKey string `json:"session_key"`
  85. }
  86. func WxGetUserInfo(openId, accessToken string) (item *WxUserInfo, err error) {
  87. requestUrl := `https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s`
  88. requestUrl = fmt.Sprintf(requestUrl, accessToken, openId)
  89. result, err := http.Get(requestUrl)
  90. utils.FileLog.Info("WxGetUserInfo Result:%s ", string(result))
  91. if err != nil {
  92. return
  93. }
  94. fmt.Println("result:", string(result))
  95. err = json.Unmarshal(result, &item)
  96. return
  97. }
  98. func GetWxSignature(ticket, url, noncestr string) (string, string, int64) {
  99. timestamp := time.Now().Unix()
  100. signStr := strings.Join([]string{"jsapi_ticket=", ticket,
  101. "&noncestr=", noncestr,
  102. "&timestamp=", strconv.FormatInt(timestamp, 10), "&url=", url}, "")
  103. signature := utils.Sha1(signStr)
  104. fmt.Println("signStr", signStr)
  105. return signature, noncestr, timestamp
  106. }
  107. type WxUserDetail struct {
  108. Unionid string
  109. Headimgurl string
  110. Nickname string
  111. }