CrazyIter_Bin 4 月之前
父節點
當前提交
a81c8518fd

+ 52 - 5
TEAMModelOS.Extension/IES.Exam/IES.ExamServer/Controllers/IndexController.cs

@@ -34,16 +34,63 @@ namespace IES.ExamServer.Controllers
             _connectionService=connectionService;
             _connectionService=connectionService;
             _liteDBFactory=liteDBFactory;
             _liteDBFactory=liteDBFactory;
         }
         }
+        [HttpPost("list-schools")]
+        public async Task<IActionResult> ListSchool(JsonNode json) 
+        {
+            string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "package", "schools.json");
+            string schoolText = await System.IO.File.ReadAllTextAsync(filePath);
+            JsonNode? node = schoolText.ToObject<JsonNode>();
+            return Ok(new {code =200, schools= node?["schools"] });
+        }
         [HttpPost("bind-school")]
         [HttpPost("bind-school")]
         public async Task<IActionResult> BindSchool(JsonNode json) 
         public async Task<IActionResult> BindSchool(JsonNode json) 
         {
         {
             string id=$"{json["id"]}";
             string id=$"{json["id"]}";
             string name= $"{json["name"]}";
             string name= $"{json["name"]}";
-            string picture = $"{json["picture"]}";
-            string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "package", "schools.json");
-            string schools = await System.IO.File.ReadAllTextAsync(filePath);
-
-            return Ok();
+            string fp = $"{json["fp"]}";
+            if (!string.IsNullOrWhiteSpace(id) && !string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(fp))
+            {
+                string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "package", "schools.json");
+                string schoolText = await System.IO.File.ReadAllTextAsync(filePath);
+                List<School>? schools = schoolText.ToObject<JsonNode>()?["schools"]?.ToObject<List<School>>();
+                School? school = schools?.Find(x => id.Equals(x.id)  && name.Equals(x.name));
+                if (school!=null)
+                {
+                    _liteDBFactory.GetLiteDatabase().GetCollection<School>().DeleteAll();
+                    _liteDBFactory.GetLiteDatabase().GetCollection<School>().Upsert(school);
+                    IEnumerable<School> schoolsDb = _liteDBFactory.GetLiteDatabase().GetCollection<School>().FindAll();
+                    School? schoolDb = schools?.FirstOrDefault();
+                    _memoryCache.TryGetValue(Constant._KeyServerDevice, out ServerDevice? server);
+                    if (server!=null)
+                    {
+                        server.school = school;
+                    }
+                    string ip = GetIP();
+                    var device = IndexService.GetDeviceInit(HttpContext, $"{fp}", ip, _memoryCache);
+                    int hybrid = 0;
+                    _memoryCache.Set(Constant._KeyServerDevice, server);
+                    _memoryCache.TryGetValue(Constant._KeyServerCenter, out JsonNode? data);
+                    _memoryCache.TryGetValue(Constant._KeyServerDevice, out server);
+                    if (data!=null)
+                    {
+                        hybrid=1;
+                        msg="云端服务连接成功!";
+                        return Ok(new { code = 200, msg, data = new { hybrid, device, centerUrl = data["centerUrl"], region = data["region"], ip = data["ip"], nowtime = DateTimeOffset.Now.ToUnixTimeMilliseconds(), server } });
+                    }
+                    else
+                    {
+                        msg="云端服务未连接!";
+                        return Ok(new { code = 200, msg, data = new { hybrid, device, centerUrl = "", region = "局域网·内网", ip = ip, nowtime = DateTimeOffset.Now.ToUnixTimeMilliseconds(), server } });
+                    }
+                }
+                else
+                {
+                    return Ok(new { code = 400, msg = "绑定失败!" });
+                }
+            }
+            else {
+                return Ok(new { code = 400, msg = "参数错误!" });
+            }
         }
         }
         [HttpPost("device")]
         [HttpPost("device")]
         public IActionResult Device(JsonElement json )
         public IActionResult Device(JsonElement json )

+ 0 - 4
TEAMModelOS.Extension/IES.Exam/IES.ExamServer/DI/DataCenterConnectionService.cs

@@ -2,10 +2,6 @@
 {
 {
     public class DataCenterConnectionService
     public class DataCenterConnectionService
     {
     {
-        
-
-
-
         private bool _dataCenterIsConnected;
         private bool _dataCenterIsConnected;
         public string? centerUrl { get; set; }
         public string? centerUrl { get; set; }
 
 

+ 1 - 1
TEAMModelOS.Extension/IES.Exam/IES.ExamServer/IES.ExamServer.csproj

@@ -50,7 +50,7 @@
 	    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 	    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 	  </Content>
 	  </Content>
 	  <Content Update="wwwroot\package\schools.json">
 	  <Content Update="wwwroot\package\schools.json">
-	    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+	    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 	  </Content>
 	  </Content>
 	</ItemGroup>
 	</ItemGroup>