CrazyIter_Bin 2 jaren geleden
bovenliggende
commit
4e59bebe3a

+ 1 - 0
TEAMModelOS.SDK/Models/Cosmos/Student/Student.cs

@@ -72,6 +72,7 @@ namespace TEAMModelOS.SDK.Models
 
         //姓名 姓名
         public string name { get; set; }
+        public string nickname { get; set; }
         //监护人:父亲,母亲,或者其他监护人
 
         /// <summary>

+ 49 - 7
TEAMModelOS/Controllers/Client/AClassONEController.cs

@@ -3,6 +3,7 @@ using Azure.Storage.Blobs.Models;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.Options;
 using StackExchange.Redis;
 using System;
@@ -47,14 +48,14 @@ namespace TEAMModelOS.Controllers
         private readonly DingDing _dingDing;
         private readonly Option _option;
         private readonly SnowflakeId _snowflakeId;
-
+        private readonly IConfiguration _configuration;
         public AClassONEController(
             AzureStorageFactory azureStorage,
             AzureRedisFactory azureRedis,
             AzureCosmosFactory azureCosmos,
             DingDing dingDing,
             SnowflakeId snowflakeId,
-            IOptionsSnapshot<Option> option, HttpClient httpClient)
+            IOptionsSnapshot<Option> option, HttpClient httpClient, IConfiguration configuration)
         {
             _azureStorage = azureStorage;
             _azureRedis = azureRedis;
@@ -63,6 +64,7 @@ namespace TEAMModelOS.Controllers
             _snowflakeId = snowflakeId;
             _option = option?.Value;
             _httpClient = httpClient;
+            _configuration = configuration;
         }
 
         /// <summary>
@@ -74,14 +76,28 @@ namespace TEAMModelOS.Controllers
         [HttpPost("get-miniapp-userinfo")]
         //[Authorize(Roles = "AClassONE")]
         public async Task<IActionResult> GetMiniAPPOpenid( JsonElement json) {
-            if(!json.TryGetProperty("js_code", out JsonElement js_code))return BadRequest("js_code is null") ;
+            // string Content = await responseMessage.Content.ReadAsStringAsync();
+            var location = _option.Location;
+            var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
+            var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
+            if (location.Contains("China"))
+            {
+                location = "China";
+            }
+            else if (location.Contains("Global"))
+            {
+                location = "Global";
+            }
+            var token = await CoreTokenExtensions.CreateAccessToken(clientID, clientSecret, location);
+
+            if (!json.TryGetProperty("js_code", out JsonElement js_code))return BadRequest("js_code is null") ;
             string url = $"https://api.weixin.qq.com/sns/jscode2session?appid=wx5705da8747c77cfe&secret=d5adf260a866ca43e74fbb40cec00799&js_code={js_code}&grant_type=authorization_code";
             HttpResponseMessage responseMessage = await _httpClient.GetAsync(url);
             if (responseMessage.StatusCode == HttpStatusCode.OK)
             {
                JsonDocument document= JsonDocument.Parse(responseMessage.Content.ReadAsStream());
-               // string Content = await responseMessage.Content.ReadAsStringAsync();
-                return Ok(new { miniappData= document });
+               
+                return Ok(new { miniappData= document , accessToken =token});
             }
             else
             {
@@ -92,7 +108,7 @@ namespace TEAMModelOS.Controllers
 
 
         /// <summary>
-        /// js_code获取小程序用户信息
+        /// 根据家长手机号获取监护学生的信息
         /// </summary>
         /// <param name="json"></param>
         /// <returns></returns>
@@ -101,7 +117,33 @@ namespace TEAMModelOS.Controllers
         //[Authorize(Roles = "AClassONE")]
         public async Task<IActionResult> GetStudentsByPhone(JsonElement json)
         {
-            string sql = "";
+            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") })) {
+                guardians.Add(item);
+            }
+            if (guardians.Any()) {
+                var students = guardians.SelectMany(x => x.students);
+                var first = guardians.First();
+                if (guardians.Count > 1)
+                {
+                    //合并,并移除多余的绑定的手机号。
+                    first.students = students.ToList();
+                    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));
+            }
             return Ok();
         }
     }

+ 5 - 1
TEAMModelOS/appsettings.Development.json

@@ -46,7 +46,7 @@
     },
     "Account": "https://account.teammodel.cn",
     "CoreAPI": "https://api2.teammodel.cn",
-	"CoreBBAPI": "https://bb.teammodel.cn",
+    "CoreBBAPI": "https://bb.teammodel.cn",
     "CoreService": {
       "clientID": "c7317f88-7cea-4e48-ac57-a16071f7b884",
       "clientSecret": "kguxh:V.PLmxBdaI@jnrTrDSth]A3346",
@@ -54,6 +54,10 @@
       "sendnotification": "https://api2.teammodel.cn/service/sendnotification",
       "getnotification": "https://api2.teammodel.cn/service/getnotification",
       "delnotification": "https://api2.teammodel.cn/service/delnotification"
+    },
+    "AClassONE": {
+      "clientID": "c7317f88-7cea-4e48-ac57-a16071f7b884",
+      "clientSecret": "kguxh:V.PLmxBdaI@jnrTrDSth]A3346"
     }
   },
   "DingDingAuth": {