60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package cms
|
||
|
||
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"
|
||
)
|
||
|
||
var (
|
||
ErrUserIDAndNameMustGiven = errors.New("用户ID2,用户名及手机号必须不为空")
|
||
)
|
||
|
||
var (
|
||
userProvider = &UserProvider{}
|
||
authTypeFieldMap = map[string]string{
|
||
auth2.UserIDID: "user_id",
|
||
auth2.UserIDID2: "user_id2",
|
||
auth2.UserIDMobile: "mobile",
|
||
auth2.UserIDEmail: "email",
|
||
}
|
||
)
|
||
|
||
type UserProvider struct {
|
||
}
|
||
|
||
func (*UserProvider) GetUser(authID, authIDType string) (user auth2.IUser) {
|
||
fieldName := authTypeFieldMap[authIDType]
|
||
if fieldName != "" {
|
||
user, _ = dao.GetUserByID(dao.GetDB(), fieldName, authID)
|
||
}
|
||
return user
|
||
}
|
||
|
||
func init() {
|
||
auth2.Init(userProvider)
|
||
}
|
||
|
||
func RegisterUser(user *model.User, mobileVerifyCode string, inAuthInfo *auth2.AuthInfo) (outAuthInfo *auth2.AuthInfo, err error) {
|
||
if user == nil || user.UserID2 == "" || user.Name == "" || user.Mobile == "" {
|
||
return nil, ErrUserIDAndNameMustGiven
|
||
}
|
||
mobileAuth, err2 := auth2.Login(auth2.AuthTypeMobile, user.Mobile, auth2.UserIDNone, mobileVerifyCode)
|
||
if err = err2; err == nil {
|
||
if mobileAuth.IUser != nil {
|
||
return nil, auth2.ErrUserAlreadyExist
|
||
}
|
||
dao.WrapAddIDCULDEntity(user, "RegisterUser")
|
||
user.UserID = utils.GetUUID()
|
||
if err = dao.CreateEntity(nil, user); err == nil {
|
||
if outAuthInfo, err = auth2.BindUser(mobileAuth, user); err == nil && inAuthInfo != nil {
|
||
err = auth2.AddAuthBind(outAuthInfo, inAuthInfo)
|
||
}
|
||
}
|
||
}
|
||
return outAuthInfo, err
|
||
}
|