user.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package user
  2. import (
  3. "dashoo.cn/opms_libary/plugin/wechat/base"
  4. "dashoo.cn/opms_libary/plugin/wechat/context"
  5. "encoding/json"
  6. "fmt"
  7. )
  8. const (
  9. userInfoURL = "https://api.weixin.qq.com/cgi-bin/user/info"
  10. )
  11. //User 用户管理
  12. type User struct {
  13. base.WechatBase
  14. }
  15. //NewUser 实例化
  16. func NewUser(context *context.Context) *User {
  17. user := new(User)
  18. user.Context = context
  19. return user
  20. }
  21. //Info 用户基本信息
  22. type Info struct {
  23. context.WxError
  24. Subscribe int32 `json:"subscribe"`
  25. OpenID string `json:"openid"`
  26. Nickname string `json:"nickname"`
  27. Sex int `json:"sex"`
  28. City string `json:"city"`
  29. Country string `json:"country"`
  30. Province string `json:"province"`
  31. Language string `json:"language"`
  32. Headimgurl string `json:"headimgurl"`
  33. SubscribeTime int64 `json:"subscribe_time"`
  34. UnionID string `json:"unionid"`
  35. Remark string `json:"remark"`
  36. GroupID int32 `json:"groupid"`
  37. TagidList []string `json:"tagid_list"`
  38. }
  39. //GetUserInfo 获取用户基本信息
  40. func (user *User) GetUserInfo(openID string) (userInfo *Info, err error) {
  41. url := fmt.Sprintf("%s?openid=%s&lang=zh_CN", userInfoURL, openID)
  42. var response []byte
  43. response, err = user.HTTPGetWithAccessToken(url)
  44. if err != nil {
  45. return
  46. }
  47. userInfo = new(Info)
  48. err = json.Unmarshal(response, userInfo)
  49. return
  50. }
  51. //IsSubscribed 是否已经关注公众号
  52. func (user *User) IsSubscribed(openID string) (subscribed bool, err error) {
  53. var userInfo *Info
  54. userInfo, err = user.GetUserInfo(openID)
  55. if err != nil {
  56. return
  57. }
  58. subscribed = userInfo.Subscribe == 1
  59. return
  60. }