|
@@ -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();
|
|
|
}
|
|
|
}
|