ToolController.cs 4.96 KB
using Hh.Mes.Common;
using Hh.Mes.Common.Http;
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Linq;

namespace WebMvc.Areas.Planned.Controllers
{
    [Area("Planned")]
    public class ToolController : Controller
    {
        #region postman

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public string PostMan(HttpItem httpItem)
        {
            var response = HttpSend(httpItem);
            return response;
        }

        private string HttpSend(HttpItem httpItem)
        {
            if (string.IsNullOrEmpty(httpItem.URL)) return "{\"error\":\"URL is null\"}";
            var method = httpItem.Method;

            var item = new HttpItem()
            {
                URL = httpItem.URL,
                Method = method,

                Referer = httpItem.Referer
            };
            if (method.ToUpper() == "POST")
            {
                if (string.IsNullOrEmpty(httpItem.Postdata)) return "{\"error\":\"The option is post, and the parameter【Postdata】cannot be empty\"}";
                item.Postdata = httpItem.Postdata;
                item.ContentType = httpItem.ContentType;
            }

            if (!string.IsNullOrEmpty(httpItem.Referer)) item.Referer = httpItem.Referer;

            //设置head信息
            if (!string.IsNullOrEmpty(httpItem.HeaderStr))
            {
                var arr = httpItem.HeaderStr.ToString().Split("&$");
                foreach (var value in arr)
                {
                    var temp = value.Split(":");
                    item.Header.Add(temp[0], temp[1]);
                }
            }

            var http = new HttpHelper();
            var result = http.GetHtml(item);
            return result.Html;
        }
        #endregion

        #region josn美化
        public IActionResult JosnIndex()
        {
            return View();
        }
        #endregion

        #region 系统监控

        public IActionResult Monitor()
        {
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                //非Windows系统
                return Ok($"当前系统{Environment.OSVersion.Platform}不支持此功能!");
            }
            return View();
        }

        [HttpGet]
        public IActionResult GetSystemInfo()
        {
            try
            {
                var allDirves = ComputerHelp.GetDriveInfos();
                var driveInfos = allDirves.Select(x => new
                {
                    dirName = x.Name,
                    sysTypeName = x.DriveFormat,
                    free = (x.TotalFreeSpace / 1024 / 1024 / 1024).ToString("N1") + "G",
                    total = (x.TotalSize / 1024 / 1024 / 1024).ToString("N1") + "G",
                    used = ((x.TotalSize - x.TotalFreeSpace) / 1024 / 1024 / 1024).ToString("N1") + "G",
                    usage = ((x.TotalSize - x.TotalFreeSpace) * 100 / x.TotalSize).ToString("N1") + "%"
                }).ToList();
                var memery = ComputerHelp.GetMemery();
                var os = ComputerHelp.GetOSInfo();
                var cpuInfo = ComputerHelp.GetCPUInfo();
                var timeSpan = DateTime.Now - SystemVariable.StartTime;
                var result = new
                {
                    cpu = new
                    {
                        name = string.Join("|", cpuInfo.Where(x => x.Key.StartsWith("Name")).Select(x => x.Value).ToArray()),
                        supper = string.Join("|", cpuInfo.Where(x => x.Key.StartsWith("Manufacturer")).Select(x => x.Value).ToArray()),
                        num = string.Join("|", cpuInfo.Where(x => x.Key.StartsWith("ThreadCount")).Select(x => x.Value).ToArray()),
                        used = ComputerHelp.GetCpuUsage(),
                    },
                    mem = new
                    {
                        total = memery.Item1,
                        free = memery.Item2,
                        sys = memery.Item3,
                        used = memery.Item4,
                    },
                    sys = new
                    {
                        name = ComputerHelp.GetComputerName(),
                        startTime = SystemVariable.StartTime.ToString(),
                        runTime = $"{timeSpan.Days}天{timeSpan.Hours}时{timeSpan.Minutes}分{timeSpan.Seconds}秒",
                        path = Directory.GetCurrentDirectory(),
                        caption = os["Caption"],
                        osArchitecture = os["OSArchitecture"],
                        ip = ComputerHelp.GetAddressIP(),
                    },
                    sysFiles = driveInfos
                };
                return Ok(result);
            }
            catch (Exception ex)
            {
                Log4NetHelper.Instance.Error($"获取系统信息失败,{ex}");
                return BadRequest(ex);
            }
        }

        #endregion
    }
}