master_sxs_new #1
|
|
@ -28,5 +28,58 @@ namespace langguanApi.Common
|
|||
}
|
||||
return pwd;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加密
|
||||
/// </summary>
|
||||
/// <param name="strSource">需要加密的字符串</param>
|
||||
/// <returns>加密后的字符串</returns>
|
||||
//[OperationContract]
|
||||
public static string Encrypt(string strSource)
|
||||
{
|
||||
//把字符串放到byte数组中
|
||||
byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
|
||||
//建立加密对象的密钥和偏移量
|
||||
byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量
|
||||
byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥
|
||||
//实例DES加密类
|
||||
DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider();
|
||||
mobjCryptoService.Key = iv;
|
||||
mobjCryptoService.IV = key;
|
||||
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
|
||||
//实例MemoryStream流加密密文件
|
||||
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
||||
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
|
||||
cs.Write(bytIn, 0, bytIn.Length);
|
||||
cs.FlushFinalBlock();
|
||||
|
||||
string strOut = System.Convert.ToBase64String(ms.ToArray());
|
||||
return strOut;
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 解密
|
||||
/// </summary>
|
||||
/// <param name="Source">需要解密的字符串</param>
|
||||
/// <returns>解密后的字符串</returns>
|
||||
//[OperationContract]
|
||||
public static string Decrypt(string Source)
|
||||
{
|
||||
if (Source == null) return string.Empty;
|
||||
//将解密字符串转换成字节数组
|
||||
byte[] bytIn = System.Convert.FromBase64String(Source);
|
||||
//给出解密的密钥和偏移量,密钥和偏移量必须与加密时的密钥和偏移量相同
|
||||
byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量
|
||||
byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥
|
||||
DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider();
|
||||
mobjCryptoService.Key = iv;
|
||||
mobjCryptoService.IV = key;
|
||||
//实例流进行解密
|
||||
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
|
||||
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
|
||||
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
|
||||
StreamReader strd = new StreamReader(cs, Encoding.Default);
|
||||
return strd.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,13 +57,13 @@ namespace langguanApi.Controllers
|
|||
/// <summary>
|
||||
/// 删除菜单
|
||||
/// </summary>
|
||||
/// <param name="ids"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("remove")]
|
||||
public async Task<IActionResult> Remove(IEnumerable<string> ids)
|
||||
[HttpDelete("DeleteMenu")]
|
||||
public async Task<IActionResult> DeleteMenu(string id)
|
||||
{
|
||||
await _menuService.BatchRemoveAsync(ids);
|
||||
return Ok(new ApiResult());
|
||||
var result = await _menuService.DeleteMenu(id);
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using langguanApi.Model;
|
||||
using langguanApi.Model.Dto;
|
||||
using langguanApi.Model.Dto.SystemConfigurationDto;
|
||||
using langguanApi.Service;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
|
@ -55,26 +56,67 @@ namespace langguanApi.Controllers
|
|||
var result = await _userService.GetPage(input);
|
||||
return Ok(result);
|
||||
}
|
||||
///删除用户
|
||||
|
||||
#region 用户管理相关接口
|
||||
/// <summary>
|
||||
///新增用户
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("Delete")]
|
||||
public async Task<IActionResult> Delete(string id)
|
||||
[HttpPost("AddUser")]
|
||||
public async Task<IActionResult> AddUser([FromBody] UserDto input)
|
||||
{
|
||||
await _userService.DeleteUser(id);
|
||||
return Ok(new ApiResult() { code = 0 });
|
||||
var result = await _userService.AddUser(input);
|
||||
return Ok(result);
|
||||
}
|
||||
//获取用户列表
|
||||
/// <summary>
|
||||
/// 获取用户列表
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("GetUserList")]
|
||||
public async Task<IActionResult> GetUserList([FromQuery] UserQueryDto input)
|
||||
{
|
||||
var result = await _userService.GetUserList(input);
|
||||
return Ok(result);
|
||||
}
|
||||
//根据用户Id获取用户信息
|
||||
/// <summary>
|
||||
/// 根据用户Id获取用户信息
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("GetUserById")]
|
||||
public async Task<IActionResult> GetUserById(string userId)
|
||||
{
|
||||
var result = await _userService.GetUserById(userId);
|
||||
return Ok(result);
|
||||
}
|
||||
//修改用户信息
|
||||
/// <summary>
|
||||
/// 修改用户信息
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut("Update")]
|
||||
public async Task<IActionResult> Update([FromBody] UserUpdate input)
|
||||
[HttpPost("UpdateUser")]
|
||||
public async Task<IActionResult> UpdateUser([FromBody] UserDto input)
|
||||
{
|
||||
await _userService.UpdateUser(input);
|
||||
return Ok(new ApiResult() { code = 0 });
|
||||
var result = await _userService.UpdateUser(input);
|
||||
return Ok(result);
|
||||
}
|
||||
//删除用户
|
||||
/// <summary>
|
||||
/// 删除用户
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("DeleteUser")]
|
||||
public async Task<IActionResult> DeleteUser(string userId)
|
||||
{
|
||||
var result = await _userService.DeleteUser(userId);
|
||||
return Ok(result);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,93 @@
|
|||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace langguanApi.Model.Dto.SystemConfigurationDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 用于用户管理的Dto
|
||||
/// </summary>
|
||||
public class UserDto
|
||||
{
|
||||
/// <summary>
|
||||
///用户ID
|
||||
/// </summary>
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
public string Id { get; set; }
|
||||
/// <summary>
|
||||
/// 用户名
|
||||
/// </summary>
|
||||
public string Username { get; set; }
|
||||
/// <summary>
|
||||
/// 角色id
|
||||
/// </summary>
|
||||
public string RoleId { get; set; }
|
||||
/// <summary>
|
||||
/// 邮箱
|
||||
/// </summary>
|
||||
public string Email { get; set; }
|
||||
/// <summary>
|
||||
/// 手机号
|
||||
/// </summary>
|
||||
public string Tel { get; set; }
|
||||
/// <summary>
|
||||
/// 是否删除 0否 1是
|
||||
/// </summary>
|
||||
public byte IsDel { get; set; }
|
||||
/// <summary>
|
||||
/// 密码 md5加密
|
||||
/// </summary>
|
||||
public string Password { get; set; }
|
||||
}
|
||||
/// <summary>
|
||||
///根据用户条件查询
|
||||
/// </summary>
|
||||
public class UserQueryDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 关键字查询,可根据userName,Tel,Email
|
||||
/// </summary>
|
||||
public string key { get; set; } = "";
|
||||
/// <summary>
|
||||
/// 当前条数
|
||||
/// </summary>
|
||||
public int PageSize { get; set; } = 10;
|
||||
/// <summary>
|
||||
/// 当前页数
|
||||
/// </summary>
|
||||
public int PageIndex { get; set; } = 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户列表展示
|
||||
/// </summary>
|
||||
public class UserDetailDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户ID,方便查看详情操作
|
||||
/// </summary>
|
||||
public string UserId { get; set; }
|
||||
/// <summary>
|
||||
/// 用户名
|
||||
/// </summary>
|
||||
public string Username { get; set; }
|
||||
/// <summary>
|
||||
/// 邮箱
|
||||
/// </summary>
|
||||
public string Email { get; set; }
|
||||
/// <summary>
|
||||
/// 角色id
|
||||
/// </summary>
|
||||
public string RoleId { get; set; }
|
||||
/// <summary>
|
||||
/// 角色名称
|
||||
/// </summary>
|
||||
public string RoleName{ get; set; }
|
||||
/// <summary>
|
||||
/// 联系方式
|
||||
/// </summary>
|
||||
public string Tel { get; set; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,4 +1,7 @@
|
|||
namespace langguanApi.Model.Entity
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace langguanApi.Model.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户实体
|
||||
|
|
@ -16,7 +19,7 @@
|
|||
/// <summary>
|
||||
/// 角色id
|
||||
/// </summary>
|
||||
public string roleId { get; set; }
|
||||
public string RoleId { get; set; }
|
||||
/// <summary>
|
||||
/// 邮箱
|
||||
/// </summary>
|
||||
|
|
@ -25,6 +28,7 @@
|
|||
/// 手机号
|
||||
/// </summary>
|
||||
public string Phone { get; set; }
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加用户DTO
|
||||
|
|
|
|||
|
|
@ -11,25 +11,75 @@ namespace langguanApi.Service
|
|||
[ServiceInjection(InjectionType.Transient)]
|
||||
public class MenuService : BaseService<Menu>
|
||||
{
|
||||
public MenuService(IConfiguration config) : base(config, nameof(Menu))
|
||||
private ILogger<MenuService> _logger;
|
||||
public MenuService(IConfiguration config, ILogger<MenuService> logger) : base(config, nameof(Menu))
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
public async Task<List<Menu>> GetMenusByParentId(string parentId)
|
||||
{
|
||||
Expression<Func<Menu, bool>> exp = filter => filter.IsDelete == false && filter.ParentId == parentId;
|
||||
return (await base.GetListWithExp(exp)).OrderBy(x => x.Sort).ToList();
|
||||
}
|
||||
/// <summary>
|
||||
/// 新增菜单
|
||||
/// </summary>
|
||||
/// <param name="menu">菜单实体</param>
|
||||
/// <returns></returns>
|
||||
public async Task<ApiResult> AddMenu(AddMenuDto menu)
|
||||
{
|
||||
var entity = menu.Adapt<Menu>();
|
||||
await base.CreateAsync(entity);
|
||||
return new ApiResult();
|
||||
try
|
||||
{
|
||||
var entity = menu.Adapt<Menu>();
|
||||
await base.CreateAsync(entity);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
_logger.LogError($"新增菜单出现异常,请求参数:{Newtonsoft.Json.JsonConvert.SerializeObject(menu)}," +
|
||||
$"请求接口:'api/Menu/AddMenu'," +
|
||||
$"异常信息:{ex.Message}," +
|
||||
$"异常位置:{ex.StackTrace}"
|
||||
);
|
||||
return new ApiResult { code = 1, msg = "保存菜单失败", data = false };
|
||||
}
|
||||
finally
|
||||
{
|
||||
_logger.LogInformation($"新增菜单参数:menu:{Newtonsoft.Json.JsonConvert.SerializeObject(menu)}");
|
||||
}
|
||||
return new ApiResult { code = 0, msg = "保存菜单信息成功", data = true };
|
||||
}
|
||||
/// <summary>
|
||||
/// 更改菜单
|
||||
/// </summary>
|
||||
/// <param name="menu">菜单实体类</param>
|
||||
/// <returns></returns>
|
||||
public async Task<ApiResult> UpdateMenu(UpdateMenuDto menu)
|
||||
{
|
||||
var entity = menu.Adapt<Menu>();
|
||||
await base.UpdateAsync(entity.Id, entity);
|
||||
return new ApiResult();
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(menu.Id))
|
||||
{
|
||||
return new ApiResult() { code = 0, data = false, msg = "更新菜单失败,Id不能为空" };
|
||||
}
|
||||
var entity = menu.Adapt<Menu>();
|
||||
await base.UpdateAsync(entity.Id, entity);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
_logger.LogError($"修改菜单出现异常,请求参数:{Newtonsoft.Json.JsonConvert.SerializeObject(menu)}," +
|
||||
$"请求接口:'api/Menu/UpdateMenu'," +
|
||||
$"异常信息:{ex.Message}," +
|
||||
$"异常位置:{ex.StackTrace}"
|
||||
);
|
||||
return new ApiResult { code = 1, msg = "保存菜单失败", data = false };
|
||||
}
|
||||
finally
|
||||
{
|
||||
_logger.LogInformation($"修改菜单参数:menu:{Newtonsoft.Json.JsonConvert.SerializeObject(menu)}");
|
||||
}
|
||||
return new ApiResult() { code = 0, data = true, msg = "更新菜单成功" };
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取菜单树
|
||||
|
|
@ -38,32 +88,67 @@ namespace langguanApi.Service
|
|||
/// <returns></returns>
|
||||
public async Task<ApiResult> Pager(reqpage input)
|
||||
{
|
||||
Expression<Func<Menu, bool>> exp = filter => filter.IsDelete == false;
|
||||
if (!string.IsNullOrEmpty(input.key))
|
||||
{
|
||||
exp = exp.And(filter => filter.Name.Contains(input.key));
|
||||
}
|
||||
List<MenuTreeDto> dto = new List<MenuTreeDto>();
|
||||
var MenuList = await GetChildList(null);
|
||||
foreach (var item in MenuList)
|
||||
try
|
||||
{
|
||||
dto.Add(new MenuTreeDto()
|
||||
var MenuList = await GetChildList("0");//获取跟节点
|
||||
if (MenuList.Any())
|
||||
{
|
||||
Id = item.Id,
|
||||
Name = item.Name,
|
||||
Sort = item.Sort,
|
||||
ParentId = item.ParentId,
|
||||
ParentName = item.ParentName,
|
||||
Children = await GetChildList(item.Id)
|
||||
});
|
||||
foreach (var item in MenuList)
|
||||
{
|
||||
dto.Add(new MenuTreeDto()
|
||||
{
|
||||
Id = item.Id,
|
||||
Name = item.Name,
|
||||
Sort = item.Sort,
|
||||
ParentId = item.ParentId,
|
||||
ParentName = item.ParentName,
|
||||
Children = await GetChildList(item.Id)
|
||||
});
|
||||
}
|
||||
//筛选数据
|
||||
if (!string.IsNullOrEmpty(input.key))
|
||||
{
|
||||
if (dto.Exists(p => p.Name == input.key) ||
|
||||
dto.Exists(p => p.Children.Exists(c => c.Name == input.key)) ||
|
||||
dto.Exists(p => p.ParentName == input.key))
|
||||
{
|
||||
if (dto.Exists(p => p.Name == input.key))
|
||||
{
|
||||
dto = dto.Where(p => p.Name == input.key).ToList();
|
||||
}
|
||||
else if (dto.Exists(p => p.Children.Exists(c => c.Name == input.key)))
|
||||
{
|
||||
dto = dto.SelectMany(p => p.Children).Where(p => p.Name == input.key).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
dto = dto.SelectMany(p => p.Children).Where(p => p.ParentName == input.key).ToList();
|
||||
}
|
||||
return new ApiResult() { code = 0, data = dto, msg = "获取菜单列表" };
|
||||
}
|
||||
else
|
||||
{
|
||||
return new ApiResult() { code = 0, data = null, msg = "获取菜单列表不存在" };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ApiResult() { data = dto };
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
//return await base.GetPager(new ReqPaing()
|
||||
//{
|
||||
// pageSize = input.pageSize,
|
||||
// current = input.current
|
||||
//}, exp);
|
||||
_logger.LogError($"获取菜单列表出现异常,请求参数:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}," +
|
||||
$"请求接口:'api/Menu/Pager'," +
|
||||
$"异常信息:{ex.Message}," +
|
||||
$"异常位置:{ex.StackTrace}"
|
||||
);
|
||||
return new ApiResult { code = 1, msg = "获取菜单列表失败", data = false };
|
||||
}
|
||||
finally
|
||||
{
|
||||
_logger.LogInformation($"获取菜单列表参数:menu:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}");
|
||||
}
|
||||
return new ApiResult() { code = 0, data = dto, msg = "获取菜单列表" };
|
||||
}
|
||||
/// <summary>
|
||||
/// 递归获取子菜单列表
|
||||
|
|
@ -73,15 +158,54 @@ namespace langguanApi.Service
|
|||
public async Task<List<MenuTreeDto>> GetChildList(string parentId)
|
||||
{
|
||||
Expression<Func<Menu, bool>> exp = filter => filter.IsDelete == false && filter.ParentId == parentId;
|
||||
Expression<Func<Menu, bool>> expDataSource = filter => filter.IsDelete;
|
||||
var list = (await GetListWithExp(exp))
|
||||
.OrderByDescending(x => x.Sort)
|
||||
.OrderBy(x => x.Sort)
|
||||
.ToList().Adapt<List<MenuTreeDto>>();
|
||||
var DataSourceList = (await GetAsync())
|
||||
.OrderBy(x => x.Sort)
|
||||
.ToList();//拿到所有数据源,筛选结果
|
||||
foreach (var item in list)
|
||||
{
|
||||
item.Children = await GetChildList(item.Id);
|
||||
item.ParentName = (await base.GetAsync(item.ParentId))?.Name;
|
||||
item.ParentName = DataSourceList.FirstOrDefault(p => p.Id == item.ParentId)?.Name;
|
||||
item.ParentId = DataSourceList.FirstOrDefault(p => p.Id == item.ParentId)?.Id;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 作废菜单
|
||||
/// </summary>
|
||||
/// <param name="id">当前菜单id</param>
|
||||
/// <returns></returns>
|
||||
public async Task<ApiResult> DeleteMenu(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var entity = await base.GetAsync(id);
|
||||
if (entity == null)
|
||||
{
|
||||
return new ApiResult() { code = 0, data = false, msg = "删除菜单失败,Id不存在" };
|
||||
}
|
||||
entity.IsDelete = true;
|
||||
await base.UpdateAsync(id, entity);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
_logger.LogError($"删除菜单出现异常,请求参数:{Newtonsoft.Json.JsonConvert.SerializeObject(id)}," +
|
||||
$"请求接口:'api/Menu/DeleteMenu'," +
|
||||
$"异常信息:{ex.Message}," +
|
||||
$"异常位置:{ex.StackTrace}"
|
||||
);
|
||||
return new ApiResult { code = 1, msg = "删除菜单失败", data = false };
|
||||
}
|
||||
finally
|
||||
{
|
||||
_logger.LogInformation($"删除菜单参数:menuId:{id}");
|
||||
}
|
||||
return new ApiResult() { code = 0, data = true, msg = "删除菜单成功" };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,20 @@
|
|||
using langguanApi.Extensions;
|
||||
using langguanApi.Extensions.AutoDI;
|
||||
using langguanApi.Model.Dto.SystemConfigurationDto;
|
||||
using langguanApi.Model;
|
||||
using langguanApi.Model.Entity;
|
||||
using Mapster;
|
||||
using NPOI.SS.Formula.Functions;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace langguanApi.Service
|
||||
{
|
||||
/// <summary>
|
||||
/// 角色服务
|
||||
/// </summary>
|
||||
[ServiceInjection(InjectionType.Transient)]
|
||||
public class RoleService : BaseService<Role>
|
||||
{
|
||||
|
|
@ -108,5 +115,29 @@ namespace langguanApi.Service
|
|||
current = input.current
|
||||
}, exp);
|
||||
}
|
||||
|
||||
#region 用户管理角色相关
|
||||
/// <summary>
|
||||
/// 根据id获取角色信息
|
||||
/// </summary>
|
||||
/// <param name="roleId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Role> GetRoleById(string roleId)
|
||||
{
|
||||
return await base.GetAsync(roleId);
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据Id,获取多个校色信息
|
||||
/// </summary>
|
||||
/// <param name="ids"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<Role>> GetRoleListByIds(IEnumerable<string> ids)
|
||||
{
|
||||
Expression<Func<Role, bool>> exp = filter => ids.Contains(filter.Id) && filter.IsDelete == false;
|
||||
var list = (await base.GetListWithExp(exp)).ToList();
|
||||
return list;
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,14 @@
|
|||
using langguanApi.Extensions.AutoDI;
|
||||
using langguanApi.Model;
|
||||
using langguanApi.Model.Dto;
|
||||
using langguanApi.Model.Dto.SystemConfigurationDto;
|
||||
using langguanApi.Model.Entity;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using MongoDB.Bson.IO;
|
||||
using MongoDB.Driver.Linq;
|
||||
using Org.BouncyCastle.Asn1.Ocsp;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace langguanApi.Service
|
||||
|
|
@ -11,8 +17,12 @@ namespace langguanApi.Service
|
|||
[ServiceInjection(InjectionType.Transient)]
|
||||
public class UserService : BaseService<UserEntity>
|
||||
{
|
||||
public UserService(IConfiguration config) : base(config, nameof(UserEntity).Replace("Entity", ""))
|
||||
private ILogger<UserService> _logger;
|
||||
private RoleService _roleService;
|
||||
public UserService(IConfiguration config, ILogger<UserService> logger, RoleService roleService) : base(config, nameof(UserEntity).Replace("Entity", ""))
|
||||
{
|
||||
_logger = logger;
|
||||
_roleService = roleService;
|
||||
}
|
||||
/// <summary>
|
||||
/// 登录
|
||||
|
|
@ -78,17 +88,288 @@ namespace langguanApi.Service
|
|||
current = input.current
|
||||
}, exp);
|
||||
}
|
||||
///更新用户信息
|
||||
public async Task UpdateUser(UserUpdate user)
|
||||
|
||||
#region 用户管理
|
||||
/// <summary>
|
||||
/// 用户是否存在
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> Exist(UserDto input)
|
||||
{
|
||||
var entity = user.Adapt<UserEntity>();
|
||||
entity.Password = StringHelper.MD5Encrypt32(user.Password);
|
||||
await base.UpdateAsync(user.Id, entity);
|
||||
var entity = input.Adapt<UserEntity>();
|
||||
Expression<Func<UserEntity, bool>> exp = filter => filter.Id == entity.Id;
|
||||
return await base.Exist(exp);
|
||||
}
|
||||
///删除用户
|
||||
public async Task DeleteUser(string id)
|
||||
/// <summary>
|
||||
///新增用户
|
||||
/// </summary>
|
||||
/// <param name="input">新增用户dto</param>
|
||||
/// <returns></returns>
|
||||
public async Task<ApiResult> AddUser(UserDto input)
|
||||
{
|
||||
await base.RemoveAsync(id);
|
||||
try
|
||||
{
|
||||
#region 校验参数
|
||||
if (string.IsNullOrEmpty(input.Username))
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "用户名非空" };
|
||||
}
|
||||
if (string.IsNullOrEmpty(input.Tel))
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "手机号非空" };
|
||||
}
|
||||
if (string.IsNullOrEmpty(input.Email))
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "邮箱非空" };
|
||||
}
|
||||
if (string.IsNullOrEmpty(input.RoleId))
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "角色不能为空" };
|
||||
}
|
||||
#endregion
|
||||
#region 组织数据
|
||||
var entity = input.Adapt<UserEntity>();
|
||||
entity.Phone = input.Tel;
|
||||
if (!string.IsNullOrEmpty(input.Password))
|
||||
{
|
||||
entity.Password = StringHelper.MD5Encrypt32(input.Password);
|
||||
}
|
||||
#endregion
|
||||
#region 保存数据
|
||||
if (entity != null && string.IsNullOrEmpty(entity.Id))
|
||||
{
|
||||
await base.CreateAsync(entity);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"新增用户出现异常,请求参数:user:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}," +
|
||||
$"请求接口:'api/User/AddUser'," +
|
||||
$"异常信息:{ex.Message}," +
|
||||
$"异常位置:{ex.StackTrace}"
|
||||
);
|
||||
return new ApiResult { code = 1, msg = "保存用户信息失败" };
|
||||
}
|
||||
finally
|
||||
{
|
||||
_logger.LogInformation($"新增用户参数:user:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}");
|
||||
}
|
||||
return new ApiResult { code = 0, msg = "保存用户信息成功" };
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取用户列表
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ApiResult> GetUserList(UserQueryDto input)
|
||||
{
|
||||
List<UserDetailDto> list = new List<UserDetailDto>();
|
||||
try
|
||||
{
|
||||
#region 组织查询条件
|
||||
Expression<Func<UserEntity, bool>> exp = filter => filter.IsDelete == false;
|
||||
if (!string.IsNullOrEmpty(input.key))
|
||||
{
|
||||
exp = filter => filter.Username.Contains(input.key)|| filter.Phone.Contains(input.key)|| filter.Email.Contains(input.key);
|
||||
}
|
||||
#endregion
|
||||
#region 获取数据
|
||||
var result = await base.GetListWithExp(exp);//获取人员信息
|
||||
if (result.Count() > 0)
|
||||
{
|
||||
result = result.Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new ApiResult { code = 0, data = null, msg = "没有用户信息" };
|
||||
}
|
||||
var roleList = await _roleService.GetRoleListByIds(result.Select(s => s.RoleId).ToList());//根据角色Id获取角色信息
|
||||
#endregion
|
||||
#region 组装返回数据
|
||||
foreach (var item in result)
|
||||
{
|
||||
list.Add(new UserDetailDto
|
||||
{
|
||||
UserId = item.Id,
|
||||
Username = item.Username,
|
||||
Email = item.Email,
|
||||
RoleId = item.RoleId,
|
||||
RoleName = roleList.FirstOrDefault(s => s.Id == item.RoleId)?.RoleName,
|
||||
Tel = item.Phone,
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"获取用户列表出现异常,请求参数:userQuery:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}," +
|
||||
$"请求接口:'api/User/GetUserList'," +
|
||||
$"异常信息:{ex.Message}," +
|
||||
$"异常位置:{ex.StackTrace}"
|
||||
);
|
||||
return new ApiResult { code = 1, msg = "获取用户列表失败" };
|
||||
}
|
||||
finally
|
||||
{
|
||||
_logger.LogInformation($"获取用户列表参数:userQuery:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}");
|
||||
}
|
||||
return new ApiResult { code = 0, data = list,msg="获取信息成功" };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据用户Id获取用户信息
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ApiResult> GetUserById(string userId)
|
||||
{
|
||||
UserDetailDto userDetail = null;
|
||||
try
|
||||
{
|
||||
#region 校验数据
|
||||
if (string.IsNullOrEmpty(userId))
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "获取用户信息失败,userId非空" };
|
||||
}
|
||||
#endregion
|
||||
#region 获取数据
|
||||
var user = await base.GetAsync(userId);//根据userId获取用户信息
|
||||
if (user == null)
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "用户不存在" };
|
||||
}
|
||||
var role = await _roleService.GetRoleById(user.RoleId);//根据角色Id获取角色信息
|
||||
if (role == null)
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "角色不存在" };
|
||||
}
|
||||
#endregion
|
||||
#region 组织需要展示的数据
|
||||
userDetail = new UserDetailDto
|
||||
{
|
||||
UserId = user.Id,
|
||||
Username = user.Username,
|
||||
Email = user.Email,
|
||||
RoleId = user.RoleId,
|
||||
RoleName = role.RoleName,
|
||||
Tel = user.Phone
|
||||
};
|
||||
#endregion
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"获取用户信息出现异常,请求参数:userId:{userId}," +
|
||||
$"请求接口:'api/User/GetUserById'," +
|
||||
$"异常信息:{ex.Message}," +
|
||||
$"异常位置:{ex.StackTrace}"
|
||||
);
|
||||
return new ApiResult { code = 1, msg = "获取用户信息失败" };
|
||||
}
|
||||
finally
|
||||
{
|
||||
_logger.LogInformation($"获取用户信息参数:userId:{userId}");
|
||||
}
|
||||
return new ApiResult { code = 0, data = userDetail };
|
||||
}
|
||||
|
||||
|
||||
//更新用户信息方法
|
||||
public async Task<ApiResult> UpdateUser(UserDto input)
|
||||
{
|
||||
try
|
||||
{
|
||||
#region 校验参数
|
||||
if (string.IsNullOrEmpty(input.Id))
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "用户Id不能为空" };
|
||||
}
|
||||
if (string.IsNullOrEmpty(input.Username))
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "用户名不能为空" };
|
||||
}
|
||||
if (string.IsNullOrEmpty(input.Tel))
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "手机号不能为空" };
|
||||
}
|
||||
if (string.IsNullOrEmpty(input.Email))
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "邮箱不能为空" };
|
||||
}
|
||||
if (string.IsNullOrEmpty(input.RoleId))
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "角色不能为空" };
|
||||
}
|
||||
#endregion
|
||||
#region 组织数据
|
||||
var userEntity = input.Adapt<UserEntity>();
|
||||
userEntity.Phone = input.Tel;
|
||||
if (!string.IsNullOrEmpty(input.Password))
|
||||
{
|
||||
userEntity.Password = StringHelper.MD5Encrypt32(input.Password);
|
||||
}
|
||||
#endregion
|
||||
#region 更新数据
|
||||
await base.UpdateAsync(input.Id, userEntity);//更新用户信息
|
||||
#endregion
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"更新用户信息出现异常,请求参数:user:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}," +
|
||||
$"请求接口:'api/User/UpdateUser'," +
|
||||
$"异常信息:{ex.Message}," +
|
||||
$"异常位置:{ex.StackTrace}"
|
||||
);
|
||||
return new ApiResult { code = 1, msg = "更新用户信息失败" };
|
||||
}
|
||||
finally
|
||||
{
|
||||
_logger.LogInformation($"更新用户信息参数:user:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}");
|
||||
}
|
||||
return new ApiResult { code = 0, msg = "更新用户信息成功" };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 作废用户的方法
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ApiResult> DeleteUser(string userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
#region 校验数据
|
||||
if (string.IsNullOrEmpty(userId))
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "用户Id不能为空" };
|
||||
}
|
||||
#endregion
|
||||
#region 更新数据
|
||||
var user = await base.GetAsync(userId);//根据userId获取用户信息
|
||||
if (user == null)
|
||||
{
|
||||
return new ApiResult { code = 1, msg = "用户不存在" };
|
||||
}
|
||||
user.IsDelete = true;
|
||||
await base.UpdateAsync(userId, user);//更新用户信息
|
||||
#endregion
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"删除用户信息出现异常,请求参数:userId:{userId}," +
|
||||
$"请求接口:'api/User/DeleteUser'," +
|
||||
$"异常信息:{ex.Message}," +
|
||||
$"异常位置:{ex.StackTrace}"
|
||||
);
|
||||
return new ApiResult { code = 1, msg = "删除用户信息失败" };
|
||||
}
|
||||
finally
|
||||
{
|
||||
_logger.LogInformation($"删除用户信息参数:userId:{userId}");
|
||||
}
|
||||
return new ApiResult { code = 0, msg = "删除用户信息成功" };
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue