123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Options;
- using ZhonTai.Admin.Core.Attributes;
- using ZhonTai.Admin.Core.Configs;
- using ZhonTai.Admin.Core.Repositories;
- using ZhonTai.Common.Helpers;
- using ZhonTai.Admin.Core.Dto;
- using ZhonTai.Admin.Domain.Api;
- using ZhonTai.Admin.Domain.PermissionApi;
- using ZhonTai.Admin.Domain.Role;
- using ZhonTai.Admin.Domain.RolePermission;
- using ZhonTai.Admin.Domain.Tenant;
- using ZhonTai.Admin.Domain.User;
- using ZhonTai.Admin.Domain.UserRole;
- using ZhonTai.Admin.Services.Auth.Dto;
- using ZhonTai.Admin.Services.User.Dto;
- using ZhonTai.DynamicApi;
- using ZhonTai.DynamicApi.Attributes;
- using ZhonTai.Admin.Core.Helpers;
- using ZhonTai.Admin.Core.Consts;
- using ZhonTai.Admin.Domain.User.Dto;
- using ZhonTai.Admin.Services.Role.Dto;
- using Org.BouncyCastle.Crypto;
- namespace ZhonTai.Admin.Services.User;
- /// <summary>
- /// 用户服务
- /// </summary>
- [DynamicApi(Area = AdminConsts.AreaName)]
- public class UserService : BaseService, IUserService, IDynamicApi
- {
- private readonly AppConfig _appConfig;
- private readonly IUserRepository _userRepository;
- private readonly IRepositoryBase<UserRoleEntity> _userRoleRepository;
- private readonly ITenantRepository _tenantRepository;
- private readonly IApiRepository _apiRepository;
- private IRoleRepository _roleRepository => LazyGetRequiredService<IRoleRepository>();
- public UserService(
- AppConfig appConfig,
- IUserRepository userRepository,
- IRepositoryBase<UserRoleEntity> userRoleRepository,
- ITenantRepository tenantRepository,
- IApiRepository apiRepository
- )
- {
- _appConfig = appConfig;
- _userRepository = userRepository;
- _userRoleRepository = userRoleRepository;
- _tenantRepository = tenantRepository;
- _apiRepository = apiRepository;
- }
- /// <summary>
- /// 查询用户
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<IResultOutput> GetAsync(long id)
- {
- var entity = await _userRepository.Select
- .WhereDynamic(id)
- .IncludeMany(a => a.Roles.Select(b => new RoleEntity { Id = b.Id }))
- .ToOneAsync();
- var roles = await _roleRepository.Select.ToListAsync(a => new { a.Id, a.Name });
- return ResultOutput.Ok(new { Form = Mapper.Map<UserGetOutput>(entity), Select = new { roles } });
- }
- /// <summary>
- /// 查询列表
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task<IResultOutput> GetListAsync([FromQuery] UserGetListInput input)
- {
- var list = await _userRepository.Select
- .WhereIf(input.Name.NotNull(), a => a.Name.Contains(input.Name))
- .OrderByDescending(true, c => c.Id)
- .ToListAsync<UserGetListOutput>();
- return ResultOutput.Ok(list);
- }
- /// <summary>
- /// 查询分页
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [HttpPost]
- public async Task<IResultOutput> GetPageAsync(PageInput input)
- {
- var list = await _userRepository.Select
- .WhereDynamicFilter(input.DynamicFilter)
- .Count(out var total)
- .OrderByDescending(true, a => a.Id)
- .IncludeMany(a => a.Roles.Select(b => new RoleEntity { Name = b.Name }))
- .Page(input.CurrentPage, input.PageSize)
- .ToListAsync();
- var data = new PageOutput<UserGetPageOutput>()
- {
- List = Mapper.Map<List<UserGetPageOutput>>(list),
- Total = total
- };
- return ResultOutput.Ok(data);
- }
- /// <summary>
- /// 查询登录用户信息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<ResultOutput<AuthLoginOutput>> GetLoginUserAsync(long id)
- {
- var output = new ResultOutput<AuthLoginOutput>();
- var entityDto = await _userRepository.Select.DisableGlobalFilter("Tenant").WhereDynamic(id).ToOneAsync<AuthLoginOutput>();
- if (_appConfig.Tenant && entityDto?.TenantId.Value > 0)
- {
- var tenant = await _tenantRepository.Select.DisableGlobalFilter("Tenant").WhereDynamic(entityDto.TenantId).ToOneAsync(a => new { a.TenantType, a.DataIsolationType });
- if (null != tenant)
- {
- entityDto.TenantType = tenant.TenantType;
- entityDto.DataIsolationType = tenant.DataIsolationType;
- }
- }
- return output.Ok(entityDto);
- }
- /// <summary>
- /// 查询下拉数据
- /// </summary>
- /// <returns></returns>
- public async Task<IResultOutput> GetSelectAsync()
- {
- var roles = await _roleRepository.Select.ToListAsync(a => new { a.Id, a.Name });
- return ResultOutput.Ok(new { Select = new { roles } });
- }
- /// <summary>
- /// 查询用户基本信息
- /// </summary>
- /// <returns></returns>
- public async Task<IResultOutput> GetBasicAsync()
- {
- if (!(User?.Id > 0))
- {
- return ResultOutput.NotOk("未登录!");
- }
- var data = await _userRepository.GetAsync<UserUpdateBasicInput>(User.Id);
- return ResultOutput.Ok(data);
- }
- /// <summary>
- /// 查询用户权限信息
- /// </summary>
- /// <returns></returns>
- public async Task<IList<UserPermissionsOutput>> GetPermissionsAsync()
- {
- var key = string.Format(CacheKeys.UserPermissions, User.Id);
- var result = await Cache.GetOrSetAsync(key, async () =>
- {
- return await _apiRepository
- .Where(a => _userRoleRepository.Orm.Select<UserRoleEntity, RolePermissionEntity, PermissionApiEntity>()
- .InnerJoin((b, c, d) => b.RoleId == c.RoleId && b.UserId == User.Id)
- .InnerJoin((b, c, d) => c.PermissionId == d.PermissionId)
- .Where((b, c, d) => d.ApiId == a.Id).Any())
- .ToListAsync<UserPermissionsOutput>();
- });
- return result;
- }
- /// <summary>
- /// 新增用户
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [Transaction]
- public async Task<IResultOutput> AddAsync(UserAddInput input)
- {
- if (input.Password.IsNull())
- {
- input.Password = _appConfig.DefaultPassword;
- }
- input.Password = MD5Encrypt.Encrypt32(input.Password);
- var entity = Mapper.Map<UserEntity>(input);
- var user = await _userRepository.InsertAsync(entity);
- if (!(user?.Id > 0))
- {
- return ResultOutput.NotOk();
- }
- if (input.RoleIds != null && input.RoleIds.Any())
- {
- var roles = input.RoleIds.Select(a => new UserRoleEntity { UserId = user.Id, RoleId = a });
- await _userRoleRepository.InsertAsync(roles);
- }
- return ResultOutput.Ok();
- }
- /// <summary>
- /// 修改用户
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [Transaction]
- public async Task<IResultOutput> UpdateAsync(UserUpdateInput input)
- {
- if (!(input?.Id > 0))
- {
- return ResultOutput.NotOk();
- }
- var user = await _userRepository.GetAsync(input.Id);
- if (!(user?.Id > 0))
- {
- return ResultOutput.NotOk("用户不存在!");
- }
- Mapper.Map(input, user);
- await _userRepository.UpdateAsync(user);
- await _userRoleRepository.DeleteAsync(a => a.UserId == user.Id);
- if (input.RoleIds != null && input.RoleIds.Any())
- {
- var roles = input.RoleIds.Select(a => new UserRoleEntity { UserId = user.Id, RoleId = a });
- await _userRoleRepository.InsertAsync(roles);
- }
- return ResultOutput.Ok();
- }
- /// <summary>
- /// 更新用户基本信息
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task<IResultOutput> UpdateBasicAsync(UserUpdateBasicInput input)
- {
- var entity = await _userRepository.GetAsync(input.Id);
- entity = Mapper.Map(input, entity);
- var result = (await _userRepository.UpdateAsync(entity)) > 0;
- //清除用户缓存
- await Cache.DelAsync(string.Format(CacheKeys.UserInfo, input.Id));
- return ResultOutput.Result(result);
- }
- /// <summary>
- /// 修改用户密码
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task<IResultOutput> ChangePasswordAsync(UserChangePasswordInput input)
- {
- if (input.ConfirmPassword != input.NewPassword)
- {
- return ResultOutput.NotOk("新密码和确认密码不一致!");
- }
- var entity = await _userRepository.GetAsync(input.Id);
- var oldPassword = MD5Encrypt.Encrypt32(input.OldPassword);
- if (oldPassword != entity.Password)
- {
- return ResultOutput.NotOk("旧密码不正确!");
- }
- input.Password = MD5Encrypt.Encrypt32(input.NewPassword);
- entity = Mapper.Map(input, entity);
- var result = (await _userRepository.UpdateAsync(entity)) > 0;
- return ResultOutput.Result(result);
- }
- /// <summary>
- /// 彻底删除用户
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [Transaction]
- public async Task<IResultOutput> DeleteAsync(long id)
- {
- await _userRoleRepository.DeleteAsync(a => a.UserId == id);
- await _userRepository.DeleteAsync(m => m.Id == id);
- return ResultOutput.Ok();
- }
- /// <summary>
- /// 批量彻底删除用户
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- [Transaction]
- public async Task<IResultOutput> BatchDeleteAsync(long[] ids)
- {
- await _userRoleRepository.DeleteAsync(a => ids.Contains(a.UserId));
- await _userRepository.DeleteAsync(a => ids.Contains(a.Id));
- return ResultOutput.Ok();
- }
- /// <summary>
- /// 删除用户
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [Transaction]
- public async Task<IResultOutput> SoftDeleteAsync(long id)
- {
- await _userRoleRepository.DeleteAsync(a => a.UserId == id);
- await _userRepository.SoftDeleteAsync(id);
- return ResultOutput.Ok();
- }
- /// <summary>
- /// 批量删除用户
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- [Transaction]
- public async Task<IResultOutput> BatchSoftDeleteAsync(long[] ids)
- {
- await _userRoleRepository.DeleteAsync(a => ids.Contains(a.UserId));
- await _userRepository.SoftDeleteAsync(ids);
- return ResultOutput.Ok();
- }
- /// <summary>
- /// 上传头像
- /// </summary>
- /// <param name="file"></param>
- /// <returns></returns>
- [HttpPost]
- [Login]
- public async Task<IResultOutput> AvatarUpload([FromForm] IFormFile file)
- {
- var uploadConfig = LazyGetRequiredService<IOptionsMonitor<UploadConfig>>().CurrentValue;
- var uploadHelper = LazyGetRequiredService<UploadHelper>();
- var config = uploadConfig.Avatar;
- var res = await uploadHelper.UploadAsync(file, config, new { User.Id });
- if (res.Success)
- {
- return ResultOutput.Ok(res.Data.FileRelativePath);
- }
- return ResultOutput.NotOk(res.Msg ?? "上传失败!");
- }
- }
|