You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
202 lines
7.5 KiB
202 lines
7.5 KiB
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; |
|
|
|
namespace AKS.EnterpriseLibrary.WebBrowser |
|
{ |
|
public class FrmMain : Window |
|
{ |
|
|
|
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 = 1080; |
|
this.Height = 1920; |
|
//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 = 1080; |
|
Parame.webBrowser.Height = 1920; |
|
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) |
|
{ |
|
new MainModel().CLoseCOM(); |
|
MainModel.KillProcessByName("AksWebBrowser"); |
|
} |
|
bool showDev = false; |
|
private void WebBrowser_LoadEnd(object sender, LoadEndEventArgs e) |
|
{ |
|
if (!showDev) |
|
{ |
|
showDev = true; |
|
Parame.webBrowser.ShowDev(); |
|
} |
|
//获取授权 |
|
GetSQObject(); |
|
} |
|
|
|
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() == "200") |
|
{ |
|
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; |
|
}); |
|
} |
|
} |
|
}
|
|
|