using System; using System.Linq; using System.Text; using WX.CRM.BLL.Base; using WX.CRM.BLL.Res; using WX.CRM.BLL.Util; using WX.CRM.Common; using WX.CRM.IBLL.Sms; using WX.CRM.Model.Entity; using WX.CRM.Model.Enum; namespace WX.CRM.BLL.Sms { public class SMS_USERVERIFYCODE_BLL : ISMS_USERVERIFYCODE, ISMS_USERVERIFYCODE_Q { ValidationErrors errors = new ValidationErrors(); /// /// 手机号验证获取验证码 /// /// /// public string GetVerifyCodeHandler(string mobile, int sendcodetime, int sendcodecount, string subTypeCode, string typeCode, decimal createuser = 1) { string returnMessage = string.Empty; CACHE_BL cacheQ = new CACHE_BL(); try { string resid = ResUtil.CreateResId(mobile); string cilentCode = GetCilentCode(subTypeCode); RES_CUSTOMER_BL regcustomer = new RES_CUSTOMER_BL(); regcustomer.ResgisterCustomer(mobile, resid, "web_applySignCode"); Add_Apply(mobile, resid, "web_applySignCode"); if (checkSendCodeToUser(resid, subTypeCode, -sendcodetime, sendcodecount)) { string message = string.Empty; string code = CreateVerifyCode("", resid, subTypeCode); //获取验证码 switch (subTypeCode) { case "SMS_VERIFY_OpenUser": message = EypnStrMessage(subTypeCode, code); break; case "SMS_VERIFY_ResetBind": message = EypnStrMessage(subTypeCode, code); break; } if (string.IsNullOrEmpty(message)) { returnMessage = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode.参数错误, retMessage = "参数错误" }); return returnMessage; } SendMessageToUser(resid, typeCode, subTypeCode, message, cilentCode, createuser); //发送验证码给用户 returnMessage = Utility.ObjectToJson(new { result = true, retcode = (int)EnumInterfaceErrcode.调用成功, retMessage = "调用成功" }); } else { returnMessage = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode.十五分钟已连续发送三次, retMessage = "十五分钟已连续发送三次" }); } return returnMessage; } catch { throw; } } public string EypnStrMessage(string subTypeCode, string code) { string message = ""; using (var db = new crmContext()) { var tab = db.BAS_PARAMETER.Where(p => p.PARAKEY == subTypeCode + "_Message").FirstOrDefault(); if (tab != null) { message = string.Format(tab.PARAVALUE, code); } return message; //"尊敬的用户您好,您正进行天贵所网络开户,验证码为" + code + ",为保证安全!请确保是您本人操作" } } /// /// 短信发送判断 /// /// /// /// /// /// public bool checkSendCodeToUser(string resid, string type, int time, int count) { bool checkReslut = false; using (var db = new crmContext()) { DateTime expTime = DateTime.Now.AddMinutes(time); var tab = db.SMS_USERVERIFYCODE.Where(m => m.VERIFYRESID.Trim() == resid.Trim() && m.VERIFYTYPE == type && m.CTIME > expTime).ToList(); if (tab != null && tab.Count < count) { checkReslut = true; } } return checkReslut; } public bool CheckSms_UserVerifyCode(string resid) { bool relust = false; try { using (crmContext db = new crmContext()) { var ds = db.SMS_USERVERIFYCODE.Where(p => p.VERIFYRESID.Trim() == resid.Trim()).Count(); if (ds > 0) { relust = true; } } } catch (Exception ex) { LogHelper.Error(ex.Message + ex.StackTrace); relust = false; } return relust; } /// /// 添加到资源系统 /// /// /// /// public void Add_Apply(string mobile, string resid, string campaignId) { RES_APPLY_BL res_apply = new RES_APPLY_BL(); RES_APPLY applyinfo = new RES_APPLY(); applyinfo.CTIME = DateTime.Now; applyinfo.JSONDATA = string.Empty; applyinfo.JSONTYPE = 0; applyinfo.MOBILE = mobile; applyinfo.RESID = resid; applyinfo.RESOURCETAG = campaignId; applyinfo.RTIME = DateTime.Now; applyinfo.STATUS = 0; res_apply.Create(ref errors, applyinfo); } /// /// 发送认证码到用户手机上 /// /// 认证码 public void SendMessageToUser(string resId, string typeCode, string subTypeCode, string message, string clientCode, decimal createiser = 1) { using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext()) { SMS_MESSAGE msg = new SMS_MESSAGE(); msg.CTIME = DateTime.Now; msg.MESSAGEID = new SEQUENCES_BL().Seq_base_get(WX.CRM.Model.Enum.PKIDType.LargeTable); msg.MESSAGE = message; msg.RESID = resId; msg.SUBTYPECODE = subTypeCode; msg.TYPECODE = typeCode; msg.CLIENTCODE = clientCode; msg.CREATEUSER = createiser; db.SMS_MESSAGE.Add(msg); db.SaveChanges(); } } /// /// 生成验证码(4位数字+大写字母) /// /// /// /// /// public string CreateVerifyCode(string username, string resid, string VerifyType) { byte[] bytes = new byte[100]; Random randObj = new Random(); int code; for (int i = 0; i < 6; i++) { code = randObj.Next(48, 57);//65-90 bytes[i] = Convert.ToByte(code); } ASCIIEncoding ascii = new ASCIIEncoding(); string verifycode = ascii.GetString(bytes, 0, 6); SMS_USERVERIFYCODE _UserVerify = new SMS_USERVERIFYCODE(); _UserVerify.USERNAME = username; _UserVerify.VERIFYRESID = resid; _UserVerify.VERIFYCODE = verifycode; _UserVerify.VERIFYTYPE = VerifyType; _UserVerify.EXPIRETIME = DateTime.Now.AddMinutes(30); _UserVerify.CTIME = DateTime.Now; using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext()) { _UserVerify.PKID = new SEQUENCES_BL().Seq_base_get(WX.CRM.Model.Enum.PKIDType.LargeTable); db.SMS_USERVERIFYCODE.Add(_UserVerify); db.SaveChanges(); } return verifycode; } /// /// 获取短信帐号 /// /// /// public string GetCilentCode(string subTypeCode) { string cilentCode = ""; using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext()) { var sms_subtype = db.SMS_MSGSUBTYPE.FirstOrDefault(p => p.SUBTYPECODE.Trim() == subTypeCode.Trim()); if (sms_subtype != null) { var cilent = db.SMS_MSGTYPE_CLIENT.FirstOrDefault(p => p.SUBTYPEID == sms_subtype.SUBTYPEID); cilentCode = cilent.CLIENTCODE; } } return cilentCode; } /// /// 开户认证 /// /// /// /// public string CheckOpenAccount(string resid, string code, string subtypeCode) { string returnMessage = ""; using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext()) { var ds = db.SMS_USERVERIFYCODE.Where(p => p.VERIFYCODE.Trim() == code.Trim() && p.VERIFYRESID.Trim() == resid.Trim() && p.VERIFYTYPE.Trim() == subtypeCode.Trim()).OrderByDescending(p => p.CTIME).FirstOrDefault(); if (ds == null) { returnMessage = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode.验证码错误 }); return returnMessage; } else if (ds.EXPIRETIME < DateTime.Now) { returnMessage = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode.验证码过期 }); return returnMessage; } returnMessage = Utility.ObjectToJson(new { result = true, retcode = (int)EnumInterfaceErrcode.调用成功 }); return returnMessage; } } } }