RequestHandler.cs 42 KB

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