/* * 提交注册码 * * 1.永久注册码 * * 2.有有效期的注册码 */ private void Reg_Submit_Button_Click(object sender, RoutedEventArgs e) { string regCodeVal = regCodeText.Text; if (string.IsNullOrWhiteSpace(regCodeVal)) { MessageBox.Show("请填写注册码"); return; } bool isRight = RegCodeUtil.isRightOfRegCode(regCodeVal, machineCodeVal); //1.注册码正确 if (isRight) { //更新注册码到数据库,并显示注册成功 Cfg2 cfg21 = cfg2Bll.get(1); cfg21.rg3 = regCodeVal; string validateCode1 = RegCodeUtil.generateValidateCode(cfg21, machineCodeVal); cfg21.validateCode = validateCode1; cfg2Bll.update(cfg21); successGrid.Visibility = Visibility.Visible; regGrid.Visibility = Visibility.Collapsed; return; } // 2.判断是否为有效期注册码 int limitDay = RegCodeUtil.isRightOfLimitDayRegCode(regCodeVal, machineCodeVal); if (limitDay <= 0) { MessageBox.Show("注册码无效,请重试"); return; } //更新注册码到数据库 Cfg2 cfg2 = cfg2Bll.get(1); cfg2.ld = EncryptionUtil.encode(limitDay.ToString(), machineCodeVal); string validateCode = RegCodeUtil.generateValidateCode(cfg2, machineCodeVal); cfg2.validateCode = validateCode; cfg2Bll.update(cfg2); //显示剩余试用天数 TimeSpan ts1 = DateTime.Now - DateTime.Parse("1970-1-1"); int currDay = (int)Math.Floor(ts1.TotalDays); int leaveDays = limitDay - currDay; tipsLabel.Content = leaveDays + "天后,试用到期"; tips.Visibility = Visibility.Visible; successGrid.Visibility = Visibility.Visible; regGrid.Visibility = Visibility.Collapsed; }
/* * 提交 */ private void Reg_Submit_Button_Click(object sender, RoutedEventArgs e) { string machineCodeVal = machineCodeText.Text; string regCodeVal = regCodeText.Text; string limitDayVal = limitDayText.Text; if (string.IsNullOrWhiteSpace(machineCodeVal)) { MessageBox.Show("请填写机器码"); return; } if (string.IsNullOrWhiteSpace(regCodeVal)) { MessageBox.Show("请填写永久注册码"); return; } if (string.IsNullOrWhiteSpace(limitDayVal)) { MessageBox.Show("请填写有效期"); return; } bool isRight = RegCodeUtil.isRightOfRegCode(regCodeVal, machineCodeVal); if (!isRight) { MessageBox.Show("永久注册码无效,请检查机器码是否正确"); return; } DateTimeFormatInfo dtFormat = new DateTimeFormatInfo(); dtFormat.ShortDatePattern = "yyyy-MM-dd"; DateTime dt = Convert.ToDateTime(limitDayVal, dtFormat); TimeSpan ts1 = dt - DateTime.Parse("1970-1-1"); //更新注册码到数据库,并显示注册成功 int days = (int)Math.Floor(ts1.TotalDays); string limitDayRegCodeVal = RegCodeUtil.generateWithLimitDay(days, machineCodeVal); limitDayRegCodeText.Text = limitDayRegCodeVal; calendar1.Visibility = Visibility.Collapsed; limitDayCanvas.Visibility = Visibility.Visible; }
/* * 软件是否已注册 */ private Boolean isRegistered() { //1.获取数据库信息 Cfg2 cfg2 = cfg2Bll.get(1); string machineCode = MachineCodeUtil.GetMachineCodeString(); machineCodeVal = machineCode; //2.数据完整性校验 if (RegCodeUtil.validate(cfg2, machineCode) == false) { return(false); } //3.注册码验证 if (RegCodeUtil.isRightOfRegCode(cfg2.rg3, machineCode) == false) { return(false); } return(true); }
/* * 验证是否注册 * * errorCode=1001,提示用户,点击关闭软件(需要注册) * * errorCode=1002,提示用户,可以继续试用(非静默试用) * * errorCode=1003,不提示用户,继续试用(静默试用) * */ public BaseResult isReg() { BaseResult baseResult = new BaseResult(); baseResult.result = false; baseResult.errorCode = 1001; baseResult.message = "未知"; //1.获取数据库信息,并验证数据完整性 Cfg2 cfg2 = cfg2Bll.get(1); string machineCode = MachineCodeUtil.GetMachineCodeString(); //3.注册码是否存在,存在则验证 if (!string.IsNullOrWhiteSpace(cfg2.rg3)) { if (RegCodeUtil.isRightOfRegCode(cfg2.rg3, machineCode)) { baseResult.result = true; baseResult.errorCode = 0; baseResult.message = "注册码正确"; return(baseResult); } else { baseResult.result = false; baseResult.errorCode = 1001; baseResult.message = "注册码无效"; return(baseResult); } } //4.验证数据库注册日期是否存在,不存在则填写 if (!string.IsNullOrWhiteSpace(cfg2.sd)) { if (cfg2.sd == "sgfdo_fdgrexfhtreKUFSSA") { //第一次打开软件 TimeSpan ts1 = DateTime.Now - DateTime.Parse("1970-1-1"); int regDay = (int)Math.Floor(ts1.TotalDays); cfg2.sd = EncryptionUtil.encode(regDay.ToString(), machineCode); cfg2.cd = cfg2.sd; if (string.IsNullOrWhiteSpace(cfg2.ld)) { int limitDay = regDay + 30; cfg2.ld = EncryptionUtil.encode(limitDay.ToString(), machineCode); } string validateCode1 = RegCodeUtil.generateValidateCode(cfg2, machineCode); cfg2.validateCode = validateCode1; cfg2 = cfg2Bll.update(cfg2); baseResult.result = false; baseResult.errorCode = 1003; baseResult.message = "第一次打开软件"; return(baseResult); } } else { baseResult.result = false; baseResult.errorCode = 1001; baseResult.message = "注册日期异常(空),试用结束"; return(baseResult); } //2.数据完整性校验 if (RegCodeUtil.validate(cfg2, machineCode) == false) { baseResult.result = false; baseResult.errorCode = 1001; baseResult.message = "数据完整性校验失败"; return(baseResult); } //5.1 获取开始时间 string sdStr = EncryptionUtil.decode(cfg2.sd, machineCode); if (!DataUtil.isInt(sdStr)) { baseResult.result = false; baseResult.errorCode = 1001; baseResult.message = "注册日期异常(非整数),试用结束"; return(baseResult); } else if (Int32.Parse(sdStr) < 0) { baseResult.result = false; baseResult.errorCode = 1001; baseResult.message = "注册日期异常(小于0),试用结束"; return(baseResult); } int sd = DataUtil.ToInt(sdStr); //5.2获取数据库中截止日期 string ldStr = EncryptionUtil.decode(cfg2.ld, machineCode); int ld = DataUtil.ToInt(ldStr); //5.3获取数据库中当前时间 string cdStr_old = EncryptionUtil.decode(cfg2.cd, machineCode); int cd_old = DataUtil.ToInt(cdStr_old); //5.4 获取当前时间,并比较 TimeSpan ts2 = DateTime.Now - DateTime.Parse("1970-1-1"); int cd = (int)Math.Floor(ts2.TotalDays); if (cd_old > cd) { baseResult.result = false; baseResult.errorCode = 1001; baseResult.message = "计算机时间异常,试用结束"; return(baseResult); } //6.注册日期>截止日期,试用结束 if (cd > ld) { baseResult.result = false; baseResult.errorCode = 1001; baseResult.message = "试用结束"; return(baseResult); } //7. 更新当前日期 cfg2.cd = EncryptionUtil.encode(cd.ToString(), machineCode); string validateCode2 = RegCodeUtil.generateValidateCode(cfg2, machineCode); cfg2.validateCode = validateCode2; cfg2 = cfg2Bll.update(cfg2); //8. 剩余试用天数,当restDays<10,则弹窗提示 int restDays = ld - cd; if (restDays < 10) { baseResult.result = false; baseResult.errorCode = 1002; baseResult.message = restDays + " 天后,试用结束"; return(baseResult); } baseResult.result = false; baseResult.errorCode = 1003; baseResult.message = restDays + " 天后,试用结束"; return(baseResult); //baseResult.result = false; //baseResult.errorCode = 1002; //baseResult.message = restDays + " 天后,试用结束"; //return baseResult; }