package cms import ( "errors" "git.rosy.net.cn/baseapi/platformapi/dingdingapi" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/auth2" "git.rosy.net.cn/jx-callback/business/auth2/authprovider/dingding" "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" "git.rosy.net.cn/jx-callback/globals" "git.rosy.net.cn/jx-callback/globals/api" ) 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) { globals.SugarLogger.Debugf("GetUser, authID:%s, authIDType:%s", authID, authIDType) 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, }, model.AdminName, 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, }, model.AdminName, map[string]interface{}{ "UserID": userID, }) return err } func (*UserProvider) CreateUser(userID2, mobile, email, name string) (user auth2.IUser, err error) { realUser := &model.User{ UserID2: userID2, Mobile: mobile, Email: email, Name: name, } return realUser, CreateUser(realUser) } 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 mobileAuth, err2 := auth2.Login(auth2.AuthTypeMobile, user.Mobile, auth2.UserIDMobile, mobileVerifyCode) if err = err2; err == nil { if !mobileAuth.IsUserEmpty() { return nil, model.ErrCodeUserAlreadyExist, auth2.ErrUserMobileAlreadyExist } user.Type = model.UserTypeConsumer if inAuthInfo.AuthBindInfo.Type == dingding.AuthTypeStaff { user.Type |= model.UserTypeOperator } if err = CreateUser(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 auth2.GetUserBindAuthInfo(authInfo.GetID()) } return nil, err } func CreateUser(user *model.User) (err error) { if user == nil || user.UserID2 == "" || user.Name == "" || user.Mobile == "" { return ErrUserIDAndNameMustGiven } dao.WrapAddIDCULDEntity(user, "RegisterUser") user.UserID = utils.GetUUID() user.Status = model.UserStatusNormal return dao.CreateEntity(nil, user) } func DisableUser(ctx *jxcontext.Context, userID string) (err error) { userName := ctx.GetUserName() if _, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{ "Status": model.UserStatusDisabled, }, userName, map[string]interface{}{ "UserID": userID, }); err == nil { auth2.DisableUser(userID, userName) } return err } func OnDingDingMsg(msg map[string]interface{}) (callbackResponse *dingdingapi.CallbackResponse) { eventType := utils.Interface2String(msg[dingdingapi.KeyEventType]) if eventType == dingdingapi.CBTagUserLeaveOrg { var ( authBind *model.AuthBind err error ) db := dao.GetDB() for _, userID := range msg[dingdingapi.KeyUserID].([]interface{}) { userIDStr := utils.Interface2String(userID) globals.SugarLogger.Debugf("OnDingDingMsg dingding user:%s left company", userIDStr) if authBind, err = dao.GetAuthBind(db, "", dingding.AuthTypeStaff, userIDStr); err == nil { // 直接找到了 globals.SugarLogger.Debugf("OnDingDingMsg dingding user:%s, userID:%s left company", userIDStr, authBind.UserID) if err = DisableUser(jxcontext.AdminCtx, authBind.UserID); err != nil { globals.SugarLogger.Errorf("OnDingDingMsg failed with error:%v", err) } } } } return api.DingDingAPI.Err2CallbackResponse(nil) }