package wx import ( "encoding/json" "errors" "fmt" ) var Wxopen = &wxopen{} type wxopen struct { appid string secret string } // InitWxopen @Title 初始化开放平台 func InitWxopen(appid, secret string) { Wxopen.appid = appid Wxopen.secret = secret } type AccessInfo struct { AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` RefreshToken string `json:"refresh_token"` Openid string `json:"openid"` Scope string `json:"scope"` Unionid string `json:"unionid"` } type errInfo struct { Errcode int `json:"errcode"` Errmsg string `json:"errmsg"` } type resAccess struct { errInfo AccessInfo } // GetAccessInfo @Title 获取微信登录验证结构体 func (w *wxopen) GetAccessInfo(code string) (result AccessInfo, err error) { res, err := request(get, fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%v&secret=%v&code=%v&grant_type=authorization_code", w.appid, w.secret, code), "") if err != nil { return } var resAcc resAccess if err = json.Unmarshal(res, &resAcc); err != nil { return } if resAcc.Errcode != 0 { return result, errors.New(resAcc.Errmsg) } result = resAcc.AccessInfo return } type userInfo struct { City string `json:"city"` Country string `json:"country"` Headimgurl string `json:"headimgurl"` Nickname string `json:"nickname"` Openid string `json:"openid"` Privilege []string `json:"privilege"` Province string `json:"province"` Sex uint `json:"sex"` Unionid string `json:"unionid"` } // GetUserInfo @Title 获取微信登录会员信息 func (w *wxopen) GetUserInfo(accessToken, openId string) (*userInfo, error) { res, err := request(get, fmt.Sprintf("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN", accessToken, openId), "") if err != nil { return nil, err } result := userInfo{} if err := json.Unmarshal(res, &result); err != nil { return nil, err } return &result, nil }