RequestHandler.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. using CefSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Linq;
  7. using System.Text.RegularExpressions;
  8. using System.Windows.Forms;
  9. using System.Drawing.Printing;
  10. using FastReport;
  11. using System.Data;
  12. using System.Threading;
  13. using System.Net;
  14. using System.Text;
  15. using System.Web;
  16. using System.Diagnostics;
  17. namespace UAS_Web.tool
  18. {
  19. class RequestHandler : IRequestHandler
  20. {
  21. public event Action<byte[]> NotifyMsg;
  22. //打印图片集合
  23. List<Bitmap> bitmaps = new List<Bitmap>();
  24. Report BarcodeReport = new Report();
  25. Dictionary<string, object> itemlast;
  26. Dictionary<string, object> itemBox;
  27. //图片打印中间变量
  28. int imagecount = 0;
  29. int index = 0;
  30. string PrinterName = "";
  31. string PrintType = "";
  32. PrinterList print;
  33. string LABELNAME = "";
  34. string labelview = "";
  35. Dictionary<string, object> DataCollection = null;
  36. //打印全部标签所需变量
  37. string AllLabelname = "";
  38. string AllBoxname = "";
  39. //打印类型,箱号条码统一打印或者是单一打印
  40. string PType = "";
  41. /// <summary>
  42. /// 拦截指定请求
  43. /// </summary>
  44. /// <param name="browserControl"></param>
  45. /// <param name="browser"></param>
  46. /// <param name="frame"></param>
  47. /// <param name="request"></param>
  48. /// <param name="response"></param>
  49. /// <returns></returns>
  50. IResponseFilter IRequestHandler.GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
  51. {
  52. MessageFilter filter;
  53. switch (request.Url.Substring(request.Url.LastIndexOf("/") + 1))
  54. {
  55. case "zplPrinter":
  56. filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
  57. return filter;
  58. case "zplPrint.action":
  59. filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
  60. return filter;
  61. case "getPrintType.action":
  62. filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
  63. return filter;
  64. case "vendorZplPrint.action":
  65. filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
  66. return filter;
  67. default:
  68. break;
  69. }
  70. return null;
  71. }
  72. /// <summary>
  73. /// 完成响应后获取Js返回的数据
  74. /// </summary>
  75. /// <param name="browserControl"></param>
  76. /// <param name="browser"></param>
  77. /// <param name="frame"></param>
  78. /// <param name="request"></param>
  79. /// <param name="response"></param>
  80. /// <param name="status"></param>
  81. /// <param name="receivedContentLength"></param>
  82. void IRequestHandler.OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
  83. {
  84. MessageFilter filter;
  85. string str;
  86. Dictionary<string, object> data;
  87. //打印条码时总页数与当前页变量
  88. switch (request.Url.Substring(request.Url.LastIndexOf("/") + 1))
  89. {
  90. case "zplPrinter":
  91. var m = request.Method;
  92. if (request.Method == "POST")
  93. {
  94. using (var postData = request.PostData)
  95. {
  96. if (postData != null)
  97. {
  98. var elements = postData.Elements;
  99. var charSet = request.GetCharSet();
  100. foreach (var element in elements)
  101. {
  102. if (element.Type == PostDataElementType.Bytes)
  103. {
  104. string body = HttpUtility.UrlDecode(element.GetBody(charSet),Encoding.UTF8);
  105. LABELNAME = body.Substring(body.LastIndexOf('=')+1);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. PType = "normal";
  112. //弹出打印机选择列表
  113. print = new PrinterList(browser) { StartPosition = FormStartPosition.CenterScreen,TopMost=true };
  114. print.Controls["Confirm"].Click += RequestHandler_Click;
  115. print.PrintDoc.PrintPage += PrintPage;
  116. print.ShowDialog();
  117. break;
  118. case "zplPrinterAll":
  119. if (request.Method == "POST")
  120. {
  121. using (var postData = request.PostData)
  122. {
  123. if (postData != null)
  124. {
  125. var elements = postData.Elements;
  126. var charSet = request.GetCharSet();
  127. foreach (var element in elements)
  128. {
  129. if (element.Type == PostDataElementType.Bytes)
  130. {
  131. string body = HttpUtility.UrlDecode(element.GetBody(charSet), Encoding.UTF8);
  132. //LABELNAME = body.Substring(body.LastIndexOf('=') + 1);
  133. AllLabelname = body.Substring(body.IndexOf("la_name=") + 8, body.IndexOf("&la_idBox")- (body.IndexOf("la_name=") + 8));
  134. AllBoxname = body.Substring(body.LastIndexOf('=') + 1);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. PType = "all";
  141. //弹出打印机选择列表
  142. print = new PrinterList(browser) { StartPosition = FormStartPosition.CenterScreen, TopMost = true };
  143. print.Controls["Confirm"].Click += RequestHandler_Click;
  144. print.PrintDoc.PrintPage += PrintPage;
  145. print.ShowDialog();
  146. break;
  147. case "zplPrint.action":
  148. filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter;
  149. str = System.Text.Encoding.UTF8.GetString(filter.dataAll.ToArray());
  150. data = ToDictionary(str);
  151. //获取所有的打印格式数据
  152. if (data.ContainsKey("data"))
  153. {
  154. string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "");
  155. List<string> PrintList = new List<string>();
  156. int PrintTime = Regex.Matches(PrintCode, "XA").Count;
  157. for (int i = 0; i < PrintTime; i++)
  158. {
  159. PrintList.Add(PrintCode.Substring(0, PrintCode.IndexOf("XZ") + 2));
  160. PrintCode = PrintCode.Substring(PrintCode.IndexOf("XZ") + 2);
  161. }
  162. for (int i = 0; i < PrintList.Count; i++)
  163. {
  164. PrintHelper.SendStringToPrinter(PrinterName, PrintList[i]);
  165. }
  166. data.Clear();
  167. }
  168. else if (data.ContainsKey("exceptionInfo"))
  169. {
  170. //string PrintCode = data["exceptionInfo"].ToString();
  171. //MessageBox.Show(PrintCode);
  172. }
  173. break;
  174. case "getPrintType.action":
  175. filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter;
  176. str = System.Text.Encoding.UTF8.GetString(filter.dataAll.ToArray());
  177. data = ToDictionary(str);
  178. PrintType = data["data"].ToString().Replace("\"", "");
  179. break;
  180. case "vendorZplPrint.action":
  181. try
  182. {
  183. filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter;
  184. str = System.Text.Encoding.UTF8.GetString(filter.dataAll.ToArray());
  185. if (str.Contains("不允许打印")||(str.Contains("exceptionInfo")))
  186. return;
  187. data = ToDictionary(str);
  188. if (DataCollection == null)
  189. {
  190. DataCollection = new Dictionary<string, object>();
  191. DataCollection.Add(((string)data["currentPage"]).Replace("\"", ""), data);
  192. }
  193. if (DataCollection.Count < int.Parse(((string)data["totalPage"]).Replace("\"", "")))
  194. {
  195. if(!DataCollection.ContainsKey(((string)data["currentPage"]).Replace("\"", "")))
  196. DataCollection.Add(((string)data["currentPage"]).Replace("\"", ""), data);
  197. }
  198. if (DataCollection.Count == int.Parse(((string)data["totalPage"]).Replace("\"", "")))
  199. {
  200. if (PType == "normal")
  201. {
  202. for (int i = 0; i < DataCollection.Count; i++)
  203. {
  204. vendorZplPrint(DataCollection[(i + 1).ToString()]);
  205. }
  206. }
  207. else
  208. {
  209. Console.WriteLine("打印ALL");
  210. for (int i = 0; i < DataCollection.Count; i++)
  211. {
  212. vendorZplPrintAll(DataCollection[(i + 1).ToString()]);
  213. }
  214. }
  215. DataCollection = null;
  216. }
  217. //vendorZplPrint(data);
  218. //vendorZplPrintAll(data);
  219. }
  220. catch (Exception ex)
  221. {
  222. MessageBox.Show(ex.Message + "请重新进行打印");
  223. DataCollection = null;
  224. return;
  225. }
  226. break;
  227. default:
  228. break;
  229. }
  230. }
  231. public void vendorZplPrint(Object data)
  232. {
  233. DataTable dt = new DataTable();
  234. RadioButton PRFR = (RadioButton)print.Controls["PRFR"];
  235. RadioButton PRPIC = (RadioButton)print.Controls["PRPIC"];
  236. string type;
  237. bitmaps.Clear();
  238. List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
  239. List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
  240. try
  241. {
  242. parameter = (List<Dictionary<string, object>>)((Dictionary<string, object>)data)["parameter"];
  243. barcode = (List<Dictionary<string, object>>)((Dictionary<string, object>)data)["barcode"];
  244. }
  245. catch {
  246. MessageBox.Show("打印数据异常,请联系管理员");
  247. return;
  248. }
  249. try
  250. {
  251. type = ((String)((Dictionary<string, object>)data)["type"]).Replace("\"", "");
  252. }
  253. catch
  254. {
  255. type = "no";
  256. }
  257. try
  258. {
  259. labelview = ((String)((Dictionary<string, object>)data)["label_type"]).Replace("\"", "");
  260. }
  261. catch
  262. {
  263. labelview = "";
  264. }
  265. if (PRFR.Checked)
  266. {
  267. Thread.Sleep(1000);
  268. int i = 0;
  269. while (DownLoadFile.downloading && i < 2)
  270. {
  271. Thread.Sleep(1000);
  272. i++;
  273. }
  274. if (DownLoadFile.downloading)
  275. MessageBox.Show("标签更新有误");
  276. try
  277. {
  278. BarcodeReport.Load(System.AppDomain.CurrentDomain.BaseDirectory + LABELNAME + ".frx");
  279. }
  280. catch
  281. {
  282. MessageBox.Show("未找到可使用的标签文件,请重试");
  283. return;
  284. }
  285. }
  286. int la_width = int.Parse(parameter.First()["LA_WIDTH"].ToString());
  287. int la_height = int.Parse(parameter.First()["LA_HEIGHT"].ToString());
  288. foreach (var item in barcode)
  289. {
  290. Bitmap bit = new Bitmap(la_width * 10, la_height * 4);
  291. using (Graphics g = Graphics.FromImage(bit))
  292. {
  293. foreach (var itemc in item)
  294. {
  295. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  296. if (Sidc != null)
  297. {
  298. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  299. {
  300. string itemstr = "";
  301. if (itemc.Value != null)
  302. {
  303. itemstr = itemc.Value.ToString();
  304. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  305. {
  306. itemstr = itemstr.Remove(0, 2);
  307. }
  308. if (itemstr.StartsWith("\""))
  309. {
  310. itemstr = itemstr.Remove(0, 1);
  311. }
  312. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  313. {
  314. itemstr = itemstr.Remove(itemstr.Length - 2);
  315. }
  316. if (itemstr.EndsWith("\""))
  317. {
  318. itemstr = itemstr.Remove(itemstr.Length - 1);
  319. }
  320. if (itemstr.Contains("\\\""))
  321. {
  322. itemstr = itemstr.Replace("\\\"", "\"");
  323. }
  324. }
  325. if (PRFR.Checked)
  326. {
  327. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  328. if (!dt.Columns.Contains(itemc.Key))
  329. dt.Columns.Add(DC);
  330. }
  331. if (PRPIC.Checked)
  332. {
  333. PrintHelper.Printstring(itemc.Value != null ? itemstr : " ", int.Parse(Sidc["LP_SIZE"].ToString()), g, int.Parse(Sidc["LP_LEFTRATE"].ToString()), int.Parse(Sidc["LP_TOPRATE"].ToString()));
  334. }
  335. }
  336. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  337. {
  338. if (PRFR.Checked)
  339. {
  340. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  341. if (!dt.Columns.Contains(itemc.Key))
  342. dt.Columns.Add(DC);
  343. }
  344. if (PRPIC.Checked)
  345. {
  346. PrintHelper.DrawBarcode(itemc.Value != null ? itemc.Value.ToString().Replace("\"", "") : " ", ref bit, double.Parse(Sidc["LP_LEFTRATE"].ToString()), double.Parse(Sidc["LP_TOPRATE"].ToString()), double.Parse(Sidc["LP_WIDTH"].ToString()), double.Parse(Sidc["LP_HEIGHT"].ToString()) - 2);
  347. PrintHelper.Printstring(itemc.Value != null ? itemc.Value.ToString().Replace("\"", "") : " ", int.Parse(Sidc["LP_SIZE"].ToString()), g, int.Parse(Sidc["LP_LEFTRATE"].ToString()), int.Parse(Sidc["LP_TOPRATE"].ToString()) + double.Parse(Sidc["LP_HEIGHT"].ToString()) - 2);
  348. }
  349. }
  350. }
  351. }
  352. }
  353. bitmaps.Add(bit);
  354. }
  355. if (PRFR.Checked)
  356. {
  357. foreach (var item in barcode)
  358. {
  359. DataRow dr = dt.NewRow();
  360. //g.CompositingQuality = CompositingQuality.HighQuality;
  361. //g.SmoothingMode = SmoothingMode.HighQuality;
  362. //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  363. ////定义一枝画笔
  364. //Pen pen = new Pen(Color.Black);
  365. ////用这个画笔在左上角画一个矩形
  366. //g.DrawRectangle(pen, new Rectangle(10, 10, 50, 20));
  367. foreach (var itemc in item)
  368. {
  369. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  370. if (Sidc != null)
  371. {
  372. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  373. {
  374. string itemstr = "";
  375. if (itemc.Value != null)
  376. {
  377. itemstr = itemc.Value.ToString();
  378. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  379. {
  380. itemstr = itemstr.Remove(0, 2);
  381. }
  382. if (itemstr.StartsWith("\""))
  383. {
  384. itemstr = itemstr.Remove(0, 1);
  385. }
  386. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  387. {
  388. itemstr = itemstr.Remove(itemstr.Length - 2);
  389. }
  390. if (itemstr.EndsWith("\""))
  391. {
  392. itemstr = itemstr.Remove(itemstr.Length - 1);
  393. }
  394. if (itemstr.Contains("\\\""))
  395. {
  396. itemstr = itemstr.Replace("\\\"", "\"");
  397. }
  398. }
  399. dr[itemc.Key] = itemstr;
  400. }
  401. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  402. {
  403. dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
  404. }
  405. }
  406. }
  407. dt.Rows.Add(dr);
  408. }
  409. }
  410. if (PRPIC.Checked)
  411. {
  412. if (bitmaps.Count > 0)
  413. {
  414. imagecount = bitmaps.Count;
  415. print.PrintDoc.Print();
  416. }
  417. }
  418. if (PRFR.Checked)
  419. {
  420. try
  421. {
  422. if (labelview == "")
  423. {
  424. if (type == "box")
  425. {
  426. BarcodeReport.RegisterData(dt, "VENDORBARCODE_BOX_VIEW");
  427. BarcodeReport.GetDataSource("VENDORBARCODE_BOX_VIEW").Enabled = true;
  428. BarcodeReport.PrintSettings.ShowDialog = false;
  429. BarcodeReport.PrintSettings.Printer = PrinterName;
  430. BarcodeReport.Print();
  431. }
  432. else
  433. {
  434. BarcodeReport.RegisterData(dt, "VENDORBARCODE_VIEW");
  435. BarcodeReport.GetDataSource("VENDORBARCODE_VIEW").Enabled = true;
  436. BarcodeReport.PrintSettings.ShowDialog = false;
  437. BarcodeReport.PrintSettings.Printer = PrinterName;
  438. BarcodeReport.Print();
  439. }
  440. }
  441. else
  442. {
  443. BarcodeReport.RegisterData(dt, labelview);
  444. BarcodeReport.GetDataSource(labelview).Enabled = true;
  445. BarcodeReport.PrintSettings.ShowDialog = false;
  446. BarcodeReport.PrintSettings.Printer = PrinterName;
  447. BarcodeReport.Print();
  448. }
  449. }
  450. catch(Exception ex)
  451. {
  452. MessageBox.Show(ex.Message+"打印出现错误,请检查标签文件选择是否正确");
  453. }
  454. dt.Clear();
  455. }
  456. }
  457. string nowboxcode = "";
  458. public void vendorZplPrintAll(Object data)
  459. {
  460. //条码标签结构datatable
  461. DataTable dt = new DataTable();
  462. //箱号标签结构datatable
  463. DataTable box = new DataTable();
  464. //记录最新打印出来的箱号
  465. string type;
  466. //箱号对应视图名
  467. string box_type;
  468. bitmaps.Clear();
  469. List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
  470. List<Dictionary<string, object>> BoxParameter = new List<Dictionary<string, object>>();
  471. //List<Dictionary<string, object>> parameter2 = new List<Dictionary<string, object>>();
  472. List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
  473. List<Dictionary<string, object>> BoxBarcode = new List<Dictionary<string, object>>();
  474. try
  475. {
  476. parameter = (List<Dictionary<string, object>>)((Dictionary<string, object>)data)["parameter"];
  477. BoxParameter = (List<Dictionary<string, object>>)((Dictionary<string, object>)data)["BoxParameter"];
  478. barcode = (List<Dictionary<string, object>>)((Dictionary<string, object>)data)["barcode"];
  479. BoxBarcode = (List<Dictionary<string, object>>)((Dictionary<string, object>)data)["BoxBarcode"];
  480. }
  481. catch(Exception ex)
  482. {
  483. MessageBox.Show(ex.Message+"打印数据异常,请联系管理员");
  484. return;
  485. }
  486. try
  487. {
  488. type = ((String)((Dictionary<string, object>)data)["type"]).Replace("\"", "");
  489. }
  490. catch
  491. {
  492. type = "no";
  493. }
  494. try
  495. {
  496. labelview = ((String)((Dictionary<string, object>)data)["label_type"]).Replace("\"", "");
  497. }
  498. catch
  499. {
  500. labelview = "";
  501. }
  502. try
  503. {
  504. box_type = ((String)((Dictionary<string, object>)data)["BoxLabel_type"]).Replace("\"", "");
  505. }
  506. catch
  507. {
  508. box_type = "";
  509. }
  510. Thread.Sleep(1000);
  511. int k = 0;
  512. while (DownLoadFile.downloading && k < 2)
  513. {
  514. Thread.Sleep(1000);
  515. k++;
  516. }
  517. if (DownLoadFile.downloading)
  518. MessageBox.Show("标签更新有误");
  519. //barcode条码datable格式
  520. foreach (var item in parameter)
  521. {
  522. foreach (var itemc in item)
  523. {
  524. string itemstr = "";
  525. if (itemc.Value != null && itemc.Key.ToString().ToUpper() == "LP_NAME")
  526. {
  527. itemstr = itemc.Value.ToString();
  528. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  529. {
  530. itemstr = itemstr.Remove(0, 2);
  531. }
  532. if (itemstr.StartsWith("\""))
  533. {
  534. itemstr = itemstr.Remove(0, 1);
  535. }
  536. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  537. {
  538. itemstr = itemstr.Remove(itemstr.Length - 2);
  539. }
  540. if (itemstr.EndsWith("\""))
  541. {
  542. itemstr = itemstr.Remove(itemstr.Length - 1);
  543. }
  544. if (itemstr.Contains("\\\""))
  545. {
  546. itemstr = itemstr.Replace("\\\"", "\"");
  547. }
  548. DataColumn DC = new DataColumn(itemstr, Type.GetType("System.String"));
  549. if (!dt.Columns.Contains(itemc.Key))
  550. dt.Columns.Add(DC);
  551. }
  552. }
  553. }
  554. //box箱号datatable格式
  555. foreach (var item in BoxParameter)
  556. {
  557. foreach (var itemc in item)
  558. {
  559. string itemstr = "";
  560. if (itemc.Value != null && itemc.Key.ToString().ToUpper() == "LP_NAME")
  561. {
  562. itemstr = itemc.Value.ToString();
  563. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  564. {
  565. itemstr = itemstr.Remove(0, 2);
  566. }
  567. if (itemstr.StartsWith("\""))
  568. {
  569. itemstr = itemstr.Remove(0, 1);
  570. }
  571. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  572. {
  573. itemstr = itemstr.Remove(itemstr.Length - 2);
  574. }
  575. if (itemstr.EndsWith("\""))
  576. {
  577. itemstr = itemstr.Remove(itemstr.Length - 1);
  578. }
  579. if (itemstr.Contains("\\\""))
  580. {
  581. itemstr = itemstr.Replace("\\\"", "\"");
  582. }
  583. DataColumn DC = new DataColumn(itemstr, Type.GetType("System.String"));
  584. if (!box.Columns.Contains(itemc.Key))
  585. box.Columns.Add(DC);
  586. }
  587. }
  588. }
  589. for (int i = 0; i < barcode.Count; i++)
  590. {
  591. Dictionary<string, object> item = barcode[i];
  592. if (i == 0 && item["BAN_OUTBOXCODE"].ToString() != nowboxcode && nowboxcode != "")
  593. {
  594. try
  595. {
  596. itemBox = BoxBarcode.Find(p => p["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", "") == itemlast["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", ""));
  597. FillPrintData(box, itemBox, BoxParameter);
  598. PrintLabel(box, box_type, AllBoxname);
  599. Console.WriteLine("一箱" + itemlast["BAN_OUTBOXCODE"].ToString());
  600. }
  601. catch
  602. {
  603. nowboxcode = "";
  604. }
  605. }
  606. if (i > 0 && item["BAN_OUTBOXCODE"].ToString() != barcode[i - 1]["BAN_OUTBOXCODE"].ToString())
  607. {
  608. PrintLabel(dt,labelview, AllLabelname);
  609. itemlast = barcode[i - 1];
  610. try
  611. {
  612. itemBox = BoxBarcode.Find(p => p["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", "") == itemlast["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", ""));
  613. FillPrintData(box, itemBox, BoxParameter);
  614. PrintLabel(box, box_type, AllBoxname);
  615. Console.WriteLine("一箱" + itemlast["BAN_OUTBOXCODE"].ToString());
  616. }
  617. catch {}
  618. }
  619. FillPrintData(dt, item, parameter);
  620. }
  621. PrintLabel(dt, labelview,AllLabelname);
  622. itemlast = barcode[barcode.Count-1];
  623. if (((String)((Dictionary<string, object>)data)["currentPage"]).Replace("\"", "") == ((String)((Dictionary<string, object>)data)["totalPage"]).Replace("\"", ""))
  624. {
  625. try
  626. {
  627. itemBox = BoxBarcode.Find(p => p["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", "") == itemlast["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", ""));
  628. FillPrintData(box, itemBox, BoxParameter);
  629. PrintLabel(box, box_type, AllBoxname);
  630. Console.WriteLine("一箱" + itemBox["BAN_OUTBOXCODE"].ToString());
  631. }
  632. catch { }
  633. nowboxcode = "";
  634. }
  635. else {
  636. nowboxcode = itemlast["BAN_OUTBOXCODE"].ToString();
  637. }
  638. dt.Clear();
  639. }
  640. //给datable添加行(添加打印具体数据)
  641. private void FillPrintData(DataTable dt, Dictionary<string, object> item, List<Dictionary<string, object>> parameter1) {
  642. DataRow dr = dt.NewRow();
  643. foreach (var itemc in item)
  644. {
  645. Dictionary<string, object> Sidc = parameter1.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  646. if (Sidc != null)
  647. {
  648. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  649. {
  650. string itemstr = "";
  651. if (itemc.Value != null)
  652. {
  653. itemstr = itemc.Value.ToString();
  654. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  655. {
  656. itemstr = itemstr.Remove(0, 2);
  657. }
  658. if (itemstr.StartsWith("\""))
  659. {
  660. itemstr = itemstr.Remove(0, 1);
  661. }
  662. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  663. {
  664. itemstr = itemstr.Remove(itemstr.Length - 2);
  665. }
  666. if (itemstr.EndsWith("\""))
  667. {
  668. itemstr = itemstr.Remove(itemstr.Length - 1);
  669. }
  670. if (itemstr.Contains("\\\""))
  671. {
  672. itemstr = itemstr.Replace("\\\"", "\"");
  673. }
  674. }
  675. dr[itemc.Key] = itemstr;
  676. }
  677. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  678. {
  679. dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
  680. }
  681. }
  682. }
  683. dt.Rows.Add(dr);
  684. }
  685. private void PrintLabel(DataTable dt,string labelview,string LABELNAME)
  686. {
  687. try
  688. {
  689. BarcodeReport.Load(System.AppDomain.CurrentDomain.BaseDirectory + LABELNAME + ".frx");
  690. }
  691. catch
  692. {
  693. MessageBox.Show("未找到可使用的标签文件,请重试");
  694. return;
  695. }
  696. try
  697. {
  698. BarcodeReport.RegisterData(dt, labelview);
  699. BarcodeReport.GetDataSource(labelview).Enabled = true;
  700. BarcodeReport.PrintSettings.ShowDialog = false;
  701. BarcodeReport.PrintSettings.Printer = PrinterName;
  702. BarcodeReport.Print();
  703. CleanDataTable(dt);
  704. }
  705. catch (Exception ex)
  706. {
  707. MessageBox.Show(ex.Message + "打印出现错误,请检查标签文件选择是否正确");
  708. }
  709. }
  710. private void PrintPage(object sender, PrintPageEventArgs e)
  711. {
  712. Image i = bitmaps[index];
  713. Graphics g = e.Graphics;
  714. g.DrawImage(i, 4, 10, i.Width, i.Height);
  715. if (index < imagecount - 1)
  716. {
  717. e.HasMorePages = true;
  718. index++;
  719. }
  720. else
  721. {
  722. index = 0;
  723. e.HasMorePages = false;
  724. }
  725. }
  726. private void RequestHandler_Click(object sender, EventArgs e)
  727. {
  728. //设置打印机名称
  729. print.PrintDoc.PrinterSettings.PrinterName = print.Controls["Printer"].Text;
  730. PrinterName = print.Controls["Printer"].Text;
  731. RadioButton PRXA = (RadioButton)print.Controls["PRXA"];
  732. RadioButton PRPIC = (RadioButton)print.Controls["PRPIC"];
  733. //获取矩阵图的分辨率
  734. Graphics gr = print.PrintDoc.PrinterSettings.CreateMeasurementGraphics();
  735. print.browser.FocusedFrame.ExecuteJavaScriptAsync("(function(value,value1){dpi=value,printType=value1})('" + gr.DpiX + "','" + PrintType + "')");
  736. if (PType == "all")
  737. {
  738. print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmBarcodeAndBoxPrint').click();");
  739. }
  740. else
  741. {
  742. if (PRXA.Checked)
  743. {
  744. print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmZplPrint').click();");
  745. }
  746. else
  747. {
  748. print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmPicturePrint').click();");
  749. }
  750. }
  751. print.Close();
  752. }
  753. void filter_NotifyData(byte[] data)
  754. {
  755. if (NotifyMsg != null)
  756. {
  757. NotifyMsg(data);
  758. }
  759. }
  760. public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
  761. {
  762. return false;
  763. }
  764. public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
  765. {
  766. return false;
  767. }
  768. public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
  769. {
  770. return CefReturnValue.Continue;
  771. }
  772. public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
  773. {
  774. return false;
  775. }
  776. public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
  777. {
  778. return false;
  779. }
  780. public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath)
  781. {
  782. }
  783. public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
  784. {
  785. return false;
  786. }
  787. public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
  788. {
  789. return false;
  790. }
  791. public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status)
  792. {
  793. }
  794. public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser)
  795. {
  796. }
  797. public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
  798. {
  799. }
  800. public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl)
  801. {
  802. }
  803. bool IRequestHandler.OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
  804. {
  805. return false;
  806. }
  807. public void GetData()
  808. {
  809. }
  810. public static void CleanDataTable(DataTable dt)
  811. {
  812. for (int i = dt.Rows.Count - 1; i >= 0; i--)
  813. dt.Rows.Remove(dt.Rows[i]);
  814. }
  815. public static Dictionary<string, object> ToDictionary(string JsonData)
  816. {
  817. object Data = null;
  818. Dictionary<string, object> Dic = new Dictionary<string, object>();
  819. if (JsonData.StartsWith("["))
  820. {
  821. //如果目标直接就为数组类型,则将会直接输出一个Key为List的List<Dictionary<string, object>>集合
  822. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["List"];
  823. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  824. MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  825. foreach (Match ListItem in ListMatch)
  826. {
  827. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  828. }
  829. Data = List;
  830. Dic.Add("List", Data);
  831. }
  832. else
  833. {
  834. MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\}\s*\]|null|"".+?""(,+?)|"".+?""(\s*?)(\}+?)|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值
  835. foreach (Match item in Match)
  836. {
  837. try
  838. {
  839. if (item.Groups[2].ToString().StartsWith("["))
  840. {
  841. //如果目标是数组,将会输出一个Key为当前Json的List<Dictionary<string, object>>集合
  842. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["Json中的Key"];
  843. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  844. MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  845. foreach (Match ListItem in ListMatch)
  846. {
  847. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  848. }
  849. Data = List;
  850. }
  851. else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null
  852. else if (item.Groups[2].ToString().EndsWith(","))
  853. Data = item.Groups[2].ToString().Remove(item.Groups[2].ToString().Length - 1); //数据为数字、字符串中的一类,则去掉,直接写入
  854. else if (item.Groups[2].ToString().EndsWith("}"))
  855. Data = item.Groups[2].ToString().Remove(item.Groups[2].ToString().Length - 1); //数据为数字、字符串中的一类,则去掉,直接写入
  856. else
  857. Data = item.Groups[2].ToString();
  858. Dic.Add(item.Groups[1].ToString(), Data);
  859. }
  860. catch { }
  861. }
  862. }
  863. return Dic;
  864. }
  865. }
  866. }