146 lines
5.1 KiB
C#
146 lines
5.1 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// 系统管理控制器:组织树/字典/用户管理
|
|
/// </summary>
|
|
[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;
|
|
}
|
|
|
|
/// <summary>组织架构树</summary>
|
|
[HttpGet("org/tree")]
|
|
public async Task<ApiResult> OrgTree()
|
|
{
|
|
var orgs = await _db.Queryable<BaseSys_Org>()
|
|
.Where(a => a.Flag > 0 && a.IsEnable == 1)
|
|
.OrderBy(a => a.Sort).ToListAsync();
|
|
return ApiResult.Ok(BuildOrgTree(orgs, 0));
|
|
}
|
|
|
|
private static object BuildOrgTree(List<BaseSys_Org> 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();
|
|
}
|
|
|
|
/// <summary>查询字典项(按字典类型编码)</summary>
|
|
[HttpGet("dict/{typeCode}")]
|
|
public async Task<ApiResult> GetDict(string typeCode)
|
|
{
|
|
var list = await _db.Queryable<BaseCommon_Dict>()
|
|
.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 }));
|
|
}
|
|
|
|
/// <summary>查询全部字典(按类型分组)</summary>
|
|
[HttpGet("dict/all")]
|
|
public async Task<ApiResult> GetAllDicts()
|
|
{
|
|
var types = await _db.Queryable<BaseCommon_DictType>()
|
|
.Where(a => a.Flag > 0).ToListAsync();
|
|
var dicts = await _db.Queryable<BaseCommon_Dict>()
|
|
.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 })
|
|
}));
|
|
}
|
|
|
|
/// <summary>创建用户(自动生成默认密码哈希)</summary>
|
|
[HttpPost("user/create")]
|
|
public async Task<ApiResult> CreateUser([FromBody] CreateUserDto dto)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(dto.Phone) || dto.Phone.Length < 6)
|
|
return ApiResult.Error("手机号不合法");
|
|
|
|
var exists = await _db.Queryable<BaseSys_User>()
|
|
.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);
|
|
}
|
|
|
|
/// <summary>用户绑定角色</summary>
|
|
[HttpPost("user/{userId:long}/bindRoles")]
|
|
public async Task<ApiResult> BindRoles(long userId, [FromBody] List<long> roleIds)
|
|
{
|
|
await _db.Delete<BaseSys_UserRole>().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, "绑定成功");
|
|
}
|
|
|
|
/// <summary>角色绑定菜单权限</summary>
|
|
[HttpPost("role/{roleId:long}/bindMenus")]
|
|
public async Task<ApiResult> BindMenus(long roleId, [FromBody] List<long> menuIds)
|
|
{
|
|
await _db.Delete<BaseSys_RoleMenu>().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, "授权成功");
|
|
}
|
|
|
|
/// <summary>重置用户密码</summary>
|
|
[HttpPost("user/resetPassword")]
|
|
public async Task<ApiResult> ResetPassword([FromBody] ResetPasswordDto dto)
|
|
{
|
|
return await _auth.ResetPasswordAsync(dto.UserId, dto.NewPassword ?? "123456");
|
|
}
|
|
}
|
|
|
|
/// <summary>创建用户参数</summary>
|
|
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; }
|
|
}
|
|
|
|
/// <summary>重置密码参数</summary>
|
|
public class ResetPasswordDto
|
|
{
|
|
public long UserId { get; set; }
|
|
public string? NewPassword { get; set; }
|
|
}
|