CSharp.Template/YuanXuan.IM.Api/Controllers/BaseApiController.cs

52 lines
1.6 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YuanXuan.IM.Common.Dtos.LoginMobile;
using YuanXuan.IM.Common.Response;
namespace YuanXuan.IM.Api.Controllers
{
/// <summary>
/// 自定义基础Api控制器
/// </summary>
[Authorize]
[ApiController]
public class BaseApiController : ControllerBase
{
/// <summary>
/// Api版本前缀
/// </summary>
protected const string RoutePrefix = "api/v{version:apiVersion}";
public MobileTokenInfo ZereUser => GetTokenInfo();
private MobileTokenInfo GetTokenInfo()
{
return new MobileTokenInfo
{
UserName = HttpContext?.User?.FindFirst("UserName")?.Value,
UserId = HttpContext?.User?.FindFirst("UserId")?.Value,
Jwt_id = HttpContext?.User?.FindFirst("Jwt_id")?.Value,
};
}
/// <summary>
/// 返回成功结果
/// </summary>
/// <param name="data">数据</param>
/// <returns></returns>
protected IActionResult Success(object data = null)
{
return Ok(new BaseResponse<object>(200, "success", data));
}
/// <summary>
/// 返回失败结果
/// </summary>
/// <param name="message">错误信息</param>
/// <returns></returns>
protected IActionResult Fail(string message)
{
return BadRequest(new BaseResponse<object>(400, message));
}
}
}