多账号切换

master
wanghaomei 7 months ago
parent d178c8e2a3
commit de3d081073

@ -0,0 +1,46 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Core.Entity
{
/// <summary>
/// 用户账号关联
/// </summary>
[SugarTable("sys_user_account_relation", TableDescription = "用户账号关联")]
public class SysUserAccountRelation : DBEntityTenant
{
/// <summary>
/// 分组ID
/// </summary>
[SugarColumn(ColumnName = "GroupId")]
[Description("分组ID")]
public long GroupId { get; set; }
/// <summary>
/// 用户ID
/// </summary>
[SugarColumn(ColumnName = "UserId")]
[Description("用户ID")]
public long UserId { get; set; }
/// <summary>
/// 用户名称
/// </summary>
[SugarColumn(ColumnName = "UserName")]
[Description("用户名称")]
public string UserName { get; set; }
/// <summary>
/// 租户名称
/// </summary>
[SugarColumn(ColumnName = "TenantName")]
[Description("租户名称")]
public string TenantName { get; set; }
}
}

@ -4460,6 +4460,31 @@
状态-正常_0、停用_1、删除_2
</summary>
</member>
<member name="T:Myshipping.Core.Entity.SysUserAccountRelation">
<summary>
用户账号关联
</summary>
</member>
<member name="P:Myshipping.Core.Entity.SysUserAccountRelation.GroupId">
<summary>
分组ID
</summary>
</member>
<member name="P:Myshipping.Core.Entity.SysUserAccountRelation.UserId">
<summary>
用户ID
</summary>
</member>
<member name="P:Myshipping.Core.Entity.SysUserAccountRelation.UserName">
<summary>
用户名称
</summary>
</member>
<member name="P:Myshipping.Core.Entity.SysUserAccountRelation.TenantName">
<summary>
租户名称
</summary>
</member>
<member name="T:Myshipping.Core.Entity.SysUserDataScope">
<summary>
用户数据范围表
@ -7134,6 +7159,13 @@
<param name="code"></param>
<returns></returns>
</member>
<member name="M:Myshipping.Core.Service.AuthService.ChangeLogin(System.Int64)">
<summary>
切换登录账号
</summary>
<param name="changeTo"></param>
<returns></returns>
</member>
<member name="T:Myshipping.Core.Service.LoginInput">
<summary>
登录输入参数
@ -7282,6 +7314,36 @@
前端需要使用的租户参数列表
</summary>
</member>
<member name="P:Myshipping.Core.Service.LoginOutput.UserAccountRelation">
<summary>
用户账号关联
</summary>
</member>
<member name="P:Myshipping.Core.Service.UserAccountRelationDto.Id">
<summary>
ID
</summary>
</member>
<member name="P:Myshipping.Core.Service.UserAccountRelationDto.UserId">
<summary>
用户ID
</summary>
</member>
<member name="P:Myshipping.Core.Service.UserAccountRelationDto.UserName">
<summary>
用户名称
</summary>
</member>
<member name="P:Myshipping.Core.Service.UserAccountRelationDto.TenantId">
<summary>
租户ID
</summary>
</member>
<member name="P:Myshipping.Core.Service.UserAccountRelationDto.TenantName">
<summary>
租户名称
</summary>
</member>
<member name="T:Myshipping.Core.Service.RegistInput">
<summary>
注册输入参数

@ -31,6 +31,7 @@ public class AuthService : IAuthService, IDynamicApiController, ITransient
private readonly SqlSugarRepository<SysUser> _sysUserRep; // 用户表仓储
private readonly SqlSugarRepository<SysLogVis> _sysLogVisRep;
private readonly SqlSugarRepository<SysTenant> _sysTenantRep;
private readonly SqlSugarRepository<SysUserAccountRelation> _sysUserAccountRelation;
private readonly ISysCacheService _cache;
private readonly IHttpContextAccessor _httpContextAccessor;
@ -50,7 +51,7 @@ public class AuthService : IAuthService, IDynamicApiController, ITransient
ISysEmpService sysEmpService, ISysRoleService sysRoleService, ISysMenuService sysMenuService,
ISysAppService sysAppService, IClickWordCaptcha captchaHandle, ISysConfigService sysConfigService,
IEventPublisher eventPublisher,
ILogger<AuthService> logger, IDjyTenantParamService djyTenantParamService)
ILogger<AuthService> logger, IDjyTenantParamService djyTenantParamService, SqlSugarRepository<SysUserAccountRelation> sysUserAccountRelation)
{
_sysUserRep = sysUserRep;
_sysLogVisRep = sysLogVisRep;
@ -66,6 +67,7 @@ public class AuthService : IAuthService, IDynamicApiController, ITransient
_logger = logger;
_cache = cache;
_djyTenantParamService = djyTenantParamService;
_sysUserAccountRelation = sysUserAccountRelation;
}
/// <summary>
@ -258,6 +260,14 @@ public class AuthService : IAuthService, IDynamicApiController, ITransient
var paraCodeArr = new string[] { TenantParamCode.ENABLE_SLOT_ABILITY, TenantParamCode.ENABLE_FEE_ABILITY, TenantParamCode.VESSEL_FROM_CONFIG_ONLY };
loginOutput.TenantParams = await _djyTenantParamService.GetParaCodeWithValue(paraCodeArr);
//多账号关联
var accRela = await _sysUserAccountRelation.AsQueryable().Filter(null, true).Where(x => x.UserId == userId && x.IsDeleted == false).FirstAsync();
if (accRela != null)
{
var accRelaList = await _sysUserAccountRelation.AsQueryable().Filter(null, true).Where(x => x.GroupId == accRela.GroupId && x.IsDeleted == false && x.UserId != userId).ToListAsync();
loginOutput.UserAccountRelation = accRelaList.Adapt<List<UserAccountRelationDto>>();
}
// 增加登录日志
await _eventPublisher.PublishAsync(new ChannelEventSource("Create:VisLog",
new SysLogVis
@ -427,4 +437,34 @@ public class AuthService : IAuthService, IDynamicApiController, ITransient
}
}
/// <summary>
/// 切换登录账号
/// </summary>
/// <param name="changeTo"></param>
/// <returns></returns>
[HttpPost("/ChangeLogin")]
public async Task<string> ChangeLogin(long changeTo)
{
var accRela = await _sysUserAccountRelation.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == changeTo && x.IsDeleted == false);
if (accRela == null)
{
throw Oops.Bah("未找到账号关联数据");
}
//判断当前用户的账号和要切换到的账号是否在一个关联配置中
var cc = await _sysUserAccountRelation.AsQueryable().Filter(null, true).CountAsync(x => x.GroupId == accRela.GroupId && x.UserId == UserManager.UserId && x.IsDeleted == false);
if (cc == 0)
{
throw Oops.Bah("无权切换到此账号");
}
var user = await _sysUserRep.AsQueryable().Filter(null, true).FirstAsync(u => u.Id == accRela.UserId);
//获取对应租户
var tenant = _sysTenantRep.Single(user.TenantId);
// 生成Token令牌
return await GetLoginToken(user, tenant);
}
}

@ -1,6 +1,8 @@
using Furion.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace Myshipping.Core.Service;
@ -130,6 +132,11 @@ public class LoginOutput
/// </summary>
public List<DjyTenantParamValueOutput> TenantParams { get; set; } = new List<DjyTenantParamValueOutput>();
/// <summary>
/// 用户账号关联
/// </summary>
public List<UserAccountRelationDto> UserAccountRelation { get; set; }
///// <summary>
///// 租户信息
///// </summary>
@ -170,3 +177,32 @@ public class LoginOutput
///// </summary>
//public bool Enabled { get; set; }
}
public class UserAccountRelationDto
{
/// <summary>
/// ID
/// </summary>
public long Id { get; set; }
/// <summary>
/// 用户ID
/// </summary>
public long UserId { get; set; }
/// <summary>
/// 用户名称
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 租户ID
/// </summary>
public long TenantId { get; set; }
/// <summary>
/// 租户名称
/// </summary>
public string TenantName { get; set; }
}
Loading…
Cancel
Save