Files
jx-callback/business/jxstore/cms/user2.go

98 lines
2.8 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 != "" {
user2, err := dao.GetUserByID(dao.GetDB(), fieldName, authID)
if err == nil {
user = user2 // 这样写的原因是golang nil的比较问题
}
}
return user
}
func (*UserProvider) UpdateUserMobile(userID string, mobile string) (err error) {
_, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{
"Mobile": mobile,
}, "admin", map[string]interface{}{
"UserID": userID,
})
return err
}
func (*UserProvider) UpdateUserEmail(userID string, email string) (err error) {
_, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{
"Email": email,
}, "admin", map[string]interface{}{
"UserID": userID,
})
return err
}
func init() {
auth2.Init(userProvider)
}
func RegisterUser(user *model.User, mobileVerifyCode string, inAuthInfo *auth2.AuthInfo) (outAuthInfo *auth2.AuthInfo, errCode string, err error) {
errCode = model.ErrCodeGeneralFailed
if user == nil || user.UserID2 == "" || user.Name == "" || user.Mobile == "" {
return nil, model.ErrCodeGeneralFailed, ErrUserIDAndNameMustGiven
}
mobileAuth, err2 := auth2.Login(auth2.AuthTypeMobile, user.Mobile, auth2.UserIDMobile, mobileVerifyCode)
if err = err2; err == nil {
if mobileAuth.IUser != nil {
return nil, model.ErrCodeUserAlreadyExist, auth2.ErrUserMobileAlreadyExist
}
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)
}
} else if dao.IsDuplicateError(err) {
errCode = model.ErrCodeUserAlreadyExist
err = auth2.ErrUserID2AlreadyExist
}
}
if err == nil {
errCode = model.ErrCodeSuccess
}
return outAuthInfo, errCode, 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
}