CPosPrinter.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  1. #include "../pch/pch.h"
  2. #include "CPosPrinter.h"
  3. #include "../tool/CComHelper.h"
  4. #include "../helper/CBitmapHelper.h"
  5. #include <qrencode/qrencode.h>
  6. CPosPrinter::CPosPrinter(): m_socket(m_io)
  7. {
  8. }
  9. CPosPrinter::~CPosPrinter()
  10. {
  11. }
  12. /*
  13. *usbType是1表示只查找收银小票打印机,会排除已知的标签打印机
  14. *usbType为2表示所有usb打印机都查找,包含标签与非标签
  15. **/
  16. void CPosPrinter::InitUsb(int usbType)
  17. {
  18. //设置中文字符
  19. setlocale(LC_CTYPE, "chs");
  20. //取设备路径
  21. int nDevice = GetDevicePath((LPGUID)&USB_GUID, usbType);
  22. LOG_INFO("可用的USB打印机数量:" << nDevice);
  23. //添加usb端口
  24. int i = 0;
  25. while(i < nDevice)
  26. {
  27. std::string setting_biaoqian_printer_usb = CSetting::GetParam("setting_biaoqian_printer_usb");
  28. std::wstring ws_setting_biaoqian_printer_usb = CLewaimaiString::UTF8ToUnicode(setting_biaoqian_printer_usb);
  29. if (CSetting::GetParam("setting_is_new_waimai_biaoqian_printer") == "1" && ws_setting_biaoqian_printer_usb == m_usb_devices[i])
  30. {
  31. //碰到了一个标签打印机
  32. i++;
  33. continue;
  34. }
  35. LOG_INFO("准备打开端口 Port = " << m_usb_devices[i].c_str());
  36. HANDLE hPort = CreateFile(m_usb_devices[i].c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  37. if(hPort == INVALID_HANDLE_VALUE)
  38. {
  39. // 打开端口失败
  40. DWORD error = GetLastError();
  41. if(error == 2)
  42. {
  43. //没有指定的文件
  44. LOG_INFO("没有找对对应的usb端口");
  45. }
  46. else if(error == 5)
  47. {
  48. LOG_INFO("usb端口被占用!");
  49. }
  50. continue;
  51. }
  52. LOG_INFO("打开usb端口,准备进行打印机检测! hPort:" << hPort);
  53. if(PortTest(hPort) == true)
  54. {
  55. //端口测试连通,保存起来
  56. PrinterHandle newHandle;
  57. newHandle.hPort = hPort;
  58. newHandle.type = 1;
  59. m_hPorts.push_back(newHandle);
  60. }
  61. else
  62. {
  63. //端口打印机没有连接,那么就直接关闭掉并口,避免占用
  64. CloseHandle(hPort);
  65. }
  66. i++;
  67. }
  68. }
  69. bool CPosPrinter::InitOneUsb(wstring usb_path)
  70. {
  71. if (usb_path == L"")
  72. {
  73. return false;
  74. }
  75. HANDLE hPort = CreateFile(usb_path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  76. if(hPort == INVALID_HANDLE_VALUE)
  77. {
  78. // 打开端口失败
  79. DWORD error = GetLastError();
  80. if(error == 2)
  81. {
  82. //没有指定的文件
  83. LOG_INFO("没有找对对应的usb端口");
  84. }
  85. else if(error == 5)
  86. {
  87. LOG_INFO("usb端口被占用!");
  88. }
  89. return false;
  90. }
  91. LOG_INFO("打开usb端口,准备进行打印机检测! hPort:" << hPort);
  92. if(PortTest(hPort) == true)
  93. {
  94. //端口测试连通,保存起来
  95. PrinterHandle newHandle;
  96. newHandle.hPort = hPort;
  97. newHandle.type = 1;
  98. m_hPorts.push_back(newHandle);
  99. }
  100. else
  101. {
  102. //端口打印机没有连接,那么就直接关闭掉并口,避免占用
  103. CloseHandle(hPort);
  104. return false;
  105. }
  106. return true;
  107. }
  108. bool CPosPrinter::InitBingkou()
  109. {
  110. std::wstring LptStr = L"lpt1";
  111. HANDLE hPort = CreateFile(LptStr.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  112. if(hPort == INVALID_HANDLE_VALUE)
  113. {
  114. DWORD error = GetLastError();
  115. if(error == 2)
  116. {
  117. //没有指定的文件
  118. LOG_INFO("本机器没有并口!");
  119. }
  120. else if(error == 5)
  121. {
  122. LOG_INFO("并口被占用!");
  123. }
  124. return false;
  125. }
  126. else
  127. {
  128. //这个表示并口可以使用
  129. LOG_INFO("找到并口,准备进行打印机检测! hPort:" << hPort);
  130. if(PortTest(hPort) == true)
  131. {
  132. //并口测试连通,保存起来
  133. PrinterHandle newHandle;
  134. newHandle.hPort = hPort;
  135. newHandle.type = 2;
  136. m_hPorts.push_back(newHandle);
  137. }
  138. else
  139. {
  140. //并口打印机没有连接,那么就直接关闭掉并口,避免占用
  141. CloseHandle(hPort);
  142. return false;
  143. }
  144. }
  145. return true;
  146. }
  147. void CPosPrinter::InitCom()
  148. {
  149. CComHelper helper;
  150. std::vector<std::wstring> comVector = helper.getComPort();
  151. for(std::vector<std::wstring>::iterator it = comVector.begin(); it != comVector.end(); it++)
  152. {
  153. std::wstring com2Str = *it;
  154. HANDLE hPort = CreateFile(com2Str.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  155. if(hPort == INVALID_HANDLE_VALUE)
  156. {
  157. DWORD error = GetLastError();
  158. if(error == 2)
  159. {
  160. //没有指定的文件
  161. LOG_INFO("没有找到对应的串口 " << com2Str.c_str());
  162. }
  163. else if(error == 5)
  164. {
  165. LOG_INFO("串口被占用 " << com2Str.c_str());
  166. }
  167. }
  168. else
  169. {
  170. LOG_INFO(com2Str.c_str() << " success, handle:" << hPort);
  171. //扎到了可用的串口,设置串口波特率
  172. DCB dcb;
  173. dcb.DCBlength = sizeof(dcb);
  174. GetCommState(hPort, &dcb);
  175. //佳博的串口打印机,默认是这个波特率
  176. dcb.BaudRate = 19200;
  177. if(!SetCommState(hPort, &dcb))
  178. {
  179. LOG_INFO("set baudRate failed!");
  180. CloseHandle(hPort);
  181. continue;
  182. }
  183. //设定通讯端口超时参数
  184. COMMTIMEOUTS tmouts;
  185. tmouts.ReadIntervalTimeout = 100;
  186. tmouts.ReadTotalTimeoutMultiplier = 100;
  187. tmouts.ReadTotalTimeoutConstant = 100;
  188. tmouts.WriteTotalTimeoutConstant = 100;
  189. tmouts.WriteTotalTimeoutMultiplier = 100;
  190. SetCommTimeouts(hPort, &tmouts);
  191. //设置端口缓冲
  192. SetupComm(hPort, 1024, 1024);
  193. //清除通讯端口缓存
  194. PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT);
  195. //保存端口
  196. PrinterHandle newHandle;
  197. newHandle.hPort = hPort;
  198. newHandle.type = 3;
  199. m_hPorts.push_back(newHandle);
  200. }
  201. }
  202. }
  203. /*
  204. *找到所有可用的小票打印机,包括USB、并口、串口3个类型,注意这里不包含网口
  205. *智能识别模式不再自动处理串口,串口的需要人工去选择类型
  206. **/
  207. void CPosPrinter::InitShouyin()
  208. {
  209. //开始添加usb
  210. InitUsb();
  211. //开始添加并口的端口
  212. InitBingkou();
  213. }
  214. /*
  215. *获取CreateFile的USB端口号
  216. **/
  217. int CPosPrinter::GetDevicePath(LPGUID lpGuid, int usbType)
  218. {
  219. HDEVINFO hDevInfoSet;
  220. SP_DEVINFO_DATA spDevInfoData;
  221. SP_DEVICE_INTERFACE_DATA ifData;
  222. PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
  223. int nCount;
  224. int nTotle;
  225. BOOL bResult;
  226. //这2个字符串,用于根据usb的名字对比是否为打印机设备
  227. wstring strUSBPrint = TEXT("USB 打印支持");
  228. //xp上是英文
  229. wstring strUSBPrint_EN = L"USB Printing Support";
  230. // 取得一个该GUID相关的设备信息集句柄
  231. hDevInfoSet = ::SetupDiGetClassDevs(lpGuid, // class GUID
  232. NULL, // 无关键字
  233. NULL, // 不指定父窗口句柄
  234. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
  235. // 失败...
  236. if(hDevInfoSet == INVALID_HANDLE_VALUE)
  237. {
  238. LOG_INFO("SetupDiGetClassDevs failed \r\n");
  239. return 0;
  240. }
  241. // 申请设备接口数据空间
  242. pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
  243. pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  244. nTotle = -1;
  245. nCount = 0;
  246. bResult = TRUE;
  247. // 设备序号=0,1,2... 逐一测试设备接口,到失败为止
  248. while(bResult)
  249. {
  250. nTotle++;
  251. spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  252. // 枚举符合该GUID的设备接口
  253. bResult = ::SetupDiEnumDeviceInfo(
  254. hDevInfoSet, // 设备信息集句柄
  255. (ULONG)nTotle, // 设备信息集里的设备序号
  256. &spDevInfoData); // 设备接口信息
  257. if(bResult)
  258. {
  259. DWORD DataT;
  260. TCHAR buf[MAX_PATH] = { 0 };
  261. DWORD nSize = 0;
  262. // get Friendly Name or Device Description
  263. if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  264. SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  265. {
  266. }
  267. else if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  268. SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  269. {
  270. }
  271. else
  272. {
  273. lstrcpy(buf, _T("Unknown"));
  274. }
  275. wstring ws_buf = buf;
  276. //是否是要找的设备类型
  277. if(ws_buf != strUSBPrint && ws_buf != strUSBPrint_EN)
  278. {
  279. LOG_INFO("not good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
  280. continue;
  281. }
  282. LOG_INFO("good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
  283. ifData.cbSize = sizeof(ifData);
  284. // 枚舉符合該GUID的設備接口
  285. bResult = ::SetupDiEnumDeviceInterfaces(
  286. hDevInfoSet, // 設備信息集句柄
  287. NULL, // 不需額外的設備描述
  288. lpGuid, // GUID
  289. (ULONG)nTotle, // 設備信息集里的設備序號
  290. &ifData); // 設備接口信息
  291. if(bResult)
  292. {
  293. // 取得该设备接口的细节(设备路径)
  294. bResult = SetupDiGetInterfaceDeviceDetail(
  295. hDevInfoSet, // 设备信息集句柄
  296. &ifData, // 设备接口信息
  297. pDetail, // 设备接口细节(设备路径)
  298. INTERFACE_DETAIL_SIZE, // 输出缓冲区大小
  299. NULL, // 不需计算输出缓冲区大小(直接用设定值)
  300. NULL); // 不需额外的设备描述
  301. if(bResult)
  302. {
  303. wstring DevicePath = pDetail->DevicePath;
  304. wstring vid = DevicePath.substr(DevicePath.find(_T("vid_"), 0) + 4, 4);
  305. wstring pid = DevicePath.substr(DevicePath.find(_T("pid_"), 0) + 4, 4);
  306. LOG_INFO("Vid:" << vid.c_str() << ", Pid:" << pid.c_str());
  307. if(usbType == 1 && GetPrinterType(vid, pid) == 2)
  308. {
  309. //标签打印机,暂时不处理打印
  310. LOG_INFO("标签打印机,暂时不打印!");
  311. continue;
  312. }
  313. // 复制设备路径到输出缓冲区
  314. m_usb_devices.push_back(DevicePath);
  315. // 调整计数值
  316. nCount++;
  317. LOG_INFO("Cnt = " << nCount << ",pDetail->DevicePath =" << pDetail->DevicePath);
  318. }
  319. }
  320. }
  321. else
  322. {
  323. //LOG_INFO("SetupDiEnumDeviceInfo FAILED!@");
  324. }
  325. }
  326. // 释放设备接口数据空间
  327. ::GlobalFree(pDetail);
  328. // 关闭设备信息集句柄
  329. ::SetupDiDestroyDeviceInfoList(hDevInfoSet);
  330. return nCount;
  331. }
  332. /*
  333. *返回打印机的类型, 1:普通热敏打印机 2:标签打印机
  334. **/
  335. int CPosPrinter::GetPrinterType(wstring vid, wstring pid)
  336. {
  337. if(vid == L"6868" && pid == L"0500")
  338. {
  339. //佳博标签打印机
  340. return 2;
  341. }
  342. return 1;
  343. }
  344. /*
  345. *打印收银小票
  346. **/
  347. void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
  348. {
  349. //设置模式,后面输出数据不会错
  350. m_type = 1;
  351. std::string printer_leixing = CSetting::GetParam("setting_printer_leixing");
  352. if(printer_leixing == "auto")
  353. {
  354. InitShouyin();
  355. }
  356. else if(printer_leixing == "usb")
  357. {
  358. std::string printer_usb = CSetting::GetParam("setting_printer_usb");
  359. std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
  360. bool ret = InitOneUsb(ws_printer_usb);
  361. if (ret == false)
  362. {
  363. return;
  364. }
  365. }
  366. else if(printer_leixing == "bingkou")
  367. {
  368. bool ret = InitBingkou();
  369. if (ret == false)
  370. {
  371. return;
  372. }
  373. }
  374. else if(printer_leixing == "chuankou")
  375. {
  376. InitCom();
  377. }
  378. else if(printer_leixing == "wangkou")
  379. {
  380. m_type = 2;
  381. std::string wangkou_ip = CSetting::GetParam("setting_printer_wangkou_ip");
  382. //初始化连接
  383. try
  384. {
  385. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(wangkou_ip.c_str()), 9100);
  386. m_socket.connect(ep);
  387. }
  388. catch(std::exception& e)
  389. {
  390. std::string err = e.what();
  391. LOG_INFO("打印机失败,IP地址:" << wangkou_ip.c_str() << ",错误信息:" << err.c_str());
  392. }
  393. }
  394. //读取当前收银打印机的设置
  395. std::string guige = CSetting::GetParam("setting_printer_guige");
  396. std::string lianshu = CSetting::GetParam("setting_printer_lianshu");
  397. int n_lianshu = atoi(lianshu.c_str());
  398. //正式开始打印
  399. for(int i = 0; i < n_lianshu; i++)
  400. {
  401. POS_Reset();
  402. string shop_name = "#" + order.m_restaurant_number + " " + CLewaimaiString::UTF8ToANSI(order.m_shop_name);
  403. POS_TextOut(shop_name, true, true, 1);
  404. POS_FeedLine();
  405. POS_FeedLine();
  406. bool setting_printer_quhuo_big = false;
  407. if(CSetting::GetParam("setting_printer_quhuo_big") == "1")
  408. {
  409. setting_printer_quhuo_big = true;
  410. }
  411. if(order.m_is_selftake == "1")
  412. {
  413. POS_TextOut("到店自取订单", false, setting_printer_quhuo_big, 1);
  414. POS_FeedLine();
  415. POS_FeedLine();
  416. }
  417. else
  418. {
  419. POS_TextOut("外卖订单", false, setting_printer_quhuo_big, 1);
  420. POS_FeedLine();
  421. }
  422. POS_FeedLine();
  423. //订单来源
  424. string from_type = "订单来源:" + CLewaimaiString::UTF8ToANSI(order.m_from_type);
  425. POS_TextOut(from_type);
  426. POS_FeedLine();
  427. //订单号
  428. bool setting_printer_dingdanhao_big = false;
  429. if(CSetting::GetParam("setting_printer_dingdanhao_big") == "1")
  430. {
  431. setting_printer_dingdanhao_big = true;
  432. }
  433. string order_num = "订 单 号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  434. POS_TextOut(order_num, false, setting_printer_dingdanhao_big);
  435. POS_FeedLine();
  436. //下单时间
  437. bool setting_printer_xiadanshijian_big = false;
  438. if(CSetting::GetParam("setting_printer_xiadanshijian_big") == "1")
  439. {
  440. setting_printer_xiadanshijian_big = true;
  441. }
  442. string order_date = "下单时间:" + CLewaimaiString::UTF8ToANSI(order.m_order_date);
  443. POS_TextOut(order_date, false, setting_printer_xiadanshijian_big);
  444. POS_FeedLine();
  445. //配送时间
  446. bool setting_printer_peisongshijian_big = false;
  447. if(CSetting::GetParam("setting_printer_peisongshijian_big") == "1")
  448. {
  449. setting_printer_peisongshijian_big = true;
  450. }
  451. if(order.m_is_selftake == "1")
  452. {
  453. string date = "自取时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  454. POS_TextOut(date, false, setting_printer_peisongshijian_big);
  455. POS_FeedLine();
  456. }
  457. else
  458. {
  459. string date = "配送时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  460. POS_TextOut(date, false, setting_printer_peisongshijian_big);
  461. POS_FeedLine();
  462. }
  463. //姓名
  464. bool setting_printer_name_big = false;
  465. if(CSetting::GetParam("setting_printer_name_big") == "1")
  466. {
  467. setting_printer_name_big = true;
  468. }
  469. if(order.m_is_selftake != "1")
  470. {
  471. string name = "姓名:" + CLewaimaiString::UTF8ToANSI(order.m_customer_name);
  472. POS_TextOut(name, false, setting_printer_name_big);
  473. POS_FeedLine();
  474. }
  475. //电话
  476. bool setting_printer_phone_big = false;
  477. if(CSetting::GetParam("setting_printer_phone_big") == "1")
  478. {
  479. setting_printer_phone_big = true;
  480. }
  481. string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
  482. POS_TextOut(phone, false, setting_printer_phone_big);
  483. POS_FeedLine();
  484. //地址
  485. bool setting_printer_address_big = false;
  486. if(CSetting::GetParam("setting_printer_address_big") == "1")
  487. {
  488. setting_printer_address_big = true;
  489. }
  490. if(order.m_is_selftake != "1")
  491. {
  492. string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
  493. POS_TextOut(address, false, setting_printer_address_big);
  494. POS_FeedLine();
  495. }
  496. //预设选项
  497. if(order.m_order_field.size() > 1)
  498. {
  499. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  500. {
  501. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  502. POS_TextOut(order_field, false, false, 0);
  503. POS_FeedLine();
  504. }
  505. }
  506. //准备开始打印商品详情
  507. string lines;
  508. if(guige == "58")
  509. {
  510. lines = "--------------------------------";
  511. }
  512. else
  513. {
  514. lines = "------------------------------------------------";
  515. }
  516. POS_TextOut(lines);
  517. POS_FeedLine();
  518. //商品标题
  519. bool setting_printer_shangpin_big = false;
  520. if(CSetting::GetParam("setting_printer_shangpin_big") == "1")
  521. {
  522. setting_printer_shangpin_big = true;
  523. }
  524. if(guige == "58")
  525. {
  526. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  527. }
  528. else
  529. {
  530. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  531. }
  532. POS_FeedLine();
  533. //商品内容
  534. for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  535. {
  536. std::string food_name = (*it).m_food_name;
  537. std::string food_price = (*it).m_item_price;
  538. std::string quantity = (*it).m_quantity;
  539. //计算总价
  540. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  541. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  542. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  543. //先输出商品名字
  544. std::vector<string> m_names;
  545. if(guige == "58")
  546. {
  547. m_names = HandleFoodname(food_name, 1);
  548. }
  549. else
  550. {
  551. m_names = HandleFoodname(food_name, 2);
  552. }
  553. std::string firstLine = m_names[0];
  554. int nGuige;
  555. if(guige == "58")
  556. {
  557. nGuige = 1;
  558. }
  559. else
  560. {
  561. nGuige = 2;
  562. }
  563. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  564. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  565. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  566. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  567. POS_TextOut(firstLineShow, false, setting_printer_shangpin_big);
  568. POS_FeedLine();
  569. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  570. {
  571. POS_TextOut((*it), false, setting_printer_shangpin_big);
  572. POS_FeedLine();
  573. }
  574. }
  575. //判断是否有备注
  576. if(order.m_memo.length() > 0)
  577. {
  578. bool setting_printer_memo_big = false;
  579. if(CSetting::GetParam("setting_printer_memo_big") == "1")
  580. {
  581. setting_printer_memo_big = true;
  582. }
  583. POS_FeedLine();
  584. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  585. POS_TextOut(address, false, setting_printer_memo_big);
  586. POS_FeedLine();
  587. }
  588. //结束商品详情打印
  589. POS_TextOut(lines);
  590. POS_FeedLine();
  591. //开始打印其他的费用
  592. bool is_other_money = false;
  593. bool setting_printer_price_big = false;
  594. if(CSetting::GetParam("setting_printer_price_big") == "1")
  595. {
  596. setting_printer_price_big = true;
  597. }
  598. if(order.m_delivery > 0)
  599. {
  600. string delivery = "配送费:" + CLewaimaiString::DoubleToString((double)order.m_delivery, 2);
  601. POS_TextOut(delivery, false, setting_printer_price_big);
  602. POS_FeedLine();
  603. is_other_money = true;
  604. }
  605. if(order.m_is_dabao == "1")
  606. {
  607. string dabao = "打包费:" + CLewaimaiString::UTF8ToANSI(order.m_dabao_money);
  608. POS_TextOut(dabao, false, setting_printer_price_big);
  609. POS_FeedLine();
  610. is_other_money = true;
  611. }
  612. //开始显示增值服务费
  613. if(order.m_addservie.size() > 0)
  614. {
  615. for(std::vector<CWaimaiOrderField>::iterator it = order.m_addservie.begin(); it != order.m_addservie.end(); it++)
  616. {
  617. string addservice = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::DoubleToString((double)atof((*it).value.c_str()), 2);
  618. POS_TextOut(addservice, false, setting_printer_price_big);
  619. POS_FeedLine();
  620. is_other_money = true;
  621. }
  622. is_other_money = true;
  623. }
  624. if(order.m_discount < 10)
  625. {
  626. string discount = "打折:" + CLewaimaiString::DoubleToString((double)order.m_discount, 2) + "折";
  627. POS_TextOut(discount, false, setting_printer_price_big);
  628. POS_FeedLine();
  629. is_other_money = true;
  630. }
  631. if(order.m_is_member_discount == "1")
  632. {
  633. string member_discount = "会员优惠:-" + order.m_member_discount;
  634. POS_TextOut(member_discount, false, setting_printer_price_big);
  635. POS_FeedLine();
  636. is_other_money = true;
  637. }
  638. if(order.m_is_firstcut == "1")
  639. {
  640. string firstcut = "首单优惠:-" + order.m_firstcut_value;
  641. POS_TextOut(firstcut, false, setting_printer_price_big);
  642. POS_FeedLine();
  643. is_other_money = true;
  644. }
  645. //满减
  646. string::size_type position = order.m_promotion.find(":");
  647. if(position != order.m_promotion.npos)
  648. {
  649. string promotion_value;
  650. promotion_value.assign(order.m_promotion, position + 1);
  651. string promotion = "满减优惠:-" + CLewaimaiString::DoubleToString((double)(atof(promotion_value.c_str())), 2);
  652. POS_TextOut(promotion, false, setting_printer_price_big);
  653. POS_FeedLine();
  654. is_other_money = true;
  655. }
  656. if(order.m_coupon > 0)
  657. {
  658. string coupon = "优惠券:-" + CLewaimaiString::DoubleToString((double)order.m_coupon, 2);
  659. POS_TextOut(coupon, false, setting_printer_price_big);
  660. POS_FeedLine();
  661. is_other_money = true;
  662. }
  663. if(is_other_money)
  664. {
  665. POS_TextOut(lines);
  666. POS_FeedLine();
  667. }
  668. //最后显示总价
  669. POS_TextOut("总计:¥" + CLewaimaiString::DoubleToString(order.m_price, 2), false, setting_printer_price_big, 2);
  670. POS_FeedLine();
  671. //显示付款方式
  672. bool setting_printer_pay_big = false;
  673. if(CSetting::GetParam("setting_printer_pay_big") == "1")
  674. {
  675. setting_printer_pay_big = true;
  676. }
  677. POS_TextOut("支付方式:" + CLewaimaiString::UTF8ToANSI(order.m_pay_type), false, setting_printer_pay_big, 2);
  678. POS_FeedLine();
  679. POS_FeedLine();;
  680. POS_FeedLine();
  681. //准备开始打印二维码
  682. std::string order_no = order.m_order_no;
  683. order_no = CLewaimaiString::UTF8ToANSI(order_no);
  684. if (order.m_show_qiangdan_qrcode == "1")
  685. {
  686. POS_OutQRCode(order_no);
  687. POS_FeedLine();
  688. POS_FeedLine();
  689. POS_TextOut("请使用配送员app扫码绑定订单", false, false, 1);
  690. }
  691. //走纸几行再切
  692. POS_FeedLine();
  693. POS_FeedLine();
  694. POS_FeedLine();
  695. POS_FeedLine();
  696. POS_FeedLine();
  697. POS_CutPaper();
  698. }
  699. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  700. POS_Reset();
  701. //关闭设备
  702. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  703. {
  704. CloseHandle((*it).hPort);
  705. }
  706. }
  707. void CPosPrinter::PrintWaimaiOrderBiaoqian(CWaimaiOrder& order)
  708. {
  709. m_type = 1;
  710. std::string printer_usb = CSetting::GetParam("setting_biaoqian_printer_usb");
  711. std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
  712. //连接usb端口
  713. InitOneUsb(ws_printer_usb);
  714. //初始化标签打印机
  715. BIAOQIAN_Reset();
  716. std::vector<CWaimaiOrderItem> cur_printer_use = order.m_order_items;
  717. //先计算商品的总数量
  718. int foodNum = 0;
  719. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  720. {
  721. std::string quantity = (*it).m_quantity;
  722. foodNum += atoi(quantity.c_str());
  723. }
  724. //商品内容
  725. int curFoodNum = 0;
  726. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  727. {
  728. std::string food_name = (*it).m_food_name;
  729. std::string food_price = (*it).m_item_price;
  730. std::string quantity = (*it).m_quantity;
  731. //每份商品打印一个标签
  732. int nQuantity = atoi(quantity.c_str());
  733. for(int i = 0; i < nQuantity; i++)
  734. {
  735. std::string textData = "";
  736. curFoodNum++;
  737. std::string restaurant_number = order.m_restaurant_number;
  738. std::string order_num_info = "外卖 #" + CLewaimaiString::UTF8ToANSI(restaurant_number);
  739. textData += BIAOQIAN_TEXTGet(order_num_info, 16, 24, 1, 1);
  740. //打印份数
  741. std::string numInfo = to_string(curFoodNum) + "/" + to_string(foodNum);
  742. textData += BIAOQIAN_TEXTGet(numInfo, 160, 24, 1, 1);
  743. //每行最多显示12个汉字,这里要计算一下换行(要先转成ANSI格式)
  744. std::string handle_food_name = CLewaimaiString::UTF8ToANSI(food_name);
  745. std::vector<std::string> foodNameVector = HandleBiaoqianFoodname(handle_food_name);
  746. int nRow = 0;
  747. //考虑是否用大号字体
  748. bool is_big_name = false;
  749. if(foodNameVector.size() <= 2)
  750. {
  751. //不超过2行,可以用大的字体
  752. is_big_name = true;
  753. }
  754. LOG_INFO("foodNameVector size:" << foodNameVector.size());
  755. for(std::vector<std::string>::iterator it = foodNameVector.begin(); it != foodNameVector.end(); it++)
  756. {
  757. LOG_INFO("nRow:" << nRow << ", 准备打印商品名字:" << (*it).c_str());
  758. if(is_big_name)
  759. {
  760. textData += BIAOQIAN_TEXTGet(*it, 16, 64 + 64 * nRow, 1, 2);
  761. }
  762. else
  763. {
  764. textData += BIAOQIAN_TEXTGet(*it, 16, 64 + 32 * nRow, 1, 1);
  765. }
  766. nRow++;
  767. }
  768. //打印价格
  769. std::string priceInfo = CLewaimaiString::UTF8ToANSI(food_price) + "元";
  770. textData += BIAOQIAN_TEXTGet(priceInfo, 16, 188, 1, 1);
  771. //打印订单号
  772. std::string order_num = "订单号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  773. textData += BIAOQIAN_TEXTGet(order_num, 16, 218, 1, 1);
  774. WriteData(textData);
  775. BIAOQIAN_PRINT();
  776. BIAOQIAN_CLS();
  777. }
  778. }
  779. }
  780. void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
  781. {
  782. //设置模式,后面输出数据不会错
  783. m_type = 2;
  784. //读取厨房打印机信息
  785. std::vector<ChufangPrinter> total_printers = CSetting::getChufangPrints();
  786. for(std::vector<ChufangPrinter>::iterator it = total_printers.begin(); it != total_printers.end(); it++)
  787. {
  788. ChufangPrinter printer = *it;
  789. std::string ip = printer.ip;
  790. //初始化连接
  791. try
  792. {
  793. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(ip.c_str()), 9100);
  794. m_socket.connect(ep);
  795. }
  796. catch(std::exception& e)
  797. {
  798. std::string err = e.what();
  799. LOG_INFO("连接厨房打印机失败,IP地址:" << ip.c_str() << ",错误信息:" << err.c_str());
  800. //连接失败了,处理下一个厨房打印机
  801. continue;
  802. }
  803. std::string guige = printer.guige;
  804. std::string fendan = printer.fendan;
  805. std::vector<CWaimaiOrderItem> cur_printer_use = order.m_order_items;
  806. if(fendan == "0")
  807. {
  808. POS_Reset();
  809. POS_TextOut("派工单", true, true, 1);
  810. POS_FeedLine();
  811. POS_FeedLine();
  812. string order_type = "#" + order.m_restaurant_number + " ";
  813. if(order.m_is_selftake == "1")
  814. {
  815. POS_TextOut(order_type + "到店自取订单", false, false, 1);
  816. POS_FeedLine();
  817. POS_FeedLine();
  818. }
  819. else
  820. {
  821. POS_TextOut(order_type + "外卖订单", false, false, 1);
  822. POS_FeedLine();
  823. POS_FeedLine();
  824. }
  825. //预设选项
  826. if(order.m_order_field.size() > 1)
  827. {
  828. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  829. {
  830. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  831. POS_TextOut(order_field, false, false, 0);
  832. POS_FeedLine();
  833. }
  834. }
  835. //准备开始打印商品详情
  836. string lines;
  837. if(guige == "58")
  838. {
  839. lines = "--------------------------------";
  840. }
  841. else
  842. {
  843. lines = "------------------------------------------------";
  844. }
  845. POS_TextOut(lines);
  846. POS_FeedLine();
  847. //商品标题
  848. if(guige == "58")
  849. {
  850. POS_TextOut("商品 单价 数量 金额");
  851. }
  852. else
  853. {
  854. POS_TextOut("商品 单价 数量 金额");
  855. }
  856. POS_FeedLine();
  857. //商品内容
  858. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  859. {
  860. std::string food_name = (*it).m_food_name;
  861. std::string food_price = (*it).m_item_price;
  862. std::string quantity = (*it).m_quantity;
  863. //计算总价
  864. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  865. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  866. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  867. //先输出商品名字
  868. std::vector<string> m_names;
  869. if(guige == "58")
  870. {
  871. m_names = HandleFoodname(food_name, 1);
  872. }
  873. else
  874. {
  875. m_names = HandleFoodname(food_name, 2);
  876. }
  877. std::string firstLine = m_names[0];
  878. int nGuige;
  879. if(guige == "58")
  880. {
  881. nGuige = 1;
  882. }
  883. else
  884. {
  885. nGuige = 2;
  886. }
  887. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  888. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  889. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  890. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  891. POS_TextOut(firstLineShow, false, false, 0);
  892. POS_FeedLine();
  893. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  894. {
  895. POS_TextOut((*it), false, false, 0);
  896. POS_FeedLine();
  897. }
  898. }
  899. //判断是否有备注
  900. if(order.m_memo.length() > 0)
  901. {
  902. POS_FeedLine();
  903. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  904. POS_TextOut(address, false, false, 0);
  905. POS_FeedLine();
  906. }
  907. //结束商品详情打印
  908. POS_TextOut(lines);
  909. POS_FeedLine();
  910. POS_FeedLine();
  911. POS_FeedLine();
  912. POS_FeedLine();
  913. POS_FeedLine();
  914. POS_FeedLine();
  915. POS_CutPaper();
  916. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  917. POS_Reset();
  918. }
  919. else
  920. {
  921. POS_Reset();
  922. //分单模式下,每个商品打印一张单
  923. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  924. {
  925. POS_TextOut("派工单", true, true, 1);
  926. POS_FeedLine();
  927. POS_FeedLine();
  928. string order_type = "#" + order.m_restaurant_number + " ";
  929. if(order.m_is_selftake == "1")
  930. {
  931. POS_TextOut(order_type + "到店自取订单", false, false, 1);
  932. POS_FeedLine();
  933. POS_FeedLine();
  934. }
  935. else
  936. {
  937. POS_TextOut(order_type + "外卖订单", false, false, 1);
  938. POS_FeedLine();
  939. POS_FeedLine();
  940. }
  941. //预设选项
  942. if(order.m_order_field.size() > 1)
  943. {
  944. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  945. {
  946. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  947. POS_TextOut(order_field, false, false, 0);
  948. POS_FeedLine();
  949. }
  950. }
  951. //准备开始打印商品详情
  952. string lines;
  953. if(guige == "58")
  954. {
  955. lines = "--------------------------------";
  956. }
  957. else
  958. {
  959. lines = "------------------------------------------------";
  960. }
  961. POS_TextOut(lines);
  962. POS_FeedLine();
  963. //商品标题
  964. if(guige == "58")
  965. {
  966. POS_TextOut("商品 单价 数量 金额");
  967. }
  968. else
  969. {
  970. POS_TextOut("商品 单价 数量 金额");
  971. }
  972. POS_FeedLine();
  973. std::string food_name = (*it).m_food_name;
  974. std::string food_price = (*it).m_item_price;
  975. std::string quantity = (*it).m_quantity;
  976. //计算总价
  977. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  978. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  979. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  980. //先输出商品名字
  981. std::vector<string> m_names;
  982. if(guige == "58")
  983. {
  984. m_names = HandleFoodname(food_name, 1);
  985. }
  986. else
  987. {
  988. m_names = HandleFoodname(food_name, 2);
  989. }
  990. std::string firstLine = m_names[0];
  991. std::string priceShow = HandleFoodItemPrice(food_price, 2);
  992. std::string quantityShow = HandleFoodQuantity(quantity, 2);
  993. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
  994. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  995. POS_TextOut(firstLineShow, false, false, 0);
  996. POS_FeedLine();
  997. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  998. {
  999. POS_TextOut((*it), false, false, 0);
  1000. POS_FeedLine();
  1001. }
  1002. //判断是否有备注
  1003. if(order.m_memo.length() > 0)
  1004. {
  1005. POS_FeedLine();
  1006. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  1007. POS_TextOut(address, false, false, 0);
  1008. POS_FeedLine();
  1009. }
  1010. //结束商品详情打印
  1011. POS_TextOut(lines);
  1012. POS_FeedLine();
  1013. POS_FeedLine();
  1014. POS_FeedLine();
  1015. POS_FeedLine();
  1016. POS_FeedLine();
  1017. POS_FeedLine();
  1018. POS_CutPaper();
  1019. }
  1020. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  1021. POS_Reset();
  1022. }
  1023. m_socket.close();
  1024. }
  1025. }
  1026. std::vector<std::wstring> CPosPrinter::getUsbDevices()
  1027. {
  1028. return m_usb_devices;
  1029. }
  1030. int CPosPrinter::WriteData(string msg)
  1031. {
  1032. return WriteBuf((unsigned char*)msg.c_str(), msg.length());
  1033. }
  1034. int CPosPrinter::WriteBuf(const unsigned char* buf, int len)
  1035. {
  1036. if(m_type == 1)
  1037. {
  1038. DWORD dwWrite;
  1039. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  1040. {
  1041. HANDLE hPort = (*it).hPort;
  1042. if((*it).type == 3)
  1043. {
  1044. //串口,同步写数据
  1045. WriteFile(hPort, buf, len, &dwWrite, NULL);
  1046. }
  1047. else
  1048. {
  1049. //usb和并口,异步写数据
  1050. OVERLAPPED overlap;
  1051. memset(&overlap, 0, sizeof(overlap));
  1052. BOOL rc = WriteFile(hPort, buf, len, &dwWrite, &overlap);
  1053. if(rc)
  1054. {
  1055. //LOG_INFO("writefile success immediately, handle:" << hPort);
  1056. }
  1057. else
  1058. {
  1059. if(GetLastError() == ERROR_IO_PENDING)
  1060. {
  1061. WaitForSingleObject(hPort, 1000);
  1062. //计算写入了多少字节的数据
  1063. DWORD numread;
  1064. rc = GetOverlappedResult(
  1065. hPort,
  1066. &overlap,
  1067. &numread,
  1068. FALSE
  1069. );
  1070. //LOG_INFO("Write success " << numread << " bytes");
  1071. }
  1072. }
  1073. }
  1074. }
  1075. return 1;
  1076. }
  1077. else
  1078. {
  1079. try
  1080. {
  1081. m_socket.write_some(boost::asio::buffer(buf, len));
  1082. }
  1083. catch(const std::exception& e)
  1084. {
  1085. LOG_INFO(e.what());
  1086. }
  1087. return 0;
  1088. }
  1089. }
  1090. /*
  1091. *测试打印机是否连接
  1092. **/
  1093. bool CPosPrinter::PortTest(HANDLE hPort)
  1094. {
  1095. //标签打印机的查询状态指令
  1096. char chInitCode[2] = { 0x1b, 0x40};
  1097. DWORD dwWrite;
  1098. OVERLAPPED overlap;
  1099. memset(&overlap, 0, sizeof(overlap));
  1100. BOOL rc = WriteFile(hPort, chInitCode, 2, &dwWrite, &overlap);
  1101. if(rc)
  1102. {
  1103. LOG_INFO("writefile success immediately, handle:" << hPort);
  1104. return true;
  1105. }
  1106. else
  1107. {
  1108. if(GetLastError() == ERROR_IO_PENDING)
  1109. {
  1110. DWORD ret = WaitForSingleObject(hPort, 1000);
  1111. if(ret == 0)
  1112. {
  1113. LOG_INFO("printer is connect, handle:" << hPort);
  1114. return true;
  1115. }
  1116. else
  1117. {
  1118. LOG_INFO("printer is not connect, handle:" << hPort);
  1119. }
  1120. }
  1121. }
  1122. return false;
  1123. }
  1124. int CPosPrinter::POS_Reset(void)
  1125. {
  1126. unsigned char s[2] = {0x1B, 0x40};
  1127. WriteBuf(s, 2);
  1128. return 0;
  1129. }
  1130. int CPosPrinter::POS_FeedLine(void)
  1131. {
  1132. unsigned char s[1] = {0x0A};
  1133. WriteBuf(s, 1);
  1134. return 0;
  1135. }
  1136. int CPosPrinter::POS_Feed(void)
  1137. {
  1138. unsigned char s[3] = { 0x1B, 0x4A, 0x00 };
  1139. WriteBuf(s, 3);
  1140. return 0;
  1141. }
  1142. int CPosPrinter::POS_SetMotionUnit(int x, int y)
  1143. {
  1144. string s;
  1145. s = "\x1D\x50\xB4\xB4";
  1146. WriteData(s);
  1147. s = "\x1B\x53";
  1148. WriteData(s);
  1149. return 0;
  1150. }
  1151. int CPosPrinter::POS_SET_MOVE_X()
  1152. {
  1153. unsigned char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00};
  1154. WriteBuf(s2, 6);
  1155. return 0;
  1156. }
  1157. int CPosPrinter::POS_MOVE_X()
  1158. {
  1159. unsigned char s[1] = { 0x09 };
  1160. WriteBuf(s, 1);
  1161. return 0;
  1162. }
  1163. int CPosPrinter::POS_SET_ABS_X(int x, int y)
  1164. {
  1165. unsigned char cx = (unsigned char)(x);
  1166. unsigned char cy = (unsigned char)(y);
  1167. unsigned char s1[4] = { 0x1B, 0x24, cx, cy };
  1168. WriteBuf(s1, 4);
  1169. return 0;
  1170. }
  1171. int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
  1172. {
  1173. unsigned char cx = (unsigned char)(x);
  1174. unsigned char cy = (unsigned char)(y);
  1175. unsigned char s1[4] = { 0x1D, 0x57, cx, cy };
  1176. WriteBuf(s1, 4);
  1177. return 0;
  1178. }
  1179. /*
  1180. *align_type:0 左对齐 1 居中对齐 2右对齐
  1181. **/
  1182. int CPosPrinter::POS_TextOut(string abc, bool is_double_width, bool is_double_height, int align_type)
  1183. {
  1184. if(is_double_width && is_double_height)
  1185. {
  1186. unsigned char s1[3] = { 0x1B, 0x21, 0x30 };
  1187. WriteBuf(s1, 3);
  1188. unsigned char s2[3] = { 0x1C, 0x21, 0x0c };
  1189. WriteBuf(s2, 3);
  1190. }
  1191. else if(is_double_width && !is_double_height)
  1192. {
  1193. unsigned char s1[3] = { 0x1B, 0x21, 0x20 };
  1194. WriteBuf(s1, 3);
  1195. unsigned char s2[3] = { 0x1C, 0x21, 0x04 };
  1196. WriteBuf(s2, 3);
  1197. }
  1198. else if(!is_double_width && is_double_height)
  1199. {
  1200. unsigned char s1[3] = { 0x1B, 0x21, 0x10 };
  1201. WriteBuf(s1, 3);
  1202. unsigned char s2[3] = { 0x1C, 0x21, 0x08 };
  1203. WriteBuf(s2, 3);
  1204. }
  1205. else
  1206. {
  1207. unsigned char s1[3] = { 0x1B, 0x21, 0x00 };
  1208. WriteBuf(s1, 3);
  1209. unsigned char s2[3] = { 0x1C, 0x57, 0x00 };
  1210. WriteBuf(s2, 3);
  1211. }
  1212. if(align_type == 0)
  1213. {
  1214. unsigned char s1[3] = { 0x1B, 0x61, 0x00 };
  1215. WriteBuf(s1, 3);
  1216. }
  1217. else if(align_type == 1)
  1218. {
  1219. unsigned char s1[3] = { 0x1B, 0x61, 0x01 };
  1220. WriteBuf(s1, 3);
  1221. }
  1222. else if(align_type == 2)
  1223. {
  1224. unsigned char s1[3] = { 0x1B, 0x61, 0x02 };
  1225. WriteBuf(s1, 3);
  1226. }
  1227. else
  1228. {
  1229. }
  1230. WriteData(abc);
  1231. return 0;
  1232. }
  1233. /*
  1234. *打印机切纸,直接切不走纸
  1235. **/
  1236. int CPosPrinter::POS_CutPaper()
  1237. {
  1238. unsigned char s[4] = { 0x1D, 0x56, 0x01};
  1239. WriteBuf(s, 3);
  1240. return 0;
  1241. }
  1242. /*
  1243. *先转换成ANSI格式才能传进来
  1244. **/
  1245. void CPosPrinter::POS_OutQRCode(std::string dataString)
  1246. {
  1247. QRcode * qrCode = nullptr;
  1248. qrCode = QRcode_encodeString(dataString.c_str(), 0, QR_ECLEVEL_H, QR_MODE_8, 1);
  1249. if (qrCode == nullptr)
  1250. {
  1251. printf("error occur\n");
  1252. return ;
  1253. }
  1254. unsigned int unWidth = qrCode->width;
  1255. unsigned int unWidthAdjusted = unWidth * 8;
  1256. unsigned int unHeightAjusted = unWidthAdjusted;
  1257. unsigned char* realData = new unsigned char[unWidthAdjusted * unHeightAjusted];
  1258. memset(realData, 0, unWidthAdjusted * unHeightAjusted);
  1259. unsigned char* pSourceData = qrCode->data;
  1260. for (unsigned int i = 0; i < unHeightAjusted; i++)
  1261. {
  1262. int realHeight = i / 8;
  1263. for (unsigned int j = 0; j < unWidthAdjusted; j++)
  1264. {
  1265. int realWeight = j / 8;
  1266. if (pSourceData[unWidth * realHeight + realWeight] & 1)
  1267. {
  1268. //表示这个像素要打印
  1269. realData[unWidthAdjusted * i + j] = 1;
  1270. }
  1271. else
  1272. {
  1273. realData[unWidthAdjusted * i + j] = 0;
  1274. }
  1275. }
  1276. }
  1277. //设置行间距为0
  1278. unsigned char data[3] = { 0x1B, 0x33, 0x00 };
  1279. WriteBuf(data, 3);
  1280. //设置图像居中对齐
  1281. unsigned char data2[3] = { 0x1B, 0x61, 0x01 };
  1282. WriteBuf(data2, 3);
  1283. //选择位图模式
  1284. unsigned char escBmp[5] = { 0x1B, 0x2A, 0x21, 0x00, 0x00 };
  1285. escBmp[3] = (unsigned char)(unWidthAdjusted % 256);
  1286. escBmp[4] = (unsigned char)(unWidthAdjusted / 256);
  1287. //循环图片像素打印图片
  1288. for (unsigned int i = 0; i < (unHeightAjusted / 24 + 1); i++)
  1289. {
  1290. //设置模式为位图模式
  1291. WriteBuf(escBmp, 5);
  1292. unsigned char* dataTmp = new unsigned char[unWidthAdjusted * 3];
  1293. memset(dataTmp, 0, unWidthAdjusted * 3);
  1294. //循环宽
  1295. for (unsigned int j = 0; j < unWidthAdjusted; j++)
  1296. {
  1297. for (unsigned int k = 0; k < 24; k++)
  1298. {
  1299. //找到有像素的区域,也就是未超出位图高度的区域
  1300. if (((i * 24) + k) < unHeightAjusted)
  1301. {
  1302. if (realData[unWidthAdjusted * (i * 24 + k) + j] == 1)
  1303. {
  1304. //如果本来是黑色的像素,那么就把对应的数据位设置为1
  1305. dataTmp[j * 3 + k / 8] += (unsigned char)(128 >> (k % 8));
  1306. }
  1307. }
  1308. }
  1309. }
  1310. WriteBuf(dataTmp, unWidthAdjusted * 3);
  1311. delete[] dataTmp;
  1312. }
  1313. //还原默认的行间距
  1314. unsigned char data3[2] = { 0x1B, 0x32 };
  1315. WriteBuf(data3, 2);
  1316. QRcode_free(qrCode);
  1317. delete[] realData;
  1318. return ;
  1319. }
  1320. void CPosPrinter::POS_OutBmp(std::wstring ImagePath)
  1321. {
  1322. //设置行间距为0
  1323. unsigned char data[3] = { 0x1B, 0x33, 0x00 };
  1324. WriteBuf(data, 3);
  1325. //设置图像居中对齐
  1326. unsigned char data2[3] = { 0x1B, 0x61, 0x01 };
  1327. WriteBuf(data2, 3);
  1328. CBitmapHelper helper;
  1329. helper.LoadImage(ImagePath.c_str());
  1330. Bitmap* bmp = helper.getBmp();
  1331. //先进行图片预处理
  1332. unsigned int nWidth = bmp->GetWidth();
  1333. unsigned int nHeight = bmp->GetHeight();
  1334. //压缩尺寸
  1335. if (nWidth > 380)
  1336. {
  1337. int newWidth = 380;
  1338. int newHeight = (int)(nHeight / (nWidth / 380.0));
  1339. helper.ScaleBitmap(newWidth, newHeight);;
  1340. nWidth = newWidth;
  1341. nHeight = newHeight;
  1342. }
  1343. //二值化
  1344. helper.Image2Values();
  1345. //获得最新的图像指针
  1346. bmp = helper.getBmp();
  1347. //选择位图模式
  1348. unsigned char escBmp[5] = { 0x1B, 0x2A, 0x21, 0x00, 0x00 };
  1349. escBmp[3] = (unsigned char)(nWidth % 256);
  1350. escBmp[4] = (unsigned char)(nWidth / 256);
  1351. Gdiplus::Color pixelColor;
  1352. //循环图片像素打印图片
  1353. for (unsigned int i = 0; i < (nHeight / 24 + 1); i++)
  1354. {
  1355. //设置模式为位图模式
  1356. WriteBuf(escBmp, 5);
  1357. unsigned char* dataTmp = new unsigned char[nWidth * 3];
  1358. memset(dataTmp, 0, nWidth * 3);
  1359. //循环宽
  1360. for (unsigned int j = 0; j < nWidth; j++)
  1361. {
  1362. for (unsigned int k = 0; k < 24; k++)
  1363. {
  1364. //找到有像素的区域,也就是未超出位图高度的区域
  1365. if (((i * 24) + k) < nHeight)
  1366. {
  1367. bmp->GetPixel(j, (i * 24) + k, &pixelColor);
  1368. unsigned char r = pixelColor.GetR();
  1369. if (r == 0)
  1370. {
  1371. //如果本来是黑色的像素,那么就把对应的数据位设置为1
  1372. dataTmp[j * 3 + k / 8] += (unsigned char)(128 >> (k % 8));
  1373. }
  1374. }
  1375. }
  1376. }
  1377. WriteBuf(dataTmp, nWidth * 3);
  1378. delete[] dataTmp;
  1379. }
  1380. //还原默认的行间距
  1381. unsigned char data3[2] = { 0x1B, 0x32 };
  1382. WriteBuf(data3, 2);
  1383. }
  1384. void CPosPrinter::BIAOQIAN_Reset()
  1385. {
  1386. char endTag[3] = {0x0d, 0x0a, 0x00};
  1387. std::string size = "SIZE 40 mm, 30 mm";
  1388. size += endTag;
  1389. std::string gap = "GAP 2 mm,0 mm";
  1390. gap += endTag;
  1391. std::string speed = "SPEED 4.0";
  1392. speed += endTag;
  1393. std::string density = "DENSITY 12";
  1394. density += endTag;
  1395. std::string REFERENCE = "REFERENCE 0,0";
  1396. REFERENCE += endTag;
  1397. std::string PEER = "SET PEEL OFF";
  1398. PEER += endTag;
  1399. std::string TEAR = "SET TEAR ON";
  1400. TEAR += endTag;
  1401. std::string cls = "CLS";
  1402. cls += endTag;
  1403. std::string data = size + gap + speed + density + REFERENCE + PEER + TEAR + cls;
  1404. WriteData(data);
  1405. }
  1406. /*
  1407. *前进一张纸
  1408. **/
  1409. void CPosPrinter::BIAOQIAN_FORMFEED()
  1410. {
  1411. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1412. std::string formfeed = "FORMFEED";
  1413. formfeed += endTag;
  1414. WriteData(formfeed);
  1415. }
  1416. void CPosPrinter::BIAOQIAN_TEXTOUT(std::string content, int x, int y, int x_multiplication, int y_multiplication)
  1417. {
  1418. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1419. string text = "TEXT ";
  1420. text += to_string(x) + "," + to_string(y) + ",";
  1421. text += "\"TSS24.BF2\",";
  1422. text += to_string(0) + ",";
  1423. text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
  1424. text += "\"" + content + "\"";
  1425. text += endTag;
  1426. WriteData(text);
  1427. }
  1428. std::string CPosPrinter::BIAOQIAN_TEXTGet(std::string content, int x, int y, int x_multiplication, int y_multiplication)
  1429. {
  1430. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1431. string text = "TEXT ";
  1432. text += to_string(x) + "," + to_string(y) + ",";
  1433. text += "\"TSS24.BF2\",";
  1434. text += to_string(0) + ",";
  1435. text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
  1436. text += "\"" + content + "\"";
  1437. text += endTag;
  1438. return text;
  1439. }
  1440. void CPosPrinter::BIAOQIAN_PRINT()
  1441. {
  1442. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1443. std::string print = "PRINT 1";
  1444. print += endTag;
  1445. WriteData(print);
  1446. }
  1447. void CPosPrinter::BIAOQIAN_CLS()
  1448. {
  1449. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1450. std::string cls = "CLS";
  1451. cls += endTag;
  1452. WriteData(cls);
  1453. }
  1454. void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
  1455. {
  1456. nHanzi = 0;
  1457. nZimu = 0;
  1458. const char* buffer = s.c_str();
  1459. while(*buffer != '\0')
  1460. {
  1461. if(!(*buffer >= 0 && *buffer <= 127))
  1462. {
  1463. //汉字
  1464. buffer++;
  1465. buffer++;
  1466. nHanzi++;
  1467. }
  1468. else
  1469. {
  1470. //字母
  1471. buffer++;
  1472. nZimu++;
  1473. }
  1474. }
  1475. }
  1476. /*
  1477. *规格 1:58mm 2:80mm
  1478. **/
  1479. std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
  1480. {
  1481. std::vector<std::string> newnameArray;
  1482. int nHanzi, nZimu;
  1483. CalWord(oldname, nHanzi, nZimu);
  1484. int nWidth = nHanzi * 2 + nZimu;
  1485. int maxWidth;
  1486. if(guige == 1)
  1487. {
  1488. maxWidth = 15;
  1489. }
  1490. else
  1491. {
  1492. maxWidth = 28;
  1493. }
  1494. if(nWidth <= maxWidth)
  1495. {
  1496. //对于nWidth补空格
  1497. for(int i = 0; i < maxWidth - nWidth; i++)
  1498. {
  1499. oldname += " ";
  1500. }
  1501. newnameArray.push_back(oldname);
  1502. return newnameArray;
  1503. }
  1504. //宽度大于15的情况,如果超过了,就要进行换行截取
  1505. const char* s = oldname.c_str();
  1506. int nTmp = 0;
  1507. int nTotal = 0;
  1508. while(*s != '\0')
  1509. {
  1510. if(!(*s >= 0 && *s <= 127))
  1511. {
  1512. //汉字的情况
  1513. s++;
  1514. nTmp++;
  1515. nTotal++;
  1516. s++;
  1517. nTmp++;
  1518. nTotal++;
  1519. }
  1520. else
  1521. {
  1522. //字母的情况
  1523. s++;
  1524. nTmp++;
  1525. nTotal++;
  1526. }
  1527. if(nTmp == maxWidth)
  1528. {
  1529. //这里开始要换行了
  1530. string newnameItem;
  1531. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1532. newnameArray.push_back(newnameItem);
  1533. nTmp = 0;
  1534. }
  1535. else if(nTmp == maxWidth - 1)
  1536. {
  1537. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1538. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1539. {
  1540. //下一个是中文,也要换行了,补齐一个空格
  1541. string newnameItem;
  1542. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1543. newnameItem += " ";
  1544. newnameArray.push_back(newnameItem);
  1545. nTmp = 0;
  1546. }
  1547. }
  1548. }
  1549. if(nTmp > 0)
  1550. {
  1551. //处理分隔后的最后一行
  1552. string newnameItem;
  1553. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1554. for(int i = 0; i < maxWidth - nTmp; i++)
  1555. {
  1556. newnameItem += " ";
  1557. }
  1558. newnameArray.push_back(newnameItem);
  1559. }
  1560. return newnameArray;
  1561. }
  1562. std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
  1563. {
  1564. double price = atof(oldprice.c_str());
  1565. if(price < 10.00)
  1566. {
  1567. //单位数
  1568. if(guige == 1)
  1569. {
  1570. return " " + oldprice;
  1571. }
  1572. else
  1573. {
  1574. return " " + oldprice;
  1575. }
  1576. }
  1577. else if(price > 9.99 && price < 100.00)
  1578. {
  1579. //双位数
  1580. if(guige == 1)
  1581. {
  1582. return " " + oldprice;
  1583. }
  1584. else
  1585. {
  1586. return " " + oldprice;
  1587. }
  1588. }
  1589. else if(price > 99.99 && price < 1000.00)
  1590. {
  1591. //三位数
  1592. if(guige == 1)
  1593. {
  1594. return " " + oldprice;
  1595. }
  1596. else
  1597. {
  1598. return " " + oldprice;
  1599. }
  1600. }
  1601. else
  1602. {
  1603. //四位数
  1604. if(guige == 1)
  1605. {
  1606. return " " + oldprice;
  1607. }
  1608. else
  1609. {
  1610. return " " + oldprice;
  1611. }
  1612. }
  1613. }
  1614. std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
  1615. {
  1616. int n = atoi(oldquantity.c_str());
  1617. if(n < 10)
  1618. {
  1619. if(guige == 1)
  1620. {
  1621. return " " + oldquantity;
  1622. }
  1623. else
  1624. {
  1625. return " " + oldquantity;
  1626. }
  1627. }
  1628. else if(n >= 10 && n <= 99)
  1629. {
  1630. if(guige == 1)
  1631. {
  1632. return " " + oldquantity;
  1633. }
  1634. else
  1635. {
  1636. return " " + oldquantity;
  1637. }
  1638. }
  1639. else
  1640. {
  1641. if(guige == 1)
  1642. {
  1643. return "" + oldquantity;
  1644. }
  1645. else
  1646. {
  1647. return " " + oldquantity;
  1648. }
  1649. }
  1650. }
  1651. std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
  1652. {
  1653. double price = atof(oldprice.c_str());
  1654. if(price < 10.00)
  1655. {
  1656. //单位数
  1657. if(guige == 1)
  1658. {
  1659. return " " + oldprice;
  1660. }
  1661. else
  1662. {
  1663. return " " + oldprice;
  1664. }
  1665. }
  1666. else if(price > 9.99 && price < 100.00)
  1667. {
  1668. //双位数
  1669. if(guige == 1)
  1670. {
  1671. return " " + oldprice;
  1672. }
  1673. else
  1674. {
  1675. return " " + oldprice;
  1676. }
  1677. }
  1678. else if(price > 99.99 && price < 1000.00)
  1679. {
  1680. //三位数
  1681. if(guige == 1)
  1682. {
  1683. return " " + oldprice;
  1684. }
  1685. else
  1686. {
  1687. return " " + oldprice;
  1688. }
  1689. }
  1690. else
  1691. {
  1692. //四位数
  1693. if(guige == 1)
  1694. {
  1695. return " " + oldprice;
  1696. }
  1697. else
  1698. {
  1699. return " " + oldprice;
  1700. }
  1701. }
  1702. }
  1703. std::vector<std::string> CPosPrinter::HandleBiaoqianFoodname(std::string oldname)
  1704. {
  1705. std::vector<std::string> newnameArray;
  1706. int nHanzi, nZimu;
  1707. CalWord(oldname, nHanzi, nZimu);
  1708. int nWidth = nHanzi * 2 + nZimu;
  1709. //40 *30mmm的标签,宽度最大24
  1710. int maxWidth = 24;
  1711. if(nWidth <= maxWidth)
  1712. {
  1713. //对于nWidth补空格
  1714. for(int i = 0; i < maxWidth - nWidth; i++)
  1715. {
  1716. oldname += " ";
  1717. }
  1718. newnameArray.push_back(oldname);
  1719. return newnameArray;
  1720. }
  1721. //宽度大于15的情况,如果超过了,就要进行换行截取
  1722. const char* s = oldname.c_str();
  1723. int nTmp = 0;
  1724. int nTotal = 0;
  1725. while(*s != '\0')
  1726. {
  1727. if(!(*s >= 0 && *s <= 127))
  1728. {
  1729. //汉字的情况
  1730. s++;
  1731. nTmp++;
  1732. nTotal++;
  1733. s++;
  1734. nTmp++;
  1735. nTotal++;
  1736. }
  1737. else
  1738. {
  1739. //字母的情况
  1740. s++;
  1741. nTmp++;
  1742. nTotal++;
  1743. }
  1744. if(nTmp == maxWidth)
  1745. {
  1746. //这里开始要换行了
  1747. string newnameItem;
  1748. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1749. newnameArray.push_back(newnameItem);
  1750. nTmp = 0;
  1751. }
  1752. else if(nTmp == maxWidth - 1)
  1753. {
  1754. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1755. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1756. {
  1757. //下一个是中文,也要换行了,补齐一个空格
  1758. string newnameItem;
  1759. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1760. newnameItem += " ";
  1761. newnameArray.push_back(newnameItem);
  1762. nTmp = 0;
  1763. }
  1764. }
  1765. }
  1766. if(nTmp > 0)
  1767. {
  1768. //处理分隔后的最后一行
  1769. string newnameItem;
  1770. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1771. for(int i = 0; i < maxWidth - nTmp; i++)
  1772. {
  1773. newnameItem += " ";
  1774. }
  1775. newnameArray.push_back(newnameItem);
  1776. }
  1777. return newnameArray;
  1778. }