添加项目文件。
This commit is contained in:
@ -0,0 +1,61 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.IService.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.WebApi.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 认证控制器
|
||||
/// </summary>
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class AuthController : BaseController
|
||||
{
|
||||
private readonly IAuthService _authService;
|
||||
|
||||
public AuthController(IAuthService authService)
|
||||
{
|
||||
_authService = authService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户登录
|
||||
/// </summary>
|
||||
/// <param name="input">登录信息</param>
|
||||
/// <returns>登录结果</returns>
|
||||
[AllowAnonymous]
|
||||
[HttpPost("login")]
|
||||
public async Task<BaseResponse<LoginOutput>> LoginAsync([FromBody] LoginInput input)
|
||||
{
|
||||
var result = await _authService.LoginAsync(input.Account, input.Password);
|
||||
return Success(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户注册
|
||||
/// </summary>
|
||||
/// <param name="input">注册信息</param>
|
||||
/// <returns>是否成功</returns>
|
||||
[AllowAnonymous]
|
||||
[HttpPost("register")]
|
||||
public async Task<BaseResponse<bool>> RegisterAsync([FromBody] RegisterInput input)
|
||||
{
|
||||
var result = await _authService.RegisterAsync(input);
|
||||
return Success(result, "注册成功");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 刷新令牌
|
||||
/// </summary>
|
||||
/// <param name="refreshToken">刷新令牌</param>
|
||||
/// <returns>新的登录结果</returns>
|
||||
[AllowAnonymous]
|
||||
[HttpPost("refreshToken")]
|
||||
public async Task<BaseResponse<LoginOutput>> RefreshTokenAsync([FromQuery] string refreshToken)
|
||||
{
|
||||
var result = await _authService.RefreshTokenAsync(refreshToken);
|
||||
return Success(result);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.WebApi.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 基础控制器
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public abstract class BaseController : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前用户ID
|
||||
/// </summary>
|
||||
/// <returns>用户ID</returns>
|
||||
protected long? GetCurrentUserId()
|
||||
{
|
||||
var userIdClaim = User.Claims.FirstOrDefault(c => c.Type == "userId");
|
||||
if (userIdClaim != null && long.TryParse(userIdClaim.Value, out var userId))
|
||||
{
|
||||
return userId;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前用户名
|
||||
/// </summary>
|
||||
/// <returns>用户名</returns>
|
||||
protected string? GetCurrentUserName()
|
||||
{
|
||||
return User.Claims.FirstOrDefault(c => c.Type == "userName")?.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 成功响应
|
||||
/// </summary>
|
||||
/// <typeparam name="T">数据类型</typeparam>
|
||||
/// <param name="data">数据</param>
|
||||
/// <param name="message">提示信息</param>
|
||||
/// <returns>统一响应对象</returns>
|
||||
protected BaseResponse<T> Success<T>(T data, string message = "操作成功")
|
||||
{
|
||||
return BaseResponse<T>.Success(data, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 失败响应
|
||||
/// </summary>
|
||||
/// <param name="message">提示信息</param>
|
||||
/// <param name="code">状态码</param>
|
||||
/// <returns>统一响应对象</returns>
|
||||
protected BaseResponse<object> Fail(string message, int code = 500)
|
||||
{
|
||||
return BaseResponse<object>.Fail(message, code);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.WebApi.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 健康检查控制器
|
||||
/// </summary>
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class HealthController : BaseController
|
||||
{
|
||||
/// <summary>
|
||||
/// 健康检查
|
||||
/// </summary>
|
||||
/// <returns>pong</returns>
|
||||
[HttpGet("ping")]
|
||||
public BaseResponse<string> Ping()
|
||||
{
|
||||
return Success("pong");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.IService.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.WebApi.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 微信小程序控制器
|
||||
/// </summary>
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[AllowAnonymous]
|
||||
public class WeChatController : BaseController
|
||||
{
|
||||
private readonly IWeChatMiniProgramService _weChatService;
|
||||
|
||||
public WeChatController(IWeChatMiniProgramService weChatService)
|
||||
{
|
||||
_weChatService = weChatService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 微信登录
|
||||
/// </summary>
|
||||
/// <param name="code">微信登录凭证</param>
|
||||
/// <returns>微信登录结果</returns>
|
||||
[HttpPost("login")]
|
||||
public async Task<BaseResponse<WeChatLoginOutput>> WeChatLoginAsync([FromQuery] string code)
|
||||
{
|
||||
var result = await _weChatService.WeChatLoginAsync(code);
|
||||
return Success(result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user