- first edition of auth2
This commit is contained in:
80
business/auth2/authprovider/weixin/weixin.go
Normal file
80
business/auth2/authprovider/weixin/weixin.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package weixin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/auth2"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
)
|
||||
|
||||
const (
|
||||
AuthTypeWeixin = "weixin"
|
||||
AuthTypeMP = "weixinmp"
|
||||
AuthTypeMini = "weixinmini"
|
||||
)
|
||||
|
||||
type Auther struct {
|
||||
auth2.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) (authBind *model.AuthBind, err error) {
|
||||
globals.SugarLogger.Debugf("weixin VerifySecret code:%s", code)
|
||||
if state == "" {
|
||||
token, err2 := api.WeixinAPI.SNSRetrieveToken(code)
|
||||
if err = err2; err == nil {
|
||||
wxUserinfo, err2 := api.WeixinAPI.SNSGetUserInfo(token.AccessToken, token.OpenID)
|
||||
if err = err2; err == nil {
|
||||
db := dao.GetDB()
|
||||
if authBind, err = dao.GetAuthBind(db, "", a.authType, wxUserinfo.OpenID, ""); dao.IsNoRowsError(err) {
|
||||
var authBindList []*model.AuthBind
|
||||
if wxUserinfo.UnionID != "" {
|
||||
if authBindList, err = dao.GetAuthBindsByWXUnionID(db, wxUserinfo.UnionID); err == nil && len(authBindList) > 0 {
|
||||
authBind = authBindList[0]
|
||||
authBind.Type = a.authType
|
||||
authBind.AuthID = wxUserinfo.OpenID
|
||||
authBind.DetailData = string(utils.MustMarshal(wxUserinfo))
|
||||
err = a.AddAuthBind(authBind, wxUserinfo.NickName)
|
||||
}
|
||||
}
|
||||
if err == nil && len(authBindList) == 0 {
|
||||
authBind = &model.AuthBind{
|
||||
Type: a.authType,
|
||||
AuthID: wxUserinfo.OpenID,
|
||||
AuthID2: wxUserinfo.UnionID,
|
||||
DetailData: string(utils.MustMarshal(wxUserinfo)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = ErrStateIsWrong
|
||||
}
|
||||
return authBind, err
|
||||
}
|
||||
Reference in New Issue
Block a user