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

120 lines
4.4 KiB
C#
Raw Normal View History

2021-10-11 21:50:50 +08:00
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
2022-01-11 16:40:15 +08:00
using Yi.Framework.Common.Const;
2021-11-02 14:01:37 +08:00
using Yi.Framework.Core;
2022-01-11 16:40:15 +08:00
using Yi.Framework.DTOModel;
2021-10-11 21:50:50 +08:00
using Yi.Framework.Interface;
2021-10-12 18:05:01 +08:00
using Yi.Framework.Model;
using Yi.Framework.Model.ModelFactory;
2021-10-11 21:50:50 +08:00
using Yi.Framework.Model.Models;
namespace Yi.Framework.Service
{
2021-10-25 16:52:09 +08:00
public partial class UserService : BaseService<user>, IUserService
2021-10-11 21:50:50 +08:00
{
2022-01-11 16:40:15 +08:00
CacheClientDB _cacheClientDB;
public UserService(CacheClientDB cacheClientDB, IDbContextFactory DbFactory) : base(DbFactory)
{
_cacheClientDB = cacheClientDB;
}
2021-10-27 23:03:56 +08:00
short Normal = (short)Common.Enum.DelFlagEnum.Normal;
public async Task<bool> PhoneIsExsit(string smsAddress)
2021-10-12 18:05:01 +08:00
{
var userList = await GetEntity(u => u.phone == smsAddress);
if (userList == null)
{
return false;
}
return true;
2021-10-11 21:50:50 +08:00
}
2021-10-13 23:08:42 +08:00
public async Task<bool> EmailIsExsit(string emailAddress)
{
var userList = await GetEntity(u => u.email == emailAddress);
if (userList == null)
2021-10-13 23:08:42 +08:00
{
return false;
2021-10-13 23:08:42 +08:00
}
return true;
2021-10-12 18:05:01 +08:00
}
2021-10-25 16:52:09 +08:00
/// <summary>
2021-10-27 23:03:56 +08:00
///
2021-10-25 16:52:09 +08:00
/// </summary>
2021-10-27 23:03:56 +08:00
/// <param name="userId"></param>
2021-10-25 16:52:09 +08:00
/// <returns></returns>
2021-10-27 23:03:56 +08:00
public async Task<user> GetUserById(int userId)
2021-10-12 18:05:01 +08:00
{
2022-01-11 16:40:15 +08:00
return await _DbRead.Set<user>().Include(u => u.roles).ThenInclude(u => u.menus).ThenInclude(u => u.children).ThenInclude(u => u.mould).Where(u => u.id == userId).FirstOrDefaultAsync();
2021-10-12 18:05:01 +08:00
2021-10-11 21:50:50 +08:00
}
2022-01-11 16:40:15 +08:00
public async Task<List<menu>> GetAxiosByRouter(string router, int userId, List<int> menuIds)
2021-10-17 17:35:18 +08:00
{
2022-01-11 16:40:15 +08:00
var user_data = await GetUserById(userId);
2021-10-27 23:03:56 +08:00
List<menu> menuList = new();
2022-01-11 16:40:15 +08:00
foreach (var item in user_data.roles)
2021-10-17 17:35:18 +08:00
{
2022-01-11 16:40:15 +08:00
var m = item.menus.Where(u => u?.router?.ToUpper() == router.ToUpper()).FirstOrDefault();
if (m == null) { break; }
2022-01-11 16:40:15 +08:00
menuList = m.children?.Where(u => menuIds.Contains(u.id) && u.is_delete == Normal).ToList();
2021-10-25 16:52:09 +08:00
}
2022-01-11 16:40:15 +08:00
return menuList;
}
2021-10-25 16:52:09 +08:00
2021-11-02 23:22:37 +08:00
public async Task<menu> GetMenuByHttpUser(List<int> allMenuIds)
2021-10-22 16:48:03 +08:00
{
2022-01-11 16:40:15 +08:00
var topMenu = await _DbRead.Set<menu>().Include(u => u.children).ThenInclude(u => u.children).ThenInclude(u => u.children).ThenInclude(u => u.children).ThenInclude(u => u.children).Where(u => u.is_top == (short)Common.Enum.ShowFlagEnum.Show).FirstOrDefaultAsync();
2021-11-02 14:01:37 +08:00
//现在要开始关联菜单了
return TreeMenuBuild.Sort(TreeMenuBuild.ShowFormat(topMenu, allMenuIds)); ;
2022-01-11 16:40:15 +08:00
}
2021-10-27 23:03:56 +08:00
public async Task<user> GetUserInRolesByHttpUser(int userId)
{
var data = await GetUserById(userId);
2022-01-11 16:40:15 +08:00
data.roles?.ForEach(u =>
{
u.users = null;
u.menus = null;
});
2022-01-11 16:40:15 +08:00
return data;
2021-10-27 23:03:56 +08:00
}
2021-10-23 20:23:44 +08:00
2021-10-27 23:03:56 +08:00
public async Task<user> Login(user _user)
{
2022-01-11 16:40:15 +08:00
var user_data = await _DbRead.Set<user>().Include(u => u.roles).Where(u => u.username == _user.username && u.password == _user.password && u.is_delete == Normal).FirstOrDefaultAsync();
2021-10-27 23:03:56 +08:00
return user_data;
}
2021-10-23 20:23:44 +08:00
2021-10-27 23:03:56 +08:00
public async Task<bool> Register(user _user)
2021-10-24 14:33:04 +08:00
{
2021-10-27 23:03:56 +08:00
var user_data = await GetEntity(u => u.username == _user.username);
if (user_data != null)
2021-10-24 17:13:28 +08:00
{
2021-10-27 23:03:56 +08:00
return false;
2021-10-24 14:33:04 +08:00
}
return await UpdateAsync(_user);
2021-10-27 23:03:56 +08:00
}
public async Task<bool> SetRoleByUser(List<int> roleIds, List<int> userIds)
{
2022-01-11 16:40:15 +08:00
var user_data = await _DbRead.Set<user>().Include(u => u.roles).Where(u => userIds.Contains(u.id)).ToListAsync();
2021-10-29 23:23:32 +08:00
var roleList = await _DbRead.Set<role>().Where(u => roleIds.Contains(u.id)).ToListAsync();
2022-01-11 16:40:15 +08:00
user_data.ForEach(u => u.roles = roleList);
2021-10-27 23:03:56 +08:00
return await UpdateListAsync(user_data);
2021-10-23 18:46:07 +08:00
}
2022-01-11 16:40:15 +08:00
public bool SaveUserApi(int userId, List<menuDto> menus)
{
return _cacheClientDB.Set(RedisConst.userMenusApi+":"+userId.ToString(),menus,new TimeSpan(0,30,0));
}
public List<int> GetCurrentMenuInfo(int userId)
{
return _cacheClientDB.Get<List<menuDto>>(RedisConst.userMenusApi+":"+userId).Select(u=>u.id).ToList();
}
2021-10-22 16:48:03 +08:00
}
2021-10-11 21:50:50 +08:00
}