CompetitionAPI_dotnet/CompetitionAPI/Controllers/unity/RefreshTokenController.cs

74 lines
2.4 KiB
C#

using Competition.Common.Util;
using CompetitionAPI.api.unity;
using CompetitionAPI.Util;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json.Linq;
using NPOI.POIFS.Crypt;
using System.IdentityModel.Tokens.Jwt;
using System.Text;
namespace CompetitionAPI.Controllers.unity
{
[Route("unity/[controller]")]
[ApiController]
public class RefreshTokenController : Controller
{
Competition.Mysql.BLL.admin_user bll = new Competition.Mysql.BLL.admin_user();
private readonly TokenService _tokenService;
public RefreshTokenController(TokenService tokenService)
{
_tokenService = tokenService;
}
/// <summary>
/// 刷新token接口
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
[HttpPost]
[APIFilter]
public JsonResult Index([FromBody] RefreshTokenRequest req)
{
try
{
if (req == null)
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "请求参数不能为空"));
}
if (string.IsNullOrEmpty(req.Token))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "token不能为空"));
}
var user_id = "";
var token = "";
var is_result = _tokenService.GenerateRefreshToken(req.Token, out token, out user_id);
if (is_result)
{
if (bll.UpdateToken(user_id, token))
{
return Json(Tool.GetJsonWithCode(APICode.Success, new { token = token, effective_duration = 3 * 3600 }));
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, new { token = "", effective_duration = 0 }));
}
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, new { token = token, effective_duration = 0 }));
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace);
return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。"));
}
}
}
}