| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using CefSharp;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- namespace UAS_Web.tool
- {
- class RequestHandler : IRequestHandler
- {
- public event Action<byte[]> NotifyMsg;
- string PrinterName = "";
- string PrintType = "";
- PrinterList print;
- /// <summary>
- /// 拦截指定请求
- /// </summary>
- /// <param name="browserControl"></param>
- /// <param name="browser"></param>
- /// <param name="frame"></param>
- /// <param name="request"></param>
- /// <param name="response"></param>
- /// <returns></returns>
- IResponseFilter IRequestHandler.GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
- {
- MessageFilter filter;
- switch (request.Url.Substring(request.Url.LastIndexOf("/") + 1))
- {
- case "zplPrinter":
- filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
- return filter;
- case "zplPrint.action":
- filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
- return filter;
- case "getPrintType.action":
- filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
- return filter;
- default:
- break;
- }
- return null;
- }
- /// <summary>
- /// 完成响应后获取Js返回的数据
- /// </summary>
- /// <param name="browserControl"></param>
- /// <param name="browser"></param>
- /// <param name="frame"></param>
- /// <param name="request"></param>
- /// <param name="response"></param>
- /// <param name="status"></param>
- /// <param name="receivedContentLength"></param>
- void IRequestHandler.OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
- {
- MessageFilter filter;
- string str;
- Dictionary<string, object> data;
- switch (request.Url.Substring(request.Url.LastIndexOf("/") + 1))
- {
- case "zplPrinter":
- //弹出打印机选择列表
- print = new PrinterList(browser) { StartPosition = FormStartPosition.CenterScreen,TopMost=true };
- print.Controls["Confirm"].Click += RequestHandler_Click;
- print.ShowDialog();
- break;
- case "zplPrint.action":
- filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter;
- str = System.Text.Encoding.UTF8.GetString(filter.dataAll.ToArray());
- data = ToDictionary(str);
- //获取所有的打印格式数据
- if (data.ContainsKey("data"))
- {
- string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "");
- List<string> PrintList = new List<string>();
- int PrintTime = Regex.Matches(PrintCode, "XA").Count;
- for (int i = 0; i < PrintTime; i++)
- {
- PrintList.Add(PrintCode.Substring(0, PrintCode.IndexOf("XZ") + 2));
- PrintCode = PrintCode.Substring(PrintCode.IndexOf("XZ") + 2);
- }
- for (int i = 0; i < PrintList.Count; i++)
- {
- PrintHelper.SendStringToPrinter(PrinterName, PrintList[i]);
- }
- data.Clear();
- }
- else if (data.ContainsKey("exceptionInfo"))
- {
- //string PrintCode = data["exceptionInfo"].ToString();
- //MessageBox.Show(PrintCode);
- }
- break;
- case "getPrintType.action":
- filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter;
- str = System.Text.Encoding.UTF8.GetString(filter.dataAll.ToArray());
- data = ToDictionary(str);
- PrintType = data["data"].ToString().Replace("\"", "");
- break;
- default:
- break;
- }
- }
- private void RequestHandler_Click(object sender, EventArgs e)
- {
- //设置打印机名称
- print.PrintDoc.PrinterSettings.PrinterName = print.Controls["Printer"].Text;
- PrinterName = print.Controls["Printer"].Text;
- //获取矩阵图的分辨率
- Graphics gr = print.PrintDoc.PrinterSettings.CreateMeasurementGraphics();
- print.browser.FocusedFrame.ExecuteJavaScriptAsync("(function(value,value1){dpi=value,printType=value1})('" + gr.DpiX + "','" + PrintType + "')");
- print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmZplPrint').click();");
- print.Close();
- }
- void filter_NotifyData(byte[] data)
- {
- if (NotifyMsg != null)
- {
- NotifyMsg(data);
- }
- }
- public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
- {
- return false;
- }
- public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
- {
- return false;
- }
- public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
- {
- return CefReturnValue.Continue;
- }
- public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
- {
- return false;
- }
- public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
- {
- return false;
- }
- public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath)
- {
- }
- public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
- {
- return false;
- }
- public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
- {
- return false;
- }
- public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status)
- {
- }
- public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser)
- {
- }
- public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
- {
- }
- public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl)
- {
- }
- bool IRequestHandler.OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
- {
- return false;
- }
- public void GetData()
- {
- }
- public static Dictionary<string, object> ToDictionary(string JsonData)
- {
- object Data = null;
- Dictionary<string, object> Dic = new Dictionary<string, object>();
- if (JsonData.StartsWith("["))
- {
- //如果目标直接就为数组类型,则将会直接输出一个Key为List的List<Dictionary<string, object>>集合
- //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["List"];
- List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
- MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
- foreach (Match ListItem in ListMatch)
- {
- List.Add(ToDictionary(ListItem.ToString()));//递归调用
- }
- Data = List;
- Dic.Add("List", Data);
- }
- else
- {
- MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\]|null|"".+?""|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值
- foreach (Match item in Match)
- {
- try
- {
- if (item.Groups[2].ToString().StartsWith("["))
- {
- //如果目标是数组,将会输出一个Key为当前Json的List<Dictionary<string, object>>集合
- //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["Json中的Key"];
- List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
- MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
- foreach (Match ListItem in ListMatch)
- {
- List.Add(ToDictionary(ListItem.ToString()));//递归调用
- }
- Data = List;
- }
- else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null
- else Data = item.Groups[2].ToString(); //数据为数字、字符串中的一类,直接写入
- Dic.Add(item.Groups[1].ToString(), Data);
- }
- catch { }
- }
- }
- return Dic;
- }
- }
- }
|