|
@@ -21,6 +21,17 @@ using System.Net.Http.Json;
|
|
|
using Azure.Core;
|
|
|
using Microsoft.Azure.Cosmos;
|
|
|
using System.Text.Json.Nodes;
|
|
|
+using TEAMModelOS.SDK.Context.Attributes.Azure;
|
|
|
+using Microsoft.Azure.Cosmos.Table;
|
|
|
+using System.Collections.Generic;
|
|
|
+using TEAMModelOS.SDK.Models;
|
|
|
+using Grpc.Core;
|
|
|
+using DocumentFormat.OpenXml.Spreadsheet;
|
|
|
+using System.ComponentModel.DataAnnotations;
|
|
|
+using DocumentFormat.OpenXml.InkML;
|
|
|
+using System.IdentityModel.Tokens.Jwt;
|
|
|
+using System.Linq;
|
|
|
+using System.Net.Http.Headers;
|
|
|
|
|
|
|
|
|
namespace TEAMModelOS.Controllers.Third.XunFeiJYY
|
|
@@ -40,13 +51,26 @@ namespace TEAMModelOS.Controllers.Third.XunFeiJYY
|
|
|
private static readonly string getUserInfo = "getUserInfo";
|
|
|
|
|
|
private static readonly string url= "https://open.jyyun.com";
|
|
|
- private static readonly string getUserByUserId = "getUserByUserId";
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
private static readonly string appid = "ea4e44e1dea54caba7b06bdea72970fa";
|
|
|
private static readonly string appsecret = "bb79a189fb9c22e9ae1a62b279a112c0";
|
|
|
private static readonly string apikey = "305C300D06092A864886F70D0101010500034B003048024100C362EAB80DDFD682367592DD8274A40A9FE9F37DDEC56AC8E0FBE4A5A6D9F6FDAAA495460821CA94EF4A955B582822D7BB289142F2C562FC04DA2D3B940C3D730203010001";
|
|
|
private static readonly string testuserid = "fbc284072a40da84adbe206f721b3285bc5101240c006447328c7afa3b7ae20d122a14a73144eea261756cce3e83c3f6ef940d9e8d823c7c";
|
|
|
private static readonly string testschlid = "fbc284072a40da841b643aa367013eb18a1d351ed46b0feb15f629530e2f3c1137ae30823af5c69d3b88ef0fa3c761efee102032fc5d2a5a";
|
|
|
+ private static readonly string schoolid = "fbc284072a40da84352496274afc3bc55db54e6686cf3af22a9f0a295239691707dc038c5d090450c46bdea890254258b8a118406664bdaf";
|
|
|
+ //根据学校id查询学校下在读的学生 在读:行政班级表中grade不为104和105班级下的学生
|
|
|
+ private static readonly string listStudyingStudentInSchool = "listStudyingStudentInSchool";
|
|
|
+ //根据学校角色获取用户信息
|
|
|
+ private static readonly string listUserByRoleInScool = "listUserByRoleInScool";
|
|
|
+ //查询学校下行政班列表
|
|
|
+ private static readonly string listOrgClassBySchool = "listOrgClassBySchool";
|
|
|
+ //根据ID查询用户基本信息
|
|
|
+ private static readonly string getUserByUserId = "getUserByUserId";
|
|
|
+ //根据用户id获取教师信息
|
|
|
+ private static readonly string getOrgTeacherById = "getOrgTeacherById";
|
|
|
+ //根据学生id查询行政学生
|
|
|
+ private static readonly string getOrgStudentById = "getOrgStudentById";
|
|
|
private readonly AzureCosmosFactory _azureCosmos;
|
|
|
private readonly DingDing _dingDing;
|
|
|
private readonly Option _option;
|
|
@@ -58,7 +82,7 @@ namespace TEAMModelOS.Controllers.Third.XunFeiJYY
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
|
public XunFeilJYYController(ILogger<XunFeilJYYController> logger, IConfiguration configuration, IWebHostEnvironment environment,
|
|
|
- CoreAPIHttpService coreAPIHttpService, AzureStorageFactory azureStorage, DingDing dingDing, AzureCosmosFactory azureCosmos, IOptionsSnapshot<Option> option)
|
|
|
+ CoreAPIHttpService coreAPIHttpService, AzureStorageFactory azureStorage, DingDing dingDing, AzureCosmosFactory azureCosmos, IOptionsSnapshot<Option> option, IHttpClientFactory httpClientFactory)
|
|
|
{
|
|
|
_logger = logger;
|
|
|
_configuration = configuration;
|
|
@@ -68,6 +92,14 @@ namespace TEAMModelOS.Controllers.Third.XunFeiJYY
|
|
|
_dingDing = dingDing;
|
|
|
_azureCosmos = azureCosmos;
|
|
|
_option = option?.Value;
|
|
|
+ _httpClientFactory = httpClientFactory;
|
|
|
+ }
|
|
|
+ [HttpPost("update-student-data")]
|
|
|
+ [AllowAnonymous]
|
|
|
+ public async Task<IActionResult> UpdateStudentData(JsonElement json)
|
|
|
+ {
|
|
|
+ var schoolData = XunFeiService.Request( "Post", listOrgClassBySchool, new Dictionary<string, string>() { { "schoolId", testschlid } });
|
|
|
+ return Ok();
|
|
|
}
|
|
|
[HttpPost("gen-sso")]
|
|
|
[AllowAnonymous]
|
|
@@ -89,14 +121,158 @@ namespace TEAMModelOS.Controllers.Third.XunFeiJYY
|
|
|
return Ok(new { code = 400, msg = "参数错误" });
|
|
|
}
|
|
|
}
|
|
|
+ [HttpPost("bind")]
|
|
|
+ [AllowAnonymous]
|
|
|
+ public async Task<IActionResult> Bind(XunFeiJYYBind bind)
|
|
|
+ {
|
|
|
+ string HostName = HttpContext.GetHostName();
|
|
|
+ if (!string.IsNullOrWhiteSpace(_option.HostName))
|
|
|
+ {
|
|
|
+ HostName = _option.HostName;
|
|
|
+ }
|
|
|
+ if (!_option.Location.Contains("Dep") && !_option.Location.Contains("Test"))
|
|
|
+ {
|
|
|
+ HostName = "www.teammodel.cn";
|
|
|
+ }
|
|
|
+ var rurl = new StringBuilder($"https://{HostName}/lepei-sso");
|
|
|
+ try
|
|
|
+ {
|
|
|
+ TmdidImplicit tmdidImplicit = null;
|
|
|
+ int status = 0;
|
|
|
+ string msg = "账号未关联";
|
|
|
+ XunFeiJYYUser teacherLP = null;
|
|
|
+ var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
|
|
|
+ JsonNode encryptData = HttpUtility.UrlDecode(bind.param, Encoding.UTF8).ToObject<JsonNode>();
|
|
|
+ if (!string.IsNullOrWhiteSpace(bind.mobile))
|
|
|
+ {
|
|
|
+ var coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", bind.mobile } }, _option.Location, _configuration);
|
|
|
+ if (coreUser != null)
|
|
|
+ {
|
|
|
+ var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
|
|
|
+ tmdidImplicit = await _coreAPIHttpService.Implicit(new Dictionary<string, string> { { "grant_type", "implicit" },
|
|
|
+ { "client_id",clientID },
|
|
|
+ { "account",coreUser.id },
|
|
|
+ { "nonce",Guid.NewGuid().ToString()} }, _option.Location, _configuration);
|
|
|
+ if (tmdidImplicit != null && !string.IsNullOrWhiteSpace(tmdidImplicit.id_token))
|
|
|
+ {
|
|
|
+ bind.id_token = tmdidImplicit.id_token;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ status = 7;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ status = 7;
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (string.IsNullOrWhiteSpace(bind.id_token))
|
|
|
+ {
|
|
|
+ status =7;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ JwtSecurityToken jwt = new JwtSecurityToken(bind.id_token);
|
|
|
+ var id = jwt.Payload.Sub;
|
|
|
+ CoreUser coreUserById = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{id}" } }, _option.Location, _configuration);
|
|
|
+ if (coreUserById == null || string.IsNullOrWhiteSpace(coreUserById.mobile) || coreUserById.mobile.Length != 11)
|
|
|
+ {
|
|
|
+ status = 7;
|
|
|
+ }
|
|
|
+ jwt.Payload.TryGetValue("name", out object name);
|
|
|
+ jwt.Payload.TryGetValue("picture", out object picture);
|
|
|
+
|
|
|
+ List<XunFeiJYYUser> teachers = await table.FindListByDict<XunFeiJYYUser>(new Dictionary<string, object>() { { Constant.PartitionKey, $"XunFeiJYYUser" }, { Constant.RowKey, encryptData["user"]?["userId"] } });
|
|
|
+
|
|
|
+ if (teachers.Any())
|
|
|
+ {
|
|
|
+ teacherLP=teachers.First();
|
|
|
+ teacherLP.tmdid = id;
|
|
|
+ teacherLP.mobile= coreUserById.mobile;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ teacherLP= encryptData["user"].ToJsonString().ToObject<XunFeiJYYUser>();
|
|
|
+ teacherLP.tmdid = id;
|
|
|
+ teacherLP.mobile = coreUserById.mobile;
|
|
|
+ }
|
|
|
+ await table.SaveOrUpdate<XunFeiJYYUser>(teacherLP);
|
|
|
+ status = 200;
|
|
|
+ }
|
|
|
+ if (status == 200)
|
|
|
+ {
|
|
|
+ var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
|
|
|
+ var location = _option.Location;
|
|
|
+ var implicit_token = await _coreAPIHttpService.Implicit(
|
|
|
+ new Dictionary<string, string>()
|
|
|
+ {
|
|
|
+ { "grant_type", "implicit" },
|
|
|
+ { "client_id",clientID },
|
|
|
+ { "account",teacherLP.tmdid },
|
|
|
+ { "nonce",Guid.NewGuid().ToString()}
|
|
|
+ }, location, _configuration);
|
|
|
+ if (implicit_token != null&& !string.IsNullOrWhiteSpace(implicit_token.id_token))
|
|
|
+ {
|
|
|
+ rurl.Append($"?status={status}&msg={HttpUtility.UrlEncode("登录成功", Encoding.UTF8)}&id_token={implicit_token?.id_token}&access_token={implicit_token?.access_token}&expires_in={HttpUtility.UrlEncode(implicit_token?.expires_in)}&token_type={HttpUtility.UrlEncode(implicit_token?.token_type)}").ToString();
|
|
|
+ return Ok(new {code=200, url = rurl.ToString() });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ rurl.Append($"?status={status}&msg={HttpUtility.UrlEncode("隐式登录异常", Encoding.UTF8)}");
|
|
|
+ return Ok(new { code = 400, url = rurl.ToString() });
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ else if (status == 7)
|
|
|
+ {
|
|
|
+ msg = $"教师账号:{encryptData["user"]?["userName"]}绑定失败";
|
|
|
+ rurl.Append($"?status={status}&msg={HttpUtility.UrlEncode(msg, Encoding.UTF8)}¶m={HttpUtility.UrlEncode(encryptData.ToJsonString(), Encoding.UTF8)}");
|
|
|
+ return Ok(new { code = 400, url = rurl.ToString() });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ rurl.Append($"?status={7}&msg={HttpUtility.UrlEncode("账号未关联成功", Encoding.UTF8)}");
|
|
|
+ return Ok(new { code = 400, url = rurl.ToString() });
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ await _dingDing.SendBotMsg($"讯飞绑定账号异常,{ex.Message},{ex.StackTrace},\n{bind.ToJsonString()}", GroupNames.成都开发測試群組);
|
|
|
+
|
|
|
+ }
|
|
|
+ rurl.Append($"?status={7}&msg={HttpUtility.UrlEncode("账号未关联成功", Encoding.UTF8)}");
|
|
|
+ return Ok(new { code = 400, url = rurl.ToString() });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
[HttpGet("sso")]
|
|
|
[AllowAnonymous]
|
|
|
public async Task<IActionResult> Sso([FromQuery]string accesstoken,[FromQuery]string state)
|
|
|
{
|
|
|
+ string HostName = HttpContext.GetHostName();
|
|
|
+ int status = 0;
|
|
|
+ string msg = string.Empty;
|
|
|
+ var rurl = new StringBuilder($"https://{HostName}/xunfei-sso");
|
|
|
//前端缓存accesstoken,用于做免登录验证。直接获取用户的Token .
|
|
|
var httpClient= _httpClientFactory.CreateClient();
|
|
|
+ //HttpResponseMessage response_getOpenId = await httpClient.PostAsync($"{authurl}/{getOpenId}",new { appkey= appid , appsecret= appsecret, accessToken= accesstoken });
|
|
|
+
|
|
|
+ //var request = new HttpRequestMessage();
|
|
|
+ //request.Method = new HttpMethod("POST");
|
|
|
+ //request.RequestUri = new Uri($"{authurl}/{getOpenId}");
|
|
|
+ //request.Content = new FormUrlEncodedContent(new Dictionary<string, string> { { "appkey", appid},{ "appsecret", appsecret },{ "accessToken" , "accesstoken" } });
|
|
|
+ //// 设置请求头中的Content-Type
|
|
|
+ //// httpClient.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+ //var mediaTypeHeader = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
|
|
|
+ //mediaTypeHeader.CharSet = "UTF-8";
|
|
|
+ //request.Content.Headers.ContentType=mediaTypeHeader;
|
|
|
+ // HttpResponseMessage response_getOpenId = await httpClient.SendAsync(request);
|
|
|
|
|
|
- HttpResponseMessage response_getOpenId = await httpClient.PostAsJsonAsync($"{authurl}/{getOpenId}",new { appkey= appid , appsecret= appsecret, accessToken= accesstoken });
|
|
|
+ var response_getOpenId = await httpClient.PostAsync($"{authurl}/{getOpenId}", new StringContent($"appkey={appid}&appsecret={appsecret}&accessToken={accesstoken}", Encoding.UTF8, "application/x-www-form-urlencoded"));
|
|
|
+ XunFeiJYYUser user = null;
|
|
|
if (response_getOpenId.IsSuccessStatusCode)
|
|
|
{
|
|
|
var result_getOpenId = await response_getOpenId.Content.ReadAsStringAsync();
|
|
@@ -108,20 +284,121 @@ namespace TEAMModelOS.Controllers.Third.XunFeiJYY
|
|
|
}
|
|
|
if (!string.IsNullOrEmpty(openId))
|
|
|
{
|
|
|
- HttpResponseMessage response_getUserInfo = await httpClient.PostAsJsonAsync($"{authurl}/{getUserInfo}", new { appkey = appid, openId = openId, accessToken = accesstoken });
|
|
|
+ HttpResponseMessage response_getUserInfo = await httpClient.PostAsync($"{authurl}/{getUserInfo}", new StringContent($"appkey={appid}&openId={openId}&accessToken={accesstoken}", Encoding.UTF8, "application/x-www-form-urlencoded"));
|
|
|
if (response_getUserInfo.IsSuccessStatusCode)
|
|
|
{
|
|
|
var result_getUserInfo= await response_getUserInfo.Content.ReadAsStringAsync();
|
|
|
var result_getUserInfoData = result_getUserInfo.ToObject<JsonNode>();
|
|
|
JsonNode userIdNode = result_getUserInfoData["data"]?["userId"];
|
|
|
- if (!string.IsNullOrWhiteSpace($"{userIdNode}"))
|
|
|
+ if (!string.IsNullOrWhiteSpace($"{userIdNode}"))
|
|
|
{
|
|
|
-
|
|
|
+ string result_getUserByUserId = await XunFeiJYYService.Request(httpClient, "Post", getUserByUserId, new Dictionary<string, string> { { "userId", $"{userIdNode.ToString()}" } });
|
|
|
+ var result_getUserByUserIdData = result_getUserByUserId.ToObject<JsonNode>();
|
|
|
+ var mobile = result_getUserByUserIdData["mobile"];
|
|
|
+ if (string.IsNullOrWhiteSpace($"{mobile}"))
|
|
|
+ {
|
|
|
+ mobile= result_getUserByUserIdData["phone"];
|
|
|
+ }
|
|
|
+ user = result_getUserInfoData["data"].ToJsonString().ToObject<XunFeiJYYUser>();
|
|
|
+ user.RowKey= user.userId;
|
|
|
+ user.PartitionKey="XunFeiJYYUser";
|
|
|
+ var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
|
|
|
+ StringBuilder tableSql = new($" PartitionKey eq 'OAuthShow' and RowKey eq '{user.RowKey}'");
|
|
|
+ //lambda 表达式排序
|
|
|
+ List<XunFeiJYYUser> users = await table.QueryWhereString<XunFeiJYYUser>(tableSql.ToString());
|
|
|
+ if (users.IsNotEmpty())
|
|
|
+ {
|
|
|
+ user.tmdid=users[0].tmdid;
|
|
|
+ }
|
|
|
+ await table.Update<XunFeiJYYUser>(user);
|
|
|
+ string userKey = user.tmdid;
|
|
|
+ if (string.IsNullOrWhiteSpace(userKey))
|
|
|
+ {
|
|
|
+ userKey=user.mobile;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!string.IsNullOrWhiteSpace(userKey))
|
|
|
+ {
|
|
|
+ var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
|
|
|
+ var location = _option.Location;
|
|
|
+ var implicit_token = await _coreAPIHttpService.Implicit(
|
|
|
+ new Dictionary<string, string>()
|
|
|
+ {
|
|
|
+ { "grant_type", "implicit" },
|
|
|
+ { "client_id",clientID },
|
|
|
+ { "account",userKey },
|
|
|
+ { "nonce",Guid.NewGuid().ToString()}
|
|
|
+ }, location, _configuration);
|
|
|
+ if (implicit_token != null && !string.IsNullOrWhiteSpace(implicit_token.id_token))
|
|
|
+ {
|
|
|
+ status = 200;
|
|
|
+ rurl.Append($"?status={status}&msg={HttpUtility.UrlEncode("登录成功", Encoding.UTF8)}&id_token={implicit_token?.id_token}&access_token={implicit_token?.access_token}&expires_in={HttpUtility.UrlEncode(implicit_token?.expires_in)}&token_type={HttpUtility.UrlEncode(implicit_token?.token_type)}").ToString();
|
|
|
+ return Ok(new { code = 200, url = rurl.ToString() });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ status=4;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ status=4;
|
|
|
+ }
|
|
|
}
|
|
|
+ else {
|
|
|
+ status=2;
|
|
|
+ msg = "获取讯飞用户信息失败(userId)";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ status=2;
|
|
|
+ msg = "获取讯飞用户信息失败(getUserInfo)";
|
|
|
}
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ status=2;
|
|
|
+ msg = "获取讯飞用户信息失败(openId)";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ status=2;
|
|
|
+ msg = "获取讯飞用户信息失败(getOpenId)";
|
|
|
}
|
|
|
- return Ok(new { code = 200 });
|
|
|
+ if (status==4)
|
|
|
+ {
|
|
|
+ msg = $"科大讯飞教育云账号:{user?.userName}未关联";
|
|
|
+ rurl.Append($"?status={status}&msg={HttpUtility.UrlEncode(msg, Encoding.UTF8)}¶m={HttpUtility.UrlEncode(new{ user , accesstoken = accesstoken,state}.ToJsonString(), Encoding.UTF8)}&type=xunfei-jjy&bindurl=xunfei-jjy/bind");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ rurl.Append($"?status={status}&msg={HttpUtility.UrlEncode(msg, Encoding.UTF8)}");
|
|
|
+ }
|
|
|
+ return Ok(new { code = 400, rurl= rurl.ToString() });
|
|
|
+ }
|
|
|
+ public class XunFeiJYYBind
|
|
|
+ {
|
|
|
+ [Required(ErrorMessage = "{0} 必须填写")]
|
|
|
+ public string param { get; set; }
|
|
|
+ public string id_token { get; set; }
|
|
|
+ public string mobile { get; set; }
|
|
|
+ }
|
|
|
+
|
|
|
+ [TableName(Name = "ScYxpt")]
|
|
|
+ public class XunFeiJYYUser : TableEntity
|
|
|
+ {
|
|
|
+ public string userId { get; set; }
|
|
|
+ public string userName { get; set; }
|
|
|
+ public string userPhoto { get; set; }
|
|
|
+ public string schoolId { get; set; }
|
|
|
+ public string schoolName { get; set; }
|
|
|
+ public string province { get; set; }
|
|
|
+ public string city { get; set; }
|
|
|
+ public string district { get; set; }
|
|
|
+ public string mobile { get; set; }
|
|
|
+ public string genderCode { get; set; }
|
|
|
+ public string tmdid { get; set; }
|
|
|
}
|
|
|
}
|
|
|
}
|