Commit 1efc5a30 authored by 吴春's avatar 吴春

提交

parent 23700a8f
......@@ -18,4 +18,6 @@ Mall/Mall.WebApi/bin/
Mall/Mall.WebApi/obj/
Mall/Mall.Module.User/obj/
Mall/Mall.Module.User/bin/
Mall/Mall.Common/bin/
Mall/Mall.Repository/bin/
Mall/.vs/Mall/DesignTimeBuild/.dtbcache.v2
using Mall.CacheManager.DataStatistic;
using Mall.Common.Plugin.Redis;
using System;
using System.Collections.Generic;
using System.Text;
namespace Mall.CacheManager.User
{
/// <summary>
/// redis缓存(只适用于数据统计)
/// </summary>
public class UserReidsCache
{
static RedisHelper redis = new RedisHelper(DataConstant.REDIS_DB2);
/// <summary>
/// 获取缓存时长
/// </summary>
/// <param name="JwtExpirTime"></param>
/// <returns></returns>
private static TimeSpan GetExpirTime(int JwtExpirTime)
{
DateTime dt = DateTime.Now;
DateTime dt2 = DateTime.Now;
TimeSpan ts = dt.AddSeconds(JwtExpirTime) - dt2;
return ts;
}
/// <summary>
/// 判断key是否存在
/// </summary>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static bool Exists(string cacheKey)
{
return redis.KeyExists(cacheKey);
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="Data"></param>
/// <param name="JwtExpirTime"></param>
public static void Set(string cacheKey, object Data, int JwtExpirTime)
{
try
{
TimeSpan ts = GetExpirTime(JwtExpirTime);
redis.StringSet(cacheKey, Data, ts);
}
catch (Exception)
{
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Mall.Model\Mall.Model.csproj" />
<ProjectReference Include="..\Mall.Repository\Mall.Repository.csproj" />
</ItemGroup>
</Project>
......@@ -2,8 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mall.Common;
using Mall.Common.API;
using Mall.Common.Plugin;
using Mall.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Mall.WebApi.Controllers
{
......
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using Mall.Common;
using Mall.Common.API;
using Mall.Common.Plugin;
using System.Net;
using System.Net.Http;
namespace Mall.WebApi.Filter
{
/// <summary>
/// Api异常过滤
/// </summary>
public class ApiExceptionFilterAttribute : ExceptionFilterAttribute
{
/// <summary>
/// 异常处理拦截
/// </summary>
/// <param name="context"></param>
public override void OnException(ExceptionContext context)
{
if (context.Exception.Message != "The operation was canceled." && !context.Exception.Message.Contains("已取消该操作"))
{
try
{
LogHelper.WriteInfo("拦截异常信息:" + context.Exception);
}
catch
{
}
}
//记录错误日志
if (context.HttpContext.Items[GlobalKey.UserPostInfo] != null)
{
LogHelper.Error($"InvokeServiceMethod请求地址:{Config.GetFilePath(context.HttpContext.Request.GetAbsoluteUri())},请求参数:{JsonConvert.SerializeObject(context.HttpContext.Items[GlobalKey.UserPostInfo].ToString())}", context.Exception);
}
else {
LogHelper.Error($"InvokeServiceMethod请求地址:{Config.GetFilePath(context.HttpContext.Request.GetAbsoluteUri())}", context.Exception);
}
var robj= new ApiResult()
{
resultCode = (int)ResultCode.AbnormalServer,
message = "服务器繁忙,请稍后再试",
data = null
}; ;
context.Result = new Microsoft.AspNetCore.Mvc.JsonResult(robj);
}
}
}
\ No newline at end of file
This diff is collapsed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.ServiceModel;
using System.Text;
using Microsoft.AspNetCore.Http;
namespace Mall.WebApi.Filter
{
/// <summary>
/// Http请求扩展
/// </summary>
public static class HttpRequestMessageExtensions
{
private const string HttpContext = "MS_HttpContext";
private const string RemoteEndpointMessage =
"System.ServiceModel.Channels.RemoteEndpointMessageProperty";
private const string OwinContext = "MS_OwinContext";
/// <summary>
/// 获取客户端Ip地址
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static string GetClientIpAddress(this HttpRequestMessage request)
{
// Web-hosting. Needs reference to System.Web.dll
if (request.Properties.ContainsKey(HttpContext))
{
dynamic ctx = request.Properties[HttpContext];
if (ctx != null)
{
return ctx.Request.UserHostAddress;
}
}
// Self-hosting. Needs reference to System.ServiceModel.dll.
if (request.Properties.ContainsKey(RemoteEndpointMessage))
{
dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
if (remoteEndpoint != null)
{
return remoteEndpoint.Address;
}
}
// Self-hosting using Owin. Needs reference to Microsoft.Owin.dll.
if (request.Properties.ContainsKey(OwinContext))
{
dynamic owinContext = request.Properties[OwinContext];
if (owinContext != null)
{
return owinContext.Request.RemoteIpAddress;
}
}
return null;
}
/// <summary>
/// 获取url
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static string GetAbsoluteUri(this HttpRequest request)
{
return new StringBuilder()
.Append(request.Scheme)
.Append("://")
.Append(request.Host)
.Append(request.PathBase)
.Append(request.Path)
.Append(request.QueryString)
.ToString();
}
}
}
\ No newline at end of file
......@@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JWT" Version="5.3.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
</ItemGroup>
......@@ -16,9 +17,5 @@
<ProjectReference Include="..\Mall.ThirdCore\Mall.ThirdCore.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Filter\" />
</ItemGroup>
</Project>
......@@ -29,6 +29,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mall.Model", "Mall.Model\Ma
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mall.Repository", "Mall.Repository\Mall.Repository.csproj", "{46E2983C-2CAF-4850-A6FC-804A7C425F76}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mall.Module.User", "Mall.Module.User\Mall.Module.User.csproj", "{E56423C0-5AC2-48D8-88BE-5435EF6ADB3F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -71,6 +73,10 @@ Global
{46E2983C-2CAF-4850-A6FC-804A7C425F76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46E2983C-2CAF-4850-A6FC-804A7C425F76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46E2983C-2CAF-4850-A6FC-804A7C425F76}.Release|Any CPU.Build.0 = Release|Any CPU
{E56423C0-5AC2-48D8-88BE-5435EF6ADB3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E56423C0-5AC2-48D8-88BE-5435EF6ADB3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E56423C0-5AC2-48D8-88BE-5435EF6ADB3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E56423C0-5AC2-48D8-88BE-5435EF6ADB3F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -84,6 +90,7 @@ Global
{3038BA76-834D-4FA5-ADA6-969839DDB1A1} = {2494B6F5-76EE-45EA-812E-CC815D6E4D2A}
{6FF822BD-BE47-483A-B29B-7F8A57CAF272} = {E239A6CD-DA5B-4E7A-B997-8D17C8E18EB6}
{46E2983C-2CAF-4850-A6FC-804A7C425F76} = {E239A6CD-DA5B-4E7A-B997-8D17C8E18EB6}
{E56423C0-5AC2-48D8-88BE-5435EF6ADB3F} = {034DEA5B-083C-46EC-9D3F-C8273C59C218}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {450F460D-A6AE-4FE3-948A-34E5FB8DBD7C}
......
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