Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs

65 lines
2.0 KiB
C#
Raw Normal View History

2022-04-06 22:22:45 +08:00
using SqlSugar;
2022-04-07 22:48:10 +08:00
using System;
using System.Threading;
using System.Threading.Tasks;
2022-04-06 22:22:45 +08:00
using Yi.Framework.Interface;
2021-10-11 21:50:50 +08:00
using Yi.Framework.Model.Models;
2022-04-02 17:44:50 +08:00
using Yi.Framework.Repository;
2021-10-11 21:50:50 +08:00
namespace Yi.Framework.Service
{
2022-04-06 22:22:45 +08:00
public partial class UserService
2021-10-11 21:50:50 +08:00
{
2022-04-07 22:48:10 +08:00
public async Task<bool> Exist(Guid id, Action<UserEntity> userAction = null)
2022-04-06 22:22:45 +08:00
{
2022-04-08 23:44:25 +08:00
var user = await _repository.GetByIdAsync(id);
2022-04-07 22:48:10 +08:00
userAction.Invoke(user);
if (user == null)
{
return false;
}
return true;
}
public async Task<bool> Exist(string userName, Action<UserEntity> userAction = null)
{
2022-04-08 23:44:25 +08:00
var user = await _repository.GetFirstAsync(u=>u.UserName== userName);
2022-04-07 22:48:10 +08:00
if (userAction != null)
{
userAction.Invoke(user);
}
if (user == null)
{
return false;
}
return true;
}
public async Task<bool> Login(string userName, string password,Action<UserEntity> userAction = null)
{
var user=new UserEntity();
if (await Exist(userName, o => user = o))
{
userAction.Invoke(user);
if (user.Password== Common.Helper.MD5Helper.SHA2Encode(password, user.Salt))
{
return true;
}
}
return false;
}
public async Task<bool> Register(UserEntity userEntity, Action<UserEntity> userAction = null)
{
var user = new UserEntity();
2022-04-09 00:54:13 +08:00
if (!await Exist(userEntity.UserName))
2022-04-07 22:48:10 +08:00
{
user.UserName= userEntity.UserName;
user.Salt = Common.Helper.MD5Helper.GenerateSalt();
user.Password = Common.Helper.MD5Helper.SHA2Encode(userEntity.Password,user.Salt);
2022-04-08 23:44:25 +08:00
userAction.Invoke(await _repository.InsertReturnEntityAsync(user));
2022-04-07 22:48:10 +08:00
return true;
}
return false;
2022-04-06 22:22:45 +08:00
}
2021-10-22 16:48:03 +08:00
}
2021-10-11 21:50:50 +08:00
}