/// <summary> /// 打印订单小票 /// </summary> /// <param name="order"></param> /// <param name="device"></param> /// <param name="business"></param> /// <returns></returns> public async Task <string> PrintAsync(Order order, FeyinDevice device, Business business) { var time = DateTime.Now.ToTimestamp().ToString(); var sign = UtilHelper.SHA1(user + key + time).ToLower(); var content = new StringBuilder(); content.Append($"<CB>#{order.Identifier} 简单猫</CB>"); content.Append("配送小票<BR>"); content.Append("--------------------------------<BR>"); if (!string.IsNullOrEmpty(order.Remark)) { content.Append($"<B>备注:{order.Remark}</B><BR>"); } content.Append($"<CB>{business.Name}</CB><BR>"); content.Append($"下单时间:{order.CreateTime:yyyy-MM-dd HH:mm:ss}<BR>"); content.Append($"订单编号:{order.OrderCode}<BR>"); content.Append("--------------------------------"); if (order.Products == null || order.Products.Count == 0) { content.Append("无任何商品<BR>"); } else { // 商品名称占用打印纸的20个字符位 var position = 20; var other = 6; foreach (var product in order.Products) { // 商品名称 var name = product.Name; var zhQuantity = 0; // 中文字符数 var enQuantity = 0; // 其他字符数 var cutName = string.Empty; // 截取的名称 while (true) { zhQuantity = UtilHelper.CalcZhQuantity(name); enQuantity = name.Length - zhQuantity; if (zhQuantity * 2 + enQuantity > position) { cutName += name.Substring(name.Length - 2); name = name.Substring(0, name.Length - 2); } else { break; } } var nameLen = zhQuantity * 2 + enQuantity; for (int i = 0; i < position - nameLen; i++) { name += " "; } // 商品数量 var quantity = "* " + Convert.ToDouble(product.Quantity); var quantityLen = quantity.Length; for (int i = 0; i < other - quantityLen; i++) { quantity += " "; } // 商品价格 var price = Convert.ToDouble(product.Price.Value) + ""; var priceLen = price.Length; for (int i = 0; i < other - priceLen; i++) { price = " " + price; } content.Append($"{name + quantity + price}<BR>"); if (!string.IsNullOrEmpty(cutName)) { content.Append($"{cutName}<BR>"); } if (!string.IsNullOrEmpty(product.Description)) { content.Append($"({product.Description})<BR>"); } } } content.Append("--------------其他--------------<BR>"); if (order.PackagePrice.HasValue) { content.Append($"{UtilHelper.PrintLineLeftRight("包装费", Convert.ToDouble(order.PackagePrice.Value) + "")}<BR>"); } content.Append($"{UtilHelper.PrintLineLeftRight("配送费", Convert.ToDouble(order.Freight.Value) + "")}<BR>"); //if (order.SaleCouponUser != null) //{ // content.Append($"{UtilHelper.PrintLineLeftRight("[" + order.SaleCouponUser.Name + "]", "-¥" + Convert.ToDouble(order.SaleCouponUser.Value) + "")}<BR>"); //} //if (order.SaleFullReduce != null) //{ // content.Append($"{UtilHelper.PrintLineLeftRight("[" + order.SaleFullReduce.Name + "]", "-¥" + Convert.ToDouble(order.SaleFullReduce.ReduceMoney) + "")}<BR>"); //} if (order.OrderActivities != null && order.OrderActivities.Count > 0) { order.OrderActivities.ForEach(a => content.Append($"{UtilHelper.PrintLineLeftRight(a.Remark, a.Amount + "")}<BR>")); } content.Append("--------------------------------<BR>"); if (order.OldPrice.HasValue && order.Price != order.OldPrice) { content.Append($"<RIGHT>原价:{order.OldPrice.Value}元</RIGHT><BR>"); } content.Append($"<RIGHT>实付:<B>{order.Price}元</B></RIGHT><BR>"); content.Append("--------------------------------<BR>"); content.Append($"<B>{order.ReceiverAddress}</B><BR>"); content.Append($"<B>{order.Phone}</B><BR>"); content.Append($"<B>{order.GetUserCall()}</B><BR>"); if (!string.IsNullOrEmpty(business.AppQrCode)) { content.Append($"<QR>{business.AppQrCode}</QR>"); } content.Append($"**********<B>#{order.Identifier}</B>完**********<BR>"); var orderInfo = content.ToString().ToUrlEncoding(); string postData = "sn=" + device.Code; postData += ("&content=" + orderInfo); postData += ("×=" + "1");//默认1联 //公共参数 postData += ("&user="******"&stime=" + time); postData += ("&sig=" + sign); postData += ("&apiname=" + "Open_printMsg"); using (var client = new HttpClient()) { var body = new StringContent(postData); body.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); var result = await client.PostAsync(url, body); return(await result.Content.ReadAsStringAsync()); } }
/// <summary> /// 打印订单小票 /// </summary> /// <param name="order"></param> /// <param name="device"></param> /// <param name="business"></param> /// <returns></returns> public async Task <string> PrintAsync(Order order, FeyinDevice device, Business business) { var content = new StringBuilder(); content.Append($"|8 #{order.Identifier} 简单猫"); content.Append("|6配送小票"); content.Append("|6------------------------"); if (!string.IsNullOrEmpty(order.Remark)) { content.Append($"|8备注:{order.Remark}"); } content.Append($"|7 {business.Name}"); content.Append($"|4下单时间:{order.CreateTime:yyyy-MM-dd HH:mm:ss}"); content.Append($"|4订单编号:{order.OrderCode}"); content.Append("|6------------------------"); if (order.Products == null || order.Products.Count == 0) { content.Append("|6无任何商品"); } else { // 商品名称占用打印纸的16个字符位 var position = 16; var other = 4; foreach (var product in order.Products) { // 商品名称 var name = product.Name; var zhQuantity = 0; // 中文字符数 var enQuantity = 0; // 其他字符数 var cutName = string.Empty; // 截取的名称 while (true) { zhQuantity = UtilHelper.CalcZhQuantity(name); enQuantity = name.Length - zhQuantity; if (zhQuantity * 2 + enQuantity > position) { cutName += name.Substring(name.Length - 2); name = name.Substring(0, name.Length - 2); } else { break; } } var nameLen = zhQuantity * 2 + enQuantity; for (int i = 0; i < position - nameLen; i++) { name += " "; } // 商品数量 var quantity = "*" + product.Quantity; var quantityLen = quantity.Length; for (int i = 0; i < other - quantityLen; i++) { quantity += " "; } // 商品价格 var price = product.Price.Value + ""; var priceLen = price.Length; for (int i = 0; i < other - priceLen; i++) { price = " " + price; } content.Append($"|6{name + quantity + price}"); if (!string.IsNullOrEmpty(cutName)) { content.Append($"|6{cutName}"); } if (!string.IsNullOrEmpty(product.Description)) { content.Append($"|6({product.Description})"); } } } content.Append("|6----------其他----------"); if (order.PackagePrice.HasValue) { content.Append($"|6{UtilHelper.PrintLineLeftRight("包装费", Convert.ToDouble(order.PackagePrice.Value) + "", 24)}"); } content.Append($"|6{UtilHelper.PrintLineLeftRight("配送费", Convert.ToDouble(order.Freight.Value) + "", 24)}"); //if (order.SaleCouponUser != null) //{ // content.Append($"|6{UtilHelper.PrintLineLeftRight("[" + order.SaleCouponUser.Name + "]", "-¥" + Convert.ToDouble(order.SaleCouponUser.Value) + "", 24)}"); //} //if (order.SaleFullReduce != null) //{ // content.Append($"|6{UtilHelper.PrintLineLeftRight("[" + order.SaleFullReduce.Name + "]", "-¥" + Convert.ToDouble(order.SaleFullReduce.ReduceMoney) + "", 24)}"); //} if (order.OrderActivities != null && order.OrderActivities.Count > 0) { order.OrderActivities.ForEach(a => content.Append($"|6{UtilHelper.PrintLineLeftRight("[" + a.Remark + "]", "-¥" + a.Amount + "", 24)}")); } content.Append("|6------------------------"); if (order.OldPrice.HasValue && order.Price != order.OldPrice) { content.Append($"|6原价:{order.OldPrice}元"); } content.Append($"|6实付:{order.Price}元"); content.Append("|6------------------------"); content.Append($"|8{order.ReceiverAddress}"); content.Append($"|8{order.Phone}"); content.Append($"|8{order.GetUserCall()}"); if (!string.IsNullOrEmpty(business.AppQrCode)) { content.Append($"|2{business.AppQrCode}"); } content.Append($"|6********#{order.Identifier}完********"); var postData = JsonConvert.SerializeObject(new { number = device.Code, app_key, token = device.Remark, msg = content.ToString() }); using (var client = new HttpClient()) { var body = new StringContent(postData); body.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var result = await client.PostAsync(url, body); return(await result.Content.ReadAsStringAsync()); } }