Browse Source

Merge branch 'develop' of http://52.130.252.100:10000/TEAMMODEL/TEAMModelOS into develop

XW 1 năm trước cách đây
mục cha
commit
c8a1ade869

+ 6 - 6
TEAMModelOS.SDK/Models/Service/StudentService.cs

@@ -896,7 +896,7 @@ namespace TEAMModelOS.SDK
                                     guardians.ForEach(async x => {
                                         GuardianStudent guardianStudent =  x.students.Find(s => s.id.Equals(stud.Key)  && s.code.Equals(schoolId));
                                         if (guardianStudent == null) {
-                                            x.students.Add(new GuardianStudent { type=2,id=stud.Key,code=schoolId,name=stud.Value.name });
+                                            x.students.Add(new GuardianStudent { relation=stud.Value.guardian, type=2,id=stud.Key,code=schoolId,name=stud.Value.name });
                                         }
                                        await cosmosContainer.ReplaceItemAsync(x, x.id, new PartitionKey(x.code));
                                     });
@@ -908,7 +908,7 @@ namespace TEAMModelOS.SDK
                                         pk= "Guardian",
                                         mobile=stud.Value.gPhone,
                                         name = stud.Value.gName,
-                                        students = new List<GuardianStudent> { new GuardianStudent { type = 2, id = stud.Key, code = schoolId, name = stud.Value.name } }
+                                        students = new List<GuardianStudent> { new GuardianStudent { relation=stud.Value.guardian,  type = 2, id = stud.Key, code = schoolId, name = stud.Value.name } }
                                     };
                                     await  cosmosContainer.CreateItemAsync(guardian,   new PartitionKey(guardian.code));
                                 }
@@ -1130,7 +1130,7 @@ namespace TEAMModelOS.SDK
                                 GuardianStudent guardianStudent = x.students.Find(s => s.id.Equals(studCreateInfo.id) && s.code.Equals(schoolId));
                                 if (guardianStudent == null)
                                 {
-                                    x.students.Add(new GuardianStudent { type = 2, id = studCreateInfo.id, code = schoolId, name = studCreateInfo.name });
+                                    x.students.Add(new GuardianStudent {relation=studentGuardian.relation, type = 2, id = studCreateInfo.id, code = schoolId, name = studCreateInfo.name });
                                 }
                                 await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).ReplaceItemAsync(x, x.id, new PartitionKey(x.code));
                             });
@@ -1144,7 +1144,7 @@ namespace TEAMModelOS.SDK
                                 pk = "Guardian",
                                 mobile = studentGuardian.mobile,
                                 name = studentGuardian.name,
-                                students = new List<GuardianStudent> { new GuardianStudent { type = 2, id = studCreateInfo.id, code = schoolId, name = studCreateInfo.name } }
+                                students = new List<GuardianStudent> { new GuardianStudent { relation=studentGuardian.relation, type = 2, id = studCreateInfo.id, code = schoolId, name = studCreateInfo.name } }
                             };
                             await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).CreateItemAsync(guardian, new PartitionKey(guardian.code));
                         }
@@ -2385,7 +2385,7 @@ namespace TEAMModelOS.SDK
                                                     GuardianStudent guardianStudent = x.students.Find(s => s.id.Equals(id) && s.code.Equals(schoolId));
                                                     if (guardianStudent == null)
                                                     {
-                                                        x.students.Add(new GuardianStudent { type = 2, id = id, code = schoolId, name = studentInfos[id].name });
+                                                        x.students.Add(new GuardianStudent { relation =gtudentGuardian.relation, type = 2, id = id, code = schoolId, name = studentInfos[id].name });
                                                     }
                                                     try { await cosmosContainer.ReplaceItemAsync(x, x.id, new PartitionKey(x.code)); } catch { }
                                                 });
@@ -2399,7 +2399,7 @@ namespace TEAMModelOS.SDK
                                                     pk = "Guardian",
                                                     mobile = gtudentGuardian.mobile,
                                                     name = gtudentGuardian.name,
-                                                    students = new List<GuardianStudent> { new GuardianStudent { type = 2, id = id, code = schoolId, name = studentInfos[id].name } }
+                                                    students = new List<GuardianStudent> { new GuardianStudent {relation=gtudentGuardian.relation, type = 2, id = id, code = schoolId, name = studentInfos[id].name } }
                                                 };
                                                 try { await cosmosContainer.CreateItemAsync(guardian, new PartitionKey(guardian.code)); } catch { }
                                             }

+ 185 - 123
TEAMModelOS/Controllers/Client/AClassONEController.cs

@@ -1,5 +1,6 @@
 using Azure.Cosmos;
 using Azure.Storage.Blobs.Models;
+using HTEXLib.COMM.Helpers;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
@@ -9,11 +10,13 @@ using StackExchange.Redis;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
+using System.Configuration;
 using System.Dynamic;
 using System.IdentityModel.Tokens.Jwt;
 using System.Linq;
 using System.Net;
 using System.Net.Http;
+using System.Reflection;
 using System.Text;
 using System.Text.Json;
 using System.Threading.Tasks;
@@ -124,150 +127,155 @@ namespace TEAMModelOS.Controllers
         {
             try
             {
-                if (!json.TryGetProperty("mobile", out JsonElement _mobile)) return BadRequest("mobile is null");
-                json.TryGetProperty("name", out JsonElement _name);
-                json.TryGetProperty("picture", out JsonElement _picture);
-                json.TryGetProperty("query", out JsonElement _query);
-                string sql = $"select value c from c where c.mobile='{_mobile}'";
-                List<Guardian> guardians = new List<Guardian>();
-                await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).
-                    GetItemQueryIterator<Guardian>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base-Guardian") }))
+                if (!json.TryGetProperty("code", out JsonElement _code)) return BadRequest("code is null");
+                var phoneInfo= await GetWeChatPhoneNumber(_code.ToString());
+                if (phoneInfo.code==200)
                 {
-                    guardians.Add(item);
-                }
-                //没有找到监护人信息时,尝试跨分区搜索,但是不建议。
-                if (!guardians.Any() || _query.ValueKind.Equals(JsonValueKind.True))
-                {
-                    List<Student> students = new List<Student>();
-                    string stuSql = $"select  distinct value c from c join g in c.guardians where g.mobile='{_mobile}'";
+                    string _mobile = phoneInfo.phone.phoneNumber;
+                    string sql = $"select value c from c where c.mobile='{_mobile}'";
+                    List<Guardian> guardians = new List<Guardian>();
                     await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).
-                       GetItemQueryIterator<Student>(queryText: stuSql, requestOptions: new QueryRequestOptions { }))
+                        GetItemQueryIterator<Guardian>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base-Guardian") }))
                     {
-                        students.Add(item);
+                        guardians.Add(item);
                     }
-                    if (students.Any())
+                    //没有找到监护人信息时,尝试跨分区搜索,但是不建议。
+                    if (!guardians.IsNotEmpty())
                     {
-                        string guardianName = "";
-                        string mobile = $"{_mobile}";
-                        List<GuardianStudent> guardianStudents = new List<GuardianStudent>();
-                        students.ForEach(x =>
-                        {
-                            var studentGuardian = x.guardians.Find(x => !string.IsNullOrWhiteSpace(x.mobile) && x.mobile.Equals(mobile));
-                            if (studentGuardian != null)
-                            {
-                                guardianName = studentGuardian.name;
-                                guardianStudents.Add(new GuardianStudent { type = 2, id = x.id, code = x.schoolId, name = x.name, picture = x.picture });
-                            }
-                        });
-                        if (guardians.Any())
+                        List<Student> students = new List<Student>();
+                        string stuSql = $"select  distinct value c from c join g in c.guardians where g.mobile='{_mobile}'";
+                        await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).
+                           GetItemQueryIterator<Student>(queryText: stuSql, requestOptions: new QueryRequestOptions { }))
                         {
-                            guardians.First().students.AddRange(guardianStudents);
+                            students.Add(item);
                         }
-                        else
+                        if (students.Any())
                         {
-                            Guardian guardian = new Guardian() { id = Guid.NewGuid().ToString(), name = guardianName, mobile = mobile, students = guardianStudents, code = "Base-Guardian", pk = "Guardian" };
-                            await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).CreateItemAsync(guardian, new PartitionKey("Base-Guardian"));
-                            guardians.Add(guardian);
+                            string guardianName = "";
+                            string mobile = $"{_mobile}";
+                            List<GuardianStudent> guardianStudents = new List<GuardianStudent>();
+                            students.ForEach(x =>
+                            {
+                                var studentGuardian = x.guardians.Find(x => !string.IsNullOrWhiteSpace(x.mobile) && x.mobile.Equals(mobile));
+                                if (studentGuardian != null)
+                                {
+                                    guardianName = studentGuardian.name;
+                                    guardianStudents.Add(new GuardianStudent {relation=studentGuardian.relation, type = 2, id = x.id, code = x.schoolId, name = x.name, picture = x.picture });
+                                }
+                            });
+                            if (guardians.Any())
+                            {
+                                guardians.First().students.AddRange(guardianStudents);
+                            }
+                            else
+                            {
+                                Guardian guardian = new Guardian() { id = Guid.NewGuid().ToString(), name = guardianName, mobile = mobile, students = guardianStudents, code = "Base-Guardian", pk = "Guardian" };
+                                await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).CreateItemAsync(guardian, new PartitionKey("Base-Guardian"));
+                                guardians.Add(guardian);
+                            }
                         }
                     }
-                }
-                if (guardians.Any())
-                {
-                    var students = guardians.SelectMany(x => x.students).DistinctBy(x => $"{x.type}-{x.id}{x.code}");
-                    var first = guardians.First();
-                    first.students = students.ToList();
-                    if (guardians.Count > 1)
+                    if (guardians.Any())
                     {
-                        //合并,并移除多余的绑定的手机号。
-                        guardians.Remove(first);
-                        if (guardians.Any() && !guardians.Select(x => x.id).Contains(first.id))
+                        var students = guardians.SelectMany(x => x.students).DistinctBy(x => $"{x.type}-{x.id}{x.code}");
+                        var first = guardians.First();
+                        first.students = students.ToList();
+                        if (guardians.Count > 1)
                         {
-                            await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).DeleteItemsStreamAsync(guardians.Select(x => x.id).ToList(), "Base-Guardian");
+                            //合并,并移除多余的绑定的手机号。
+                            guardians.Remove(first);
+                            if (guardians.Any() && !guardians.Select(x => x.id).Contains(first.id))
+                            {
+                                await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).DeleteItemsStreamAsync(guardians.Select(x => x.id).ToList(), "Base-Guardian");
+                            }
                         }
-                    }
-                    first.nickname = !string.IsNullOrWhiteSpace($"{_name}") ? $"{_name}" : first.name;
-                    first.picture = !string.IsNullOrWhiteSpace($"{_picture}") ? $"{_picture}" : first.picture;
-                    await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).ReplaceItemAsync(first, first.id, new PartitionKey(first.code));
-                    var schoolcodes = first.students.Where(s => !string.IsNullOrWhiteSpace(s.code)).Select(x => x.code);
-                    List<School> idSchools = new List<School>();
-                    if (schoolcodes.Any())
-                    {
-                        string sqlschool = $"select c.id,c.name,c.picture,c.period from c where c.id in ({string.Join(",", schoolcodes.Select(s => $"'{s}'"))})";
-                        await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).
-                        GetItemQueryIterator<School>(queryText: sqlschool, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
+                        first.nickname =first.name;
+                        first.picture = first.picture;
+                        await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).ReplaceItemAsync(first, first.id, new PartitionKey(first.code));
+                        var schoolcodes = first.students.Where(s => !string.IsNullOrWhiteSpace(s.code)).Select(x => x.code);
+                        List<School> idSchools = new List<School>();
+                        if (schoolcodes.Any())
                         {
-                            idSchools.Add(item);
+                            string sqlschool = $"select c.id,c.name,c.picture,c.period from c where c.id in ({string.Join(",", schoolcodes.Select(s => $"'{s}'"))})";
+                            await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).
+                            GetItemQueryIterator<School>(queryText: sqlschool, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
+                            {
+                                idSchools.Add(item);
+                            }
                         }
-                    }
-                    var stus = first.students.GroupBy(x => x.code).Select(x => new { x.Key, list = x.ToList() });
-                    List<Student> studentes = new List<Student>();
-                    List<Class> classes = new List<Class>();
-                    foreach (var stu in stus)
-                    {
-                        HashSet<string> classIds = new HashSet<string>();
-                        string sqlStudent = $"select c.name ,c.periodId ,c.id ,c.schoolId, c.picture, c.classId, c.gender, c.year  from c where c.id in ({string.Join(",", stu.list.Select(s => $"'{s.id}'"))})";
-                        await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).
-                        GetItemQueryIterator<Student>(queryText: sqlStudent, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base-{stu.Key}") }))
+                        var stus = first.students.GroupBy(x => x.code).Select(x => new { x.Key, list = x.ToList() });
+                        List<Student> studentes = new List<Student>();
+                        List<Class> classes = new List<Class>();
+                        foreach (var stu in stus)
                         {
-                            studentes.Add(item);
-                            classIds.Add(item.classId);
+                            HashSet<string> classIds = new HashSet<string>();
+                            string sqlStudent = $"select c.name ,c.periodId ,c.id ,c.schoolId, c.picture, c.classId, c.gender, c.year,c.guardians  from c where c.id in ({string.Join(",", stu.list.Select(s => $"'{s.id}'"))})";
+                            await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student).
+                            GetItemQueryIterator<Student>(queryText: sqlStudent, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base-{stu.Key}") }))
+                            {
+                                studentes.Add(item);
+                                classIds.Add(item.classId);
+                            }
+                            string sqlClassIds = $" select c.name ,c.periodId ,c.id ,c.school,  c.teacher,c.year  from c where c.id in ({string.Join(",", classIds.Select(s => $"'{s}'"))})";
+                            await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).
+                            GetItemQueryIterator<Class>(queryText: sqlClassIds, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Class-{stu.Key}") }))
+                            {
+                                classes.Add(item);
+                            }
                         }
-                        string sqlClassIds = $" select c.name ,c.periodId ,c.id ,c.school,  c.teacher,c.year  from c where c.id in ({string.Join(",", classIds.Select(s => $"'{s}'"))})";
-                        await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).
-                        GetItemQueryIterator<Class>(queryText: sqlClassIds, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Class-{stu.Key}") }))
+                        List<dynamic> stuData = new List<dynamic>();
+                        first.students.ForEach(x =>
                         {
-                            classes.Add(item);
-                        }
+                            var stu = studentes.Find(s => s.id.Equals(x.id) && s.schoolId.Equals(x.code));
+                            var school = idSchools.Find(s => s.id.Equals(x.code));
+                            string periodName = "";
+                            if (school != null && stu != null && !string.IsNullOrWhiteSpace(stu.periodId))
+                            {
+                                var period = school.period.Find(p => p != null && p.id.Equals(stu.periodId));
+                                periodName = period?.name;
+                            }
+                            string className = "";
+                            string teacherTmdid = "";
+                            string teacherName = "";
+                            int? classYear = 0;
+                            if (stu != null && !string.IsNullOrWhiteSpace(stu.classId))
+                            {
+                                var clazz = classes.Find(p => p != null && p.id.Equals(stu.classId));
+                                className = clazz?.name;
+                                teacherTmdid = clazz?.teacher?.id;
+                                teacherName = clazz?.teacher?.name;
+                                classYear = clazz?.year;
+                            }
+                            stuData.Add(new
+                            {
+                                x.id,
+                                schoolId = x.code,
+                                stu.name,
+                                stu.periodId,
+                                stu.picture,
+                                stu.classId,
+                                stu.gender,
+                                x.type,
+                                x.relation,
+                                stuYear = stu.year,
+                                schoolName = school.name,
+                                schoolPicture = school.picture,
+                                periodName,
+                                className,
+                                teacherTmdid,
+                                teacherName,
+                                classYear
+                            });
+                        });
+                        return Ok(new { code =200,guardian = new { first.id, first.name, first.picture, first.nickname, first.mobile, students = stuData } });
                     }
-                    List<dynamic> stuData = new List<dynamic>();
-                    first.students.ForEach(x =>
+                    else
                     {
-                        var stu = studentes.Find(s => s.id.Equals(x.id) && s.schoolId.Equals(x.code));
-                        var school = idSchools.Find(s => s.id.Equals(x.code));
-                        string periodName = "";
-                        if (school != null && stu != null && !string.IsNullOrWhiteSpace(stu.periodId))
-                        {
-                            var period = school.period.Find(p => p != null && p.id.Equals(stu.periodId));
-                            periodName = period?.name;
-                        }
-                        string className = "";
-                        string teacherTmdid = "";
-                        string teacherName = "";
-                        int? classYear = 0;
-                        if (stu != null && !string.IsNullOrWhiteSpace(stu.classId))
-                        {
-                            var clazz = classes.Find(p => p != null && p.id.Equals(stu.classId));
-                            className = clazz?.name;
-                            teacherTmdid = clazz?.teacher?.id;
-                            teacherName = clazz?.teacher?.name;
-                            classYear = clazz?.year;
-                        }
-                        stuData.Add(new
-                        {
-                            x.id,
-                            schoolId = x.code,
-                            stu.name,
-                            stu.periodId,
-                            stu.picture,
-                            stu.classId,
-                            stu.gender,
-                            x.type,
-                            x.relation,
-                            stuYear = stu.year,
-                            schoolName = school.name,
-                            schoolPicture = school.picture,
-                            periodName,
-                            className,
-                            teacherTmdid,
-                            teacherName,
-                            classYear
-                        });
-                    });
-                    return Ok(new { guardian = new { first.id, first.name, first.picture, first.nickname, first.mobile, students = stuData } });
+                        return Ok(new { code = 1, msg = "暂未找到学生信息", mobile = _mobile });
+                    }
                 }
-                else
-                {
-                    return Ok(new { error = 1, msg = "暂未找到学生信息" });
+                else {
+                    return BadRequest("手机授权码过期!");
                 }
             }
             catch (Exception ex)
@@ -277,6 +285,45 @@ namespace TEAMModelOS.Controllers
 
         }
 
+
+        private async Task<(int code, WeChatPhone phone)> GetWeChatPhoneNumber(string phoneCode) 
+        {
+            var wxappid = _configuration.GetValue<string>("HaBookAuth:WXMiniAPP:appid");
+            var wxsecret = _configuration.GetValue<string>("HaBookAuth:WXMiniAPP:secret");
+            string urlAccessToken = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={wxappid}&secret={wxsecret}";
+            HttpResponseMessage responseMessage = await _httpClient.CreateClient().GetAsync(urlAccessToken);
+            string contentToken = responseMessage.StatusCode == HttpStatusCode.OK ? await responseMessage.Content.ReadAsStringAsync() : "{}";
+            JsonElement WeChapToken = contentToken.ToObject<JsonElement>();
+            string WeChataccess_token = $"{WeChapToken.GetProperty("access_token")}";
+
+            Dictionary<string, object> dict = new() { { "code", phoneCode } };
+            string url = $"https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token={WeChataccess_token}";
+            // 将参数转化为HttpContent
+            HttpContent contentPhone = new StringContent(dict.ToJsonString(), Encoding.UTF8, "application/json");
+            var httpResponse = await _httpClient.CreateClient().PostAsync(url, contentPhone);
+            WeChatPhone phone = null;
+            int code = 200; 
+            if (httpResponse.StatusCode == HttpStatusCode.OK)
+            {
+                string responseContent = await httpResponse.Content.ReadAsStringAsync();
+                WeChatPhoneStatus? t = responseContent.ToObject<WeChatPhoneStatus>();
+                phone = t.phone_info;
+                if (phone==null)
+                {
+                    code = 404;
+                }
+                else { code = 200; }
+            }
+            else
+            {
+                //微信找到。
+                code = 404;
+            }
+
+            return (code, phone);
+        }
+
+
         /// <summary>
         /// 查询考试信息
         /// </summary>
@@ -303,5 +350,20 @@ namespace TEAMModelOS.Controllers
             }
 
         }
+
+        public class WeChatPhone
+        {
+            public string? phoneNumber { get; set; }
+            public string? purePhoneNumber { get; set; }
+            public string? countryCode { get; set; }
+            // public JsonElement watermark { get; set; }
+        }
+        public class WeChatPhoneStatus
+        {
+            public int? errcode { get; set; }
+            public string? errmsg { get; set; }
+            public WeChatPhone? phone_info { get; set; }
+        }
+
     }
 }