CPosPrinter.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148
  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. //POS_OutQRCode(order_no);
  685. //POS_OutBmp(L"D:\\200.jpg");
  686. //走纸几行再切
  687. POS_FeedLine();
  688. POS_FeedLine();
  689. POS_FeedLine();
  690. POS_FeedLine();
  691. POS_FeedLine();
  692. POS_CutPaper();
  693. }
  694. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  695. POS_Reset();
  696. //关闭设备
  697. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  698. {
  699. CloseHandle((*it).hPort);
  700. }
  701. }
  702. void CPosPrinter::PrintWaimaiOrderBiaoqian(CWaimaiOrder& order)
  703. {
  704. m_type = 1;
  705. std::string printer_usb = CSetting::GetParam("setting_biaoqian_printer_usb");
  706. std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
  707. //连接usb端口
  708. InitOneUsb(ws_printer_usb);
  709. //初始化标签打印机
  710. BIAOQIAN_Reset();
  711. std::vector<CWaimaiOrderItem> cur_printer_use = order.m_order_items;
  712. //先计算商品的总数量
  713. int foodNum = 0;
  714. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  715. {
  716. std::string quantity = (*it).m_quantity;
  717. foodNum += atoi(quantity.c_str());
  718. }
  719. //商品内容
  720. int curFoodNum = 0;
  721. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  722. {
  723. std::string food_name = (*it).m_food_name;
  724. std::string food_price = (*it).m_item_price;
  725. std::string quantity = (*it).m_quantity;
  726. //每份商品打印一个标签
  727. int nQuantity = atoi(quantity.c_str());
  728. for(int i = 0; i < nQuantity; i++)
  729. {
  730. std::string textData = "";
  731. curFoodNum++;
  732. std::string restaurant_number = order.m_restaurant_number;
  733. std::string order_num_info = "外卖 #" + CLewaimaiString::UTF8ToANSI(restaurant_number);
  734. textData += BIAOQIAN_TEXTGet(order_num_info, 16, 24, 1, 1);
  735. //打印份数
  736. std::string numInfo = to_string(curFoodNum) + "/" + to_string(foodNum);
  737. textData += BIAOQIAN_TEXTGet(numInfo, 160, 24, 1, 1);
  738. //每行最多显示12个汉字,这里要计算一下换行(要先转成ANSI格式)
  739. std::string handle_food_name = CLewaimaiString::UTF8ToANSI(food_name);
  740. std::vector<std::string> foodNameVector = HandleBiaoqianFoodname(handle_food_name);
  741. int nRow = 0;
  742. //考虑是否用大号字体
  743. bool is_big_name = false;
  744. if(foodNameVector.size() <= 2)
  745. {
  746. //不超过2行,可以用大的字体
  747. is_big_name = true;
  748. }
  749. LOG_INFO("foodNameVector size:" << foodNameVector.size());
  750. for(std::vector<std::string>::iterator it = foodNameVector.begin(); it != foodNameVector.end(); it++)
  751. {
  752. LOG_INFO("nRow:" << nRow << ", 准备打印商品名字:" << (*it).c_str());
  753. if(is_big_name)
  754. {
  755. textData += BIAOQIAN_TEXTGet(*it, 16, 64 + 64 * nRow, 1, 2);
  756. }
  757. else
  758. {
  759. textData += BIAOQIAN_TEXTGet(*it, 16, 64 + 32 * nRow, 1, 1);
  760. }
  761. nRow++;
  762. }
  763. //打印价格
  764. std::string priceInfo = CLewaimaiString::UTF8ToANSI(food_price) + "元";
  765. textData += BIAOQIAN_TEXTGet(priceInfo, 16, 188, 1, 1);
  766. //打印订单号
  767. std::string order_num = "订单号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  768. textData += BIAOQIAN_TEXTGet(order_num, 16, 218, 1, 1);
  769. WriteData(textData);
  770. BIAOQIAN_PRINT();
  771. BIAOQIAN_CLS();
  772. }
  773. }
  774. }
  775. void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
  776. {
  777. //设置模式,后面输出数据不会错
  778. m_type = 2;
  779. //读取厨房打印机信息
  780. std::vector<ChufangPrinter> total_printers = CSetting::getChufangPrints();
  781. for(std::vector<ChufangPrinter>::iterator it = total_printers.begin(); it != total_printers.end(); it++)
  782. {
  783. ChufangPrinter printer = *it;
  784. std::string ip = printer.ip;
  785. //初始化连接
  786. try
  787. {
  788. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(ip.c_str()), 9100);
  789. m_socket.connect(ep);
  790. }
  791. catch(std::exception& e)
  792. {
  793. std::string err = e.what();
  794. LOG_INFO("连接厨房打印机失败,IP地址:" << ip.c_str() << ",错误信息:" << err.c_str());
  795. //连接失败了,处理下一个厨房打印机
  796. continue;
  797. }
  798. std::string guige = printer.guige;
  799. std::string fendan = printer.fendan;
  800. std::vector<CWaimaiOrderItem> cur_printer_use = order.m_order_items;
  801. if(fendan == "0")
  802. {
  803. POS_Reset();
  804. POS_TextOut("派工单", true, true, 1);
  805. POS_FeedLine();
  806. POS_FeedLine();
  807. string order_type = "#" + order.m_restaurant_number + " ";
  808. if(order.m_is_selftake == "1")
  809. {
  810. POS_TextOut(order_type + "到店自取订单", false, false, 1);
  811. POS_FeedLine();
  812. POS_FeedLine();
  813. }
  814. else
  815. {
  816. POS_TextOut(order_type + "外卖订单", false, false, 1);
  817. POS_FeedLine();
  818. POS_FeedLine();
  819. }
  820. //预设选项
  821. if(order.m_order_field.size() > 1)
  822. {
  823. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  824. {
  825. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  826. POS_TextOut(order_field, false, false, 0);
  827. POS_FeedLine();
  828. }
  829. }
  830. //准备开始打印商品详情
  831. string lines;
  832. if(guige == "58")
  833. {
  834. lines = "--------------------------------";
  835. }
  836. else
  837. {
  838. lines = "------------------------------------------------";
  839. }
  840. POS_TextOut(lines);
  841. POS_FeedLine();
  842. //商品标题
  843. if(guige == "58")
  844. {
  845. POS_TextOut("商品 单价 数量 金额");
  846. }
  847. else
  848. {
  849. POS_TextOut("商品 单价 数量 金额");
  850. }
  851. POS_FeedLine();
  852. //商品内容
  853. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  854. {
  855. std::string food_name = (*it).m_food_name;
  856. std::string food_price = (*it).m_item_price;
  857. std::string quantity = (*it).m_quantity;
  858. //计算总价
  859. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  860. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  861. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  862. //先输出商品名字
  863. std::vector<string> m_names;
  864. if(guige == "58")
  865. {
  866. m_names = HandleFoodname(food_name, 1);
  867. }
  868. else
  869. {
  870. m_names = HandleFoodname(food_name, 2);
  871. }
  872. std::string firstLine = m_names[0];
  873. int nGuige;
  874. if(guige == "58")
  875. {
  876. nGuige = 1;
  877. }
  878. else
  879. {
  880. nGuige = 2;
  881. }
  882. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  883. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  884. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  885. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  886. POS_TextOut(firstLineShow, false, false, 0);
  887. POS_FeedLine();
  888. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  889. {
  890. POS_TextOut((*it), false, false, 0);
  891. POS_FeedLine();
  892. }
  893. }
  894. //判断是否有备注
  895. if(order.m_memo.length() > 0)
  896. {
  897. POS_FeedLine();
  898. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  899. POS_TextOut(address, false, false, 0);
  900. POS_FeedLine();
  901. }
  902. //结束商品详情打印
  903. POS_TextOut(lines);
  904. POS_FeedLine();
  905. POS_FeedLine();
  906. POS_FeedLine();
  907. POS_FeedLine();
  908. POS_FeedLine();
  909. POS_FeedLine();
  910. POS_CutPaper();
  911. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  912. POS_Reset();
  913. }
  914. else
  915. {
  916. POS_Reset();
  917. //分单模式下,每个商品打印一张单
  918. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  919. {
  920. POS_TextOut("派工单", true, true, 1);
  921. POS_FeedLine();
  922. POS_FeedLine();
  923. string order_type = "#" + order.m_restaurant_number + " ";
  924. if(order.m_is_selftake == "1")
  925. {
  926. POS_TextOut(order_type + "到店自取订单", false, false, 1);
  927. POS_FeedLine();
  928. POS_FeedLine();
  929. }
  930. else
  931. {
  932. POS_TextOut(order_type + "外卖订单", false, false, 1);
  933. POS_FeedLine();
  934. POS_FeedLine();
  935. }
  936. //预设选项
  937. if(order.m_order_field.size() > 1)
  938. {
  939. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  940. {
  941. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  942. POS_TextOut(order_field, false, false, 0);
  943. POS_FeedLine();
  944. }
  945. }
  946. //准备开始打印商品详情
  947. string lines;
  948. if(guige == "58")
  949. {
  950. lines = "--------------------------------";
  951. }
  952. else
  953. {
  954. lines = "------------------------------------------------";
  955. }
  956. POS_TextOut(lines);
  957. POS_FeedLine();
  958. //商品标题
  959. if(guige == "58")
  960. {
  961. POS_TextOut("商品 单价 数量 金额");
  962. }
  963. else
  964. {
  965. POS_TextOut("商品 单价 数量 金额");
  966. }
  967. POS_FeedLine();
  968. std::string food_name = (*it).m_food_name;
  969. std::string food_price = (*it).m_item_price;
  970. std::string quantity = (*it).m_quantity;
  971. //计算总价
  972. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  973. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  974. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  975. //先输出商品名字
  976. std::vector<string> m_names;
  977. if(guige == "58")
  978. {
  979. m_names = HandleFoodname(food_name, 1);
  980. }
  981. else
  982. {
  983. m_names = HandleFoodname(food_name, 2);
  984. }
  985. std::string firstLine = m_names[0];
  986. std::string priceShow = HandleFoodItemPrice(food_price, 2);
  987. std::string quantityShow = HandleFoodQuantity(quantity, 2);
  988. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
  989. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  990. POS_TextOut(firstLineShow, false, false, 0);
  991. POS_FeedLine();
  992. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  993. {
  994. POS_TextOut((*it), false, false, 0);
  995. POS_FeedLine();
  996. }
  997. //判断是否有备注
  998. if(order.m_memo.length() > 0)
  999. {
  1000. POS_FeedLine();
  1001. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  1002. POS_TextOut(address, false, false, 0);
  1003. POS_FeedLine();
  1004. }
  1005. //结束商品详情打印
  1006. POS_TextOut(lines);
  1007. POS_FeedLine();
  1008. POS_FeedLine();
  1009. POS_FeedLine();
  1010. POS_FeedLine();
  1011. POS_FeedLine();
  1012. POS_FeedLine();
  1013. POS_CutPaper();
  1014. }
  1015. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  1016. POS_Reset();
  1017. }
  1018. m_socket.close();
  1019. }
  1020. }
  1021. std::vector<std::wstring> CPosPrinter::getUsbDevices()
  1022. {
  1023. return m_usb_devices;
  1024. }
  1025. int CPosPrinter::WriteData(string msg)
  1026. {
  1027. return WriteBuf((unsigned char*)msg.c_str(), msg.length());
  1028. }
  1029. int CPosPrinter::WriteBuf(const unsigned char* buf, int len)
  1030. {
  1031. if(m_type == 1)
  1032. {
  1033. DWORD dwWrite;
  1034. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  1035. {
  1036. HANDLE hPort = (*it).hPort;
  1037. if((*it).type == 3)
  1038. {
  1039. //串口,同步写数据
  1040. WriteFile(hPort, buf, len, &dwWrite, NULL);
  1041. }
  1042. else
  1043. {
  1044. //usb和并口,异步写数据
  1045. OVERLAPPED overlap;
  1046. memset(&overlap, 0, sizeof(overlap));
  1047. BOOL rc = WriteFile(hPort, buf, len, &dwWrite, &overlap);
  1048. if(rc)
  1049. {
  1050. //LOG_INFO("writefile success immediately, handle:" << hPort);
  1051. }
  1052. else
  1053. {
  1054. if(GetLastError() == ERROR_IO_PENDING)
  1055. {
  1056. WaitForSingleObject(hPort, 1000);
  1057. //计算写入了多少字节的数据
  1058. DWORD numread;
  1059. rc = GetOverlappedResult(
  1060. hPort,
  1061. &overlap,
  1062. &numread,
  1063. FALSE
  1064. );
  1065. //LOG_INFO("Write success " << numread << " bytes");
  1066. }
  1067. }
  1068. }
  1069. }
  1070. return 1;
  1071. }
  1072. else
  1073. {
  1074. try
  1075. {
  1076. m_socket.write_some(boost::asio::buffer(buf, len));
  1077. }
  1078. catch(const std::exception& e)
  1079. {
  1080. LOG_INFO(e.what());
  1081. }
  1082. return 0;
  1083. }
  1084. }
  1085. /*
  1086. *测试打印机是否连接
  1087. **/
  1088. bool CPosPrinter::PortTest(HANDLE hPort)
  1089. {
  1090. //标签打印机的查询状态指令
  1091. char chInitCode[2] = { 0x1b, 0x40};
  1092. DWORD dwWrite;
  1093. OVERLAPPED overlap;
  1094. memset(&overlap, 0, sizeof(overlap));
  1095. BOOL rc = WriteFile(hPort, chInitCode, 2, &dwWrite, &overlap);
  1096. if(rc)
  1097. {
  1098. LOG_INFO("writefile success immediately, handle:" << hPort);
  1099. return true;
  1100. }
  1101. else
  1102. {
  1103. if(GetLastError() == ERROR_IO_PENDING)
  1104. {
  1105. DWORD ret = WaitForSingleObject(hPort, 1000);
  1106. if(ret == 0)
  1107. {
  1108. LOG_INFO("printer is connect, handle:" << hPort);
  1109. return true;
  1110. }
  1111. else
  1112. {
  1113. LOG_INFO("printer is not connect, handle:" << hPort);
  1114. }
  1115. }
  1116. }
  1117. return false;
  1118. }
  1119. int CPosPrinter::POS_Reset(void)
  1120. {
  1121. unsigned char s[2] = {0x1B, 0x40};
  1122. WriteBuf(s, 2);
  1123. return 0;
  1124. }
  1125. int CPosPrinter::POS_FeedLine(void)
  1126. {
  1127. unsigned char s[1] = {0x0A};
  1128. WriteBuf(s, 1);
  1129. return 0;
  1130. }
  1131. int CPosPrinter::POS_Feed(void)
  1132. {
  1133. unsigned char s[3] = { 0x1B, 0x4A, 0x00 };
  1134. WriteBuf(s, 3);
  1135. return 0;
  1136. }
  1137. int CPosPrinter::POS_SetMotionUnit(int x, int y)
  1138. {
  1139. string s;
  1140. s = "\x1D\x50\xB4\xB4";
  1141. WriteData(s);
  1142. s = "\x1B\x53";
  1143. WriteData(s);
  1144. return 0;
  1145. }
  1146. int CPosPrinter::POS_SET_MOVE_X()
  1147. {
  1148. unsigned char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00};
  1149. WriteBuf(s2, 6);
  1150. return 0;
  1151. }
  1152. int CPosPrinter::POS_MOVE_X()
  1153. {
  1154. unsigned char s[1] = { 0x09 };
  1155. WriteBuf(s, 1);
  1156. return 0;
  1157. }
  1158. int CPosPrinter::POS_SET_ABS_X(int x, int y)
  1159. {
  1160. unsigned char cx = (unsigned char)(x);
  1161. unsigned char cy = (unsigned char)(y);
  1162. unsigned char s1[4] = { 0x1B, 0x24, cx, cy };
  1163. WriteBuf(s1, 4);
  1164. return 0;
  1165. }
  1166. int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
  1167. {
  1168. unsigned char cx = (unsigned char)(x);
  1169. unsigned char cy = (unsigned char)(y);
  1170. unsigned char s1[4] = { 0x1D, 0x57, cx, cy };
  1171. WriteBuf(s1, 4);
  1172. return 0;
  1173. }
  1174. /*
  1175. *align_type:0 左对齐 1 居中对齐 2右对齐
  1176. **/
  1177. int CPosPrinter::POS_TextOut(string abc, bool is_double_width, bool is_double_height, int align_type)
  1178. {
  1179. if(is_double_width && is_double_height)
  1180. {
  1181. unsigned char s1[3] = { 0x1B, 0x21, 0x30 };
  1182. WriteBuf(s1, 3);
  1183. unsigned char s2[3] = { 0x1C, 0x21, 0x0c };
  1184. WriteBuf(s2, 3);
  1185. }
  1186. else if(is_double_width && !is_double_height)
  1187. {
  1188. unsigned char s1[3] = { 0x1B, 0x21, 0x20 };
  1189. WriteBuf(s1, 3);
  1190. unsigned char s2[3] = { 0x1C, 0x21, 0x04 };
  1191. WriteBuf(s2, 3);
  1192. }
  1193. else if(!is_double_width && is_double_height)
  1194. {
  1195. unsigned char s1[3] = { 0x1B, 0x21, 0x10 };
  1196. WriteBuf(s1, 3);
  1197. unsigned char s2[3] = { 0x1C, 0x21, 0x08 };
  1198. WriteBuf(s2, 3);
  1199. }
  1200. else
  1201. {
  1202. unsigned char s1[3] = { 0x1B, 0x21, 0x00 };
  1203. WriteBuf(s1, 3);
  1204. unsigned char s2[3] = { 0x1C, 0x57, 0x00 };
  1205. WriteBuf(s2, 3);
  1206. }
  1207. if(align_type == 0)
  1208. {
  1209. unsigned char s1[3] = { 0x1B, 0x61, 0x00 };
  1210. WriteBuf(s1, 3);
  1211. }
  1212. else if(align_type == 1)
  1213. {
  1214. unsigned char s1[3] = { 0x1B, 0x61, 0x01 };
  1215. WriteBuf(s1, 3);
  1216. }
  1217. else if(align_type == 2)
  1218. {
  1219. unsigned char s1[3] = { 0x1B, 0x61, 0x02 };
  1220. WriteBuf(s1, 3);
  1221. }
  1222. else
  1223. {
  1224. }
  1225. WriteData(abc);
  1226. return 0;
  1227. }
  1228. /*
  1229. *打印机切纸,直接切不走纸
  1230. **/
  1231. int CPosPrinter::POS_CutPaper()
  1232. {
  1233. unsigned char s[4] = { 0x1D, 0x56, 0x01};
  1234. WriteBuf(s, 3);
  1235. return 0;
  1236. }
  1237. /*
  1238. *先转换成ANSI格式才能传进来
  1239. **/
  1240. void CPosPrinter::POS_OutQRCode(std::string dataString)
  1241. {
  1242. QRcode * qrCode = nullptr;
  1243. qrCode = QRcode_encodeString(dataString.c_str(), 0, QR_ECLEVEL_H, QR_MODE_8, 1);
  1244. if (qrCode == nullptr)
  1245. {
  1246. printf("error occur\n");
  1247. return ;
  1248. }
  1249. unsigned int unWidth = qrCode->width;
  1250. unsigned int unWidthAdjusted = unWidth * 8;
  1251. unsigned int unHeightAjusted = unWidthAdjusted;
  1252. unsigned char* realData = new unsigned char[unWidthAdjusted * unHeightAjusted];
  1253. memset(realData, 0, unWidthAdjusted * unHeightAjusted);
  1254. unsigned char* pSourceData = qrCode->data;
  1255. for (unsigned int i = 0; i < unHeightAjusted; i++)
  1256. {
  1257. int realHeight = i / 8;
  1258. for (unsigned int j = 0; j < unWidthAdjusted; j++)
  1259. {
  1260. int realWeight = j / 8;
  1261. if (pSourceData[unWidth * realHeight + realWeight] & 1)
  1262. {
  1263. //表示这个像素要打印
  1264. realData[unWidthAdjusted * i + j] = 1;
  1265. }
  1266. else
  1267. {
  1268. realData[unWidthAdjusted * i + j] = 0;
  1269. }
  1270. }
  1271. }
  1272. //设置行间距为0
  1273. unsigned char data[3] = { 0x1B, 0x33, 0x00 };
  1274. WriteBuf(data, 3);
  1275. //设置图像居中对齐
  1276. unsigned char data2[3] = { 0x1B, 0x61, 0x01 };
  1277. WriteBuf(data2, 3);
  1278. //选择位图模式
  1279. unsigned char escBmp[5] = { 0x1B, 0x2A, 0x21, 0x00, 0x00 };
  1280. escBmp[3] = (unsigned char)(unWidthAdjusted % 256);
  1281. escBmp[4] = (unsigned char)(unWidthAdjusted / 256);
  1282. //循环图片像素打印图片
  1283. for (unsigned int i = 0; i < (unHeightAjusted / 24 + 1); i++)
  1284. {
  1285. //设置模式为位图模式
  1286. WriteBuf(escBmp, 5);
  1287. unsigned char* dataTmp = new unsigned char[unWidthAdjusted * 3];
  1288. memset(dataTmp, 0, unWidthAdjusted * 3);
  1289. //循环宽
  1290. for (unsigned int j = 0; j < unWidthAdjusted; j++)
  1291. {
  1292. for (unsigned int k = 0; k < 24; k++)
  1293. {
  1294. //找到有像素的区域,也就是未超出位图高度的区域
  1295. if (((i * 24) + k) < unHeightAjusted)
  1296. {
  1297. if (realData[unWidthAdjusted * (i * 24 + k) + j] == 1)
  1298. {
  1299. //如果本来是黑色的像素,那么就把对应的数据位设置为1
  1300. dataTmp[j * 3 + k / 8] += (unsigned char)(128 >> (k % 8));
  1301. }
  1302. }
  1303. }
  1304. }
  1305. WriteBuf(dataTmp, unWidthAdjusted * 3);
  1306. delete[] dataTmp;
  1307. }
  1308. //还原默认的行间距
  1309. unsigned char data3[2] = { 0x1B, 0x32 };
  1310. WriteBuf(data3, 2);
  1311. QRcode_free(qrCode);
  1312. delete[] realData;
  1313. return ;
  1314. }
  1315. void CPosPrinter::POS_OutBmp(std::wstring ImagePath)
  1316. {
  1317. //设置行间距为0
  1318. unsigned char data[3] = { 0x1B, 0x33, 0x00 };
  1319. WriteBuf(data, 3);
  1320. //设置图像居中对齐
  1321. unsigned char data2[3] = { 0x1B, 0x61, 0x01 };
  1322. WriteBuf(data2, 3);
  1323. CBitmapHelper helper;
  1324. helper.LoadImage(ImagePath.c_str());
  1325. Bitmap* bmp = helper.getBmp();
  1326. //先进行图片预处理
  1327. unsigned int nWidth = bmp->GetWidth();
  1328. unsigned int nHeight = bmp->GetHeight();
  1329. //压缩尺寸
  1330. if (nWidth > 380)
  1331. {
  1332. int newWidth = 380;
  1333. int newHeight = (int)(nHeight / (nWidth / 380.0));
  1334. helper.ScaleBitmap(newWidth, newHeight);;
  1335. nWidth = newWidth;
  1336. nHeight = newHeight;
  1337. }
  1338. //二值化
  1339. helper.Image2Values();
  1340. //获得最新的图像指针
  1341. bmp = helper.getBmp();
  1342. //选择位图模式
  1343. unsigned char escBmp[5] = { 0x1B, 0x2A, 0x21, 0x00, 0x00 };
  1344. escBmp[3] = (unsigned char)(nWidth % 256);
  1345. escBmp[4] = (unsigned char)(nWidth / 256);
  1346. Gdiplus::Color pixelColor;
  1347. //循环图片像素打印图片
  1348. for (unsigned int i = 0; i < (nHeight / 24 + 1); i++)
  1349. {
  1350. //设置模式为位图模式
  1351. WriteBuf(escBmp, 5);
  1352. unsigned char* dataTmp = new unsigned char[nWidth * 3];
  1353. memset(dataTmp, 0, nWidth * 3);
  1354. //循环宽
  1355. for (unsigned int j = 0; j < nWidth; j++)
  1356. {
  1357. for (unsigned int k = 0; k < 24; k++)
  1358. {
  1359. //找到有像素的区域,也就是未超出位图高度的区域
  1360. if (((i * 24) + k) < nHeight)
  1361. {
  1362. bmp->GetPixel(j, (i * 24) + k, &pixelColor);
  1363. unsigned char r = pixelColor.GetR();
  1364. if (r == 0)
  1365. {
  1366. //如果本来是黑色的像素,那么就把对应的数据位设置为1
  1367. dataTmp[j * 3 + k / 8] += (unsigned char)(128 >> (k % 8));
  1368. }
  1369. }
  1370. }
  1371. }
  1372. WriteBuf(dataTmp, nWidth * 3);
  1373. delete[] dataTmp;
  1374. }
  1375. //还原默认的行间距
  1376. unsigned char data3[2] = { 0x1B, 0x32 };
  1377. WriteBuf(data3, 2);
  1378. }
  1379. void CPosPrinter::BIAOQIAN_Reset()
  1380. {
  1381. char endTag[3] = {0x0d, 0x0a, 0x00};
  1382. std::string size = "SIZE 40 mm, 30 mm";
  1383. size += endTag;
  1384. std::string gap = "GAP 2 mm,0 mm";
  1385. gap += endTag;
  1386. std::string speed = "SPEED 4.0";
  1387. speed += endTag;
  1388. std::string density = "DENSITY 12";
  1389. density += endTag;
  1390. std::string REFERENCE = "REFERENCE 0,0";
  1391. REFERENCE += endTag;
  1392. std::string PEER = "SET PEEL OFF";
  1393. PEER += endTag;
  1394. std::string TEAR = "SET TEAR ON";
  1395. TEAR += endTag;
  1396. std::string cls = "CLS";
  1397. cls += endTag;
  1398. std::string data = size + gap + speed + density + REFERENCE + PEER + TEAR + cls;
  1399. WriteData(data);
  1400. }
  1401. /*
  1402. *前进一张纸
  1403. **/
  1404. void CPosPrinter::BIAOQIAN_FORMFEED()
  1405. {
  1406. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1407. std::string formfeed = "FORMFEED";
  1408. formfeed += endTag;
  1409. WriteData(formfeed);
  1410. }
  1411. void CPosPrinter::BIAOQIAN_TEXTOUT(std::string content, int x, int y, int x_multiplication, int y_multiplication)
  1412. {
  1413. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1414. string text = "TEXT ";
  1415. text += to_string(x) + "," + to_string(y) + ",";
  1416. text += "\"TSS24.BF2\",";
  1417. text += to_string(0) + ",";
  1418. text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
  1419. text += "\"" + content + "\"";
  1420. text += endTag;
  1421. WriteData(text);
  1422. }
  1423. std::string CPosPrinter::BIAOQIAN_TEXTGet(std::string content, int x, int y, int x_multiplication, int y_multiplication)
  1424. {
  1425. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1426. string text = "TEXT ";
  1427. text += to_string(x) + "," + to_string(y) + ",";
  1428. text += "\"TSS24.BF2\",";
  1429. text += to_string(0) + ",";
  1430. text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
  1431. text += "\"" + content + "\"";
  1432. text += endTag;
  1433. return text;
  1434. }
  1435. void CPosPrinter::BIAOQIAN_PRINT()
  1436. {
  1437. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1438. std::string print = "PRINT 1";
  1439. print += endTag;
  1440. WriteData(print);
  1441. }
  1442. void CPosPrinter::BIAOQIAN_CLS()
  1443. {
  1444. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1445. std::string cls = "CLS";
  1446. cls += endTag;
  1447. WriteData(cls);
  1448. }
  1449. void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
  1450. {
  1451. nHanzi = 0;
  1452. nZimu = 0;
  1453. const char* buffer = s.c_str();
  1454. while(*buffer != '\0')
  1455. {
  1456. if(!(*buffer >= 0 && *buffer <= 127))
  1457. {
  1458. //汉字
  1459. buffer++;
  1460. buffer++;
  1461. nHanzi++;
  1462. }
  1463. else
  1464. {
  1465. //字母
  1466. buffer++;
  1467. nZimu++;
  1468. }
  1469. }
  1470. }
  1471. /*
  1472. *规格 1:58mm 2:80mm
  1473. **/
  1474. std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
  1475. {
  1476. std::vector<std::string> newnameArray;
  1477. int nHanzi, nZimu;
  1478. CalWord(oldname, nHanzi, nZimu);
  1479. int nWidth = nHanzi * 2 + nZimu;
  1480. int maxWidth;
  1481. if(guige == 1)
  1482. {
  1483. maxWidth = 15;
  1484. }
  1485. else
  1486. {
  1487. maxWidth = 28;
  1488. }
  1489. if(nWidth <= maxWidth)
  1490. {
  1491. //对于nWidth补空格
  1492. for(int i = 0; i < maxWidth - nWidth; i++)
  1493. {
  1494. oldname += " ";
  1495. }
  1496. newnameArray.push_back(oldname);
  1497. return newnameArray;
  1498. }
  1499. //宽度大于15的情况,如果超过了,就要进行换行截取
  1500. const char* s = oldname.c_str();
  1501. int nTmp = 0;
  1502. int nTotal = 0;
  1503. while(*s != '\0')
  1504. {
  1505. if(!(*s >= 0 && *s <= 127))
  1506. {
  1507. //汉字的情况
  1508. s++;
  1509. nTmp++;
  1510. nTotal++;
  1511. s++;
  1512. nTmp++;
  1513. nTotal++;
  1514. }
  1515. else
  1516. {
  1517. //字母的情况
  1518. s++;
  1519. nTmp++;
  1520. nTotal++;
  1521. }
  1522. if(nTmp == maxWidth)
  1523. {
  1524. //这里开始要换行了
  1525. string newnameItem;
  1526. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1527. newnameArray.push_back(newnameItem);
  1528. nTmp = 0;
  1529. }
  1530. else if(nTmp == maxWidth - 1)
  1531. {
  1532. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1533. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1534. {
  1535. //下一个是中文,也要换行了,补齐一个空格
  1536. string newnameItem;
  1537. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1538. newnameItem += " ";
  1539. newnameArray.push_back(newnameItem);
  1540. nTmp = 0;
  1541. }
  1542. }
  1543. }
  1544. if(nTmp > 0)
  1545. {
  1546. //处理分隔后的最后一行
  1547. string newnameItem;
  1548. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1549. for(int i = 0; i < maxWidth - nTmp; i++)
  1550. {
  1551. newnameItem += " ";
  1552. }
  1553. newnameArray.push_back(newnameItem);
  1554. }
  1555. return newnameArray;
  1556. }
  1557. std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
  1558. {
  1559. double price = atof(oldprice.c_str());
  1560. if(price < 10.00)
  1561. {
  1562. //单位数
  1563. if(guige == 1)
  1564. {
  1565. return " " + oldprice;
  1566. }
  1567. else
  1568. {
  1569. return " " + oldprice;
  1570. }
  1571. }
  1572. else if(price > 9.99 && price < 100.00)
  1573. {
  1574. //双位数
  1575. if(guige == 1)
  1576. {
  1577. return " " + oldprice;
  1578. }
  1579. else
  1580. {
  1581. return " " + oldprice;
  1582. }
  1583. }
  1584. else if(price > 99.99 && price < 1000.00)
  1585. {
  1586. //三位数
  1587. if(guige == 1)
  1588. {
  1589. return " " + oldprice;
  1590. }
  1591. else
  1592. {
  1593. return " " + oldprice;
  1594. }
  1595. }
  1596. else
  1597. {
  1598. //四位数
  1599. if(guige == 1)
  1600. {
  1601. return " " + oldprice;
  1602. }
  1603. else
  1604. {
  1605. return " " + oldprice;
  1606. }
  1607. }
  1608. }
  1609. std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
  1610. {
  1611. int n = atoi(oldquantity.c_str());
  1612. if(n < 10)
  1613. {
  1614. if(guige == 1)
  1615. {
  1616. return " " + oldquantity;
  1617. }
  1618. else
  1619. {
  1620. return " " + oldquantity;
  1621. }
  1622. }
  1623. else if(n >= 10 && n <= 99)
  1624. {
  1625. if(guige == 1)
  1626. {
  1627. return " " + oldquantity;
  1628. }
  1629. else
  1630. {
  1631. return " " + oldquantity;
  1632. }
  1633. }
  1634. else
  1635. {
  1636. if(guige == 1)
  1637. {
  1638. return "" + oldquantity;
  1639. }
  1640. else
  1641. {
  1642. return " " + oldquantity;
  1643. }
  1644. }
  1645. }
  1646. std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
  1647. {
  1648. double price = atof(oldprice.c_str());
  1649. if(price < 10.00)
  1650. {
  1651. //单位数
  1652. if(guige == 1)
  1653. {
  1654. return " " + oldprice;
  1655. }
  1656. else
  1657. {
  1658. return " " + oldprice;
  1659. }
  1660. }
  1661. else if(price > 9.99 && price < 100.00)
  1662. {
  1663. //双位数
  1664. if(guige == 1)
  1665. {
  1666. return " " + oldprice;
  1667. }
  1668. else
  1669. {
  1670. return " " + oldprice;
  1671. }
  1672. }
  1673. else if(price > 99.99 && price < 1000.00)
  1674. {
  1675. //三位数
  1676. if(guige == 1)
  1677. {
  1678. return " " + oldprice;
  1679. }
  1680. else
  1681. {
  1682. return " " + oldprice;
  1683. }
  1684. }
  1685. else
  1686. {
  1687. //四位数
  1688. if(guige == 1)
  1689. {
  1690. return " " + oldprice;
  1691. }
  1692. else
  1693. {
  1694. return " " + oldprice;
  1695. }
  1696. }
  1697. }
  1698. std::vector<std::string> CPosPrinter::HandleBiaoqianFoodname(std::string oldname)
  1699. {
  1700. std::vector<std::string> newnameArray;
  1701. int nHanzi, nZimu;
  1702. CalWord(oldname, nHanzi, nZimu);
  1703. int nWidth = nHanzi * 2 + nZimu;
  1704. //40 *30mmm的标签,宽度最大24
  1705. int maxWidth = 24;
  1706. if(nWidth <= maxWidth)
  1707. {
  1708. //对于nWidth补空格
  1709. for(int i = 0; i < maxWidth - nWidth; i++)
  1710. {
  1711. oldname += " ";
  1712. }
  1713. newnameArray.push_back(oldname);
  1714. return newnameArray;
  1715. }
  1716. //宽度大于15的情况,如果超过了,就要进行换行截取
  1717. const char* s = oldname.c_str();
  1718. int nTmp = 0;
  1719. int nTotal = 0;
  1720. while(*s != '\0')
  1721. {
  1722. if(!(*s >= 0 && *s <= 127))
  1723. {
  1724. //汉字的情况
  1725. s++;
  1726. nTmp++;
  1727. nTotal++;
  1728. s++;
  1729. nTmp++;
  1730. nTotal++;
  1731. }
  1732. else
  1733. {
  1734. //字母的情况
  1735. s++;
  1736. nTmp++;
  1737. nTotal++;
  1738. }
  1739. if(nTmp == maxWidth)
  1740. {
  1741. //这里开始要换行了
  1742. string newnameItem;
  1743. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1744. newnameArray.push_back(newnameItem);
  1745. nTmp = 0;
  1746. }
  1747. else if(nTmp == maxWidth - 1)
  1748. {
  1749. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1750. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1751. {
  1752. //下一个是中文,也要换行了,补齐一个空格
  1753. string newnameItem;
  1754. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1755. newnameItem += " ";
  1756. newnameArray.push_back(newnameItem);
  1757. nTmp = 0;
  1758. }
  1759. }
  1760. }
  1761. if(nTmp > 0)
  1762. {
  1763. //处理分隔后的最后一行
  1764. string newnameItem;
  1765. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1766. for(int i = 0; i < maxWidth - nTmp; i++)
  1767. {
  1768. newnameItem += " ";
  1769. }
  1770. newnameArray.push_back(newnameItem);
  1771. }
  1772. return newnameArray;
  1773. }