CrazyIter_Bin il y a 1 an
Parent
commit
c25fecb860

+ 29 - 0
TEAMModelOS.SDK/Extension/JwtAuthExtension.cs

@@ -254,6 +254,35 @@ namespace TEAMModelOS.SDK.Extension
                 return false;
             }
         }
+        /// <summary>
+        /// 刷新token,不验证过期时间
+        /// </summary>
+        /// <param name="token"></param>
+        /// <param name="salt"></param>
+        /// <returns></returns>
+        public static bool ValidateAuthTokenRefresh(string token, string salt)
+        {
+            try
+            {
+                var handler = new JwtSecurityTokenHandler();
+                var validationParameters = new TokenValidationParameters
+                {
+                    RequireExpirationTime = false,
+                    ValidateIssuer = false,
+                    ValidateAudience = false,
+                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(salt)),
+                    ValidateLifetime = true,
+                    ClockSkew = TimeSpan.Zero
+                };
+                ClaimsPrincipal principal = handler.ValidateToken(token, validationParameters, out SecurityToken securityToken);
+                return true;
+            }
+            catch (Exception)
+            {
+                //Trace.WriteLine(ex.Message);
+                return false;
+            }
+        }
 
         /// <summary>
         /// 第三方登录后的id_token

+ 20 - 4
TEAMModelOS.SDK/Models/Cosmos/Common/Activity.cs

@@ -314,9 +314,7 @@ namespace TEAMModelOS.SDK.Models
     }
     public class ContestSign
     {
-        /// <summary>
-        ///  "name",   "phone",   "period",   "subject",  "school", "contestType",  "job"
-        /// </summary>
+       
         public List<string> field { get; set; } = new List<string>();
         /// <summary>
         /// 报名类型,0个人,1团队
@@ -328,8 +326,23 @@ namespace TEAMModelOS.SDK.Models
         /// //报名人数限制
         /// </summary>
         public int limit { get; set; }
-    }
 
+        public List<ContestSignField> fields { get; set; }= new List<ContestSignField>();
+    }
+    public class ContestSignField {
+        /// <summary>
+        ///  "name",   "phone",   "period",   "subject",  "school", "contestType",  "job"
+        /// </summary>
+        public string field { get; set; }
+        /// <summary>
+        /// text select radio checkbox  password  file  textarea
+        /// </summary>
+        public string type { get; set; }
+        /// <summary>
+        /// 单选,复选,下拉列表
+        /// </summary>
+        public List<string> item { get; set; }= new List<string> ();
+    }
     /// <summary>
     /// 在线培训模块的数据结构
     /// </summary>
@@ -490,8 +503,11 @@ namespace TEAMModelOS.SDK.Models
     }
     public class Expert { 
         public string id { get; set; }
+        public string iname { get; set; }
         public string name { get; set; }
         public string mobile { get; set; }
+        public string email { get; set; }
+        public string tmdid { get; set; }
         public string picture { get; set; }
         /// <summary>
         /// 0 未确认,1 已确认  确认状态

+ 188 - 11
TEAMModelOS/Controllers/Common/ActivityController.cs

@@ -37,6 +37,8 @@ using Azure.Storage.Sas;
 using DocumentFormat.OpenXml.Bibliography;
 using System.Runtime.Intrinsics.X86;
 using Microsoft.IdentityModel.Tokens;
+using TEAMModelOS.SDK.Models.Dtos;
+using System.Net.Http;
 
 namespace TEAMModelOS.Controllers
 {
@@ -987,6 +989,174 @@ namespace TEAMModelOS.Controllers
                             }
                             return Ok(new { inviteTeachers });
                         }
+                    //导入评审专家
+                    case bool when $"{grant_type}".Equals("invited-teachers", StringComparison.OrdinalIgnoreCase):
+                        {
+                            if (!request.TryGetProperty("activityId", out JsonElement _activityId)) return BadRequest();
+                            if (!request.TryGetProperty("experts", out JsonElement _experts) || !_experts.ValueKind.Equals(JsonValueKind.Array)) { return BadRequest(); }
+                            List<Expert> experts = _experts.ToObject<List<Expert>>();
+                            var tmdids = experts.Where(x => !string.IsNullOrWhiteSpace(x.tmdid)).Select(z => z.tmdid);
+                            var phones = experts.Where(x => !string.IsNullOrWhiteSpace(x.mobile)).Select(z => z.mobile);
+                            var emails = experts.Where(x => !string.IsNullOrWhiteSpace(x.email)).Select(z => z.email);
+                            List<string> keys = new List<string>();
+                            if (tmdids.Any())
+                            {
+                                keys.AddRange(tmdids);
+                            }
+                            if (phones.Any())
+                            {
+                                keys.AddRange(phones);
+                            }
+                            if (emails.Any())
+                            {
+                                keys.AddRange(emails);
+                            }
+                            ActivityExpert activityExpert = null;
+                            experts.ForEach(x => { x.status = 0;  x.iname = x.name; x.name = null; });
+                            List<CoreUser> coreUsers = new List<CoreUser>();
+                            if (keys.Any())
+                            {
+                                try
+                                {
+                                    var content = new StringContent(keys.ToJsonString(), Encoding.UTF8, "application/json");
+                                    string json = await _coreAPIHttpService.GetUserInfos(content);
+                                    if (!string.IsNullOrWhiteSpace(json))
+                                    {
+                                        coreUsers = json.ToObject<List<CoreUser>>();
+                                    }
+                                }
+                                catch (Exception ex)
+                                {
+                                    await _dingDing.SendBotMsg($"{_option.Location},导入名单时,查验key信息错误{ex.Message}\n{ex.StackTrace}\n\n{keys.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
+                                }
+                            }
+                            if (coreUsers.IsNotEmpty())
+                            {
+                                foreach (var t in experts)
+                                {
+                                    if (!string.IsNullOrWhiteSpace(t.tmdid))
+                                    {
+                                        CoreUser coreUser = coreUsers.Find(x => x.id.Equals(t.tmdid));
+                                        if (coreUser != null)
+                                        {
+                                            t.id = coreUser.id;
+                                            t.name = coreUser.name;
+                                            t.picture = coreUser.picture;
+                                            t.tmdid = coreUser.id;
+                                            if (!string.IsNullOrWhiteSpace(coreUser.mobile))
+                                            {
+                                                t.mobile = coreUser.mobile;
+                                            }
+                                            if (!string.IsNullOrWhiteSpace(coreUser.mail))
+                                            {
+                                                t.email = coreUser.mail;
+                                            }
+                                        }
+                                    }
+                                    if (string.IsNullOrWhiteSpace(t.id))
+                                    {
+                                        if (!string.IsNullOrWhiteSpace(t.mobile))
+                                        {
+                                            CoreUser coreUser = coreUsers.Find(x => !string.IsNullOrWhiteSpace(x.mobile) && x.mobile.Equals(t.mobile));
+                                            if (coreUser != null)
+                                            {
+                                                t.id = coreUser.id;
+                                                t.name = coreUser.name;
+                                                t.picture = coreUser.picture;
+                                                t.tmdid = coreUser.id;
+                                                if (!string.IsNullOrWhiteSpace(coreUser.mobile))
+                                                {
+                                                    t.mobile = coreUser.mobile;
+                                                }
+                                                if (!string.IsNullOrWhiteSpace(coreUser.mail))
+                                                {
+                                                    t.email = coreUser.mail;
+                                                }
+                                            }
+                                        }
+                                    }
+                                    if (string.IsNullOrWhiteSpace(t.id))
+                                    {
+                                        if (!string.IsNullOrWhiteSpace(t.email))
+                                        {
+                                            CoreUser coreUser = coreUsers.Find(x => !string.IsNullOrWhiteSpace(x.mail) && x.mail.Equals(t.email));
+                                            if (coreUser != null)
+                                            {
+                                                t.id = coreUser.id;
+                                                t.name = coreUser.name;
+                                                t.picture = coreUser.picture;
+                                                t.tmdid = coreUser.id;
+                                                if (!string.IsNullOrWhiteSpace(coreUser.mobile))
+                                                {
+                                                    t.mobile = coreUser.mobile;
+                                                }
+                                                if (!string.IsNullOrWhiteSpace(coreUser.mail))
+                                                {
+                                                    t.email = coreUser.mail;
+                                                }
+                                            }
+                                        }
+                                    }
+
+                                }
+                                Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).ReadItemStreamAsync($"{_activityId}", new PartitionKey("ActivityExpert"));
+                                if (response.Status == 200)
+                                {
+                                     activityExpert = JsonDocument.Parse(response.Content).RootElement.Deserialize<ActivityExpert>();
+                                    experts.ForEach(x =>
+                                    {
+                                        Expert tch = null;
+                                        if (string.IsNullOrWhiteSpace(x.id))
+                                        {
+                                            tch = activityExpert.experts.Find(t => !string.IsNullOrWhiteSpace(t.iname) && t.iname.Equals(x.iname));
+                                        }
+                                        else
+                                        {
+                                            tch = activityExpert.experts.Find(t => !string.IsNullOrWhiteSpace(x.id) && !string.IsNullOrWhiteSpace(t.id) && t.id.Equals(x.id));
+                                        }
+                                        if (tch != null)
+                                        {
+                                            tch.status = x.status;
+                                            tch.name = x.name;
+                                            tch.iname = x.iname;
+                                            tch.picture = x.picture;
+                                            tch.mobile = x.mobile;
+                                            tch.tmdid = x.tmdid;
+                                            tch.email = x.email;
+                                            tch.id = x.id;
+                                        }
+                                        else
+                                        {
+                                            activityExpert.experts.Add(new Expert
+                                            {
+                                                status = x.status,
+                                                name = x.name,
+                                                iname = x.iname,
+                                                picture = x.picture,
+                                                mobile = x.mobile,
+                                                tmdid = x.tmdid,
+                                                email = x.email,
+                                                id = x.id,
+                                                title = x.title,
+                                                subjects = x.subjects,
+                                            });
+                                        }
+                                    });
+                                    await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).UpsertItemAsync(activityExpert, new PartitionKey("ActivityExpert"));
+                                }
+                                else
+                                {
+                                      activityExpert = new ActivityExpert { id = $"{_activityId}", code = "ActivityExpert", pk = "ActivityExpert", experts = experts };
+                                    await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).UpsertItemAsync(activityExpert, new PartitionKey("ActivityExpert"));
+                                }
+                            }
+                            else {
+                                  activityExpert = new ActivityExpert { id = $"{_activityId}", code = "ActivityExpert", pk = "ActivityExpert", experts = experts };
+                                await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).UpsertItemAsync(activityExpert, new PartitionKey("ActivityExpert"));
+                            }
+                            return Ok(new { activityExpert});
+                        }
+
                 }
             } catch (Exception ex)
             {
@@ -1032,19 +1202,26 @@ namespace TEAMModelOS.Controllers
                     
                      }
             }
-            if (tmdid == null  && !string.IsNullOrWhiteSpace($"{_token}")) {
+            if (tmdid == null  && !string.IsNullOrWhiteSpace($"{_token}"))
+            {
                 var jwt = new JwtSecurityToken(_token.GetString());
-                tmdid = jwt.Payload.Sub;
-              
-                if (HttpContext.Request.Headers.TryGetValue("lang", out var _lang))
+                if (JwtAuthExtension.ValidateAuthTokenRefresh(_token.GetString(), _option.JwtSecretKey))
                 {
-                    head_lang = $"{_lang}";
+                    tmdid = jwt.Payload.Sub;
+
+                    if (HttpContext.Request.Headers.TryGetValue("lang", out var _lang))
+                    {
+                        head_lang = $"{_lang}";
+                    }
+                    jwt.Payload.TryGetValue("name", out name);
+                    jwt.Payload.TryGetValue("picture", out picture);
+                    jwt.Payload.TryGetValue("lang", out object _jwtlang);
+                    head_lang = !string.IsNullOrWhiteSpace($"{_jwtlang}") ? $"{_jwtlang}" : head_lang;
                 }
-                jwt.Payload.TryGetValue("name", out   name);
-                jwt.Payload.TryGetValue("picture", out   picture);
-                jwt.Payload.TryGetValue("lang", out object _jwtlang);
-                head_lang = !string.IsNullOrWhiteSpace($"{_jwtlang}") ? $"{_jwtlang}" : head_lang;
+                else { return Ok(new { code = 2, msg = "Token验证失败" }); }
+
             }
+            else { return Ok(new { code = 3, msg = "凭证验证失败" }); }
             teacherInfo = await TeacherService.TeacherInfoLite(_azureCosmos,  $"{name}", $"{picture}", tmdid, _azureStorage, _option, _azureRedis, ip, _httpTrigger, head_lang);
             string sql = $"select value c from c where c.route='{_route}'";
             var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).GetList<ActivityWebsite>(sql, "ActivityWebsite");
@@ -1066,7 +1243,7 @@ namespace TEAMModelOS.Controllers
             CoreUser coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", teacherInfo.teacher.id } }, _option.Location, _configuration);
             string sqlExpert = $"select value c from c join e in c.experts   where e.id='{teacherInfo.teacher.id}'";
             if (!string.IsNullOrWhiteSpace(coreUser.mobile)) {
-                sqlExpert=$"  or  e.mobile='{coreUser.mobile}' ";
+                sqlExpert=$" {sqlExpert } or  e.mobile='{coreUser.mobile}' ";
 
             }
             var resultActivityExpert = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).GetList<ActivityExpert>(sqlExpert, "ActivityExpert",pageSize:1);
@@ -1090,7 +1267,7 @@ namespace TEAMModelOS.Controllers
             // 產出所需要的 JWT securityToken 物件,並取得序列化後的 Token 結果(字串格式)
             var tokenHandler = new JwtSecurityTokenHandler();
             var serializeToken = tokenHandler.WriteToken(secToken);
-            return Ok(new { website, code =200,token =serializeToken, schools= teacherInfo.teacher.schools.Where(z=>z.status.Equals("join"))});
+            return Ok(new {  code =200,token =serializeToken, schools= teacherInfo.teacher.schools.Where(z=>z.status.Equals("join"))});
         }