Files
jx-callback/business/jxstore/cms/user2.go
2019-03-04 14:36:00 +08:00

88 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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/jxutils/jxcontext"
"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()
user.Status = model.UserStatusNormal
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
}
func GetUserBindAuthInfo(ctx *jxcontext.Context) (authList []*model.AuthBind, err error) {
authInfo, err := ctx.GetV2AuthInfo()
if err == nil {
return dao.GetUserBindAuthInfo(dao.GetDB(), authInfo.GetID())
}
return nil, err
}
func ChangeMobile2(ctx *jxcontext.Context, mobile, mobileVerifyCode string) (err error) {
authInfo, err := ctx.GetV2AuthInfo()
if err == nil {
mobileAuth, err2 := auth2.Login(auth2.AuthTypeMobile, mobile, auth2.UserIDNone, mobileVerifyCode)
if err = err2; err == nil {
if mobileAuth.IUser != nil && authInfo.GetID() != mobileAuth.GetID() {
return errors.New("手机号已经存在")
}
_, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{
"Mobile": mobile,
}, ctx.GetUserName(), map[string]interface{}{
"UserID": authInfo.GetID(),
})
}
}
return err
}