2025-06-04 09:42:48 +08:00
|
|
|
|
using DotNetCore.CAP;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Syc.Authorize.JwtBearer;
|
|
|
|
|
|
|
|
|
|
//using Syc.Basic.Web.Contracts;
|
|
|
|
|
using Syc.Basic.Web.WMS.Dtos;
|
|
|
|
|
using Syc.Basic.Web.WMS.Dtos.Auth;
|
|
|
|
|
using Syc.Basic.Web.WMS.Dtos.Role;
|
|
|
|
|
using Syc.Basic.Web.WMS.Dtos.User;
|
|
|
|
|
using Syc.Basic.Web.WMS.Entitys;
|
|
|
|
|
using Syc.Basic.Web.WMS.IServices;
|
|
|
|
|
using Syc.Basic.Web.WMS.Repository;
|
|
|
|
|
using Syc.Basic.Web.Services;
|
|
|
|
|
using Syc.Core.Tools;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Volo.Abp;
|
|
|
|
|
using Volo.Abp.Application.Services;
|
|
|
|
|
using Volo.Abp.Domain.Repositories;
|
|
|
|
|
|
|
|
|
|
namespace Syc.Basic.Web.WMS.Service
|
|
|
|
|
{
|
2025-06-23 14:22:59 +08:00
|
|
|
|
//[ApiExplorerSettings(GroupName = "鉴权/授权服务"), Authorize]
|
|
|
|
|
[AllowAnonymous]
|
2025-06-04 09:42:48 +08:00
|
|
|
|
public class AuthService : ApiService, IAuthService
|
|
|
|
|
{
|
|
|
|
|
private readonly IJwtTokenService jwtTokenService;
|
|
|
|
|
private readonly IUserDomainService userDomainService;
|
|
|
|
|
private readonly IRoleRepository roleRepository;
|
|
|
|
|
private readonly IRepository<Menu> menuRepository;
|
|
|
|
|
private readonly ICapPublisher capPublisher;
|
|
|
|
|
|
2025-06-23 14:22:59 +08:00
|
|
|
|
public AuthService(
|
|
|
|
|
IJwtTokenService jwtTokenService
|
|
|
|
|
,IUserDomainService userDomainService
|
2025-06-04 09:42:48 +08:00
|
|
|
|
, IRoleRepository roleRepository
|
|
|
|
|
, IRepository<Menu> menuRepository
|
2025-06-23 14:22:59 +08:00
|
|
|
|
//, ICapPublisher capPublisher
|
|
|
|
|
)
|
2025-06-04 09:42:48 +08:00
|
|
|
|
{
|
|
|
|
|
this.jwtTokenService = jwtTokenService;
|
|
|
|
|
this.userDomainService = userDomainService;
|
|
|
|
|
this.roleRepository = roleRepository;
|
|
|
|
|
this.menuRepository = menuRepository;
|
|
|
|
|
this.capPublisher = capPublisher;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 账号密码登录
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
/// <returns></returns>
|
2025-06-23 14:22:59 +08:00
|
|
|
|
[HttpPost]
|
2025-06-04 09:42:48 +08:00
|
|
|
|
public async Task<AuthResultDto> LoginAsync(LoginInput input)
|
|
|
|
|
{
|
|
|
|
|
var userDO = await userDomainService.GetUserByAccountAsync(input.UserName);
|
|
|
|
|
|
|
|
|
|
if (userDO is null)
|
|
|
|
|
throw Oops.Oh($"{input.UserName} 用户不存在");
|
|
|
|
|
if (!userDO.CheckPassword(Tools.SecurityUtil.MD5Encrypt(input.Password, Encoding.ASCII)))
|
|
|
|
|
throw Oops.Oh("密码有误,请重试");
|
|
|
|
|
if (!userDO.IsEnable)
|
|
|
|
|
throw Oops.Oh("用户已被冻结,请联系系统管理员");
|
|
|
|
|
|
2025-06-23 14:22:59 +08:00
|
|
|
|
// var userInfo = ObjectMapper.Map(userDO.User(), new Identity());
|
|
|
|
|
var token = jwtTokenService.GenerateToken(userDO);
|
2025-06-04 09:42:48 +08:00
|
|
|
|
return new AuthResultDto(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 注销
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[AllowAnonymous]
|
2025-06-23 14:22:59 +08:00
|
|
|
|
[HttpPost]
|
2025-06-04 09:42:48 +08:00
|
|
|
|
public async Task Logout()
|
|
|
|
|
{
|
|
|
|
|
await capPublisher.PublishAsync("mes.test", "测试消息");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取角色菜单
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<RoleMenusDto> GetRoleMenus([FromQuery] string code)
|
|
|
|
|
{
|
|
|
|
|
if (code.IsNullOrWhiteSpace())
|
|
|
|
|
throw Oops.Oh("code 必填");
|
|
|
|
|
var role = await roleRepository.GetRoleByCode(code, true, false);
|
|
|
|
|
if (role is null)
|
|
|
|
|
throw Oops.Oh("角色不存在或已被删除");
|
|
|
|
|
return new RoleMenusDto()
|
|
|
|
|
{
|
|
|
|
|
code = role.Code,
|
|
|
|
|
menuIds = role.Menus.Select(e => e.Id).ToArray()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 更新角色菜单
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task UpdateRoleMenus(UpdateRoleMenusInput input)
|
|
|
|
|
{
|
|
|
|
|
if (input.code.Equals(ConstPool.AdministratorRoleCode))
|
|
|
|
|
throw Oops.Oh($"超级管理员禁止修改");
|
|
|
|
|
var role = await roleRepository.GetRoleByCode(input.code, true, false);
|
|
|
|
|
if (role.IsNullOrEmpty())
|
|
|
|
|
throw Oops.Oh("角色不存在或已被删除");
|
|
|
|
|
var menus = await menuRepository.GetListAsync(e => input.menuIds.Contains(e.Id));
|
|
|
|
|
role.Menus.Clear();
|
|
|
|
|
role.Menus.AddRange(menus);
|
|
|
|
|
await roleRepository.Update(role);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取当前用户菜单权限
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
public async Task<List<string>> GetPerm()
|
|
|
|
|
{
|
|
|
|
|
return await userDomainService.GetPermissionByUserId(Identity.Id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|