77 lines
1.8 KiB
Go
77 lines
1.8 KiB
Go
package weixin
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"git.rosy.net.cn/baseapi/platformapi/weixinapi"
|
|
"git.rosy.net.cn/jx-callback/business/auth2"
|
|
"git.rosy.net.cn/jx-callback/business/auth2/authprovider"
|
|
"git.rosy.net.cn/jx-callback/business/model"
|
|
"git.rosy.net.cn/jx-callback/globals"
|
|
"git.rosy.net.cn/jx-callback/globals/api"
|
|
)
|
|
|
|
const (
|
|
AuthTypeWeixin = "wxqrcode" // 微信扫码
|
|
AuthTypeMP = "weixinsns" // 公众号
|
|
AuthTypeMini = "weixinmini" // 小程序
|
|
)
|
|
|
|
type Auther struct {
|
|
authprovider.DefAuther
|
|
authType string
|
|
}
|
|
|
|
var (
|
|
AutherObjWX *Auther
|
|
AutherObjMP *Auther
|
|
)
|
|
|
|
var (
|
|
ErrStateIsWrong = errors.New("登录state非法")
|
|
)
|
|
|
|
func init() {
|
|
AutherObjWX = &Auther{
|
|
authType: AuthTypeWeixin,
|
|
}
|
|
auth2.RegisterAuther(AuthTypeWeixin, AutherObjWX)
|
|
|
|
AutherObjMP = &Auther{
|
|
authType: AuthTypeMP,
|
|
}
|
|
auth2.RegisterAuther(AuthTypeMP, AutherObjMP)
|
|
}
|
|
|
|
func (a *Auther) VerifySecret(state, code string) (authBindEx *auth2.AuthBindEx, err error) {
|
|
globals.SugarLogger.Debugf("weixin VerifySecret code:%s", code)
|
|
if state == "" {
|
|
token, err2 := a.getAPI().SNSRetrieveToken(code)
|
|
if err = err2; err == nil {
|
|
wxUserinfo, err2 := a.getAPI().SNSGetUserInfo(token.AccessToken, token.OpenID)
|
|
if err = err2; err == nil {
|
|
if authBindEx, err = a.UnionFindAuthBind(a.authType, []string{AuthTypeWeixin, AuthTypeMP, AuthTypeMini}, wxUserinfo.OpenID, wxUserinfo.UnionID, wxUserinfo); err == nil {
|
|
authBindEx.UserHint = &auth2.UserBasic{
|
|
Name: wxUserinfo.NickName,
|
|
Avatar: wxUserinfo.HeadImgURL,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
err = ErrStateIsWrong
|
|
}
|
|
return authBindEx, err
|
|
}
|
|
|
|
func (a *Auther) getAPI() *weixinapi.API {
|
|
if a.authType == AuthTypeWeixin {
|
|
return api.WeixinPageAPI
|
|
}
|
|
return api.WeixinAPI
|
|
}
|
|
|
|
func (a *Auther) GetUserType() (userType int8) {
|
|
return model.UserTypeStoreBoss
|
|
}
|