public virtual async Task ChangeMcode(MCodeChangeLogCreateInput input) { if (input.NewMCode.Length != 32) { throw new UserFriendlyException("新机器码格式不正确(32位)!"); } var softUserLicenseMcode = await _softUserLicenseMcodeRepository.GetAsync(input.SoftUserLicenseMcodeId); if (softUserLicenseMcode == null) { throw new UserFriendlyException("旧机器码数据不存在!"); } if (!softUserLicenseMcode.IsActive) { throw new UserFriendlyException("旧机器码被封禁,不能换绑!"); } if (softUserLicenseMcode.Mcode == input.NewMCode) { throw new UserFriendlyException("新旧机器码是一样的,不需要换绑!"); } if (await _softUserLicenseMcodeRepository.CountAsync(t => t.SoftUserLicenseId == softUserLicenseMcode.SoftUserLicenseId && t.Mcode == input.NewMCode) > 0) { throw new UserFriendlyException("新旧机器码已存在,可以正常使用,不需要换绑!"); } long softId = softUserLicenseMcode.SoftUserLicense.SoftId; var softBindOption = await _softBindOptionRepository.FirstOrDefaultAsync(t => t.SoftId == softId); if (softBindOption != null) { if (await _mCodeChangeLogRepository.CountAsync(t => t.SoftUserLicenseId == softUserLicenseMcode.SoftUserLicenseId) >= softBindOption.AllowChangeBindCount) { throw new UserFriendlyException("换绑次数已经超过限制!"); } } MCodeChangeLog mCodeChangeLog = new MCodeChangeLog(); mCodeChangeLog.SoftUserLicenseId = softUserLicenseMcode.SoftUserLicenseId; mCodeChangeLog.SoftUserId = softUserLicenseMcode.SoftUserLicense.SoftUserId; mCodeChangeLog.SoftId = softUserLicenseMcode.SoftUserLicense.SoftId; mCodeChangeLog.OldMCode = softUserLicenseMcode.Mcode; mCodeChangeLog.NewMCode = input.NewMCode; mCodeChangeLog.Source = MCodeChangeSource.System; await _mCodeChangeLogRepository.InsertAsync(mCodeChangeLog); softUserLicenseMcode.Mcode = input.NewMCode; await _softUserLicenseMcodeRepository.UpdateAsync(softUserLicenseMcode); }
public virtual async Task <SoftAuthorizeResult> ChangeMcode(string appId, string loginName, string password, string oldmcode, string newmcode) { #region 检查参数是否正确 if (string.IsNullOrEmpty(appId)) { return(new SoftAuthorizeResult() { errormsg = "appId不允许为空!" }); } if (string.IsNullOrEmpty(loginName)) { return(new SoftAuthorizeResult() { errormsg = "用户名不允许为空!" }); } if (string.IsNullOrEmpty(password)) { return(new SoftAuthorizeResult() { errormsg = "密码不允许为空!" }); } if (string.IsNullOrEmpty(oldmcode)) { return(new SoftAuthorizeResult() { errormsg = "原机器码不允许为空!" }); } if (string.IsNullOrEmpty(newmcode)) { return(new SoftAuthorizeResult() { errormsg = "新机器码不允许为空!" }); } if (newmcode.Length != 32) { return(new SoftAuthorizeResult() { errormsg = "机器码格式不正确(正确的格式为32位英文字符和数字)!" }); } if (oldmcode.Equals(newmcode)) { return(new SoftAuthorizeResult() { errormsg = "新旧机器码是一样的,不需要换绑!" }); } var soft = await _softRepository.FirstOrDefaultAsync(t => t.AppId == appId); if (soft == null) { return(new SoftAuthorizeResult() { errormsg = "软件不存在!" }); } if (!soft.IsActive) { return(new SoftAuthorizeResult() { errormsg = "软件已停用,有问题请联系客服!" }); } var softUser = await _softUserRepository.FirstOrDefaultAsync(t => t.LoginName == loginName); if (softUser == null) { return(new SoftAuthorizeResult() { errormsg = "用户名或密码不正确!" }); } if (new PasswordHasher().VerifyHashedPassword(softUser.Password, password) != PasswordVerificationResult.Success) { return(new SoftAuthorizeResult() { errormsg = "用户名或密码不正确!" }); } if (!softUser.IsActive) { return(new SoftAuthorizeResult() { errormsg = "该用户已被禁用,如有疑问请联系客服!" }); } var softUserLicense = await _softUserLicenseRepository.FirstOrDefaultAsync(t => t.SoftUserId == softUser.Id && t.SoftId == soft.Id); if (softUserLicense == null) { return(new SoftAuthorizeResult() { errormsg = "软件未授权,请先使用卡密授权该软件!" }); } if (!softUserLicense.IsActive) { return(new SoftAuthorizeResult() { errormsg = "该软件授权已被禁用,如有疑问请联系客服!" }); } if (softUserLicense.ExpireTime.HasValue && DateTime.Now >= softUserLicense.ExpireTime.Value) { return(new SoftAuthorizeResult() { errormsg = "该软件授权已到期!" }); } var userLicenseMcode = await _softUserLicenseMcodeRepository.FirstOrDefaultAsync(t => t.SoftUserLicenseId == softUserLicense.Id && t.Mcode == oldmcode); if (userLicenseMcode == null) { return(new SoftAuthorizeResult() { errormsg = "原机器码没有绑定,无法换绑!" }); } if (!userLicenseMcode.IsActive) { return(new SoftAuthorizeResult() { errormsg = "原机器码已被禁用,无法换绑,如有疑问请联系客服!" }); } if (await _softUserLicenseMcodeRepository.CountAsync(t => t.SoftUserLicenseId == softUserLicense.Id && t.Mcode == newmcode) > 0) { return(new SoftAuthorizeResult() { errormsg = "新旧机器码已存在,可以正常使用,不需要换绑!" }); } long softId = softUserLicense.SoftId; var softBindOption = await _softBindOptionRepository.FirstOrDefaultAsync(t => t.SoftId == softId); if (softBindOption != null) { if (await _mCodeChangeLogRepository.CountAsync(t => t.SoftUserLicenseId == softUserLicense.Id) >= softBindOption.AllowChangeBindCount) { return(new SoftAuthorizeResult() { errormsg = "换绑次数已经超过限制!" }); } } #endregion MCodeChangeLog mCodeChangeLog = new MCodeChangeLog(); mCodeChangeLog.SoftUserLicenseId = softUserLicense.Id; mCodeChangeLog.SoftUserId = softUserLicense.SoftUserId; mCodeChangeLog.SoftId = softUserLicense.SoftId; mCodeChangeLog.OldMCode = userLicenseMcode.Mcode; mCodeChangeLog.NewMCode = newmcode; mCodeChangeLog.Source = MCodeChangeSource.User; await _mCodeChangeLogRepository.InsertAsync(mCodeChangeLog); userLicenseMcode.Mcode = newmcode; await _softUserLicenseMcodeRepository.UpdateAsync(userLicenseMcode); string okMsg = ""; if (softBindOption != null) { int hasChgCount = await _mCodeChangeLogRepository.CountAsync(t => t.SoftUserLicenseId == softUserLicense.Id); okMsg = string.Format("软件允许换绑次数:{0},您已换绑:{1}次,还剩:{2}次换绑!", softBindOption.AllowChangeBindCount, hasChgCount, softBindOption.AllowChangeBindCount - hasChgCount); } return(new SoftAuthorizeResult() { success = true, msg = okMsg }); }