|
@@ -1,13 +1,17 @@
|
|
|
using Azure.Cosmos;
|
|
|
using Azure.Storage.Blobs.Models;
|
|
|
+using DocumentFormat.OpenXml.Office2010.Excel;
|
|
|
using DocumentFormat.OpenXml.Spreadsheet;
|
|
|
+using DocumentFormat.OpenXml.Wordprocessing;
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
+using Microsoft.Azure.Amqp.Framing;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
+using Microsoft.OData.UriParser;
|
|
|
using StackExchange.Redis;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
@@ -25,6 +29,7 @@ using TEAMModelOS.SDK.Models;
|
|
|
using TEAMModelOS.SDK.Models.Dtos;
|
|
|
using TEAMModelOS.SDK.Models.Service;
|
|
|
using TEAMModelOS.SDK.Services;
|
|
|
+using Top.Api;
|
|
|
using static TEAMModelOS.SDK.Services.BlobService;
|
|
|
|
|
|
namespace TEAMModelOS.Controllers.Client
|
|
@@ -77,10 +82,44 @@ namespace TEAMModelOS.Controllers.Client
|
|
|
[Required(ErrorMessage = "{0} 必须填写")]
|
|
|
public string school { get; set; }
|
|
|
public long ts { get; set; }
|
|
|
+ public string id_token { get; set; }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 扫码加入学校
|
|
|
+ ///
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="json"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [HttpPost("school-join")]
|
|
|
+ //[Authorize(Roles = "HiTA")]
|
|
|
+ public async Task<IActionResult> SchoolJoin(HiTAJoinSchool join)
|
|
|
+ {
|
|
|
+ if (!string.IsNullOrWhiteSpace(join.id_token) && !string.IsNullOrWhiteSpace(join.school))
|
|
|
+ {
|
|
|
+ var jwt = new JwtSecurityToken(join.id_token);
|
|
|
+ var id = jwt.Payload.Sub;
|
|
|
+ string school = join.school;
|
|
|
+ //權限token
|
|
|
+ jwt.Payload.TryGetValue("name", out object name);
|
|
|
+ jwt.Payload.TryGetValue("picture", out object picture);
|
|
|
+ long ts = join.ts;
|
|
|
+ (int code, string msg, object data) res = await SchoolService.JoinScool(school, id, $"{name}", $"{picture}", ts, _azureCosmos, _azureStorage, _coreAPIHttpService, _dingDing, _option, _configuration, _environment);
|
|
|
+ if (res.data != null)
|
|
|
+ {
|
|
|
+ return Ok(res.data);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return Ok(new { error=res.code, res.msg });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return Ok(new { error=400,msg="参数错误" });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 扫码加入学校 //已被school-join 替代
|
|
|
/// </summary>
|
|
|
/// <param name="json"></param>
|
|
|
/// <returns></returns>
|
|
@@ -89,139 +128,21 @@ namespace TEAMModelOS.Controllers.Client
|
|
|
//[Authorize(Roles = "HiTA")]
|
|
|
public async Task<IActionResult> ScanCodeJoinSchool([FromQuery] HiTAJoinSchool join)
|
|
|
{
|
|
|
- SchoolTeacher schoolTeacher = null;
|
|
|
+
|
|
|
string school = join.school;
|
|
|
string id = join.id;
|
|
|
string name = join.name;
|
|
|
string picture = join.pic;
|
|
|
long ts = join.ts;
|
|
|
- School schoolInfo = null;
|
|
|
- long nowTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
- try {
|
|
|
-
|
|
|
- if (nowTime - ts > 3600000)
|
|
|
- {
|
|
|
- return Ok(new { error = 1, msg = "超时!" });
|
|
|
- }
|
|
|
- var client = _azureCosmos.GetCosmosClient();
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- schoolInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{school}", new PartitionKey("Base"));
|
|
|
- }
|
|
|
- catch (CosmosException ex)
|
|
|
- {
|
|
|
- if (ex.Status == 404)
|
|
|
- {
|
|
|
- return Ok(new { error = 2, msg = "该学校不存在!" });
|
|
|
- }
|
|
|
- }
|
|
|
- Teacher teacher = null;
|
|
|
- try
|
|
|
- {
|
|
|
- teacher = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{id}", new PartitionKey("Base"));
|
|
|
- var sch = teacher.schools.Find(x => x.schoolId.Equals($"{school}"));
|
|
|
- if (sch != null)
|
|
|
- {
|
|
|
- if (sch.status.Equals("join"))
|
|
|
- {
|
|
|
- return Ok(new { error = 3, msg = "该教师之前已经加入学校!" });
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- sch.status = "join";
|
|
|
- sch.time = nowTime;
|
|
|
- sch.name = schoolInfo?.name;
|
|
|
- sch.picture = schoolInfo?.picture;
|
|
|
- sch.areaId = schoolInfo?.areaId;
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- teacher.schools.Add(new Teacher.TeacherSchool { areaId = schoolInfo?.areaId, schoolId = schoolInfo.id, status = "join", name = schoolInfo.name, picture = schoolInfo.picture, time = nowTime });
|
|
|
- }
|
|
|
- if (teacher.size < 2)
|
|
|
- {
|
|
|
- teacher.size = 2;
|
|
|
- }
|
|
|
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey($"Base"));
|
|
|
- }
|
|
|
- catch (CosmosException ex)
|
|
|
- {
|
|
|
- if (ex.Status == 404)
|
|
|
- {
|
|
|
- //如果沒有,則初始化Teacher基本資料到Cosmos
|
|
|
- teacher = new Teacher
|
|
|
- {
|
|
|
- createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
|
|
|
- id = id,
|
|
|
- pk = "Base",
|
|
|
- code = "Base",
|
|
|
- name = name?.ToString(),
|
|
|
- picture = picture?.ToString(),
|
|
|
- //创建账号并第一次登录IES5则默认赠送1G
|
|
|
- defaultSchool = null,
|
|
|
- size = 2,
|
|
|
- schools = new List<Teacher.TeacherSchool>() { new Teacher.TeacherSchool { areaId = schoolInfo?.areaId, schoolId = schoolInfo.id, status = "join", name = schoolInfo.name, picture = schoolInfo.picture, time = nowTime } }
|
|
|
- };
|
|
|
- var container = _azureStorage.GetBlobContainerClient(id);
|
|
|
- await container.CreateIfNotExistsAsync(PublicAccessType.None); //嘗試創建Teacher私有容器,如存在則不做任何事,保障容器一定存在
|
|
|
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(teacher.id, new PartitionKey($"Teacher-{schoolInfo.id}"));
|
|
|
- if (response.Status != 200)
|
|
|
- {
|
|
|
- schoolTeacher = new SchoolTeacher
|
|
|
- {
|
|
|
- id = teacher.id,
|
|
|
- pk = "Teacher",
|
|
|
- name = teacher.name,
|
|
|
- picture = teacher.picture,
|
|
|
- size = 0,
|
|
|
- roles = new List<string> { "teacher" },
|
|
|
- permissions = new List<string>(),
|
|
|
- status = "join",
|
|
|
- createTime = nowTime,
|
|
|
- code = $"Teacher-{schoolInfo.id}",
|
|
|
- ttl = -1
|
|
|
- };
|
|
|
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(schoolTeacher, new PartitionKey($"Teacher-{schoolInfo.id}"));
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- JsonElement data = JsonDocument.Parse(response.ContentStream).RootElement;
|
|
|
- schoolTeacher = data.ToObject<SchoolTeacher>();
|
|
|
- schoolTeacher.status = "join";
|
|
|
- schoolTeacher.name = schoolInfo.name;
|
|
|
- schoolTeacher.picture = schoolInfo.picture;
|
|
|
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<SchoolTeacher>(schoolTeacher, schoolTeacher.id, new PartitionKey($"Teacher-{ schoolInfo.id }"));
|
|
|
- }
|
|
|
-
|
|
|
- //通知管理员
|
|
|
- string sql = "select distinct value(c.id) from c where array_contains(c.roles,'admin')";
|
|
|
- List<string> ids = new List<string>();
|
|
|
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<string>(queryText: sql,
|
|
|
- requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Teacher-{school}") }))
|
|
|
- {
|
|
|
- ids.Add(item);
|
|
|
- }
|
|
|
- string bizcode = "scan-join";
|
|
|
- string tsql = $"select c.id, c.name ,c.lang as code from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))})";
|
|
|
- List<IdNameCode> idNameCodes = new List<IdNameCode>();
|
|
|
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher)
|
|
|
- .GetItemQueryIterator<IdNameCode>(queryText: tsql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
|
|
|
- {
|
|
|
- idNameCodes.Add(item);
|
|
|
- }
|
|
|
- _coreAPIHttpService.PushNotify(idNameCodes, $"{bizcode}_school", Constant.NotifyType_IES5_Management,
|
|
|
- new Dictionary<string, object> { { "tmdname", teacher.name }, { "schoolName", schoolInfo.name }, { "schoolId", $"{school}" }, { "tmdid", teacher.id } }, _option.Location, _configuration, _dingDing, _environment.ContentRootPath);
|
|
|
+ (int code ,string msg ,object data )res= await SchoolService. JoinScool(school, id, name, picture, ts,_azureCosmos,_azureStorage,_coreAPIHttpService,_dingDing,_option,_configuration,_environment);
|
|
|
+ if (res.data != null)
|
|
|
+ {
|
|
|
+ return Ok(res.data);
|
|
|
}
|
|
|
- catch (Exception ae) {
|
|
|
- await _dingDing.SendBotMsg($"HiTA扫码加入学校失败!{ae.Message},{ae.StackTrace}", GroupNames.成都开发測試群組);
|
|
|
+ else {
|
|
|
+ return Ok(new { res.code,res.msg});
|
|
|
}
|
|
|
- return Ok(new { schoolTeacher.roles, schoolTeacher.status, school = $"{school}", schoolInfo.name, schoolInfo.picture });
|
|
|
+ //return Ok(new { schoolTeacher.roles, schoolTeacher.status, school = $"{school}", schoolInfo.name, schoolInfo.picture });
|
|
|
}
|
|
|
|
|
|
//[Authorize(Roles = "HiTA")]
|