wechat.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/http"
  6. "hongze/hongze_mfyx/utils"
  7. "io/ioutil"
  8. http2 "net/http"
  9. "strings"
  10. )
  11. type WxAccessToken struct {
  12. AccessToken string `json:"access_token"`
  13. ExpiresIn int `json:"expires_in"`
  14. RefreshToken string `json:"refresh_token"`
  15. Openid string `json:"openid"`
  16. Unionid string `json:"unionid"`
  17. Scope string `json:"scope"`
  18. Errcode int `json:"errcode"`
  19. Errmsg string `json:"errmsg"`
  20. }
  21. type WxToken struct {
  22. AccessToken string `json:"access_token"`
  23. ExpiresIn int `json:"expires_in"`
  24. Errcode int `json:"errcode"`
  25. Errmsg string `json:"errmsg"`
  26. }
  27. func WxGetToken() (item *WxToken, err error) {
  28. requestUrl := `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s`
  29. requestUrl = fmt.Sprintf(requestUrl, utils.WxAppId, utils.WxAppSecret)
  30. result, err := http.Get(requestUrl)
  31. if err != nil {
  32. utils.FileLog.Info("WxGetToken Result:%s ", string(result))
  33. return nil, err
  34. }
  35. err = json.Unmarshal(result, &item)
  36. return
  37. }
  38. type WxUserInfo struct {
  39. Openid string `json:"openid"`
  40. Nickname string `json:"nickname"`
  41. Sex int `json:"sex"`
  42. Language string `json:"language"`
  43. City string `json:"city"`
  44. Province string `json:"province"`
  45. Country string `json:"country"`
  46. Headimgurl string `json:"headimgurl"`
  47. SubscribeTime int `json:"subscribe_time"`
  48. Unionid string `json:"unionid"`
  49. Remark string `json:"remark"`
  50. Groupid int `json:"groupid"`
  51. SubscribeScene string `json:"subscribe_scene"`
  52. Errcode int `json:"errcode"`
  53. Errmsg string `json:"errmsg"`
  54. SessionKey string `json:"session_key"`
  55. }
  56. type WxUserDetail struct {
  57. Unionid string
  58. Headimgurl string
  59. Nickname string
  60. }
  61. type WxuserphonenumberResp struct {
  62. Errcode int `json:"errcode"`
  63. Errmsg string `json:"errmsg"`
  64. PhoneInfo PhoneInfo `json:"phone_info"`
  65. }
  66. type PhoneInfo struct {
  67. PhoneNumber string `json:"phoneNumber"`
  68. PurePhoneNumber string `json:"purePhoneNumber"`
  69. CountryCode string `json:"countryCode"`
  70. Watermark Watermark `json:"watermark"`
  71. }
  72. type Watermark struct {
  73. Timestamp int64 `json:"timestamp"`
  74. Appid string `json:"appid"`
  75. }
  76. func Getuserphonenumber(code string) (item *WxuserphonenumberResp, err error) {
  77. url := "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"
  78. itemToken, err := WxGetToken()
  79. if err != nil {
  80. fmt.Println(err)
  81. return
  82. }
  83. url = fmt.Sprintf(url, itemToken.AccessToken)
  84. method := "POST"
  85. payload := strings.NewReader(`{
  86. "code":"` + code + `"
  87. }`)
  88. client := &http2.Client{}
  89. req, err := http2.NewRequest(method, url, payload)
  90. if err != nil {
  91. fmt.Println(err)
  92. return
  93. }
  94. req.Header.Add("Content-Type", "application/json")
  95. res, err := client.Do(req)
  96. if err != nil {
  97. fmt.Println(err)
  98. return
  99. }
  100. defer res.Body.Close()
  101. body, err := ioutil.ReadAll(res.Body)
  102. if err != nil {
  103. fmt.Println(err)
  104. return
  105. }
  106. err = json.Unmarshal(body, &item)
  107. return
  108. }