Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 使用方法:
- * 1、printApi.init();
- * 2、printApi.printOrder(vm.print);
- * 3、printApi.close();
- */
- window.printApi = (function(w, undefined){
- var devices = [];
- var device = app.getSettings('print_device') || null;
- var rate = 9600;
- var printControl = null;
- var loadPlugin = function () {
- var _PRINT = 'PluginPrintFunction', B = window.plus.bridge;
- this.getDevicePaths = function () {
- return B.execSync(_PRINT, "PluginGetDevice", []);
- }
- //初始化信息
- this.initMachine = function (path ,rate, successCallback) {
- var success = typeof successCallback !== 'function' ? null : function(args){
- successCallback(args);
- }
- callbackID = B.callbackId(success , null);
- return B.exec(_PRINT, "PluginInit", [callbackID,path,rate]);
- }
- this.closeMachine = function () {
- return B.execSync(_PRINT, "PluginClose", []);
- }
- this.printPic = function (base64 , successCallback) {
- var success = typeof successCallback !== 'function' ? null : function(args){
- successCallback(args);
- }
- callbackID = B.callbackId(success , null);
- return B.exec(_PRINT, "PluginPrintPic", [callbackID , base64]);
- }
- this.printText = function (txt , successCallback) {
- var success = typeof successCallback !== 'function' ? null : function(args){
- successCallback(args);
- }
- callbackID = B.callbackId(success , null);
- return B.exec(_PRINT, "PluginPrintText", [callbackID , txt]);
- }
- this.runPaper = function (row ,successCallback) {
- var success = typeof successCallback !== 'function' ? null : function(args){
- successCallback(args);
- }
- callbackID = B.callbackId(success , null);
- return B.exec(_PRINT, "PluginRunPaper", [callbackID , row]);
- }
- window.plus.print = this;
- };
- var getDevicePaths = function(callback){
- if (typeof plus.print == 'undefined') {
- mui.alert('打印插件未安装,请联系客服');
- return ;
- }
- try {
- var msg = plus.print.getDevicePaths();
- devices = JSON.parse(msg);
- if (devices.length < 1) {
- device = null;
- return ;
- }
- var buttons = [];
- mui.each(devices, function(i, n){
- buttons.push({
- title: n
- });
- });
- plus.nativeUI.actionSheet({
- title: "请选择打印机",
- cancel: "取消",
- buttons: buttons
- }, function(b) {
- if (b.index > 0) {
- device = devices[b.index - 1];
- callback();
- }
- });
- } catch (e) {
- devices = [];
- device = null;
- mui.alert('未检测到打印机设备,请确认');
- }
- };
- var init = function(){
- if (printControl === null) {
- printControl = new loadPlugin();
- }
- if (device === null) {
- getDevicePaths(init);
- }
- var result = plus.print.initMachine(device, rate, function(msg) {
- console.log("print init1: " + msg);
- });
- console.log("print init2: " + result);
- return true;
- };
- var printOrder = function(data) {
- if (device == null) {
- mui.toast('打印插件初始化失败');
- return ;
- }
- var txt = "门店:" + data.order.store_name + "\n";
- txt = txt + '单号:' + data.order.order_sn + "\n";
- if (data.order.user_name) {
- txt = txt + '会员:' + data.order.user_name + "\n";
- }
- if (data.order.buyer_name) {
- txt = txt + '收银员:' + data.order.buyer_name + "\n";
- }
- txt = txt + '时间:' + data.order.pay_time + "\n";
- txt = txt + '===============================' + "\n";
- txt = txt + '品名 单价 数量 金额' + "\n";
- var num = 0;
- mui.each(data.goods, function(i, n){
- txt = txt + n.name + ' ' + n.price +' ' + n.number + ' ' + (n.price * n.number).toFixed(2) + "\n";
- num += n.number;
- });
- txt = txt + '===============================' + "\n";
- txt = txt + '商品数: ' + num + '件' + "\n";
- txt = txt + '总金额: ' + data.order.amount + "\n";
- txt = txt + '实收金额: ' + data.order.real + "\n";
- txt = txt + '找零: ' + data.order.balance + "\n";
- txt = txt + '===============================' + "\n";
- txt = txt + '温馨提示:商品如出现质量问题7日内凭此票办理退货,开具发票请在本月内办理!谢谢惠顾,欢迎下次光临!' + "\n";
- txt = txt + '门店客服电话:'+data.order.store_phone + "\n";
- console.log(txt);
- try {
- plus.print.printText(txt, function (msg) {
- console.log("printTest:" + msg);
- plus.print.runPaper(5, function (msg) {
- console.log("runPaper:" + msg);
- });
- app.setSettings(device, 'print_device');
- });
- } catch (e) {
- app.setSettings(null, 'print_device');
- mui.alert('打印失败: ' + e.message);
- }
- };
- var printPic = function(base64, callback){
- if (device == null) {
- mui.toast('打印插件初始化失败');
- return ;
- }
- var next = callback || function (msg) {
- console.log('printPic:' + msg);
- };
- return plus.print.printPic(base64, next);
- };
- var printText = function(txt, callback){
- if (device == null) {
- mui.toast('打印插件初始化失败');
- return ;
- }
- var next = callback || function (msg) {
- console.log("printText:" + msg);
- };
- return plus.print.printText(txt, next);
- };
- var runPaper = function(lines, callback){
- if (device == null) {
- mui.toast('打印插件初始化失败');
- return ;
- }
- var next = callback || function (msg) {
- console.log("runPaper:" + msg);
- };
- return plus.print.runPaper(5, next);
- };
- var close = function(){
- if (device == null) {
- //mui.toast('打印插件初始化失败');
- return ;
- }
- return window.plus.print.closeMachine();
- }
- return {
- init: init,
- printOrder: printOrder,
- printPic: printPic,
- printText: printText,
- runPaper: runPaper,
- close: close
- };
- })(window);
Add Comment
Please, Sign In to add comment