kefu/service/wechat_official_auth.go

38 lines
980 B
Go

package service
import (
"github.com/silenceper/wechat/v2"
offConfig "github.com/silenceper/wechat/v2/officialaccount/config"
"github.com/silenceper/wechat/v2/officialaccount/oauth"
"kefu/lib"
)
// 微信公众号网页授权
func GetWechatUserInfo(weixinCode, entId string) (oauth.UserInfo, error) {
var userinfo oauth.UserInfo
wechatConfig, err := lib.NewWechatLib(entId)
if wechatConfig.AppId == "" {
return userinfo, err
}
cfg := &offConfig.Config{
AppID: wechatConfig.AppId,
AppSecret: wechatConfig.AppSecret,
Token: wechatConfig.Token,
//EncodingAESKey: "xxxx",
Cache: memory,
}
wc := wechat.NewWechat()
officialAccount := wc.GetOfficialAccount(cfg)
oauth := officialAccount.GetOauth()
accessToken, err := oauth.GetUserAccessToken(weixinCode)
if err != nil {
return userinfo, err
}
userinfo, err = oauth.GetUserInfo(accessToken.AccessToken, accessToken.OpenID, "")
if err != nil {
return userinfo, err
}
return userinfo, nil
}