Ejemplo n.º 1
0
        public Dictionary <string, int> InviteMembers(int teamID, string currentUserMail, List <string> invitedUserMails)
        {
            invitedUserMails = invitedUserMails.Where(s => !string.IsNullOrEmpty(s)).Distinct().ToList();

            var dict        = new Dictionary <string, int>();
            var currentTeam = this.getTeamInfo(teamID);

            if (currentTeam == null)
            {
                Enforce.Throw(new LogicErrorException("当前团队已被删除"));
            }

            var teamMembers = this.getTeamMemberList(teamID);
            //仅返回存在的用户
            var userList = this._userRepository.GetUserInfoModelList(invitedUserMails);

            //需要插入的未注册的用户
            var insertedUserList = Mapper.Map <List <UserInfoModel>, List <T_USER> >(
                invitedUserMails
                .Where(s => !userList.Exists(m => m.mail.Equals(s.ToLower())))
                .Select(s => new UserInfoModel()
            {
                userName     = s.Split('@')[0],
                userTrueName = s.Split('@')[0],
                userLevel    = 1,
                sex          = null,
                mail         = s.ToLower(),
                //未激活
                userStatus    = 0,
                lastLoginTime = DateTime.Now,
                createTime    = DateTime.Now
            })
                .ToList()
                );
            //需要插入的群成员
            var insertMemberList = invitedUserMails.Where(s => !teamMembers.Exists(m => m.memberMail.Equals(s.ToLower()))).Select(s => new TeamMemberModel()
            {
                teamID         = teamID,
                userID         = userList.Exists(m => m.mail.Equals(s.ToLower())) ? userList.Single(m => m.mail.Equals(s.ToLower())).userID : 0,
                roleCode       = RoleCode.Member,
                tags           = new List <string>(),
                state          = userList.Exists(m => m.mail.Equals(s.ToLower())) ? 1 : 0,
                memberName     = userList.Exists(m => m.mail.Equals(s.ToLower())) ? userList.Single(m => m.mail.Equals(s.ToLower())).userName : string.Empty,
                memberMail     = s.ToLower(),
                createUserMail = currentUserMail,
                createTime     = DateTime.Now
            }).ToList();

            var insertedTeamMemberList = Mapper.Map <List <TeamMemberModel>, List <T_TEAM_MEMBER> >(insertMemberList);

            this._teamMemberBll.InviteMembers(teamID, insertedUserList, insertedTeamMemberList);

            invitedUserMails.ForEach(s =>
            {
                if (insertMemberList.Exists(m => m.memberMail.Equals(s.ToLower())))
                {
                    dict.Add(s, insertMemberList.Single(m => m.memberMail.Equals(s.ToLower())).state);
                }
            });

            //发送邮件
            foreach (string key in dict.Keys)
            {
                //Todo:后续需要优化
                _mailService.SendMailAsync(key,
                                           _mailConfig.InviteContent.Subject.Text,
                                           _templateHelper.GenerateContent(_mailConfig.InviteContent.Body.Text, "inviteContent", new { Name = currentTeam.teamName })
                                           , true);
            }


            return(dict);
        }
Ejemplo n.º 2
0
        public async Task <string> SendAuthMail(string reSendEmailToken, string identity, string code, string mail, int codeType, string agent)
        {
            if (!Enum.IsDefined(typeof(EmailAuthCodeType), codeType))
            {
                Enforce.Throw(new FrontInputValidateErrorException("验证码邮件类型错误"));
            }
            EmailAuthCodeType mailCodeType = (EmailAuthCodeType)Enum.ToObject(typeof(EmailAuthCodeType), codeType);

            //判断是否包含重发token
            if (string.IsNullOrEmpty(reSendEmailToken))
            {
                if (string.IsNullOrEmpty(identity))
                {
                    Enforce.Throw(new FrontInputValidateErrorException("验证码identity不能为空"));
                }

                //去认证验证码,验证码通过才能发邮件
                var verifyAuthCodeResult = await this.verifyAuthCode(identity, code);

                if (!verifyAuthCodeResult)
                {
                    Enforce.Throw(new LogicErrorException("验证码错误"));
                }
            }
            else
            {
                //验证重发token,重发token通过才会发邮件
                bool verifyReSendMailToken = await this.verifyReSendAuthMailToken(mail, mailCodeType, agent, reSendEmailToken);

                if (!verifyReSendMailToken)
                {
                    Enforce.Throw(new LogicErrorException("重发邮件失败"));
                }
            }

            //验证码
            var authCode = AuthCode.CreateAuthCode(100, 40, ImageFormat.Png);

            var setCodeStatus = await _database.StringSetAsync(this.generateEmailCodeKey(mail, mailCodeType), authCode.Code.ToLower(), TimeSpan.FromMinutes(10));


            //重发token
            string token          = this.generateToken(agent);
            var    setTokenStatus = await _database.StringSetAsync(this.generateReSendMailTokenKey(mail, mailCodeType), token, TimeSpan.FromMinutes(20));


            if (setCodeStatus && setTokenStatus)
            {
                //异步发送邮件
                //_mailService.SendCodeMailAsync(mail, authCode.Code);
                //_mailService.SendMailAsync(mail,
                //    _mailConfig.CodeContent.Subject.Text,
                //    string.Format(_mailConfig.CodeContent.Body.Text, authCode.Code), true);
                //Todo:后续需要优化
                _mailService.SendMailAsync(mail,
                                           _mailConfig.CodeContent.Subject.Text,
                                           _templateHelper.GenerateContent(_mailConfig.CodeContent.Body.Text, "codeContent", new { Code = authCode.Code })
                                           , true);

                return(token);
            }


            return(string.Empty);
        }