北鸣对接T+畅捷通,看板API
yl
2023-11-20 f84de3e390ddb9f637342f16e758da4cbef5216e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using VueWebApi.Models;
 
namespace VueWebApi.Tools
{
    /// <summary>
    /// 渠道过滤器
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public class ChannelActionFilterAttributeApp : ActionFilterAttribute
    {
        ToMessage mes = new ToMessage();
        RedisHelper redis = new RedisHelper();
        /// <summary>
        /// 请求接口之前渠道过滤
        /// </summary>
        /// <param name="actionContext"></param>
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            try
            {
                var rediskey = HttpContext.Current.Request.Headers["rediskey"].ToString();
                var guid = HttpContext.Current.Request.Headers["guid"].ToString();
                base.OnActionExecuting(actionContext);
                //如果存在
                if (redis.Get<User>(rediskey, 1).userid.ToString() != "")
                {
                    //获取redis中当前用户信息
                    User r_loginUser = redis.Get<User>(rediskey, 1);
                    //如果session中的用户guid和redis中的用户guid匹配
                    if (r_loginUser.guid.Equals(guid))
                    {
                        //重置过期时间
                        redis.KeyExpire(rediskey, redis.secondsTimeOut);
                    }
                    else
                    {
                        mes.code = "303";
                        mes.Message = "登录已超时,请重新登录!";
                        actionContext.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mes), Encoding.GetEncoding("UTF-8"), "application/json") };
                    }
                }
                else //如果redis登录用户中不存在当前用户
                {
                    mes.code = "303";
                    mes.Message = "登录已超时,请重新登录!";
                    actionContext.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mes), Encoding.GetEncoding("UTF-8"), "application/json") };
                }
            }
            catch (Exception)
            {
                mes.code = "303";
                mes.Message = "登录已超时,请重新登录!";
                actionContext.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mes), Encoding.GetEncoding("UTF-8"), "application/json") };
            }
 
        }
    }
}