|
|
|
using AksWebBrowser;
|
|
|
|
using CPF;
|
|
|
|
using CPF.Cef;
|
|
|
|
using CPF.Controls;
|
|
|
|
using CPF.Platform;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using AKSWebBrowser.Commen;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Reflection.Metadata;
|
|
|
|
using AksWebBrowser.Common;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace AKS.EnterpriseLibrary.WebBrowser
|
|
|
|
{
|
|
|
|
public class FrmMain : Window
|
|
|
|
{
|
|
|
|
public int w = 1080;
|
|
|
|
public int h = 1920;
|
|
|
|
|
|
|
|
|
|
|
|
protected override void InitializeComponent()
|
|
|
|
{
|
|
|
|
LoadStyleFile("res://AksWebBrowser/StyleSheet.css");
|
|
|
|
//加载样式文件,文件需要设置为内嵌资源
|
|
|
|
Title = "控申业务专用浏览器";
|
|
|
|
CanResize = false;
|
|
|
|
ShowInTaskbar = true;
|
|
|
|
WindowState = WindowState.Maximized;
|
|
|
|
Children.Add(
|
|
|
|
new Border
|
|
|
|
{
|
|
|
|
Width = "100%",
|
|
|
|
Height = "100%",
|
|
|
|
Child = new Panel
|
|
|
|
{
|
|
|
|
Size = SizeField.Fill,
|
|
|
|
Children =
|
|
|
|
{
|
|
|
|
new CusWebBrowser
|
|
|
|
{
|
|
|
|
PresenterFor = this,
|
|
|
|
Name = nameof(Parame.webBrowser),
|
|
|
|
Bindings =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
nameof(CusWebBrowser.Title),
|
|
|
|
"Title",
|
|
|
|
this,
|
|
|
|
BindingMode.OneWayToSource
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MarginTop=0,
|
|
|
|
MarginLeft=0,
|
|
|
|
MarginRight=0,
|
|
|
|
MarginBottom=0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
private TextBox textBox;
|
|
|
|
protected override async void OnInitialized()
|
|
|
|
{
|
|
|
|
//窗体大小
|
|
|
|
this.Width = w;
|
|
|
|
this.Height = h;
|
|
|
|
//SetTaskStatus.Hidetask();
|
|
|
|
base.OnInitialized();
|
|
|
|
Parame.webBrowser = FindPresenterByName<CusWebBrowser>(nameof(Parame.webBrowser));
|
|
|
|
textBox = FindPresenterByName<TextBox>(nameof(textBox));
|
|
|
|
Parame.webBrowser.CusRequest.CusResquestEvent += CusRequest_CusResquestEvent;
|
|
|
|
//浏览器大小
|
|
|
|
Parame.webBrowser.Width = w;
|
|
|
|
Parame.webBrowser.Height = h;
|
|
|
|
Parame.webBrowser.Url = "http://192.168.0.34:8078/#/main-out";
|
|
|
|
//Parame.webBrowser.Url = Application.StartupPath + @"\html\index.html";
|
|
|
|
//开发者工具暂时只能支持Windows
|
|
|
|
//webBrowser.ShowDev();
|
|
|
|
//SetTaskStatus.Showtask();
|
|
|
|
Parame.webBrowser.LoadEnd += WebBrowser_LoadEnd;
|
|
|
|
this.Closing += MainWindow_Closing;
|
|
|
|
}
|
|
|
|
|
|
|
|
//关闭事件
|
|
|
|
private void MainWindow_Closing(object sender, ClosingEventArgs e)
|
|
|
|
{
|
|
|
|
MainModel.KillProcessByName("AksWebBrowser");
|
|
|
|
}
|
|
|
|
bool showDev = false;
|
|
|
|
private void WebBrowser_LoadEnd(object sender, LoadEndEventArgs e)
|
|
|
|
{
|
|
|
|
if (!showDev)
|
|
|
|
{
|
|
|
|
showDev = true;
|
|
|
|
Parame.webBrowser.ShowDev();
|
|
|
|
}
|
|
|
|
//获取授权
|
|
|
|
GetSQObject();
|
|
|
|
////删除临时文件
|
|
|
|
//Task.Run(() => { DeleteFile(); });
|
|
|
|
////开启定时清理任务
|
|
|
|
//Task.Run(() => { Time(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
//凌晨2点执行,清理当天缓存文件
|
|
|
|
public void Time()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// 获取当前时间
|
|
|
|
DateTime now = DateTime.Now;
|
|
|
|
// 计算明天凌晨的时间// 假设凌晨2点执行
|
|
|
|
DateTime tomorrowMorning = new DateTime(now.Year, now.Month, now.Day) + TimeSpan.FromDays(1) + TimeSpan.FromHours(2);
|
|
|
|
// 计算时间差(明天凌晨时间减去现在时间)
|
|
|
|
TimeSpan timeToGo = tomorrowMorning - now;
|
|
|
|
// 设置定时器
|
|
|
|
Timer timer = new Timer(ExecuteTask, null, timeToGo, Timeout.InfiniteTimeSpan);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Log.Error("定时任务开启异常: " + ex.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 要执行的任务
|
|
|
|
private static void ExecuteTask(object state)
|
|
|
|
{
|
|
|
|
DeleteFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
//删除临时文件
|
|
|
|
public static void DeleteFile()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Log.Info("定时任务执行时间: " + DateTime.Now);
|
|
|
|
string file = System.IO.Directory.GetCurrentDirectory();
|
|
|
|
file = file + @"/wwwroot";
|
|
|
|
// 这里添加你的任务代码
|
|
|
|
Utils.DeleteAllFiles(file);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Log.Error("定时任务执行异常: " + ex.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Writelog(string str, string dirName = @"logs")
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(str)) return;
|
|
|
|
|
|
|
|
var baseDir = AppDomain.CurrentDomain.BaseDirectory + dirName;
|
|
|
|
if (!Directory.Exists(baseDir))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(baseDir);
|
|
|
|
}
|
|
|
|
string filePath = System.IO.Path.Combine(baseDir, DateTime.Now.ToString("yyyy-MM-dd") + "_log.txt");
|
|
|
|
using (StreamWriter sw = File.AppendText(filePath))
|
|
|
|
{
|
|
|
|
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "[" + str + "]" + "\r\n");
|
|
|
|
sw.Flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch { }
|
|
|
|
}
|
|
|
|
private void CusRequest_CusResquestEvent(CefPostData postData, CefRequest request)
|
|
|
|
{
|
|
|
|
string postParams = string.Empty;
|
|
|
|
if (postData != null)
|
|
|
|
{
|
|
|
|
//取提交的参数
|
|
|
|
CefPostData cefPostData = postData;
|
|
|
|
var elements = cefPostData.GetElements();
|
|
|
|
foreach (var element in elements)
|
|
|
|
{
|
|
|
|
if (element.ElementType == CefPostDataElementType.Bytes)
|
|
|
|
{
|
|
|
|
var bytes = element.GetBytes();
|
|
|
|
postParams = Encoding.UTF8.GetString(bytes);
|
|
|
|
//Console.WriteLine("请求参数:" + postParams);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Console.WriteLine("请求地址:" + request.Url.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
//调用JS内的JS方法
|
|
|
|
async void InvokeJS(CpfObject obj, RoutedEventArgs eventArgs)
|
|
|
|
{
|
|
|
|
var r = await Parame.webBrowser.ExecuteJavaScript("callback('调用绑定到JS里的C#方法')");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 或者可以用授权接口
|
|
|
|
/// </summary>
|
|
|
|
public void GetSQObject()
|
|
|
|
{
|
|
|
|
Task.Run(async () =>
|
|
|
|
{
|
|
|
|
var client = new HttpClient();
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, $"{Parame.apiUrl}/api/Interface/Getlist?Ytjbm={Parame.key}");
|
|
|
|
var response = await client.SendAsync(request);
|
|
|
|
List<Func> list = new List<Func>();
|
|
|
|
if (response.StatusCode.ToString() == "OK")
|
|
|
|
{
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
var body = await response.Content.ReadAsStringAsync();
|
|
|
|
JObject jo = (JObject)JsonConvert.DeserializeObject(body);
|
|
|
|
if (jo["IsSucceed"].ToString() == "True")
|
|
|
|
{
|
|
|
|
string result = jo["result"].ToString();
|
|
|
|
if (!string.IsNullOrEmpty("result"))
|
|
|
|
{
|
|
|
|
JArray array = (JArray)JsonConvert.DeserializeObject(result);
|
|
|
|
foreach (JObject item in array)
|
|
|
|
{
|
|
|
|
Func func = new Func()
|
|
|
|
{
|
|
|
|
Id = item["Id"].ToString(),
|
|
|
|
Platform = item["Platform"].ToString(),
|
|
|
|
Interfaceaddress = item["Interfaceaddress"].ToString(),
|
|
|
|
};
|
|
|
|
list.Add(func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Log.Info("未获取授权");
|
|
|
|
Utils.MessagesBox("未获取授权");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Log.Info("未获取授权");
|
|
|
|
Utils.MessagesBox("未获取授权");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Parame.FuncObject = list;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|