标准化统一返回对象

ApiResultFilter

1、code = 200 为成功,500 为失败,401 为未授权

2、message 返回一段字符串消息文本

3、data 返回你的业务数据

返回值统一格式如下:

{
    "code": 200,
    "message": "请求成功",
    "data": {}|[]
}

1、业务中返回业务消息

业务中返回业务消息

我们通过 MessageBox.Show("请输入账户名!"); 返回业务消息 , 更多的扩展代码请查看源代码。

/// <summary>
/// 检查账户 登录信息 并返回 token
/// </summary>
/// <param name="authUserFormDto"></param>
/// <returns>账户 id</returns>
public virtual async Task<Guid> LoginAsync(AuthUserFormDto authUserFormDto)
{
    if (string.IsNullOrWhiteSpace(authUserFormDto.UserName))
        MessageBox.Show("请输入账户名!");

    if (string.IsNullOrWhiteSpace(authUserFormDto.UserPassword))
        MessageBox.Show("请输入密码!");

    if (authUserFormDto.UserPassword.Length < 6)
    {
        MessageBox.Show("密码长度不能少于6位!");
    }

    if (authUserFormDto.UserPassword.Length > 20)
    {
        MessageBox.Show("密码长度不能大于20位!");
    }

    // if (string.IsNullOrWhiteSpace(code))
    //  MessageBox.Show("请输入验证码!");

    var sysUser = await _sysUserRepository.FindAsync(w => w.LoginName == authUserFormDto.UserName);
    if (sysUser == null)
    {
        MessageBox.Show("账户或者密码错误!");
    }

    if (sysUser.Password.Trim() != Tools.Md5Encrypt(authUserFormDto.UserPassword))
    {
        MessageBox.Show("账户或者密码错误!");
    }

    //string code = Tools.GetCookie("loginCode");
    //if (string.IsNullOrEmpty(code)) throw new MessageBox("验证码失效");
    //if (!code.ToLower().Equals(loginCode.ToLower())) throw new MessageBox("验证码不正确");

    //return _tokenService.CreateTokenByAccountId(sysUser.Id);
    return sysUser.Id;
}

2、基类 AdminControllerBase 中默认带有 [ApiResultFilter] 特性


namespace MiaoYu.Api.Admin.Controllers;

/// <summary>
/// 后台系统基础控制器
/// </summary>
[ApiResultFilter]
[Route(AdminControllerBase.RoutePrefix + "/[controller]/[action]")]
[Authorize]//是否授权 Authorize
[ApiController]
public class AdminControllerBase : ControllerBase
{
    /// <summary>
    /// 路由前缀
    /// </summary>
    public const string RoutePrefix = $"/api/v1/admin";

    /// <summary>
    /// 管理系统基础控制器构造函数
    /// </summary>
    public AdminControllerBase() { }
}

/// <summary>
/// 后台系统基础控制器
/// </summary>
/// <typeparam name="TService"></typeparam>
public class AdminControllerBase<TService> : AdminControllerBase where TService : class
{
    /// <summary>
    /// 默认服务
    /// </summary>
    protected readonly TService _defaultService;

    /// <summary>
    /// 管理系统基础控制器构造函数
    /// </summary>
    /// <param name="defaultService">默认服务</param>
    public AdminControllerBase(TService defaultService)
    {
        _defaultService = defaultService;
    }
}


Last Updated:
Contributors: hzy