205 lines
7.0 KiB
C#
205 lines
7.0 KiB
C#
|
|
//===================================================
|
|||
|
|
// License: Apache-2.0
|
|||
|
|
// Contributors: yiyungent@gmail.com
|
|||
|
|
// Project: https://moeci.com/PluginCore
|
|||
|
|
// GitHub: https://github.com/yiyungent/PluginCore
|
|||
|
|
//===================================================
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
using PluginCore.AspNetCore.Authorization;
|
|||
|
|
using PluginCore.Config;
|
|||
|
|
using PluginCore.AspNetCore.RequestModel.User;
|
|||
|
|
using PluginCore.AspNetCore.ResponseModel;
|
|||
|
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
using Admin.NET.Core.Service;
|
|||
|
|
using Admin.NET.Core;
|
|||
|
|
using Furion.DataEncryption;
|
|||
|
|
using Furion.FriendlyException;
|
|||
|
|
using Lazy.Captcha.Core;
|
|||
|
|
using System.ComponentModel.DataAnnotations;
|
|||
|
|
using System.ComponentModel;
|
|||
|
|
|
|||
|
|
namespace PluginCore.AspNetCore.Controllers
|
|||
|
|
{
|
|||
|
|
[Route("api/plugincore/admin/[controller]/[action]")]
|
|||
|
|
[ApiController]
|
|||
|
|
[NonUnify]
|
|||
|
|
public class UserController : ControllerBase
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
|
|||
|
|
public string RemoteFronted
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return PluginCore.Config.PluginCoreConfigFactory.Create().RemoteFrontend;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private readonly IUserManager _userManager;
|
|||
|
|
private readonly SqlSugarRepository<SysUser> _sysUserRep;
|
|||
|
|
private readonly SysOrgService _sysOrgService;
|
|||
|
|
private readonly SysUserExtOrgService _sysUserExtOrgService;
|
|||
|
|
private readonly SysUserRoleService _sysUserRoleService;
|
|||
|
|
private readonly SysConfigService _sysConfigService;
|
|||
|
|
|
|||
|
|
public UserController(IUserManager userManager,
|
|||
|
|
SqlSugarRepository<SysUser> sysUserRep,
|
|||
|
|
SysOrgService sysOrgService,
|
|||
|
|
SysUserExtOrgService sysUserExtOrgService,
|
|||
|
|
SysUserRoleService sysUserRoleService,
|
|||
|
|
SysConfigService sysConfigService)
|
|||
|
|
{
|
|||
|
|
_userManager = userManager;
|
|||
|
|
_sysUserRep = sysUserRep;
|
|||
|
|
_sysOrgService = sysOrgService;
|
|||
|
|
_sysUserExtOrgService = sysUserExtOrgService;
|
|||
|
|
_sysUserRoleService = sysUserRoleService;
|
|||
|
|
_sysConfigService = sysConfigService;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 登录系统
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="input"></param>
|
|||
|
|
/// <remarks>用户名/密码:superadmin/123456</remarks>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
[AllowAnonymous]
|
|||
|
|
[HttpGet, HttpPost]
|
|||
|
|
[DisplayName("登录系统")]
|
|||
|
|
public async Task<ActionResult<BaseResponseModel>> Login([FromBody] LoginRequestModel input)
|
|||
|
|
{
|
|||
|
|
BaseResponseModel responseModel = new BaseResponseModel();
|
|||
|
|
|
|||
|
|
|
|||
|
|
// 账号是否存在
|
|||
|
|
var user = await _sysUserRep.AsQueryable().Includes(t => t.SysOrg).Filter(null, true).FirstAsync(u => u.Account.Equals(input.UserName));
|
|||
|
|
_ = user ?? throw Oops.Oh(ErrorCodeEnum.D0009);
|
|||
|
|
|
|||
|
|
// 账号是否被冻结
|
|||
|
|
if (user.Status == StatusEnum.Disable)
|
|||
|
|
throw Oops.Oh(ErrorCodeEnum.D1017);
|
|||
|
|
|
|||
|
|
// 租户是否被禁用
|
|||
|
|
var tenant = await _sysUserRep.ChangeRepository<SqlSugarRepository<SysTenant>>().GetFirstAsync(u => u.Id == user.TenantId);
|
|||
|
|
if (tenant != null && tenant.Status == StatusEnum.Disable)
|
|||
|
|
throw Oops.Oh(ErrorCodeEnum.Z1003);
|
|||
|
|
|
|||
|
|
// 密码是否正确
|
|||
|
|
if (CryptogramUtil.CryptoType == CryptogramEnum.MD5.ToString())
|
|||
|
|
{
|
|||
|
|
if (user.Password != MD5Encryption.Encrypt(input.Password))
|
|||
|
|
throw Oops.Oh(ErrorCodeEnum.D1000);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
if (CryptogramUtil.Decrypt(user.Password) != input.Password)
|
|||
|
|
throw Oops.Oh(ErrorCodeEnum.D1000);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
var tokenExpire = await _sysConfigService.GetTokenExpire();
|
|||
|
|
var refreshTokenExpire = await _sysConfigService.GetRefreshTokenExpire();
|
|||
|
|
|
|||
|
|
// 生成Token令牌
|
|||
|
|
var accessToken = JWTEncryption.Encrypt(new Dictionary<string, object>
|
|||
|
|
{
|
|||
|
|
{ ClaimConst.UserId, user.Id },
|
|||
|
|
{ ClaimConst.TenantId, user.TenantId },
|
|||
|
|
{ ClaimConst.Account, user.Account },
|
|||
|
|
{ ClaimConst.RealName, user.RealName },
|
|||
|
|
{ ClaimConst.AccountType, user.AccountType },
|
|||
|
|
{ ClaimConst.OrgId, user.OrgId },
|
|||
|
|
{ ClaimConst.OrgName, user.SysOrg?.Name },
|
|||
|
|
{ ClaimConst.OrgType, user.SysOrg?.OrgType },
|
|||
|
|
}, tokenExpire);
|
|||
|
|
|
|||
|
|
// 生成刷新Token令牌
|
|||
|
|
var refreshToken = JWTEncryption.GenerateRefreshToken(accessToken, refreshTokenExpire);
|
|||
|
|
|
|||
|
|
responseModel.Code = 1;
|
|||
|
|
responseModel.Message = "登录成功";
|
|||
|
|
responseModel.Data = new
|
|||
|
|
{
|
|||
|
|
token = accessToken,
|
|||
|
|
userName = user.NickName,
|
|||
|
|
RefreshToken = refreshToken
|
|||
|
|
};
|
|||
|
|
return await Task.FromResult(responseModel);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
[HttpGet, HttpPost]
|
|||
|
|
public async Task<ActionResult<BaseResponseModel>> Logout()
|
|||
|
|
{
|
|||
|
|
BaseResponseModel responseModel = new BaseResponseModel()
|
|||
|
|
{
|
|||
|
|
Code = 1,
|
|||
|
|
Message = "退出登录成功"
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
return await Task.FromResult(responseModel);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
[HttpGet, HttpPost]
|
|||
|
|
public async Task<ActionResult<BaseResponseModel>> Info()
|
|||
|
|
{
|
|||
|
|
BaseResponseModel responseModel = new BaseResponseModel();
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
string adminUserName = PluginCoreConfigFactory.Create().Admin.UserName;
|
|||
|
|
|
|||
|
|
responseModel.Code = 1;
|
|||
|
|
responseModel.Message = "成功";
|
|||
|
|
responseModel.Data = new
|
|||
|
|
{
|
|||
|
|
name = adminUserName,
|
|||
|
|
//avatar = this.RemoteFronted + "/images/avatar.gif"
|
|||
|
|
avatar = ""
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
responseModel.Code = -1;
|
|||
|
|
responseModel.Message = "失败: " + ex.Message;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return await Task.FromResult(responseModel);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
[HttpGet, HttpPost]
|
|||
|
|
public async Task<ActionResult<BaseResponseModel>> Update([FromBody] UpdateRequestModel requestModel)
|
|||
|
|
{
|
|||
|
|
BaseResponseModel responseModel = new BaseResponseModel();
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
PluginCoreConfig pluginCoreConfig = PluginCoreConfigFactory.Create();
|
|||
|
|
pluginCoreConfig.Admin.UserName = requestModel.UserName;
|
|||
|
|
pluginCoreConfig.Admin.Password = requestModel.Password;
|
|||
|
|
PluginCoreConfigFactory.Save(pluginCoreConfig);
|
|||
|
|
|
|||
|
|
responseModel.Code = 1;
|
|||
|
|
responseModel.Message = "修改成功, 需要重新登录";
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
responseModel.Code = -1;
|
|||
|
|
responseModel.Message = "失败: " + ex.Message;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return await Task.FromResult(responseModel);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|