Example #1
0
        /// <summary>
        /// 设置验证码
        /// </summary>
        /// <returns></returns>
        public void SendCaptchas(Captcha model, ref Meta meta)
        {
            string phone = model.Phone.ToString();

            if (string.IsNullOrWhiteSpace(phone))
            {
                meta.ErrorCode = ErrorCode.SystemError.GetHashCode().ToString();
                meta.ErrorMsg  = "账户不能为空,请输入账号";
                return;
            }
            Regex rx = new Regex("^1[34578][0-9]{9}$");

            if (!rx.IsMatch(phone))
            {
                meta.ErrorCode = ErrorCode.SystemError.GetHashCode().ToString();
                meta.ErrorMsg  = "请输入正确的手机号码";
                return;
            }
            //用户是否注册
            Users clientinfo = UserService.First(model.Phone);

            //登录、找回密码验证码请求需验证用户是否存在
            if (((int)CaptchaType.ResetPwd).Equals((int)model.Type) ||
                ((int)CaptchaType.Login).Equals((int)model.Type))
            {
                if (clientinfo == null)
                {
                    meta.ErrorCode = ErrorCode.NoReg.GetHashCode().ToString();
                    meta.ErrorMsg  = "您的账号还未注册,请先注册";
                    return;
                }
            }
            //注册验证码请求需验证用户是否存在
            if (((int)CaptchaType.Register).Equals((int)model.Type))
            {
                if (clientinfo != null)
                {
                    meta.ErrorCode = ErrorCode.ExistReg.GetHashCode().ToString();
                    meta.ErrorMsg  = EnumHelper.GetDescriptionFromEnumValue(ErrorCode.ExistReg);
                    return;
                }
            }
            string randomstr = WebCommon.GetRandNumber(4);

            if (string.Equals(phone, "13750890964")) //手机号17767076165,发送验证码8888
            {
                randomstr = "8888";
            }
            SMS mySms = new SMS();

            mySms.SendSMS(model.Phone.ToString(),
                          string.Format(MessageHelper.GetMessageContent("captcha", "content"), randomstr));
            CAPTCHA captcha = new CAPTCHA();

            if (((int)CaptchaType.Register).Equals((int)model.Type))
            {
                captcha.Key = CaptchaType.Register + phone;
            }
            else if (((int)CaptchaType.ResetPwd).Equals((int)model.Type))
            {
                captcha.Key = CaptchaType.ResetPwd + phone;
            }
            else if (((int)CaptchaType.Login).Equals((int)model.Type))
            {
                captcha.Key = CaptchaType.Login + phone;
            }
            captcha.DateTime = DateTimeOffset.Now.AddMinutes(5);
            captcha.Value    = randomstr;
            model.DateTime   = DateTime.Now.AddMinutes(5);
            model.Value      = randomstr;
            model.ID         = captcha.Key;

            CaptchaService.Add(model, ref meta);
            meta.ErrorCode = ErrorCode.Success.GetHashCode().ToString();
            meta.ErrorMsg  = "验证码已发送至手机" + phone;
        }