Commit fa300548 authored by 黄奎's avatar 黄奎

页面修改

parent 4676ab69
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using VTX.FW.Helper;
using VTX.FW.VtController;
namespace EduSpider.WebApi.Controllers.Base
......@@ -9,6 +11,24 @@ namespace EduSpider.WebApi.Controllers.Base
[ApiController]
public class BaseController : ApiBaseController
{
/// <summary>
/// 获取用户编号
/// </summary>
public int BaseUserId
{
get
{
int UserId = 0;
if (!string.IsNullOrEmpty(base.RequestParm.Token))
{
var jObj = VTX.FW.Helper.JwtHelper.AnalysisToken(base.RequestParm.Token);
if (jObj != null)
{
UserId = jObj.GetInt("BaseUserId");
}
}
return UserId;
}
}
}
}
using EduSpider.IServices;
using EduSpider.WebApi.Controllers.Base;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using VTX.FW.Api;
using VTX.FW.Attr;
using VTX.FW.Helper;
namespace EduSpider.WebApi.Controllers
{
/// <summary>
/// 课程控制器
/// 学员相关信息控制器
/// </summary>
public class CourseController : BaseController
public class StuController : BaseController
{
/// <summary>
/// 课程仓储接口
......@@ -20,19 +21,37 @@ namespace EduSpider.WebApi.Controllers
public ICourseService CourseService { get; set; }
/// <summary>
/// 获取课程列表
/// 获取学员课程列表
/// </summary>
/// <returns></returns>
[HttpPost]
[HttpGet]
public ApiResult GetCoursePage()
public ApiResult GetStuCourse()
{
var query = new Model.Query.CourseQuery() { StuIds = "1976394" };
var query = new Model.Query.CourseQuery()
{
StuIds = base.BaseUserId.ToString()
};
var list = CourseService.GetStuCourseList(query);
query.StuIds = "23067080";
return ApiResult.Success(data: list);
}
/// <summary>
/// 获取学员作业和考试信息
/// </summary>
/// <returns></returns>
[HttpPost]
[HttpGet]
public ApiResult GetStuWork()
{
var query = new Model.Query.CourseQuery()
{
StuIds = base.BaseUserId.ToString(),
courseId = base.ReqParameters.GetInt("courseId")
};
var homeworkExamList = CourseService.GetStuWorkAndExamList(query);
return ApiResult.Success(data: homeworkExamList, message: "成功!");
return ApiResult.Success(data: homeworkExamList);
}
}
}
using JWT.Exceptions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using VTX.FW.Api;
using VTX.FW.Filter;
using VTX.FW.Helper;
namespace Edu.WebApi.Filter
{
/// <summary>
/// Api过滤属性
/// </summary>
public class ActionFilterAttribute : ApiFilterAttribute
{
/// <summary>
/// token验证
/// </summary>
/// <param name="actionContext"></param>
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
base.OnActionExecuting(actionContext);
HttpRequest request = actionContext.HttpContext.Request;
request.EnableBuffering();
RequestParm requestParm = new RequestParm
{
Token = request.Headers["Authorization"]
};
bool isCheckToken = true;
var endpoint = actionContext.HttpContext.Features.Get<IEndpointFeature>()?.Endpoint;
if (endpoint != null && endpoint.Metadata.GetMetadata<AllowAnonymousAttribute>() != null)
{
isCheckToken = false;
}
string token = requestParm.Token.Replace("Bearer","").Trim();
//Token校验
if (isCheckToken)
{
JWTValidat(actionContext, token);
}
}
/// <summary>
/// token校验
/// </summary>
/// <param name="actionContext"></param>
/// <param name="token"></param>
private static void JWTValidat(ActionExecutingContext actionContext, string token)
{
if (!string.IsNullOrEmpty(token))
{
//解析token,校验是否失效
try
{
JObject jwtJson = VTX.FW.Helper.JwtHelper.AnalysisToken(token);
var BaseUserId = jwtJson.GetInt("BaseUserId");
if (BaseUserId > 0)
{
var cacheUser = EduSpider.Cache.User.UserReidsCache.GetUserLoginInfo(BaseUserId);
if ((cacheUser.Token != token || string.IsNullOrWhiteSpace(cacheUser.Token)))
{
actionContext.Result = new Microsoft.AspNetCore.Mvc.JsonResult(
new ApiResult
{
Code = (int)ResultCode.TokenOverdue,
Message = "用户凭证失效,请重新登录0",
Data = null
});
}
}
}
catch (SignatureVerificationException sve)
{
string message = sve.Message;
actionContext.Result = new Microsoft.AspNetCore.Mvc.JsonResult(
new ApiResult
{
Code = (int)ResultCode.TokenOverdue,
Message = "用户凭证失效,请重新登录1",
Data = null
});
}
catch (ArgumentException ae)
{
string message = ae.Message;
actionContext.Result = new Microsoft.AspNetCore.Mvc.JsonResult(
new ApiResult
{
Code = (int)ResultCode.TokenIllegal,
Message = "用户凭证失效,请重新登录2",
Data = ""
});
}
catch (Exception ex)
{
VTX.FW.Helper.LogHelper.WriteError("JWTValidat3", ex: ex);
actionContext.Result = new Microsoft.AspNetCore.Mvc.JsonResult(
HttpStatusCode.OK,
new ApiResult
{
Code = (int)ResultCode.TokenIllegal,
Message = "用户凭证失效,请重新登录3",
Data = ""
});
}
}
else
{
actionContext.Result = new Microsoft.AspNetCore.Mvc.JsonResult(
new ApiResult
{
Code = (int)ResultCode.TokenIllegal,
Message = "用户凭证为空,请重新登录4",
Data = ""
});
}
}
}
}
\ No newline at end of file
using Autofac;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Controllers;
......@@ -43,7 +44,7 @@ namespace EduSpider.WebApi
services.AddMvc(options =>
{
options.Filters.Add<ApiExceptionFilterAttribute>();
options.Filters.Add<ApiFilterAttribute>();
options.Filters.Add<ActionFilterAttribute>();
});
//处理josn格式
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment