using FreeSql; using F9MES.Application.Auth; using F9MES.Common.Result; using F9MES.Domain.BaseCommon; using F9MES.Domain.BaseSys; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace F9MES.Api.Controllers; /// /// 系统管理控制器:组织树/字典/用户管理 /// [ApiController] [Route("api/basesys")] [Authorize] public class BaseSysController : ControllerBase { private readonly IFreeSql _db; private readonly AuthService _auth; public BaseSysController(IFreeSql db, AuthService auth) { _db = db; _auth = auth; } /// 组织架构树 [HttpGet("org/tree")] public async Task OrgTree() { var orgs = await _db.Queryable() .Where(a => a.Flag > 0 && a.IsEnable == 1) .OrderBy(a => a.Sort).ToListAsync(); return ApiResult.Ok(BuildOrgTree(orgs, 0)); } private static object BuildOrgTree(List all, long parentId) { return all.Where(o => o.ParentId == parentId).Select(o => new { o.Id, o.Name, o.Code, o.OrgType, o.Manager, o.Phone, children = BuildOrgTree(all, o.Id) }).ToList(); } /// 查询字典项(按字典类型编码) [HttpGet("dict/{typeCode}")] public async Task GetDict(string typeCode) { var list = await _db.Queryable() .Where(a => a.Flag > 0 && a.TypeCode == typeCode && a.IsEnable == 1) .OrderBy(a => a.Sort) .ToListAsync(); return ApiResult.Ok(list.Select(d => new { d.Id, d.DictKey, d.DictValue, d.ExtData })); } /// 查询全部字典(按类型分组) [HttpGet("dict/all")] public async Task GetAllDicts() { var types = await _db.Queryable() .Where(a => a.Flag > 0).ToListAsync(); var dicts = await _db.Queryable() .Where(a => a.Flag > 0 && a.IsEnable == 1) .OrderBy(a => a.Sort).ToListAsync(); return ApiResult.Ok(types.Select(t => new { t.Id, t.Code, t.Name, items = dicts.Where(d => d.TypeCode == t.Code).Select(d => new { d.Id, d.DictKey, d.DictValue, d.ExtData }) })); } /// 创建用户(自动生成默认密码哈希) [HttpPost("user/create")] public async Task CreateUser([FromBody] CreateUserDto dto) { if (string.IsNullOrWhiteSpace(dto.Phone) || dto.Phone.Length < 6) return ApiResult.Error("手机号不合法"); var exists = await _db.Queryable() .Where(a => a.Phone == dto.Phone && a.Flag > 0).AnyAsync(); if (exists) return ApiResult.Error("该手机号已注册"); var password = string.IsNullOrWhiteSpace(dto.Password) ? "123456" : dto.Password!; var user = new BaseSys_User { Phone = dto.Phone, Password = AuthService.HashPassword(password), Name = dto.Name ?? dto.Phone, Gender = dto.Gender, IsSuperAdmin = 0, Status = dto.Status ?? 1, AddTime = DateTime.Now, UpdateTime = DateTime.Now }; await _db.Insert(user).ExecuteAffrowsAsync(); return ApiResult.Ok(new { user.Id }, "创建成功,初始密码 " + password); } /// 用户绑定角色 [HttpPost("user/{userId:long}/bindRoles")] public async Task BindRoles(long userId, [FromBody] List roleIds) { await _db.Delete().Where(a => a.UserId == userId).ExecuteAffrowsAsync(); var binds = roleIds.Select(rid => new BaseSys_UserRole { UserId = userId, RoleId = rid }).ToList(); if (binds.Count > 0) await _db.Insert(binds).ExecuteAffrowsAsync(); return ApiResult.Ok(null, "绑定成功"); } /// 角色绑定菜单权限 [HttpPost("role/{roleId:long}/bindMenus")] public async Task BindMenus(long roleId, [FromBody] List menuIds) { await _db.Delete().Where(a => a.RoleId == roleId).ExecuteAffrowsAsync(); var binds = menuIds.Select(mid => new BaseSys_RoleMenu { RoleId = roleId, MenuId = mid }).ToList(); if (binds.Count > 0) await _db.Insert(binds).ExecuteAffrowsAsync(); return ApiResult.Ok(null, "授权成功"); } /// 重置用户密码 [HttpPost("user/resetPassword")] public async Task ResetPassword([FromBody] ResetPasswordDto dto) { return await _auth.ResetPasswordAsync(dto.UserId, dto.NewPassword ?? "123456"); } } /// 创建用户参数 public class CreateUserDto { public string? Phone { get; set; } public string? Password { get; set; } public string? Name { get; set; } public int Gender { get; set; } public int? Status { get; set; } } /// 重置密码参数 public class ResetPasswordDto { public long UserId { get; set; } public string? NewPassword { get; set; } }