Explorar o código

日志保存多语系的问题。

Li %!s(int64=2) %!d(string=hai) anos
pai
achega
7730de6d60

+ 50 - 0
TEAMModelOS.SDK/DI/AzureStorage/AzureStorageBlobExtensions.cs

@@ -21,6 +21,10 @@ using TEAMModelOS.SDK.Models.Table;
 using Microsoft.AspNetCore.Http;
 using TEAMModelOS.Models;
 using Microsoft.Azure.Cosmos.Table;
+using DocumentFormat.OpenXml.Spreadsheet;
+using System.Text.Json;
+using HTEXLib.DOCX.Models;
+using static TEAMModelOS.SDK.CoreAPIHttpService;
 
 namespace TEAMModelOS.SDK.DI
 {
@@ -328,6 +332,52 @@ namespace TEAMModelOS.SDK.DI
                 _ = dingDing.SendBotMsg($"日志保存失败:{ex.Message}\n{ex.StackTrace},,{log.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
             }
         }
+
+
+        public static async Task SaveLogLang(this AzureStorageFactory azureStorage, string type,string rootPath,  Dictionary<string, object> replaceData, DingDing dingDing, string scope = null, string bizId = null, Option option = null, HttpContext httpContext = null)
+        {
+            string msg = FileWay.FileValue(rootPath, "zh-cn", type, replaceData);
+            var table = azureStorage.GetCloudTableClient().GetTableReference("IESOptLog");
+            OptLog log = new() { RowKey = Guid.NewGuid().ToString() };
+            try
+            {
+                object id = null, school = null, name = null, website = null;
+                httpContext?.Items.TryGetValue("ID", out id);
+                httpContext?.Items.TryGetValue("School", out school);
+                httpContext?.Items.TryGetValue("Name", out name);
+                httpContext?.Items.TryGetValue("Website", out website);
+                log.tmdId = id != null ? $"{id}" : log.tmdId;
+                log.name = name != null ? $"{name}" : log.name;
+                string host = httpContext?.Request?.Host.Value;
+                log.school = school != null ? $"{school}" : log.school;
+                log.PartitionKey = type != null ? $"Log-{type}" : "Log-Default";
+                log.RowKey = bizId != null ? bizId : log.RowKey;
+                log.platform = website != null ? $"{website}" : "Default";
+                log.msg = msg;
+                log.tmsg = FileWay.FileValue(rootPath, "zh-tw", type, replaceData);
+                log.emsg = FileWay.FileValue(rootPath, "en-us", type, replaceData);
+                log.type = type;
+                log.scope = scope;
+                host = !string.IsNullOrWhiteSpace($"{host}") ? $"{host}" : option?.Location != null ? $"{host}" : "Default";
+                log.url = $"{host}{httpContext?.Request.Path}";
+                if (!string.IsNullOrWhiteSpace(msg) && msg.Length > 256)
+                {
+                    log.saveMod = 1;
+                    log.jsonfile = $"/0-public/optlog/{log.RowKey}-{log.PartitionKey}.json";
+                    await azureStorage.GetBlobContainerClient("0-public").UploadFileByContainer(log.ToJsonString(), "optlog", $"{log.RowKey}-{log.PartitionKey}.json");
+                    log.msg = null;
+                    await table.SaveOrUpdate<OptLog>(log);
+                }
+                else
+                {
+                    await table.SaveOrUpdate<OptLog>(log);
+                }
+            }
+            catch (Exception ex)
+            {
+                _ = dingDing.SendBotMsg($"日志保存失败:{ex.Message}\n{ex.StackTrace},,{log.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
+            }
+        }
         /// <summary>
         /// 系统管理员 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
         /// "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],

+ 48 - 0
TEAMModelOS.SDK/Models/Service/BI/FileWay.cs

@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.Extension;
+
+namespace TEAMModelOS.SDK
+{
+
+    /// <summary>
+    /// 文件方法
+    /// </summary>
+    public static class FileWay
+    {
+        /// <summary>
+        /// 读取文件类容替换方法
+        /// </summary>
+        /// <param name="rootPath"></param>
+        /// <param name="fileLang"></param>
+        /// <param name="keyType"></param>
+        /// <param name="replaceData"></param>
+        /// <returns></returns>
+        public static string FileValue(string rootPath, string fileLang, string keyType, Dictionary<string, object> replaceData)
+        {
+            string name = "";
+            string path = Path.Combine(rootPath, $"LogLang/{fileLang}.json");
+            var sampleJson = File.ReadAllBytes(path).AsSpan();
+            Utf8JsonReader reader = new Utf8JsonReader(sampleJson);
+            if (JsonDocument.TryParseValue(ref reader, out JsonDocument jsonDoc) && jsonDoc.RootElement.TryGetProperty(keyType, out JsonElement json))
+            {
+                string msgs = json.ToObject<string>();
+                if (!string.IsNullOrEmpty(msgs))
+                {
+                    replaceData.Keys.ToList().ForEach(x => {
+                        msgs = msgs.Replace("{" + x + "}", $"{replaceData[x]}");
+                    });
+                    name = msgs;
+                    //notifyData.body = msgs[1];
+                }
+            }
+
+            return name;
+        }
+    }
+}

+ 11 - 1
TEAMModelOS.SDK/Models/Table/OptLog.cs

@@ -25,10 +25,20 @@ namespace TEAMModelOS.SDK.Models.Table
         public string name { get; set; }
 
         /// <summary>
-        /// 操作描述
+        /// 操作描述 简体
         /// </summary>
         public string msg { get; set; }
 
+        /// <summary>
+        /// 操作描述  繁体
+        /// </summary>
+        public string tmsg { get; set; }
+
+        /// <summary>
+        /// 操作描述  英文
+        /// </summary>
+        public string emsg { get; set; }
+
         /// <summary>
         /// json文件地址
         /// </summary>

+ 4 - 4
TEAMModelOS/Controllers/Teacher/InitController.cs

@@ -105,8 +105,6 @@ namespace TEAMModelOS.Controllers
 
         }
 
-
-
         /// <summary>
         /// 修改教师信息
         /// </summary>
@@ -278,8 +276,10 @@ namespace TEAMModelOS.Controllers
                                 strMsg.Append($"{targetTeacher.name}[{targetTeacher.id}]");
                                 _coreAPIHttpService.PushNotify(new List<IdNameCode> { new IdNameCode { id= targetTeacher.id,name= targetTeacher.name,code=targetTeacher.lang} }, "transfer-admin_school", Constant.NotifyType_IES5_Management,
                                     new Dictionary<string, object> { { "tmdname", name },{ "schoolName", schoolBase.name }, { "schoolId", $"{school}" } , { "tmdid", userid } }, _option.Location, _configuration, _dingDing, _environment.ContentRootPath);
-                                _ = _azureStorage.SaveLog("transfer-admin-role", new { request, userid, name, school, schoolName = schoolBase.name, targetTecher = _targetTecher, }.ToJsonString(), bizId: $"{userid}-{schoolBase.id}-{_targetTecher}", httpContext: HttpContext, dingDing: _dingDing, scope: "school");
-
+                               // _ = _azureStorage.SaveLog("transfer-admin-role", new { request, userid, name, school, schoolName = schoolBase.name, targetTecher = _targetTecher, }.ToJsonString(), bizId: $"{userid}-{schoolBase.id}-{_targetTecher}", httpContext: HttpContext, dingDing: _dingDing, scope: "school");
+                               //保存日志
+                                Dictionary<string, object> replaceData = new() { { "oName" ,$"{name}" }, { "oId", $"{userid}" }, { "scName", $"{schoolBase.name}" }, { "scId", $"{schoolBase.id}" } , { "tName", $"{targetTeacher.name}" }, { "tId", $"{targetTeacher.id}" } };
+                                _ = _azureStorage.SaveLogLang("transfer-admin-role", _environment.ContentRootPath, replaceData, _dingDing, bizId: $"{userid}-{schoolBase.id}-{_targetTecher}", httpContext: HttpContext, scope: "school");
 
                                 //保存管理员移交至BI
                                 await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "school-transfer-admin", strMsg?.ToString(), _dingDing, twebsite: "IES5", httpContext: HttpContext);

+ 3 - 0
TEAMModelOS/LogLang/en-us.json

@@ -0,0 +1,3 @@
+{
+  "transfer-admin-role": "{oName}[{oId}] Transfer the administration of this {scName}[{scId}] school to {tName}[{tId}]"
+}

+ 3 - 0
TEAMModelOS/LogLang/zh-cn.json

@@ -0,0 +1,3 @@
+{
+  "transfer-admin-role": "{oName}[{oId}]将学校{scName}[{scId}] 的管理员权限移交给{tName}[{tId}]"
+}

+ 3 - 0
TEAMModelOS/LogLang/zh-tw.json

@@ -0,0 +1,3 @@
+{
+  "transfer-admin-role": "{oName}[{oId}]將學校{scName}[{scId}] 的管理員權限移交給{tName}[{tId}]"
+}

+ 9 - 0
TEAMModelOS/TEAMModelOS.csproj

@@ -46,6 +46,15 @@
       <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
       <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
     </Content>
+    <Content Update="LogLang\en-us.json">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+    <Content Update="LogLang\zh-cn.json">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+    <Content Update="LogLang\zh-tw.json">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
   </ItemGroup>
 	<ItemGroup>
 		<None Update="JsonFile\Core\ip2region.db">