105 lines
2.5 KiB
Go
105 lines
2.5 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" // 公众号
|
|
AuthTypeWXNative = "wxnative" // 微信APP
|
|
AuthTypeWxApp = "weixinapp" //app微信登录
|
|
)
|
|
|
|
type Auther struct {
|
|
authprovider.DefAuther
|
|
authType string
|
|
}
|
|
|
|
var (
|
|
AutherObjWX *Auther
|
|
AutherObjMP *Auther
|
|
AutherObjNative *Auther
|
|
AutherObjApp *Auther
|
|
)
|
|
|
|
var (
|
|
ErrStateIsWrong = errors.New("登录state非法")
|
|
)
|
|
|
|
func init() {
|
|
AutherObjWX = &Auther{
|
|
authType: AuthTypeWeixin,
|
|
}
|
|
auth2.RegisterAuther(AuthTypeWeixin, AutherObjWX)
|
|
|
|
AutherObjMP = &Auther{
|
|
authType: AuthTypeMP,
|
|
}
|
|
auth2.RegisterAuther(AuthTypeMP, AutherObjMP)
|
|
|
|
AutherObjNative = &Auther{
|
|
authType: AuthTypeWXNative,
|
|
}
|
|
auth2.RegisterAuther(AuthTypeWXNative, AutherObjNative)
|
|
|
|
AutherObjApp = &Auther{
|
|
authType: AuthTypeWxApp,
|
|
}
|
|
auth2.RegisterAuther(AuthTypeWxApp, AutherObjApp)
|
|
}
|
|
|
|
func (a *Auther) VerifySecret(id, secret string) (authBindEx *auth2.AuthBindEx, err error) {
|
|
globals.SugarLogger.Debugf("weixin VerifySecret id:%s secret:%s", secret, secret)
|
|
var openID, accessToken string
|
|
if a.authType != AuthTypeWXNative {
|
|
state := id
|
|
code := secret
|
|
if state == "" {
|
|
token, err2 := a.getAPI().SNSRetrieveToken(code)
|
|
if err = err2; err == nil {
|
|
openID = token.OpenID
|
|
accessToken = token.AccessToken
|
|
}
|
|
} else {
|
|
err = ErrStateIsWrong
|
|
}
|
|
} else {
|
|
openID = id
|
|
accessToken = secret
|
|
}
|
|
if err == nil {
|
|
wxUserinfo, err2 := a.getAPI().SNSGetUserInfo(accessToken, openID)
|
|
if err = err2; err == nil {
|
|
if authBindEx, err = a.UnionFindAuthBind(a.authType, a.getAPI().GetAppID(), []string{AuthTypeWeixin, AuthTypeMP, AuthTypeMini, AuthTypeWXNative, AuthTypeWxApp}, wxUserinfo.OpenID, wxUserinfo.UnionID, wxUserinfo); err == nil {
|
|
authBindEx.UserHint = &auth2.UserBasic{
|
|
Name: wxUserinfo.NickName,
|
|
Avatar: wxUserinfo.HeadImgURL,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return authBindEx, err
|
|
}
|
|
|
|
func (a *Auther) getAPI() *weixinapi.API {
|
|
if a.authType == AuthTypeWeixin {
|
|
return api.WeixinPageAPI
|
|
}
|
|
if a.authType == AuthTypeWxApp {
|
|
return api.WeixinApp
|
|
}
|
|
return api.WeixinAPI
|
|
}
|
|
|
|
func (a *Auther) GetUserType() (userType int8) {
|
|
return model.UserTypeStoreBoss
|
|
}
|