public virtual async Task Authorize(SoftLicense softLicense, SoftUser softUser) { #region 检查参数是否有效 if (softLicense == null) { throw new UserFriendlyException("卡密数据不存在!"); } if (softUser == null) { throw new UserFriendlyException("用户不存在!"); } if (softLicense.Status != SoftLicenseStatus.Sell) { throw new UserFriendlyException("卡密不存在或已被使用!"); } if (!softUser.IsActive) { throw new UserFriendlyException("用户账号被停用,无法授权!"); } //判断用户是否已经授权本软件至永久 var userLicenseInfo = await _softUserLicenseRepository.FirstOrDefaultAsync(t => t.SoftId == softLicense.SoftId && t.SoftUserId == softUser.Id); if (userLicenseInfo != null && !userLicenseInfo.ExpireTime.HasValue) { throw new UserFriendlyException("软件已经授权到永久,无需授权!"); } #endregion #region 授权 if (userLicenseInfo == null) { userLicenseInfo = new SoftUserLicense(); userLicenseInfo.SoftId = softLicense.SoftId; userLicenseInfo.SoftUserId = softUser.Id; userLicenseInfo.AuthorizeTime = DateTime.Now; userLicenseInfo.IsActive = true; userLicenseInfo.Type = SoftUserLicenseType.Fee; switch (softLicense.LicenseType) { case SoftLicenseType.Day: userLicenseInfo.ExpireTime = DateTime.Now.AddDays(1); break; case SoftLicenseType.Forever: userLicenseInfo.ExpireTime = null; break; case SoftLicenseType.Hour: userLicenseInfo.ExpireTime = DateTime.Now.AddHours(1); break; case SoftLicenseType.Month: userLicenseInfo.ExpireTime = DateTime.Now.AddMonths(1); break; case SoftLicenseType.Week: userLicenseInfo.ExpireTime = DateTime.Now.AddDays(7); break; case SoftLicenseType.Year: userLicenseInfo.ExpireTime = DateTime.Now.AddYears(1); break; } await _softUserLicenseRepository.InsertAsync(userLicenseInfo); } else { userLicenseInfo.Type = SoftUserLicenseType.Fee; switch (softLicense.LicenseType) { case SoftLicenseType.Day: userLicenseInfo.ExpireTime = userLicenseInfo.ExpireTime.Value.AddDays(1); break; case SoftLicenseType.Forever: userLicenseInfo.ExpireTime = null; break; case SoftLicenseType.Hour: userLicenseInfo.ExpireTime = userLicenseInfo.ExpireTime.Value.AddHours(1); break; case SoftLicenseType.Month: userLicenseInfo.ExpireTime = userLicenseInfo.ExpireTime.Value.AddMonths(1); break; case SoftLicenseType.Week: userLicenseInfo.ExpireTime = userLicenseInfo.ExpireTime.Value.AddDays(7); break; case SoftLicenseType.Year: userLicenseInfo.ExpireTime = userLicenseInfo.ExpireTime.Value.AddYears(1); break; } await _softUserLicenseRepository.UpdateAsync(userLicenseInfo); } #endregion softLicense.Status = SoftLicenseStatus.HasUse; softLicense.SoftUserId = softUser.Id; softLicense.UseTime = DateTime.Now; await _softLicenseRepository.UpdateAsync(softLicense); }
public virtual async Task <SoftAuthorizeResult> Register(string appId, string loginName, string password, string mobile = "", string qq = "") { #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 (loginName.Length < 4) { return(new SoftAuthorizeResult() { errormsg = "用户名长度必须在4位以上!" }); } if (password.Length < 6) { return(new SoftAuthorizeResult() { errormsg = "密码长度必须在6位以上!" }); } var soft = await _softRepository.FirstOrDefaultAsync(t => t.AppId == appId); if (soft == null) { return(new SoftAuthorizeResult() { errormsg = "软件不存在!" }); } if (!soft.IsActive) { return(new SoftAuthorizeResult() { errormsg = "软件已停用,有问题请联系客服!" }); } #endregion #region 用户注册 var softRegisterOption = soft.SoftRegisterOption; if (softRegisterOption != null) { if (!softRegisterOption.AllowRegister) { return(new SoftAuthorizeResult() { errormsg = "软件不允许注册,有需要请联系客服!" }); } } SoftUser softUser = await _softUserRepository.FirstOrDefaultAsync(t => t.LoginName == loginName); if (softUser != null) { if (new PasswordHasher().VerifyHashedPassword(softUser.Password, password) == PasswordVerificationResult.Success && softUser.IsActive) { //如果,注册的账号已经存在,则直接返回成功 softUser.Mobile = mobile; softUser.QQ = qq; await _softUserRepository.UpdateAsync(softUser); } else { return(new SoftAuthorizeResult() { errormsg = "用户名已存在!" }); } } else { softUser = new SoftUser(); softUser.IsActive = true;//默认启用 softUser.LoginName = loginName; softUser.Source = SoftUserSource.Register; softUser.Password = new PasswordHasher().HashPassword(password); var entity = await _softUserRepository.InsertAsync(softUser); } #endregion #region 写入免费和试用授权 if (await _softUserLicenseRepository.CountAsync(t => t.SoftId == soft.Id && t.SoftUserId == softUser.Id) == 0) { if (soft.RunMode == SoftRunMode.Free) { //免费用户,直接写入授权到永久 SoftUserLicense softUserLicense = new SoftUserLicense(); softUserLicense.SoftId = soft.Id; softUserLicense.SoftUserId = softUser.Id; softUserLicense.AuthorizeTime = DateTime.Now; softUserLicense.IsActive = true; softUserLicense.ExpireTime = null; softUserLicense.Type = SoftUserLicenseType.Free; await _softUserLicenseRepository.InsertAsync(softUserLicense); } else { if (softRegisterOption != null && softRegisterOption.TrialTime > 0) { //试用用户,直接写入授权到 系统时间+试用分钟 SoftUserLicense softUserLicense = new SoftUserLicense(); softUserLicense.SoftId = soft.Id; softUserLicense.SoftUserId = softUser.Id; softUserLicense.AuthorizeTime = DateTime.Now; softUserLicense.IsActive = true; softUserLicense.ExpireTime = softUserLicense.AuthorizeTime.AddMinutes(softRegisterOption.TrialTime); softUserLicense.Type = SoftUserLicenseType.Trial; await _softUserLicenseRepository.InsertAsync(softUserLicense); } } } #endregion return(new SoftAuthorizeResult() { success = true }); }