From 57c0156fe021f9c690993e91da5dd280187f4fad Mon Sep 17 00:00:00 2001
From: liaoxujun@qq.com <liaoxujun@qq.com>
Date: 星期二, 01 八月 2023 17:15:03 +0800
Subject: [PATCH] Merge branch 'master' of http://47.108.235.38:8080/r/DocumentService

---
 DocumentServiceAPI.Application/UserAndLogin/Services/TokenService.cs |   96 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 96 insertions(+), 0 deletions(-)

diff --git a/DocumentServiceAPI.Application/UserAndLogin/Services/TokenService.cs b/DocumentServiceAPI.Application/UserAndLogin/Services/TokenService.cs
new file mode 100644
index 0000000..f7e4355
--- /dev/null
+++ b/DocumentServiceAPI.Application/UserAndLogin/Services/TokenService.cs
@@ -0,0 +1,96 @@
+锘縰sing DocumentServiceAPI.Model;
+using DocumentServiceAPI.Services.IService;
+using Furion.Authorization;
+using Furion.DistributedIDGenerator;
+using Furion.JsonSerialization;
+using NetTaste;
+using SqlSugar.Extensions;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Security.Claims;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DocumentServiceAPI.Application.UserAndLogin.Services
+{
+    /// <summary>
+    /// token鏈嶅姟绫�
+    /// </summary>
+    public  class TokenService: IScoped
+    {
+        /// <summary>
+        /// Token鏄暐
+        /// </summary>
+        static string TokenKeys = "TokenKeys:";
+        private ISqlSugarClient _db;
+        private UserService _UserService;
+        private IRedisCacheService _RedisCase;
+        public TokenService(ISqlSugarClient db, UserService  UserInfoService, IRedisCacheService RedisCase ) {
+            _db = db;
+            _UserService = UserInfoService;
+            _RedisCase = RedisCase;
+        }
+        /// <summary>
+        /// 鏂板缓涓�涓猅Oken 
+        /// </summary>
+        /// <returns></returns>
+        public string  CreateToken(JwtInfo jwt)
+        {
+            string TokenKey=TokenKeys+ getTokenKey(jwt);
+            IDictionary<string, object> propertyDictionary = new Dictionary<string, object>();
+
+            PropertyInfo[] properties = jwt.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
+
+            foreach (PropertyInfo property in properties)
+            {
+                string propertyName = property.Name;
+                object propertyValue = property.GetValue(jwt);
+
+                propertyDictionary.Add(propertyName.ToLower(), propertyValue);
+            }
+            var ID = IDGen.NextID();
+             jwt.JID = ID;  
+         var token=   JWTEncryption.Encrypt(propertyDictionary, App.GetConfig<JWTSettingsOptions>("JWTSettings").ExpiredTime ?? 3600);          
+            if(jwt.LogInSource==LogInFrom.PC)
+            _RedisCase.Add<string>(TokenKey, ID.ToString(), expireSeconds: (int )(App.GetConfig<JWTSettingsOptions>("JWTSettings").ExpiredTime??3600));
+            return token;
+        }
+
+
+
+
+        /// <summary>
+        /// 鏍¢獙jwt淇℃伅鏄惁鏈夋晥锛屽垽鍐冲崟绔櫥褰曢獙璇�
+        /// </summary>
+        /// <returns></returns>
+         public bool  CheckToken()
+        {
+           
+       var Jwtinfo=     _UserService.GetJwtInfo();
+
+        var Key=    getTokenKey(Jwtinfo);
+            
+        string T  = _RedisCase.Get<string>(Key);
+            if (T == null)
+                return true;
+            if (T != Jwtinfo.JID.ToString())
+                return false;
+            return true;
+              
+
+        }
+        /// <summary>
+        /// 鑾峰彇TokenKey
+        /// </summary>
+        /// <param name="jwt"></param>
+        /// <returns></returns>
+        private string getTokenKey(JwtInfo jwt)
+        {
+            string TokenKey = TokenKeys + jwt.EID?.ToString() + jwt.UID?.ToString()  + jwt.TID?.ToString();
+            return TokenKey;
+        }
+    }
+   
+}

--
Gitblit v1.9.1