88 lines
2.7 KiB
Go
88 lines
2.7 KiB
Go
package authprovider
|
||
|
||
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"
|
||
)
|
||
|
||
type DefAuther struct {
|
||
}
|
||
|
||
// 此函数为空
|
||
func (a *DefAuther) AddAuthBind(authBindEx *auth2.AuthBindEx, userName string) (err error) {
|
||
authBind := &authBindEx.AuthBind
|
||
dao.WrapAddIDCULDEntity(authBind, userName)
|
||
authBind.Status = model.AuthBindStatusNormal
|
||
err = dao.CreateEntity(nil, authBind)
|
||
return err
|
||
}
|
||
|
||
func (a *DefAuther) UnbindAuth(authInfo *auth2.AuthInfo, authType string) (err error) {
|
||
_, err = dao.DeleteEntityLogically(nil, &model.AuthBind{}, nil, authInfo.GetID(), map[string]interface{}{
|
||
"UserID": authInfo.GetID(),
|
||
"Type": authType,
|
||
model.FieldDeletedAt: utils.DefaultTimeValue,
|
||
})
|
||
return err
|
||
}
|
||
|
||
func (a *DefAuther) SendVerifyCode(authID string) error {
|
||
return errors.New("当前登录类型不支持此操作")
|
||
}
|
||
|
||
// 此函数为空
|
||
func (a *DefAuther) Logout(authInfo *auth2.AuthInfo) error {
|
||
return nil
|
||
}
|
||
|
||
// 此函数用于联合(通过unionID)查找用户
|
||
func (a *DefAuther) UnionFindAuthBind(curAuthType string, unionAuthTypeList []string, openID, unionID string, authDetail interface{}) (authBindEx *auth2.AuthBindEx, err error) {
|
||
db := dao.GetDB()
|
||
var authBind *model.AuthBind
|
||
if authBind, err = dao.GetAuthBind(db, "", curAuthType, openID); err == nil { // 直接找到了
|
||
authBindEx = &auth2.AuthBindEx{
|
||
AuthBind: *authBind,
|
||
}
|
||
if authDetail != nil {
|
||
authBindEx.DetailData = string(utils.MustMarshal(authDetail))
|
||
}
|
||
} else if dao.IsNoRowsError(err) { // 直接找不到,尝试unionID
|
||
if unionID != "" { // 且有unionID
|
||
var authBindList []*model.AuthBind
|
||
if authBindList, err = dao.GetAuthBindsByAuthID2(db, unionID, unionAuthTypeList); err == nil && len(authBindList) > 0 { // 通过unionID找到至少一个认证方式
|
||
authBind = authBindList[0]
|
||
authBind.Type = curAuthType
|
||
authBind.AuthID = openID
|
||
if authDetail != nil {
|
||
authBind.DetailData = string(utils.MustMarshal(authDetail))
|
||
}
|
||
authBindEx = &auth2.AuthBindEx{
|
||
AuthBind: *authBind,
|
||
}
|
||
err = a.AddAuthBind(authBindEx, "admin") // 自动绑定
|
||
} else if dao.IsNoRowsError(err) {
|
||
err = nil
|
||
}
|
||
} else {
|
||
err = nil
|
||
}
|
||
}
|
||
if err == nil && authBindEx == nil { //如果没有报错,且没有找到一个认证方式,创建无用户(UserID为空)的认证方式
|
||
authBindEx = &auth2.AuthBindEx{
|
||
AuthBind: model.AuthBind{
|
||
Type: curAuthType,
|
||
AuthID: openID,
|
||
AuthID2: unionID,
|
||
},
|
||
}
|
||
if authDetail != nil {
|
||
authBindEx.DetailData = string(utils.MustMarshal(authDetail))
|
||
}
|
||
}
|
||
return authBindEx, err
|
||
}
|