#自动打印小票
注意前端需要引用(LodopFuncs.js,JsBarcode.all.min.js,jquery.js)当然还有安装打印机驱动还有Lodop的驱动
前端代码调用打印方法部分
json.printinfo是后端传向前端的生成好的打印代码。
//执行打印小票
var orderids = "";
if (json.printinfo != "")
{
var imgsrc = "\"../../../Content/img/wjywx.jpg\"";
function print() {
if (needCLodop()) { loadCLodop(); }
window.On_CLodop_Opened = function () {
eval(atou(json.printinfo)); //OpenPreview()是调用打印方法
window.On_CLodop_Opened = null;
};
if ("undefined" != typeof CLodopMessage) {
layer.msg(CLodopMessage, {
time: 20000, //20s后自动关闭
btn: ['前往下载'],
id: 'layerDemo',
yes: function () {
window.open('../../Upload/驱动/CLodop_Setup_for_Win32NT.exe')
}
});
}
//打印小票完成根据订单id去修改订单在数据库中的状态
if (orderids != "") {
$.ajax({
type: 'POST',
url: "../Home/UpdateOrderStatic?Orderid=" + orderids,
data: "",
dataType: 'json',
async: false,
cache: false,
success: function (json) {
if (json.responseText == "True") {
alert("打印完成")
} else {
alert("修改订单状态失败")
}
}
})
}
}
print();
}
打印小票用到的实体
///
/// 打印小票实体
///
public class Printmodel
{
///
/// 小票顶部标题例如【含处方药】
///
private string title;
///
/// 平台例如【京东到家】
///
private string platform;
///
/// 订单序号例如【#4】顾客联
///
private string number;
///
/// 门店名称【例如阳光小镇24H店】
///
private string storeName;
///
/// 下单时间
///
private string orderCreateTime;
///
/// 预计送达时间
///
private string deliveryTime;
///
/// 订单id
///
private string orderId;
///
/// 客户名称
///
private string userName;
///
/// 客户联系电话
///
private string userTel;
///
/// 客户备注
///
private string userNote;
///
/// 用户地址
///
private string userAddress;
///
/// 商品信息
///
private List<GoodsModel> goods;
///
/// 商品优惠金额
///
private decimal preferential;
///
/// 配送全额
///
private decimal distribution;
///
/// 包装费
///
private decimal packaging;
///
/// 门店联系电话
///
private string storeTel;
///
/// 投诉电话
///
private string complaintsTel;
///
/// 温馨·提示内容
///
private string prompt;
///
/// 微信二维码路径
///
private string wxImg;
///
/// 图片下面的消息
///
private string message;
///
/// 标题
///
public string Title { get => title; set => title = value; }
///
/// 平台
///
public string Platform { get => platform; set => platform = value; }
///
/// 订单序号例如【#4】
///
public string Number { get => number; set => number = value; }
///
/// 门店名称
///
public string StoreName { get => storeName; set => storeName = value; }
///
/// 下单时间
///
public string OrderCreateTime { get => orderCreateTime; set => orderCreateTime = value; }
///
/// 预计到达时间
///
public string DeliveryTime { get => deliveryTime; set => deliveryTime = value; }
///
/// 订单id
///
public string OrderId { get => orderId; set => orderId = value; }
///
/// 顾客姓名
///
public string UserName { get => userName; set => userName = value; }
///
/// 顾客联系电话
///
public string UserTel { get => userTel; set => userTel = value; }
///
/// 顾客地址
///
public string UserAddress { get => userAddress; set => userAddress = value; }
///
/// 商品信息
///
public List<GoodsModel> Goods { get => goods; set => goods = value; }
///
/// 计算商品总价
///
/// 商品集合
///
private decimal Suninfull(List<GoodsModel> goods)
{
decimal sun = 0;
if (goods.Count > 0 && goods != null)
{
foreach (GoodsModel item in goods)
{
sun += item.Money;
};
};
return sun;
}
///
/// 商品全额
///
public decimal SumInFull { get => Suninfull(this.Goods); }
///
/// 商品优惠金额
///
public decimal Preferential { get => preferential; set => preferential = value; }
///
/// 配送费
///
public decimal Distribution { get => distribution; set => distribution = value; }
///
/// 包装费
///
public decimal Packaging { get => packaging; set => packaging = value; }
///
/// 获取商品总件数
///
/// 商品实体集合
///
private int getGoodsNum(List<GoodsModel> goods)
{
int num = 0;
foreach (GoodsModel item in goods)
{
num += item.Number;
}
return num;
}
///
/// 商品总件数
///
public int GoodsNum { get => getGoodsNum(this.goods); }
///
/// 实付金额[总金额-优惠金额+配送费+包装费]
///
public decimal ActualMoney { get => (SumInFull - Preferential + Distribution + Packaging); }
///
/// 门店电话
///
public string StoreTel { get => storeTel; set => storeTel = value; }
///
/// 投诉电话
///
public string ComplaintsTel { get => complaintsTel; set => complaintsTel = value; }
///
/// 温馨提示内容
///
public string Prompt { get => prompt; set => prompt = value; }
///
/// 微信二维码路径(相对路径)
///
public string WxImg { get => wxImg; set => wxImg = value; }
///
/// 二维码下部信息
///
public string Message { get => message; set => message = value; }
///
/// 客户备注
///
public string UserNote { get => userNote; set => userNote = value; }
}
打印小票的方法
M_Order_Info实体是订单相关信息。根据自己需求而定
///
/// 打印小票方法
///
///
public string Print_Receipts(List<M_Order_Info> orderInfo)
{
#region 打印小票
int magin = 0;
string orderids = string.Empty;
string html = "";
html += " var LODOP;";
html += " function tprint(){";
html += " LODOP = getLodop();";
html += "LODOP.PRINT_INIT('');";
html += "LODOP.SET_PRINT_PAGESIZE('3', '58mm', '', '热敏');";
D_Order_Info doi = new D_Order_Info();
D_WX_StoreInfos StoreInfos = new D_WX_StoreInfos();
foreach (M_Order_Info item in orderInfo)
{
if (item.isprint == 0&&item.deliveryStationNoIsv!="19176")
{
Printmodel model = new Printmodel();
if(item.businessTag!=null&& item.businessTag.Contains("dj_prescription_order"))
{
model.Title = "含处方药";
}
else
{
model.Title = "";
}
model.Platform = GetEnumByValue(typeof(OrderSourceType), item.orderType.ToString()).ToString(); ;
model.Number = item.orderNum.ToString();
model.StoreName = item.deliveryStationName;
model.OrderCreateTime = item.orderStartTime.ToString();
model.DeliveryTime = item.orderPreEndDeliveryTime.ToString();
model.OrderId = item.orderId;
orderids += "'" + model.OrderId + "',";
model.UserName = item.buyerFullName;
model.UserTel = item.buyerMobile;
model.UserNote = GetRealRemark(item.orderBuyerRemark).Trim();
model.UserAddress = Regex.Replace(item.buyerFullAddress,@"[^a-zA-Z0-9\u4e00-\u9fa5\s]", "");
List<GoodsModel> goodslist = new List<GoodsModel>();
foreach (Order_Products item2 in doi.get_Products(item.orderId))
{
GoodsModel gmodel = new GoodsModel();
gmodel.GoodsName = item2.skuName;
gmodel.Number = item2.skuCount;
gmodel.Price = Convert.ToInt32(item2.skuStorePrice)/100M;
gmodel.SKU = item2.skuId.ToString();
gmodel.UPC = item2.upcCode.ToString();
goodslist.Add(gmodel);
}
model.Goods = goodslist;
model.Preferential = Convert.ToInt32(item.orderDiscountMoney)/100M;
model.Packaging = Convert.ToInt32(item.packagingMoney)/100M;
model.Distribution = Convert.ToInt32(item.orderFreightMoney)/100M;
model.StoreTel = StoreInfos.GetStoreInfoByStoreID(item.deliveryStationNoIsv).telephone;
model.ComplaintsTel = "4000239900";
model.Prompt = "此商品属于特殊商品,非质量问题概不退换";
model.WxImg ="\'+imgsrc+\'";
model.Message = "小票底部信息";
html += Print(model, ref magin);
magin += magin;
}
}
html += " LODOP.PREVIEW();";
//LODOP.PRINT();
html += "}tprint();orderids=" + orderids.Substring(0, orderids.Length - 1)+";";
return html;
#endregion
}
生成打印小票的前端代码
///
/// 生成前端打印小票代码
///
///
///
public string Print(Printmodel per,ref int magin)
{
string html = "";
int topmagin = 5+ magin;
//html += " var LODOP;";
//html += " function tprint(){";
//html += " LODOP = getLodop();";
//html += "LODOP.PRINT_INIT('');";
//html += "LODOP.SET_PRINT_PAGESIZE('3', '58mm', '', '热敏');";
if (!string.IsNullOrWhiteSpace(per.Title))
{
html += "LODOP.ADD_PRINT_TEXT('" + topmagin + "mm', 0,183, 100, '※" + per.Title + "※');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 13);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 1);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2); ";
}
else
{
topmagin=topmagin - 5;
}
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 7) + "mm', 0, 183, 100, '" + per.Platform + "|#" + per.Number + "顾客联');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 11);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 1);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2);";
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 6) + "mm', 0, 183, 100, '" + per.StoreName + "');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2);";
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 6) + "mm', 5, 183, 100, '下单时间:" + per.OrderCreateTime + "');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '预计送达:" + per.DeliveryTime + "');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '订单编号:" + per.OrderId + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_BARCODE('" + (topmagin += 4) + "mm','5','25%', 90, '128Auto', '" + per.OrderId + "');";
html += "LODOP.SET_PRINT_STYLEA(0, 'AlignJustify', 2);";
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 34) + "mm', 0, 183, 100, '***顾客信息***');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 10);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2);";
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '客户:" + per.UserName + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '联系电话:" + per.UserTel + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
if (!string.IsNullOrWhiteSpace(per.UserNote))
{
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 8) + "mm', 5, 183, 100, '备注:" + per.UserNote + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 2);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
}
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 8) + "mm', 5, 183, 100, '地址:" + per.UserAddress + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 11) + "mm', 5, 183, 100, '* ');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '* ');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 8) + "mm', 5, 183, 100, '裁剪线:✂↘️ ');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_LINE('" + (topmagin += 11) + "mm', 0, '" + topmagin + "mm', 183, 1, 1);";
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '* ');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '* ');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 12) + "mm', 0, 183, 100, '***商品信息***');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 10);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2);";
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '商品名称');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_TEXT('" + topmagin + "mm', '35%', 183, 100, '数量');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_TEXT('" + topmagin + "mm', '55%', 183, 100, '单价');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_TEXT('" + topmagin + "mm', '75%', 183, 100, '金额');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_LINE('" + (topmagin += 3) + "mm', 0, '" + topmagin + "mm', 183, 2, 1);";
foreach (GoodsModel item in per.Goods)
{
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 1) + "mm', 5, 183, 100,'" + item.GoodsName + "');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 8) + "mm', '35%', 183, 100, 'X" + item.Number + "');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_TEXT('" + topmagin + "mm', '55%', 183, 100, '" + item.Price + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += " LODOP.ADD_PRINT_TEXT('" + topmagin + "mm', '75%', 183, 100, '" + item.Money + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, 'SKU码:" + item.SKU + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, 'UCP码:" + item.UPC + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
topmagin += 4;
}
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 0, 183, 100, '***结算信息***');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 10);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2);";
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 0, 183, 100, '商品金额:¥" + per.SumInFull + " 商品优惠:¥" + per.Preferential + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 7.5);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 3);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_TEXT('"+ (topmagin += 4) + "mm', 0, 183, 100, '配送金额:¥" + per.Distribution + " 包装费:¥" + per.Packaging + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 7.5);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 3);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += " LODOP.ADD_PRINT_LINE('" + (topmagin += 5) + "mm', 0, '" + topmagin + "mm', 183, 2, 1);";
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 1) + "mm', 0, 183, 100, '总件数:" + per.GoodsNum + "');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 12);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 1);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 0, 183, 100, '实付:" + per.ActualMoney + "');";
html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 12);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 1);";
html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 6) + "mm', 5, 183, 100, '门店电话:" + per.StoreTel + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '投诉电话:" + per.ComplaintsTel + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
if (!string.IsNullOrWhiteSpace(per.Prompt))
{
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '温馨提示:" + per.Prompt + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
}
html += "LODOP.ADD_PRINT_IMAGE('" + (topmagin += 14) + "mm', 0, 183, '60mm', '+ per.WxImg + " />');";
html += "LODOP.SET_PRINT_STYLEA(0, 'Stretch', 2);";
html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 70) + "mm', 5, 183, 100, '" + per.Message + "');";
html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);";
html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);";
LODOP.PRINT();
//html += "}";
//html += " tprint()";
magin=topmagin;
return html;
}
//这个路径就是要打印的页面
var url = '被打印网页路径';
var LODOP;
LODOP = getLodop();
LODOP.PRINT_INIT('');
var ele = document.createElement("img");
JsBarcode(ele, thisorderid, {
displayValue: true,
fontSize: 10, height: 50, margin: 0, width: 1
});
var srcdata = ele.src;
srcdata = srcdata.replace(/\+/g, "%2B");
srcdata = srcdata.replace(/\&/g, "%26");
//生成订单二维码,临时保存到缓存内
$.ajax({
type: 'POST',
url: '@Url.Content("../orders/export/saveimgdata")',
data: { orderid: thisorderid, imgdata: encodeURI(srcdata) },
dataType: 'text',
async: false,
cache: false,
success: function (text) {
},
error: function (e) { }
});
var playerOrder = $("#player10")[0];
if (playerOrder != undefined && playerOrder.paused) /*如果已经暂停*/ {
playerOrder.play(); /*播放*/
}
//截取新订单id
LODOP.ADD_PRINT_URL(0, 0, "100%", "100%", "" + url + "?orderIds=" + thisorderid + "&IsAutoPrint=1");
LODOP.PRINT();