| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package user
- import (
- "dashoo.cn/opms_libary/plugin/wechat/context"
- "dashoo.cn/opms_libary/plugin/wechat/mp/base"
- "encoding/json"
- "fmt"
- )
- const (
- userInfoURL = "https://api.weixin.qq.com/cgi-bin/user/info"
- )
- //User 用户管理
- type User struct {
- base.MpBase
- }
- //NewUser 实例化
- func NewUser(context *context.Context) *User {
- user := new(User)
- user.Context = context
- return user
- }
- //Info 用户基本信息
- type Info struct {
- context.WxError
- Subscribe int32 `json:"subscribe"`
- OpenID string `json:"openid"`
- Nickname string `json:"nickname"`
- Sex int `json:"sex"`
- City string `json:"city"`
- Country string `json:"country"`
- Province string `json:"province"`
- Language string `json:"language"`
- Headimgurl string `json:"headimgurl"`
- SubscribeTime int64 `json:"subscribe_time"`
- UnionID string `json:"unionid"`
- Remark string `json:"remark"`
- GroupID int32 `json:"groupid"`
- TagidList []string `json:"tagid_list"`
- }
- //GetUserInfo 获取用户基本信息
- func (user *User) GetUserInfo(openID string) (userInfo *Info, err error) {
- url := fmt.Sprintf("%s?openid=%s&lang=zh_CN", userInfoURL, openID)
- var response []byte
- response, err = user.HTTPGetWithAccessToken(url)
- if err != nil {
- return
- }
- userInfo = new(Info)
- err = json.Unmarshal(response, userInfo)
- return
- }
- //IsSubscribed 是否已经关注公众号
- func (user *User) IsSubscribed(openID string) (subscribed bool, err error) {
- var userInfo *Info
- userInfo, err = user.GetUserInfo(openID)
- if err != nil {
- return
- }
- subscribed = userInfo.Subscribe == 1
- return
- }
|