|
|
@ -33,6 +33,8 @@ namespace GDZZ.Application.Service.Auth
|
|
|
|
#region 仓储
|
|
|
|
#region 仓储
|
|
|
|
private readonly SqlSugarRepository<BaseUser> Baseuser; // wx用户仓储
|
|
|
|
private readonly SqlSugarRepository<BaseUser> Baseuser; // wx用户仓储
|
|
|
|
private readonly SqlSugarRepository<SysUser> _sysUserRep; // 用户表仓储
|
|
|
|
private readonly SqlSugarRepository<SysUser> _sysUserRep; // 用户表仓储
|
|
|
|
|
|
|
|
private readonly SqlSugarRepository<UserScope> UserScope; //用户业务关联仓储 _
|
|
|
|
|
|
|
|
|
|
|
|
private readonly SqlSugarRepository<SysTenant> _sysTenantRep; //租户仓储
|
|
|
|
private readonly SqlSugarRepository<SysTenant> _sysTenantRep; //租户仓储
|
|
|
|
private readonly SqlSugarRepository<SeIF> Self; //职业仓储
|
|
|
|
private readonly SqlSugarRepository<SeIF> Self; //职业仓储
|
|
|
|
private readonly SqlSugarRepository<Company> CompanyRep;
|
|
|
|
private readonly SqlSugarRepository<Company> CompanyRep;
|
|
|
@ -59,6 +61,7 @@ namespace GDZZ.Application.Service.Auth
|
|
|
|
|
|
|
|
|
|
|
|
public AuthService(
|
|
|
|
public AuthService(
|
|
|
|
IOptions<OAuthOptions> options,
|
|
|
|
IOptions<OAuthOptions> options,
|
|
|
|
|
|
|
|
SqlSugarRepository<UserScope> UserScope,
|
|
|
|
SqlSugarRepository<BaseUser> Baseuser,
|
|
|
|
SqlSugarRepository<BaseUser> Baseuser,
|
|
|
|
SqlSugarRepository<SysTenant> sysTenantRep,
|
|
|
|
SqlSugarRepository<SysTenant> sysTenantRep,
|
|
|
|
SqlSugarRepository<SysUser> sysUserRep,
|
|
|
|
SqlSugarRepository<SysUser> sysUserRep,
|
|
|
@ -75,6 +78,7 @@ namespace GDZZ.Application.Service.Auth
|
|
|
|
IEventPublisher eventPublisher,
|
|
|
|
IEventPublisher eventPublisher,
|
|
|
|
IHttpContextAccessor httpContextAccessor)
|
|
|
|
IHttpContextAccessor httpContextAccessor)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
this.UserScope = UserScope;
|
|
|
|
this.CompanyRep= CompanyRep;
|
|
|
|
this.CompanyRep= CompanyRep;
|
|
|
|
this._eventPublisher= eventPublisher;
|
|
|
|
this._eventPublisher= eventPublisher;
|
|
|
|
this._sysUserRep = sysUserRep;
|
|
|
|
this._sysUserRep = sysUserRep;
|
|
|
@ -102,133 +106,144 @@ namespace GDZZ.Application.Service.Auth
|
|
|
|
public async Task<AuthUserOut> SignInAsync(PhoneModel phoneModel)
|
|
|
|
public async Task<AuthUserOut> SignInAsync(PhoneModel phoneModel)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
AuthUserOut authUserOut = new AuthUserOut();
|
|
|
|
AuthUserOut authUserOut = new AuthUserOut();
|
|
|
|
|
|
|
|
|
|
|
|
Company company = new Company();
|
|
|
|
Company company = new Company();
|
|
|
|
|
|
|
|
try
|
|
|
|
//读取凭证
|
|
|
|
|
|
|
|
var tokenModel = await this._wechatOAuth.GetCode2SessionAsync(phoneModel.Code);
|
|
|
|
|
|
|
|
//解析电话
|
|
|
|
|
|
|
|
var phoneInfo = MiniProgramUtil.AESDecrypt(phoneModel.EncryptedDataStr, tokenModel.SessionKey, phoneModel.Iv);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//查询系统用户
|
|
|
|
|
|
|
|
var sysUser = this._sysUserRep.AsQueryable()
|
|
|
|
|
|
|
|
.Filter("TenantId", true)
|
|
|
|
|
|
|
|
.First(x => x.Phone == phoneInfo.PhoneNumber);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var wxUser = await this.Baseuser.AsQueryable()
|
|
|
|
|
|
|
|
.Filter("TenantId", true)
|
|
|
|
|
|
|
|
.Where(x => x.OpenID == tokenModel.OpenId).SingleAsync();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//账号不存在 生成系统账号
|
|
|
|
|
|
|
|
if (sysUser.IsEmpty())
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sysUser = await this._sysUserRep.InsertReturnEntityAsync(new SysUser()
|
|
|
|
this.UserScope.BeginTran(); //开启事务
|
|
|
|
|
|
|
|
//读取凭证
|
|
|
|
|
|
|
|
var tokenModel = await this._wechatOAuth.GetCode2SessionAsync(phoneModel.Code);
|
|
|
|
|
|
|
|
//解析电话
|
|
|
|
|
|
|
|
var phoneInfo = MiniProgramUtil.AESDecrypt(phoneModel.EncryptedDataStr, tokenModel.SessionKey, phoneModel.Iv);
|
|
|
|
|
|
|
|
//系统用户
|
|
|
|
|
|
|
|
var sysUser = this._sysUserRep.AsQueryable()
|
|
|
|
|
|
|
|
.Filter("TenantId", true)
|
|
|
|
|
|
|
|
.First(x => x.Phone == phoneInfo.PhoneNumber);
|
|
|
|
|
|
|
|
//业务用户
|
|
|
|
|
|
|
|
var wxUser = await this.Baseuser.AsQueryable()
|
|
|
|
|
|
|
|
.Filter("TenantId", true)
|
|
|
|
|
|
|
|
.Where(x => x.OpenID == tokenModel.OpenId).SingleAsync();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//账号不存在 生成系统账号
|
|
|
|
|
|
|
|
if (sysUser.IsEmpty())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Account = phoneInfo.PurePhoneNumber,
|
|
|
|
//wxUser = await this.UserScope.InsertAsync(new UserScope)
|
|
|
|
AdminType = AdminType.None,
|
|
|
|
sysUser = await this._sysUserRep.InsertReturnEntityAsync(new SysUser()
|
|
|
|
Avatar = "https://gdzongzhi.com/assets/img/logo.png",
|
|
|
|
{
|
|
|
|
Birthday = DateTime.Now,
|
|
|
|
Account = phoneInfo.PurePhoneNumber,
|
|
|
|
CreatedTime = DateTime.Now,
|
|
|
|
AdminType = AdminType.None,
|
|
|
|
CreatedUserId = null,
|
|
|
|
Avatar = "https://gdzongzhi.com/assets/img/logo.png",
|
|
|
|
CreatedUserName = null,
|
|
|
|
Birthday = DateTime.Now,
|
|
|
|
Sex = Gender.UNKNOWN,
|
|
|
|
CreatedTime = DateTime.Now,
|
|
|
|
Status = CommonStatus.ENABLE,
|
|
|
|
CreatedUserId = null,
|
|
|
|
Email = null,
|
|
|
|
CreatedUserName = null,
|
|
|
|
IsDeleted = false,
|
|
|
|
Sex = Gender.UNKNOWN,
|
|
|
|
Name = phoneInfo.PhoneNumber,
|
|
|
|
Status = CommonStatus.ENABLE,
|
|
|
|
Password = MD5Encryption.Encrypt("123456"),
|
|
|
|
Email = null,
|
|
|
|
TenantId = 392820661919813,
|
|
|
|
IsDeleted = false,
|
|
|
|
Phone = phoneInfo.PhoneNumber,
|
|
|
|
Name = phoneInfo.PhoneNumber,
|
|
|
|
NickName = "",
|
|
|
|
Password = MD5Encryption.Encrypt("123456"),
|
|
|
|
Tel = null,
|
|
|
|
TenantId = 392820661919813,
|
|
|
|
});
|
|
|
|
Phone = phoneInfo.PhoneNumber,
|
|
|
|
}
|
|
|
|
NickName = "",
|
|
|
|
|
|
|
|
Tel = null,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (wxUser.IsEmpty())
|
|
|
|
if (wxUser.IsEmpty())
|
|
|
|
{
|
|
|
|
|
|
|
|
wxUser = await this.Baseuser.InsertReturnEntityAsync(new BaseUser()
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
UnionId = tokenModel.Unionid,
|
|
|
|
wxUser = await this.Baseuser.InsertReturnEntityAsync(new BaseUser()
|
|
|
|
CreatedUserId = sysUser.Id,
|
|
|
|
{
|
|
|
|
CreatedTime = DateTime.Now,
|
|
|
|
UnionId = tokenModel.Unionid,
|
|
|
|
CreatedUserName = sysUser.Name,
|
|
|
|
CreatedUserId = sysUser.Id,
|
|
|
|
AvatarUrl = "https://gdzongzhi.com/assets/img/logo.png",
|
|
|
|
CreatedTime = DateTime.Now,
|
|
|
|
Status = (int)CommonStatus.ENABLE,
|
|
|
|
CreatedUserName = sysUser.Name,
|
|
|
|
OpenID = tokenModel.OpenId,
|
|
|
|
AvatarUrl = "https://gdzongzhi.com/assets/img/logo.png",
|
|
|
|
UserName = phoneModel.Phone.ToString(),
|
|
|
|
Status = (int)CommonStatus.ENABLE,
|
|
|
|
});
|
|
|
|
OpenID = tokenModel.OpenId,
|
|
|
|
}
|
|
|
|
UserName = phoneModel.Phone.ToString(),
|
|
|
|
//区分账号类型
|
|
|
|
});
|
|
|
|
switch (phoneModel.LogInType)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
//区分账号类型
|
|
|
|
case (int)UserEnum.JOB:
|
|
|
|
switch (phoneModel.LogInType)
|
|
|
|
wxUser.Type = (int)UserEnum.JOB;
|
|
|
|
{
|
|
|
|
await this.Baseuser.UpdateAsync(wxUser);
|
|
|
|
case (int)UserEnum.JOB:
|
|
|
|
break;
|
|
|
|
wxUser.Type = (int)UserEnum.JOB;
|
|
|
|
case (int)UserEnum.HEADHUNTERS:
|
|
|
|
await this.Baseuser.UpdateAsync(wxUser);
|
|
|
|
wxUser.Type = (int)UserEnum.HEADHUNTERS;
|
|
|
|
break;
|
|
|
|
await this.Baseuser.UpdateAsync(wxUser);
|
|
|
|
case (int)UserEnum.HEADHUNTERS:
|
|
|
|
break;
|
|
|
|
wxUser.Type = (int)UserEnum.HEADHUNTERS;
|
|
|
|
case (int)UserEnum.ADVERTISE:
|
|
|
|
await this.Baseuser.UpdateAsync(wxUser);
|
|
|
|
wxUser.Type = (int)UserEnum.ADVERTISE;
|
|
|
|
break;
|
|
|
|
await this.Baseuser.UpdateAsync(wxUser);
|
|
|
|
case (int)UserEnum.ADVERTISE:
|
|
|
|
//获取公司信息
|
|
|
|
wxUser.Type = (int)UserEnum.ADVERTISE;
|
|
|
|
company = await this.CompanyRep.FirstOrDefaultAsync(x => x.Id == wxUser.CompanyID);
|
|
|
|
await this.Baseuser.UpdateAsync(wxUser);
|
|
|
|
authUserOut.companyDto = company.Adapt<CompanyDto>();
|
|
|
|
//获取公司信息
|
|
|
|
break;
|
|
|
|
company = await this.CompanyRep.FirstOrDefaultAsync(x => x.Id == wxUser.CompanyID);
|
|
|
|
default:
|
|
|
|
authUserOut.companyDto = company.Adapt<CompanyDto>();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (wxUser.IsEmpty() || sysUser.IsEmpty())
|
|
|
|
if (wxUser.IsEmpty() || sysUser.IsEmpty())
|
|
|
|
throw Oops.Oh(ErrorCode.xg1002);
|
|
|
|
throw Oops.Oh(ErrorCode.xg1002);
|
|
|
|
//判断是否存在邀请
|
|
|
|
var userc = await this.UserScope.FirstOrDefaultAsync(x => x.BaseUserID == wxUser.Id && x.SysUserID == sysUser.Id);
|
|
|
|
if(phoneModel.Scene != null)
|
|
|
|
if (userc.IsNullOrZero())
|
|
|
|
{
|
|
|
|
|
|
|
|
//判断当前用户是否被邀请过
|
|
|
|
|
|
|
|
var invi = await this.invitaitionRey.FirstOrDefaultAsync(x => x.UserID == UserManager.UserId);
|
|
|
|
|
|
|
|
if (invi.IsNullOrZero())
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var invres = await this.invitaitionRey.FirstOrDefaultAsync(x => x.InviteID == phoneModel.Scene);
|
|
|
|
await this.UserScope.InsertAsync(new UserScope()
|
|
|
|
//未被邀请
|
|
|
|
|
|
|
|
var invrey = await this.invitaitionRey.InsertAsync(new InviteUserPos()
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
UserID = UserManager.UserId,
|
|
|
|
SysUserID = sysUser.Id,
|
|
|
|
InviteUserID = (long)phoneModel.Scene,
|
|
|
|
BaseUserID = wxUser.Id,
|
|
|
|
InviteID = invres.InviteID
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
if (invrey > 0)
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//判断是否存在邀请
|
|
|
|
|
|
|
|
if (phoneModel.Scene != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//判断当前用户是否被邀请过
|
|
|
|
|
|
|
|
var invi = await this.invitaitionRey.FirstOrDefaultAsync(x => x.UserID == UserManager.UserId);
|
|
|
|
|
|
|
|
if (invi.IsNullOrZero())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//附加奖励给邀请人
|
|
|
|
var invres = await this.invitaitionRey.FirstOrDefaultAsync(x => x.InviteID == phoneModel.Scene);
|
|
|
|
UtilService utilService = new UtilService(this.rechargeRep);
|
|
|
|
//未被邀请
|
|
|
|
utilService.Reward((long)phoneModel.Scene, 1);
|
|
|
|
var invrey = await this.invitaitionRey.InsertAsync(new InviteUserPos()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
UserID = UserManager.UserId,
|
|
|
|
|
|
|
|
InviteUserID = (long)phoneModel.Scene,
|
|
|
|
|
|
|
|
InviteID = invres.InviteID
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
if (invrey > 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//附加奖励给邀请人
|
|
|
|
|
|
|
|
UtilService utilService = new UtilService(this.rechargeRep);
|
|
|
|
|
|
|
|
utilService.Reward((long)phoneModel.Scene, 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var Self = await this.Self.FirstOrDefaultAsync(x => x.CreatedUserId == sysUser.Id);
|
|
|
|
var Self = await this.Self.FirstOrDefaultAsync(x => x.CreatedUserId == sysUser.Id);
|
|
|
|
|
|
|
|
|
|
|
|
// 获取加密后的密码
|
|
|
|
// 获取加密后的密码
|
|
|
|
var encryptPassword = MD5Encryption.Encrypt(sysUser.Password);
|
|
|
|
var encryptPassword = MD5Encryption.Encrypt(sysUser.Password);
|
|
|
|
|
|
|
|
|
|
|
|
// 验证账号是否被冻结
|
|
|
|
// 验证账号是否被冻结
|
|
|
|
if (sysUser.Status == CommonStatus.DISABLE)
|
|
|
|
if (sysUser.Status == CommonStatus.DISABLE)
|
|
|
|
throw Oops.Oh(ErrorCode.D1017);
|
|
|
|
throw Oops.Oh(ErrorCode.D1017);
|
|
|
|
//获取对应租户
|
|
|
|
//获取对应租户
|
|
|
|
var tenant = this._sysTenantRep.Single(sysUser.TenantId);
|
|
|
|
var tenant = this._sysTenantRep.Single(sysUser.TenantId);
|
|
|
|
|
|
|
|
|
|
|
|
if (tenant.IsNullOrZero())
|
|
|
|
if (tenant.IsNullOrZero())
|
|
|
|
throw Oops.Oh(ErrorCode.F1001);
|
|
|
|
throw Oops.Oh(ErrorCode.F1001);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成Token令牌
|
|
|
|
// 生成Token令牌
|
|
|
|
authUserOut.Token = JWTEncryption.Encrypt(new Dictionary<string, object>
|
|
|
|
authUserOut.Token = JWTEncryption.Encrypt(new Dictionary<string, object>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
{ClaimConst.CLAINM_USERID, sysUser.Id},
|
|
|
|
{ClaimConst.CLAINM_USERID, sysUser.Id},
|
|
|
|
{ClaimConst.TENANT_ID, sysUser.TenantId},
|
|
|
|
{ClaimConst.TENANT_ID, sysUser.TenantId},
|
|
|
@ -239,31 +254,38 @@ namespace GDZZ.Application.Service.Auth
|
|
|
|
{ ClaimConst.CLAINM_TENANT_NAME, tenant.Name },
|
|
|
|
{ ClaimConst.CLAINM_TENANT_NAME, tenant.Name },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 设置Swagger自动登录
|
|
|
|
// 设置Swagger自动登录
|
|
|
|
_httpContextAccessor.HttpContext.SigninToSwagger(authUserOut.Token);
|
|
|
|
_httpContextAccessor.HttpContext.SigninToSwagger(authUserOut.Token);
|
|
|
|
|
|
|
|
|
|
|
|
// 生成刷新Token令牌
|
|
|
|
// 生成刷新Token令牌
|
|
|
|
var refreshToken = JWTEncryption.GenerateRefreshToken(authUserOut.Token, 30);
|
|
|
|
var refreshToken = JWTEncryption.GenerateRefreshToken(authUserOut.Token, 30);
|
|
|
|
|
|
|
|
|
|
|
|
// 设置刷新Token令牌
|
|
|
|
// 设置刷新Token令牌
|
|
|
|
_httpContextAccessor.HttpContext.Response.Headers["x-access-token"] = refreshToken;
|
|
|
|
_httpContextAccessor.HttpContext.Response.Headers["x-access-token"] = refreshToken;
|
|
|
|
|
|
|
|
|
|
|
|
var httpContext = App.HttpContext;
|
|
|
|
var httpContext = App.HttpContext;
|
|
|
|
await _eventPublisher.PublishAsync(new ChannelEventSource("Update:UserLoginInfo",
|
|
|
|
await _eventPublisher.PublishAsync(new ChannelEventSource("Update:UserLoginInfo",
|
|
|
|
new SysUser { Id = sysUser.Id, LastLoginIp = httpContext.GetLocalIpAddressToIPv4(), LastLoginTime = DateTime.Now }));
|
|
|
|
new SysUser { Id = sysUser.Id, LastLoginIp = httpContext.GetLocalIpAddressToIPv4(), LastLoginTime = DateTime.Now }));
|
|
|
|
|
|
|
|
authUserOut.Avatar = sysUser.Avatar;
|
|
|
|
|
|
|
|
authUserOut.Phone = sysUser.Phone;
|
|
|
|
|
|
|
|
authUserOut.Sex = sysUser.Sex;
|
|
|
|
|
|
|
|
authUserOut.UserId = sysUser.Id;
|
|
|
|
|
|
|
|
authUserOut.UserName = sysUser.Name;
|
|
|
|
|
|
|
|
authUserOut.Self = Self.IsEmpty() ? null : Self.Name;
|
|
|
|
|
|
|
|
authUserOut.Type = (UserEnum)wxUser.Type;
|
|
|
|
|
|
|
|
authUserOut.OpenID = tokenModel.OpenId;
|
|
|
|
|
|
|
|
authUserOut.Tenant = tenant.Adapt<TenantOutput>();
|
|
|
|
|
|
|
|
authUserOut.Describe = wxUser.Describe;
|
|
|
|
|
|
|
|
await this.cacheService.SetUserInfoAsync(authUserOut, authUserOut.UserId);
|
|
|
|
|
|
|
|
this.UserScope.CurrentCommitTran();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
this.UserScope.CurrentRollbackTran();
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
authUserOut.Avatar = sysUser.Avatar;
|
|
|
|
|
|
|
|
authUserOut.Phone = sysUser.Phone;
|
|
|
|
|
|
|
|
authUserOut.Sex = sysUser.Sex;
|
|
|
|
|
|
|
|
authUserOut.UserId = sysUser.Id;
|
|
|
|
|
|
|
|
authUserOut.UserName = sysUser.Name;
|
|
|
|
|
|
|
|
authUserOut.Self = Self.IsEmpty() ? null : Self.Name;
|
|
|
|
|
|
|
|
authUserOut.Type = (UserEnum)wxUser.Type;
|
|
|
|
|
|
|
|
authUserOut.OpenID = tokenModel.OpenId;
|
|
|
|
|
|
|
|
authUserOut.Tenant = tenant.Adapt<TenantOutput>();
|
|
|
|
|
|
|
|
authUserOut.Describe = wxUser.Describe;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await this.cacheService.SetUserInfoAsync(authUserOut, authUserOut.UserId);
|
|
|
|
|
|
|
|
return authUserOut;
|
|
|
|
return authUserOut;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|