82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
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/auth2/authprovider"
|
|
"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 = "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) (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.GetAuthBindsByAuthID2(db, wxUserinfo.UnionID, []string{AuthTypeWeixin, AuthTypeMP, AuthTypeMini}); 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
|
|
}
|