Guest User

Untitled

a guest
Jan 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. /**
  2. * 使用方法:
  3. * 1、printApi.init();
  4. * 2、printApi.printOrder(vm.print);
  5. * 3、printApi.close();
  6. */
  7. window.printApi = (function(w, undefined){
  8. var devices = [];
  9. var device = app.getSettings('print_device') || null;
  10. var rate = 9600;
  11. var printControl = null;
  12.  
  13. var loadPlugin = function () {
  14. var _PRINT = 'PluginPrintFunction', B = window.plus.bridge;
  15.  
  16. this.getDevicePaths = function () {
  17. return B.execSync(_PRINT, "PluginGetDevice", []);
  18. }
  19.  
  20. //初始化信息
  21. this.initMachine = function (path ,rate, successCallback) {
  22. var success = typeof successCallback !== 'function' ? null : function(args){
  23. successCallback(args);
  24. }
  25. callbackID = B.callbackId(success , null);
  26. return B.exec(_PRINT, "PluginInit", [callbackID,path,rate]);
  27. }
  28.  
  29. this.closeMachine = function () {
  30. return B.execSync(_PRINT, "PluginClose", []);
  31. }
  32.  
  33. this.printPic = function (base64 , successCallback) {
  34. var success = typeof successCallback !== 'function' ? null : function(args){
  35. successCallback(args);
  36. }
  37.  
  38. callbackID = B.callbackId(success , null);
  39. return B.exec(_PRINT, "PluginPrintPic", [callbackID , base64]);
  40. }
  41.  
  42. this.printText = function (txt , successCallback) {
  43. var success = typeof successCallback !== 'function' ? null : function(args){
  44. successCallback(args);
  45. }
  46. callbackID = B.callbackId(success , null);
  47. return B.exec(_PRINT, "PluginPrintText", [callbackID , txt]);
  48. }
  49.  
  50. this.runPaper = function (row ,successCallback) {
  51. var success = typeof successCallback !== 'function' ? null : function(args){
  52. successCallback(args);
  53. }
  54. callbackID = B.callbackId(success , null);
  55. return B.exec(_PRINT, "PluginRunPaper", [callbackID , row]);
  56. }
  57.  
  58. window.plus.print = this;
  59. };
  60.  
  61. var getDevicePaths = function(callback){
  62. if (typeof plus.print == 'undefined') {
  63. mui.alert('打印插件未安装,请联系客服');
  64. return ;
  65. }
  66. try {
  67. var msg = plus.print.getDevicePaths();
  68. devices = JSON.parse(msg);
  69. if (devices.length < 1) {
  70. device = null;
  71. return ;
  72. }
  73. var buttons = [];
  74. mui.each(devices, function(i, n){
  75. buttons.push({
  76. title: n
  77. });
  78. });
  79. plus.nativeUI.actionSheet({
  80. title: "请选择打印机",
  81. cancel: "取消",
  82. buttons: buttons
  83. }, function(b) {
  84. if (b.index > 0) {
  85. device = devices[b.index - 1];
  86.  
  87. callback();
  88. }
  89. });
  90. } catch (e) {
  91. devices = [];
  92. device = null;
  93. mui.alert('未检测到打印机设备,请确认');
  94. }
  95. };
  96.  
  97. var init = function(){
  98. if (printControl === null) {
  99. printControl = new loadPlugin();
  100. }
  101. if (device === null) {
  102. getDevicePaths(init);
  103. }
  104. var result = plus.print.initMachine(device, rate, function(msg) {
  105. console.log("print init1: " + msg);
  106. });
  107. console.log("print init2: " + result);
  108.  
  109. return true;
  110. };
  111.  
  112. var printOrder = function(data) {
  113. if (device == null) {
  114. mui.toast('打印插件初始化失败');
  115. return ;
  116. }
  117. var txt = "门店:" + data.order.store_name + "\n";
  118. txt = txt + '单号:' + data.order.order_sn + "\n";
  119. if (data.order.user_name) {
  120. txt = txt + '会员:' + data.order.user_name + "\n";
  121. }
  122. if (data.order.buyer_name) {
  123. txt = txt + '收银员:' + data.order.buyer_name + "\n";
  124. }
  125. txt = txt + '时间:' + data.order.pay_time + "\n";
  126. txt = txt + '===============================' + "\n";
  127. txt = txt + '品名 单价 数量 金额' + "\n";
  128. var num = 0;
  129. mui.each(data.goods, function(i, n){
  130. txt = txt + n.name + ' ' + n.price +' ' + n.number + ' ' + (n.price * n.number).toFixed(2) + "\n";
  131. num += n.number;
  132. });
  133. txt = txt + '===============================' + "\n";
  134. txt = txt + '商品数: ' + num + '件' + "\n";
  135. txt = txt + '总金额: ' + data.order.amount + "\n";
  136. txt = txt + '实收金额: ' + data.order.real + "\n";
  137. txt = txt + '找零: ' + data.order.balance + "\n";
  138. txt = txt + '===============================' + "\n";
  139. txt = txt + '温馨提示:商品如出现质量问题7日内凭此票办理退货,开具发票请在本月内办理!谢谢惠顾,欢迎下次光临!' + "\n";
  140. txt = txt + '门店客服电话:'+data.order.store_phone + "\n";
  141.  
  142. console.log(txt);
  143. try {
  144. plus.print.printText(txt, function (msg) {
  145. console.log("printTest:" + msg);
  146.  
  147. plus.print.runPaper(5, function (msg) {
  148. console.log("runPaper:" + msg);
  149. });
  150.  
  151. app.setSettings(device, 'print_device');
  152. });
  153. } catch (e) {
  154. app.setSettings(null, 'print_device');
  155. mui.alert('打印失败: ' + e.message);
  156. }
  157. };
  158.  
  159. var printPic = function(base64, callback){
  160. if (device == null) {
  161. mui.toast('打印插件初始化失败');
  162. return ;
  163. }
  164. var next = callback || function (msg) {
  165. console.log('printPic:' + msg);
  166. };
  167. return plus.print.printPic(base64, next);
  168. };
  169.  
  170. var printText = function(txt, callback){
  171. if (device == null) {
  172. mui.toast('打印插件初始化失败');
  173. return ;
  174. }
  175. var next = callback || function (msg) {
  176. console.log("printText:" + msg);
  177. };
  178. return plus.print.printText(txt, next);
  179. };
  180.  
  181. var runPaper = function(lines, callback){
  182. if (device == null) {
  183. mui.toast('打印插件初始化失败');
  184. return ;
  185. }
  186. var next = callback || function (msg) {
  187. console.log("runPaper:" + msg);
  188. };
  189. return plus.print.runPaper(5, next);
  190. };
  191.  
  192. var close = function(){
  193. if (device == null) {
  194. //mui.toast('打印插件初始化失败');
  195. return ;
  196. }
  197. return window.plus.print.closeMachine();
  198. }
  199.  
  200. return {
  201. init: init,
  202. printOrder: printOrder,
  203. printPic: printPic,
  204. printText: printText,
  205. runPaper: runPaper,
  206. close: close
  207. };
  208. })(window);
Add Comment
Please, Sign In to add comment