CrazyIter_Bin 1 year ago
parent
commit
82c8a7804e

+ 65 - 0
HTEXScreen/Controllers/IndexController.cs

@@ -0,0 +1,65 @@
+using Azure.Storage.Blobs.Specialized;
+using HTEXScreen.Service;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
+
+using PuppeteerSharp;
+using PuppeteerSharp.Media;
+using System.Configuration;
+using System.Drawing;
+using System.IO;
+using System.Net;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Text.Json;
+using System.Text.RegularExpressions;
+using System.Web;
+using TEAMModelOS.SDK;
+using TEAMModelOS.SDK.DI;
+
+namespace HTEXScreen.Controllers
+{
+    [ApiController]
+    [Route("api")]
+    public class IndexController : ControllerBase
+    {
+        private readonly AzureStorageFactory _azureStorage;
+        private readonly HttpClient _httpClient;
+        private readonly IConfiguration _configuration;
+        //  private readonly HttpContext _httpContext;
+        public IndexController(HttpClient httpClient, AzureStorageFactory azureStorage, IConfiguration configuration)
+        {
+            _httpClient = httpClient;
+            _azureStorage = azureStorage;
+            _configuration = configuration;
+            //   _httpContext=httpContext;
+        }
+        /// <summary>
+        ///  上传到blob
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [HttpPost("http-log")]
+
+        [RequestSizeLimit(102_400_000_00)] //最大10000m左右
+        public   async Task<IActionResult> HttpLog(JsonElement json)
+        {
+            string data = json.ToJsonString();
+            var gmt8Time = DateTimeOffset.Now.GetGMTTime(8);
+            var appendBlob = _azureStorage.GetBlobContainerClient("0-service-log").GetAppendBlobClient($"http-log/{gmt8Time:yyyy}/{gmt8Time:MM}/{gmt8Time:dd}/{gmt8Time:HH}.log");
+            if (!appendBlob.Exists())
+            {
+               await appendBlob.CreateAsync();
+                using var stream = new MemoryStream(Encoding.UTF8.GetBytes($"{data},\n"));
+               _=   appendBlob.AppendBlockAsync(stream);
+            }
+            else
+            {
+                using var stream = new MemoryStream(Encoding.UTF8.GetBytes($"{data},\n"));
+                _= appendBlob.AppendBlockAsync(stream);
+            }
+            return Ok(new { code=1});
+        }
+
+    }
+}

+ 2 - 0
HTEXScreen/Controllers/ScreenController.cs

@@ -30,6 +30,8 @@ namespace HTEXScreen.Controllers
             _configuration = configuration;
         //   _httpContext=httpContext;
         }
+      
+
         /// <summary>
         ///  上传到blob
         /// </summary>

+ 150 - 0
HTEXScreen/Helpers/DateTimeHelper.cs

@@ -0,0 +1,150 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace TEAMModelOS.SDK
+{
+    public static class DateTimeHelper
+    {
+
+        public static DateTime FromUnixTimestamp(this long unixtime)
+        {
+            DateTime sTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local);
+            return sTime.AddMilliseconds(unixtime);
+        }
+
+         
+        public static long ToUnixTimestamp(this DateTime datetime)
+        {
+            //DateTime sTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
+            DateTime sTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local);
+            return (long)(datetime - sTime).TotalMilliseconds;
+        }
+       
+        /// <summary>
+        /// 获得 GMT+8 时间
+        /// </summary>
+        /// <returns></returns>
+        public static DateTime GetGMTTime(this DateTime dateTime, int GMT = 8)
+        {
+            //SystemTimeZoneById  :  https://learn.microsoft.com/zh-cn/previous-versions/windows/embedded/ms912391(v=winembedded.11)
+            //TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");//设置时区
+            //DateTime easternTime = TimeZoneInfo.ConvertTimeFromUtc(dateTime, easternZone);
+            //return easternTime;
+
+
+            //处理UTC时差
+            TimeZoneInfo localTimezone = TimeZoneInfo.Local;
+            var Hours = localTimezone.BaseUtcOffset.Hours;
+            dateTime = dateTime.AddHours(GMT-Hours);
+            return dateTime;
+        }
+        /// <summary>
+        /// 默认东八区+8
+        /// </summary>
+        /// <param name="dateTime"></param>
+        /// <param name="GMT"></param>
+        /// <returns></returns>
+        public static DateTimeOffset GetGMTTime(this DateTimeOffset dateTime,int GMT=+8)
+        {
+            //处理UTC时差
+            TimeZoneInfo localTimezone = TimeZoneInfo.Local;
+            var Hours = localTimezone.BaseUtcOffset.Hours;
+            dateTime = dateTime.AddHours(GMT-Hours);
+            return dateTime;
+        }
+
+        public static  int getDays(int year) { 
+            int day = 0;
+            for (int i = 0;i<=12;i++) {
+                int days = 0;
+                if (i != 2)
+                {
+                    switch (i)
+                    {
+                        case 1:
+                        case 3:
+                        case 5:
+                        case 7:
+                        case 8:
+                        case 10:
+                        case 12:
+                            days = 31;
+                            break;
+                        case 4:
+                        case 6:
+                        case 9:
+                        case 11:
+                            days = 30;
+                            break;
+                    }
+                }
+                else {
+                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
+                    {
+                        days = 29;
+                    }
+                    else { 
+                        days = 28;
+                    }
+                }
+                day += days; 
+            }
+            return day;
+        }
+
+        #region 获取 本周、本月、本季度、本年 的开始时间或结束时间
+        /// <summary>
+        /// 获取开始时间
+        /// </summary>
+        /// <param name="TimeType">Week、Month、Season、Year</param>
+        /// <param name="now"></param>
+        /// <returns></returns>
+        public static DateTime? GetTimeStartByType(string TimeType, DateTime now)
+        {
+            
+            switch (TimeType)
+            {
+                case "Week":
+                    return now.AddDays(-(int)now.DayOfWeek + 1);
+                case "Month":
+                    return now.AddDays(-now.Day + 1);
+                case "Season":
+                    var time = now.AddMonths(0 - ((now.Month - 1) % 3));
+                    return time.AddDays(-time.Day + 1);
+                case "Year":
+                    return now.AddDays(-now.DayOfYear + 1);
+                default:
+                    return null;
+            }
+        }
+
+        /// <summary>
+        /// 获取结束时间
+        /// </summary>
+        /// <param name="TimeType">Week、Month、Season、Year</param>
+        /// <param name="now"></param>
+        /// <returns></returns>
+        public static DateTime? GetTimeEndByType(string TimeType, DateTime now)
+        {
+            switch (TimeType)
+            {
+                case "Week":
+                    return now.AddDays(7 - (int)now.DayOfWeek);
+                case "Month":
+                    return now.AddMonths(1).AddDays(-now.AddMonths(1).Day + 1).AddDays(-1);
+                case "Season":
+                    var time = now.AddMonths((3 - ((now.Month - 1) % 3) - 1));
+                    return time.AddMonths(1).AddDays(-time.AddMonths(1).Day + 1).AddDays(-1);
+                case "Year":
+                    var time2 = now.AddYears(1);
+                    return time2.AddDays(-time2.DayOfYear);
+                default:
+                    return null;
+            }
+        }
+        #endregion
+
+
+    }
+}