using WIDESEAWCS_Core.Authorization;
|
using WIDESEAWCS_Core;
|
using WIDESEAWCS_Core.BaseServices;
|
using WIDESEAWCS_Core.Const;
|
using WIDESEAWCS_Core.Helper;
|
using WIDESEAWCS_Core.HttpContextUser;
|
using WIDESEAWCS_ISystemServices;
|
using WIDESEAWCS_Model;
|
using WIDESEAWCS_Model.Models;
|
using WIDESEAWCS_Core.BaseRepository;
|
using System.Net;
|
using WIDESEAWCS_Core.Caches;
|
using WIDESEAWCS_DTO.SerialPort;
|
using Microsoft.AspNetCore.Http;
|
|
namespace WIDESEAWCS_SystemServices
|
{
|
public class Sys_UserService : ServiceBase<Sys_User, IRepository<Sys_User>>, ISys_UserService
|
{
|
private readonly IUnitOfWorkManage _unitOfWorkManage;
|
private readonly ICacheService _cacheService;
|
private readonly ISys_MenuService _menuService;
|
public Sys_UserService(IRepository<Sys_User> repository, IUnitOfWorkManage unitOfWorkManage, ICacheService cacheService, ISys_MenuService menuService) : base(repository)
|
{
|
_unitOfWorkManage = unitOfWorkManage;
|
_cacheService = cacheService;
|
_menuService = menuService;
|
}
|
|
public WebResponseContent Login(LoginInfo loginInfo)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
//BaseDal.QueryFirst(x => x.UserName == loginInfo.UserName);
|
|
string msg = string.Empty;
|
|
#region 临时使用
|
try
|
{
|
loginInfo.Password = loginInfo.Password.EncryptDES(AppSecret.User);
|
}
|
catch
|
{
|
|
}
|
#endregion
|
|
UserInfo user = BaseDal.QueryFirst(x => x.UserName == loginInfo.UserName && x.UserPwd == loginInfo.Password, x => new UserInfo { HeadImageUrl = x.HeadImageUrl, RoleId = x.Role_Id, TenantId = x.TenantId, UserId = x.User_Id, UserName = x.UserName, UserTrueName = x.UserTrueName });
|
if (user != null)
|
{
|
object obj = _menuService.GetMenuActionList(user.RoleId);
|
if (obj is not IEnumerable<object> list)
|
{
|
return WebResponseContent.Instance.Error("无登录权限");
|
}
|
if (!list.Any())
|
{
|
return WebResponseContent.Instance.Error("无登录权限");
|
}
|
|
string token = JwtHelper.IssueJwt(new TokenModelJwt()
|
{
|
UserId = user.UserId,
|
RoleId = user.RoleId,
|
UserName = user.UserName,
|
TenantId = user.TenantId,
|
});
|
|
_cacheService.AddOrUpdate(user.UserId.ToString(), token);
|
|
content = WebResponseContent.Instance.OK(data: new { token, userName = user.UserTrueName, img = user.HeadImageUrl,ID=user.UserId });
|
}
|
else
|
{
|
content = WebResponseContent.Instance.Error("账号或密码错误");
|
}
|
}
|
catch (Exception ex)
|
{
|
content = WebResponseContent.Instance.Error(ex.Message);
|
}
|
|
return content;
|
}
|
|
public override WebResponseContent UpdateData(SaveModel saveModel)
|
{
|
UpdateIgnoreColOnExecute = x =>
|
{
|
return new List<string>
|
{
|
nameof(Sys_User.UserPwd),
|
nameof(Sys_User.TenantId)
|
};
|
};
|
return base.UpdateData(saveModel);
|
}
|
|
public override WebResponseContent AddData(SaveModel saveModel)
|
{
|
string pwd = "123456";
|
string uesrName = saveModel.MainData[nameof(Sys_User.UserName).FirstLetterToLower()].ToString();
|
saveModel.MainData[nameof(Sys_User.UserPwd).FirstLetterToLower()] = pwd.EncryptDES(AppSecret.User);
|
|
WebResponseContent content = base.AddData(saveModel);
|
if (content.Status)
|
{
|
return WebResponseContent.Instance.OK($"用户新建成功.帐号{uesrName}密码{pwd}");
|
}
|
else
|
{
|
return content;
|
}
|
}
|
|
/// <summary>
|
/// 个人中心获取当前用户信息
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent GetCurrentUserInfo()
|
{
|
var data = BaseDal.QueryFirst(x => x.User_Id == App.User.UserId, s => new
|
{
|
s.UserName,
|
s.UserTrueName,
|
//s.Address,
|
//s.PhoneNo,
|
//s.Email,
|
s.Remark,
|
//s.Gender,
|
s.RoleName,
|
s.HeadImageUrl,
|
s.CreateDate
|
});
|
return WebResponseContent.Instance.OK(null, data);
|
}
|
|
/// <summary>
|
/// 修改密码
|
/// </summary>
|
/// <param name="parameters"></param>
|
/// <returns></returns>
|
public WebResponseContent ModifyPwd(string oldPwd, string newPwd)
|
{
|
WebResponseContent content = WebResponseContent.Instance;
|
oldPwd = oldPwd?.Trim();
|
newPwd = newPwd?.Trim();
|
string message = "";
|
try
|
{
|
if (string.IsNullOrEmpty(oldPwd)) return WebResponseContent.Instance.Error("旧密码不能为空");
|
if (string.IsNullOrEmpty(newPwd)) return WebResponseContent.Instance.Error("新密码不能为空");
|
if (newPwd.Length < 6) return WebResponseContent.Instance.Error("密码不能少于6位");
|
|
int userId = App.User.UserId;
|
string userCurrentPwd = BaseDal.QueryFirst(x => x.User_Id == userId, s => s.UserPwd);
|
|
string _oldPwd = oldPwd.EncryptDES(AppSecret.User);
|
if (_oldPwd != userCurrentPwd) return WebResponseContent.Instance.Error("旧密码不正确");
|
|
string _newPwd = newPwd.EncryptDES(AppSecret.User);
|
if (userCurrentPwd == _newPwd) return WebResponseContent.Instance.Error("新密码不能与旧密码相同");
|
|
|
BaseDal.UpdateData(new Sys_User
|
{
|
User_Id = userId,
|
UserPwd = _newPwd,
|
LastModifyPwdDate = DateTime.Now
|
}, new List<string>
|
{
|
nameof(Sys_User.LastModifyPwdDate),
|
nameof(Sys_User.UserPwd)
|
});
|
|
content = WebResponseContent.Instance.OK("密码修改成功");
|
}
|
catch (Exception ex)
|
{
|
message = ex.Message;
|
content = WebResponseContent.Instance.Error("服务器了点问题,请稍后再试");
|
}
|
return content;
|
}
|
|
/// <summary>
|
/// 更新密码
|
/// </summary>
|
/// <param name="id"></param>
|
/// <param name="oldPwd"></param>
|
/// <param name="newPwd"></param>
|
/// <returns></returns>
|
public WebResponseContent UpdatePwd(int id, string oldPwd, string newPwd)
|
{
|
WebResponseContent content = new WebResponseContent();
|
oldPwd = oldPwd?.Trim();
|
newPwd = newPwd?.Trim();
|
|
try
|
{
|
if (string.IsNullOrEmpty(oldPwd)) return content.Error("旧密码不能为空");
|
if (string.IsNullOrEmpty(newPwd)) return content.Error("新密码不能为空");
|
if (newPwd.Length < 6) return content.Error("密码不能少于6位");
|
|
// 获取用户当前密码
|
string userCurrentPwd = BaseDal.QueryFirst(x => x.User_Id == id, s => s.UserPwd) ?? "";
|
|
if (string.IsNullOrEmpty(userCurrentPwd)) return content.Error("用户不存在或密码未设置");
|
|
//// 进行密码加密对比
|
//string _oldPwd = oldPwd.EncryptDES(AppSecret.User);
|
//if (_oldPwd != userCurrentPwd) return content.Error("旧密码不正确");
|
|
// 生成新密码加密值
|
string _newPwd = newPwd.EncryptDES(AppSecret.User);
|
if (userCurrentPwd == _newPwd) return content.Error("新密码不能与旧密码相同");
|
|
// 更新密码
|
bool isUpdated = BaseDal.UpdateData(new Sys_User
|
{
|
User_Id = id,
|
UserPwd = _newPwd,
|
LastModifyPwdDate = DateTime.Now
|
}, new List<string>
|
{
|
nameof(Sys_User.LastModifyPwdDate),
|
nameof(Sys_User.UserPwd)
|
});
|
|
if (!isUpdated)
|
{
|
return content.Error("密码修改失败,请稍后重试");
|
}
|
|
return content.OK("密码修改成功", id);
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine($"修改密码异常: {ex.Message}");
|
return content.Error($"服务器错误: {ex.Message}");
|
}
|
}
|
|
|
public WebResponseContent Upuserbase(UserDTO userDTO)
|
{
|
try
|
{
|
var user = BaseDal.QueryData(x => x.User_Id == userDTO.id).FirstOrDefault();
|
if (user == null)
|
{
|
return new WebResponseContent { Status = false, Message = "没找到该用户" };
|
}
|
user.UserTrueName = userDTO.usertruename;
|
user.PhoneNo = userDTO.phone;
|
user.HeadImageUrl = userDTO.files;
|
BaseDal.UpdateData(user); // 确保更新到数据库
|
return new WebResponseContent { Status = true, Data = user };
|
}
|
catch (Exception ex)
|
{
|
|
return new WebResponseContent { Status = false, Message = "失败:" + ex };
|
}
|
|
}
|
|
/// <summary>
|
/// 图片文件名
|
/// </summary>
|
/// <param name="files"></param>
|
/// <returns></returns>
|
public WebResponseContent SaveFiles(IFormCollection files)
|
{
|
if (files == null || files.Files.Count == 0)
|
return new WebResponseContent { Status = false, Message = "请上传文件" };
|
|
// 1. 确保存储目录存在
|
string baseDirectory = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "image");
|
if (!Directory.Exists(baseDirectory))
|
Directory.CreateDirectory(baseDirectory);
|
|
try
|
{
|
var file = files.Files[0]; // 只处理第一个文件
|
string fileName = file.FileName; // 直接使用前端的文件名
|
|
string fullFilePath = Path.Combine(baseDirectory, fileName);
|
|
// 2. 保存文件(如果存在,则覆盖)
|
using (var stream = new FileStream(fullFilePath, FileMode.Create))
|
{
|
file.CopyTo(stream);
|
}
|
|
// 3. 返回文件名
|
return new WebResponseContent { Status = true, Message = "文件上传成功", Data = fileName };
|
}
|
catch (Exception ex)
|
{
|
return new WebResponseContent { Status = false, Message = "上传文件失败:" + ex.Message };
|
}
|
}
|
}
|
}
|