微信相关接口
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.

98 lines
2.0 KiB

2 years ago
  1. package wechat
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. type AppOpenIdData struct {
  7. AccessToken string `json:"access_token"`
  8. ExpiresIn int `json:"expires_in"`
  9. RefreshToken string `json:"refresh_token"`
  10. Openid string `json:"openid"`
  11. Scope string `json:"scope"`
  12. Unionid interface{} `json:"unionid"`
  13. Errcode int `json:"errcode"`
  14. Errmsg string `json:"errmsg"`
  15. }
  16. type MiniAppOpenidData struct {
  17. Openid string `json:"openid"`
  18. SessionKey interface{} `json:"session_key"`
  19. Unionid interface{} `json:"unionid"`
  20. Errcode int `json:"errcode"`
  21. Errmsg string `json:"errmsg"`
  22. }
  23. type MpOpenIdData struct {
  24. AccessToken string `json:"access_token"`
  25. ExpiresIn int `json:"expires_in"`
  26. RefreshToken string `json:"refresh_token"`
  27. Openid string `json:"openid"`
  28. Scope string `json:"scope"`
  29. Errcode int `json:"errcode"`
  30. Errmsg string `json:"errmsg"`
  31. }
  32. /**
  33. * 从微信api取openid
  34. */
  35. func GetAppOpenid(appid, secret, code string) (AppOpenIdData, error) {
  36. var data AppOpenIdData
  37. url := fmt.Sprintf(GET_APP_OPENID, appid, secret, code)
  38. data_byte, err := SendHttp("GET", url, nil)
  39. if err != nil {
  40. return data, err
  41. }
  42. err = json.Unmarshal(data_byte, &data)
  43. return data, err
  44. }
  45. /**
  46. * 从微信api取小程序openid
  47. */
  48. func GetMiniAppOpenid(appid, secret, code string) (MiniAppOpenidData, error) {
  49. url := fmt.Sprintf(GET_MINIAPP_OPENID_API, appid, secret, code)
  50. data_byte, err := SendHttp("GET", url, nil)
  51. var data MiniAppOpenidData
  52. if err != nil {
  53. return data, err
  54. }
  55. err = json.Unmarshal(data_byte, &data)
  56. return data, err
  57. }
  58. /**
  59. * 从微信api取公众号openid
  60. */
  61. func GetMpOpenid(appid, secret, code string) (MpOpenIdData, error) {
  62. url := fmt.Sprintf(GET_MP_OPENID_API, appid, secret, code)
  63. data_byte, err := SendHttp("GET", url, nil)
  64. var data MpOpenIdData
  65. if err != nil {
  66. return data, err
  67. }
  68. err = json.Unmarshal(data_byte, &data)
  69. if data.Errcode != 0 {
  70. return data, errors.New(data.Errmsg)
  71. }
  72. return data, err
  73. }