Пример #1
0
        public ActionResult Register()
        {
            int lastSeconds = -1;

            if (Request.HttpContext.Session.GetString("LastSendSmsTime") != null)
            {
                var lastSendSmsTime = DateTime.Parse(Request.HttpContext.Session.GetString("LastSendSmsTime"));
                var secondsSpan     = lastSendSmsTime.AddSeconds(SiteConfig.SMSSENDWAITSECONDS) - DateTime.Now;
                lastSeconds = secondsSpan.TotalSeconds > 0 ? (int)secondsSpan.TotalSeconds : -1;
            }

            var regGuid = Guid.NewGuid();

            Request.HttpContext.Session.SetString("RegGuid", regGuid.ToString("N"));
            var vd = new Home_RegisterVD()
            {
                LastSeconds = lastSeconds,
                SmsToken    = _smsRecordService.SetSendSmsToken(),
                RegGuid     = regGuid,
                QrUrl       = GetRegQrCode(regGuid)
            };

            return(View(vd));
        }
Пример #2
0
        public ActionResult Register(Home_RegisterVD vd_form)
        {
            //判断是否已经绑定微信
            var codeRegCache = SenparcDI.GetService <QrCodeRegCache>();
            var messageItem  = codeRegCache.MessageQueue.FirstOrDefault(z => z.Data.RegGuid == vd_form.RegGuid);

            if (messageItem != null)
            {
                //允许注册
                var qrCodeRegData = messageItem.Data;
                var user          = _accountService.GetObject(z => z.WeixinOpenId == qrCodeRegData.OpenId);
                if (user != null)
                {
                    return(RedirectToAction("Login", "Home"));
                }
            }
            else
            {
                //未发送验证码
                return(RedirectToAction("Register", "Home"));
            }

            var userNameForbiddenWords = new[] { "@", "~", "`", "\"", "/", "<", ">", "'", "*", " ", " " };

            this.Validator(vd_form.UserName, "用户名", "UserName", false)
            .IsFalse(z => userNameForbiddenWords.Any(z.Contains), "{0}不能包含空格及特殊字符!", true)
            .MinLength(4)
            .IsFalse(z => Regex.IsMatch(z, @"^\d+$"), "不能全是数字", true)
            .IsFalse(z => _accountService.CheckUserNameExisted(0, z), "{0}已存在!", true);

            this.Validator(vd_form.Password, "密码", "Password")
            .IsNotNull(vd_form.Password, "请填写密码!", true)
            .MinLength(6)
            .IsEqual(vd_form.RePassword, "两次密码不一致", true);
            this.Validator(vd_form.Email, "Email", "Email", false)
            .IsEmail(true)
            .IsFalse(z => _accountService.CheckEmailExisted(0, z), "{0}已存在!", true);
            this.Validator(vd_form.Phone, "手机号", "Phone", false).IsMobile(true)
            .IsFalse(z => _accountService.CheckPhoneExisted(0, z), "{0}已存在!", true);
            var phoneCheckVal  = this.Validator(vd_form.PhoneCheck, "验证码", "PhoneCheck", false);
            var qrCodeCheckVal = this.Validator(vd_form.QrCodeCheck, "微信验证码", "QrCodeCheck", false);

            if (ModelState.IsValid)
            {
                if (!SiteConfig.IsDebug)
                {
                    //短信验证
                    var phoneCheckCodeCache = SenparcDI.GetService <PhoneCheckCodeCache>();
                    var data = phoneCheckCodeCache.Get(vd_form.PhoneCheck);
                    phoneCheckVal.IsTrue(z => data != null && data.Data.Phone == vd_form.Phone, "{0}错误!", true);
                    qrCodeCheckVal.IsTrue(z => messageItem.Data != null && messageItem.Data.Code == vd_form.QrCodeCheck, "{0}错误!", true);
                }
            }

            if (!ModelState.IsValid)
            {
                var smsRecord = SenparcDI.GetService <SmsRecordService>();
                var smsToken  = smsRecord.GetSendSmsToken();//获取已经保存的令牌
                vd_form.SmsToken = smsToken;
                return(View(vd_form));
            }
            try
            {
                _accountService.CreateAccount(vd_form.UserName.Trim(), vd_form.Email.Trim(), vd_form.Phone.Trim(), vd_form.Password.Trim(),
                                              messageItem.Data.OpenId, "", "");
                codeRegCache.Remove(messageItem.Data.Key);//删除缓存记录
                SetMessager(MessageType.success, "注册成功");
                return(RedirectToAction("Login"));
            }
            catch (Exception)
            {
                SetMessager(MessageType.danger, "注册失败,请稍后重试");
                var smsRecord = SenparcDI.GetService <SmsRecordService>();
                var smsToken  = smsRecord.GetSendSmsToken();//获取已经保存的令牌
                vd_form.SmsToken = smsToken;
                return(View(vd_form));
            }
        }