人员管理,新增,查询列表,查询单个,修改,删除接口
This commit is contained in:
parent
d4ac85c525
commit
ef5cce1ce8
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,30 +56,8 @@ namespace langguanApi.Controllers
|
|||
var result = await _userService.GetPage(input);
|
||||
return Ok(result);
|
||||
}
|
||||
///删除用户
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("Delete")]
|
||||
public async Task<IActionResult> Delete(string id)
|
||||
{
|
||||
await _userService.DeleteUser(id);
|
||||
return Ok(new ApiResult() { code = 0 });
|
||||
}
|
||||
/// <summary>
|
||||
/// 修改用户信息
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut("Update")]
|
||||
public async Task<IActionResult> Update([FromBody] UserUpdate input)
|
||||
{
|
||||
await _userService.UpdateUser(input);
|
||||
return Ok(new ApiResult() { code = 0 });
|
||||
}
|
||||
|
||||
#region 用户管理相关接口
|
||||
|
||||
/// <summary>
|
||||
///新增用户
|
||||
/// </summary>
|
||||
|
|
@ -103,8 +81,42 @@ namespace langguanApi.Controllers
|
|||
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>
|
||||
[HttpPost("UpdateUser")]
|
||||
public async Task<IActionResult> UpdateUser([FromBody] UserDto input)
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,41 +27,10 @@ namespace langguanApi.Model.Dto.SystemConfigurationDto
|
|||
/// </summary>
|
||||
public string Email { get; set; }
|
||||
/// <summary>
|
||||
/// 学历
|
||||
/// </summary>
|
||||
public string Education { get; set; }
|
||||
/// <summary>
|
||||
/// 手机号
|
||||
/// </summary>
|
||||
public string Tel { get; set; }
|
||||
/// <summary>
|
||||
/// 性别 0表示男 1表示女
|
||||
/// </summary>
|
||||
public byte Sex { get; set; }
|
||||
/// <summary>
|
||||
/// 毕业院校
|
||||
/// </summary>
|
||||
public string University { get; set; }
|
||||
/// <summary>
|
||||
/// 出生日期
|
||||
/// </summary>
|
||||
public string BrithdayDateTime { get; set; }
|
||||
public DateTime? Brithday { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 入职日期
|
||||
/// </summary>
|
||||
public string HiredateDateTime { get; set; } = DateTime.Now.ToString();
|
||||
public DateTime? Hiredate { get; set; }
|
||||
/// <summary>
|
||||
/// 籍贯
|
||||
/// </summary>
|
||||
public string Native { get; set; }
|
||||
/// <summary>
|
||||
/// 居住地
|
||||
/// </summary>
|
||||
public string Address { get; set; }
|
||||
/// <summary>
|
||||
/// 是否管理员 0否 1是
|
||||
/// </summary>
|
||||
public byte IsAdmin { get; set; }
|
||||
|
|
@ -69,8 +38,10 @@ namespace langguanApi.Model.Dto.SystemConfigurationDto
|
|||
/// 是否删除 0否 1是
|
||||
/// </summary>
|
||||
public byte IsDel { get; set; }
|
||||
//是否启用 0表示未启用 1表示启用
|
||||
public byte IsEnable { get; set; }
|
||||
/// <summary>
|
||||
/// 密码 md5加密
|
||||
/// </summary>
|
||||
public string Password { get; set; }
|
||||
}
|
||||
/// <summary>
|
||||
///根据用户条件查询
|
||||
|
|
@ -85,64 +56,50 @@ namespace langguanApi.Model.Dto.SystemConfigurationDto
|
|||
/// 手机号
|
||||
/// </summary>
|
||||
public string Tel { get; set; } = "";
|
||||
//入职开始时间
|
||||
public string HiredateStart { get; set; } = "";
|
||||
//入职结束时间
|
||||
public string HiredateEnd { get; set; } = "";
|
||||
/// <summary>
|
||||
/// 当前条数
|
||||
/// </summary>
|
||||
public int PageSize { get; set; } = 10;
|
||||
/// <summary>
|
||||
/// 当前页数
|
||||
/// </summary>
|
||||
public int PageIndex { get; set; } = 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户列表展示
|
||||
/// </summary>
|
||||
public class UserListDto
|
||||
public class UserDetailDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户ID,方便查看详情操作
|
||||
/// </summary>
|
||||
public string UserId { get; set; }
|
||||
/// <summary>
|
||||
/// 用户名
|
||||
/// </summary>
|
||||
public string Username { get; set; }
|
||||
/// <summary>
|
||||
/// 学历
|
||||
/// </summary>
|
||||
public string Education { 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 Sex { get; set; }
|
||||
/// <summary>
|
||||
/// 毕业院校
|
||||
/// </summary>
|
||||
public string University { get; set; }
|
||||
public string RoleName{ get; set; }
|
||||
/// <summary>
|
||||
/// 联系方式
|
||||
/// </summary>
|
||||
public string Tel { get; set; }
|
||||
/// <summary>
|
||||
/// 出生日期
|
||||
/// </summary>
|
||||
public string Brithday { get; set; }
|
||||
/// <summary>
|
||||
/// 籍贯
|
||||
/// </summary>
|
||||
public string Native { get; set; }
|
||||
/// <summary>
|
||||
/// 居住地
|
||||
/// </summary>
|
||||
public string Address { get; set; }
|
||||
/// <summary>
|
||||
/// 入职日期
|
||||
/// </summary>
|
||||
public string Hiredate { get; set; }
|
||||
/// <summary>
|
||||
/// 是否管理员 0否 1是
|
||||
/// </summary>
|
||||
public string IsAdmin { get; set; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ namespace langguanApi.Model.Entity
|
|||
/// <summary>
|
||||
/// 角色id
|
||||
/// </summary>
|
||||
public string roleId { get; set; }
|
||||
public string RoleId { get; set; }
|
||||
/// <summary>
|
||||
/// 邮箱
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -141,5 +141,18 @@ namespace langguanApi.Service
|
|||
var list = (await base.GetListWithExp(exp)).ToList();
|
||||
return list;
|
||||
}
|
||||
|
||||
#region 用户管理角色相关
|
||||
/// <summary>
|
||||
/// 根据id获取角色信息
|
||||
/// </summary>
|
||||
/// <param name="roleId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Role> GetRoleById(string roleId)
|
||||
{
|
||||
return await base.GetAsync(roleId);
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ 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
|
||||
|
|
@ -83,18 +84,6 @@ namespace langguanApi.Service
|
|||
current = input.current
|
||||
}, exp);
|
||||
}
|
||||
///更新用户信息
|
||||
public async Task UpdateUser(UserUpdate user)
|
||||
{
|
||||
var entity = user.Adapt<UserEntity>();
|
||||
entity.Password = StringHelper.MD5Encrypt32(user.Password);
|
||||
await base.UpdateAsync(user.Id, entity);
|
||||
}
|
||||
///删除用户
|
||||
public async Task DeleteUser(string id)
|
||||
{
|
||||
await base.RemoveAsync(id);
|
||||
}
|
||||
|
||||
#region 用户管理
|
||||
/// <summary>
|
||||
|
|
@ -136,24 +125,18 @@ namespace langguanApi.Service
|
|||
}
|
||||
#endregion
|
||||
#region 组织数据
|
||||
|
||||
if (!string.IsNullOrEmpty(input.BrithdayDateTime))
|
||||
{
|
||||
input.Brithday = Convert.ToDateTime(input.BrithdayDateTime);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(input.HiredateDateTime))
|
||||
{
|
||||
input.Hiredate = Convert.ToDateTime(input.HiredateDateTime);
|
||||
}
|
||||
input.IsEnable = 1;
|
||||
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)
|
||||
|
|
@ -172,12 +155,13 @@ namespace langguanApi.Service
|
|||
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 组织查询条件
|
||||
|
|
@ -186,25 +170,40 @@ namespace langguanApi.Service
|
|||
{
|
||||
exp = filter => filter.Username.Contains(input.Username);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(input.Tel))
|
||||
{
|
||||
exp = filter => filter.Phone.Contains(input.Tel);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取数据
|
||||
var result = await base.GetListWithExp(exp);//获取人员信息
|
||||
var roleList = await _roleService.GetRoleListByIds(result.Select(s => s.roleId).ToList());
|
||||
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
|
||||
//md5解密
|
||||
//var password = StringHelper.MD5Decrypt32(item.Password);
|
||||
#region 组装返回数据
|
||||
List<UserListDto> list = new List<UserListDto>();
|
||||
foreach (var item in result)
|
||||
{
|
||||
list.Add(new UserListDto
|
||||
list.Add(new UserDetailDto
|
||||
{
|
||||
UserId = item.Id,
|
||||
Username = item.Username,
|
||||
Email = item.Email,
|
||||
roleName = roleList.FirstOrDefault(s => s.Id == item.roleId)?.RoleName,
|
||||
RoleId = item.RoleId,
|
||||
RoleName = roleList.FirstOrDefault(s => s.Id == item.RoleId)?.RoleName,
|
||||
IsAdmin = item.IsAdmin == 0 ? "男" : "女",
|
||||
Tel = item.Phone,
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
return new ApiResult { code = 0, data = list };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -219,6 +218,161 @@ namespace langguanApi.Service
|
|||
{
|
||||
_logger.LogInformation($"获取用户列表参数:userQuery:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}");
|
||||
}
|
||||
return new ApiResult { code = 0, data = list };
|
||||
}
|
||||
|
||||
/// <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,
|
||||
IsAdmin = user.IsAdmin == 0 ? "男" : "女",
|
||||
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