コード例 #1
0
ファイル: FeieHelper.cs プロジェクト: sunxiaoshuang/Cat
        /// <summary>
        /// 打印提示信息
        /// </summary>
        /// <param name="content"></param>
        /// <param name="device_no"></param>
        /// <returns></returns>
        public async Task <string> PrintAsync(string content, FeyinDevice device)
        {
            var str = string.Empty;

            str += "**************通知**************<BR>";
            str += "<B>简单猫提示您:</B><BR>" + $"<B>{content}</B><BR><BR>";
            str += "********************************<BR>";

            var time = DateTime.Now.ToTimestamp().ToString();
            var sign = UtilHelper.SHA1(user + key + time).ToLower();

            string postData = "sn=" + device.Code;

            postData += ("&content=" + str.ToUrlEncoding());
            postData += ("&times=" + "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());
            }
        }
コード例 #2
0
        /// <summary>
        /// 打印提示信息
        /// </summary>
        /// <param name="content"></param>
        /// <param name="device_no"></param>
        /// <returns></returns>
        public async Task <string> PrintAsync(string content, FeyinDevice device)
        {
            var str = string.Empty;

            str += "**************通知**************\r\n";
            str += "<FS2>简单猫提示您:</FS2>\r\n" + $"<FS2>{content}</FS2>\r\n\r\n";
            str += "********************************\r\n";
            var time = DateTime.Now.ToTimestamp().ToString();
            var dic  = new Dictionary <string, string>
            {
                { "machine_code", device.Code },
                { "partner", partner },
                { "time", time }
            };
            var sign  = GetSign(dic, apikey, device.ApiKey).ToUpper();
            var trans = $"partner={partner}&machine_code={device.Code}&time={time}&sign={sign}&content={str}";

            using (var client = new HttpClient())
            {
                var body = new StringContent(trans);
                body.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                var res = await client.PostAsync(url, body);

                var result = await res.Content.ReadAsStringAsync();

                return(result);
            }
        }
コード例 #3
0
        /// <summary>
        /// 打印提示信息
        /// </summary>
        /// <param name="content"></param>
        /// <param name="device_no"></param>
        /// <returns></returns>
        public async Task <string> PrintAsync(string content, FeyinDevice device)
        {
            var str = string.Empty;

            str += "**************通知**************<BR>";
            str += $"|8简单猫提示您:{content}";
            str += "********************************<BR>";

            var postData = JsonConvert.SerializeObject(new
            {
                number = device.Code,
                app_key,
                token = device.Remark,
                msg   = str
            });

            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());
            }
        }
コード例 #4
0
        /// <summary>
        /// 绑定打印机
        /// </summary>
        /// <param name="device"></param>
        /// <param name="helper"></param>
        /// <returns></returns>
        public async Task <IActionResult> AddBind([FromQuery] FeyinDevice device)
        {
            var result = new JsonData();

            result.Success = await Service.BindPrintDeviceAsync(Business, device);

            if (!result.Success)
            {
                result.Msg = "绑定失败,请刷新后重试";
            }
            result.Data = device;
            result.Msg  = "设备绑定成功";
            return(Json(result));
        }
コード例 #5
0
        /// <summary>
        /// ws消息处理
        /// </summary>
        /// <returns></returns>
        //public async Task<IActionResult> MessageHandler([FromQuery]string code)
        //{
        //    // Data记录语音提示方式,Msg记录提示语
        //    var result = new JsonData();
        //    if (!Business.IsAutoReceipt)
        //    {
        //        result.Success = true;
        //        result.Data = 1;
        //        result.Msg = "商家不自动接单";
        //        return Json(result);
        //    }

        //    var order = Service.GetOrderByCode(code);
        //    switch (Business.ServiceProvider)
        //    {
        //        case LogisticsType.None:
        //            order.Status = OrderStatus.Receipted;
        //            result.Data = 2;
        //            result.Success = Service.Commit() > 0;
        //            break;
        //        case LogisticsType.Self:
        //            // 自己配送
        //            result.Success = Service.SendOrderSelf(order.ID, order);
        //            result.Data = 2;
        //            result.Msg = "自动接单成功,配送方式:自己配送";
        //            break;
        //        case LogisticsType.Dada:
        //            // 达达配送
        //            result = await DadaHandler(order);
        //            result.Data = result.Success ? 2 : 3;
        //            break;
        //        case LogisticsType.Dianwoda:
        //            // 点我达配送
        //            result = await DwdHandler(order);
        //            result.Data = result.Success ? 2 : 3;
        //            break;
        //        default:
        //            break;
        //    }
        //    // 打印小票
        //    //var json = await Print(order, Business.DefaultPrinterDevice);
        //    //if (!json.Success)
        //    //{
        //    //    result.Success = false;
        //    //    result.Msg += "|" + json.Msg;
        //    //}

        //    return Json(result);
        //}

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="order"></param>
        /// <param name="device_no"></param>
        /// <returns></returns>
        private async Task <JsonData> Print(Order order, FeyinDevice device)
        {
            var result = new JsonData();

            var content = await Service.Print(order, device, Business);

            if (device.Type == PrinterType.Feyin)
            {
                var ret = JsonConvert.DeserializeObject <FeyinModel>(content);
                result.Success = ret.ErrCode == null || ret.ErrCode == 0;
                result.Msg     = ret.ErrMsg;
            }
            else if (device.Type == PrinterType.Yilianyue)
            {
                var ret = JsonConvert.DeserializeObject <YlyReturn>(content);
                result.Success = ret.state == "1";
                if (!result.Success)
                {
                    result.Msg = ret.state == "4" ? "打印签名错误" : "打印参数错误";
                }
            }
            else if (device.Type == PrinterType.Feie)
            {
                var ret = JsonConvert.DeserializeObject <FeieReturn>(content);
                result.Success = ret.ret == 0;
                if (!result.Success)
                {
                    result.Msg = ret.msg;
                }
            }
            else if (device.Type == PrinterType.Waimaiguanjia)
            {
                var ret = JsonConvert.DeserializeObject <WmgjReturn <object> >(content);
                result.Success = ret.errno == 0;
                if (!result.Success)
                {
                    result.Msg = ret.msg;
                }
            }

            if (!result.Success)
            {
                // 打印失败
                return(result);
            }
            result.Msg = "正在打印小票,请稍等";
            return(result);
        }
コード例 #6
0
        /// <summary>
        /// 添加打印机
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public async Task <WmgjReturn <WmgjNewPrinter> > AddPrintAsync(FeyinDevice device)
        {
            var authUrl = "http://open.xiaowm.com/Auth_api/auth";
            var content = new {
                number = device.Code,
                app_id,
                pwd
            };

            using (var client = new HttpClient())
            {
                var body = new StringContent(JsonConvert.SerializeObject(content));
                body.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var result = await client.PostAsync(authUrl, body);

                return(JsonConvert.DeserializeObject <WmgjReturn <WmgjNewPrinter> >(await result.Content.ReadAsStringAsync()));
            }
        }
コード例 #7
0
        /// <summary>
        /// 删除打印机
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public async Task <string> DelPrinterAsync(FeyinDevice device)
        {
            var delUrl  = "http://open.xiaowm.com/Auth_api/delete";
            var content = new {
                number = device.Code,
                token  = device.Remark
            };

            using (var client = new HttpClient())
            {
                var body = new StringContent(JsonConvert.SerializeObject(content));
                body.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var ret = await client.PostAsync(delUrl, body);

                var result = await ret.Content.ReadAsStringAsync();

                return(result);
            }
        }
コード例 #8
0
ファイル: FeieHelper.cs プロジェクト: sunxiaoshuang/Cat
        /// <summary>
        /// 添加打印机
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public async Task <FeieReturn> AddPrintAsync(FeyinDevice device)
        {
            var time = DateTime.Now.ToTimestamp();
            var sign = UtilHelper.SHA1(user + key + time).ToLower();

            var postData = $"printerContent={device.Code}# {device.ApiKey}# {device.Name} #{device.Remark}";

            postData += ("&user="******"&stime=" + time);
            postData += ("&sig=" + sign);
            postData += ("&apiname=Open_printerAddlist");

            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(JsonConvert.DeserializeObject <FeieReturn>(await result.Content.ReadAsStringAsync()));
            }
        }
コード例 #9
0
ファイル: FeieHelper.cs プロジェクト: sunxiaoshuang/Cat
        /// <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 += ("&times=" + "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());
            }
        }
コード例 #10
0
        /// <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 dic  = new Dictionary <string, string>
            {
                { "machine_code", device.Code },
                { "partner", partner },
                { "time", time }
            };
            var sign    = GetSign(dic, apikey, device.ApiKey).ToUpper();
            var content = new StringBuilder();

            content.Append("<MS>1,3</MS>");
            content.Append($"<center><FS2>#{order.Identifier} 简单猫</FS2></center>\r\n");
            content.Append("配送小票\r\n");
            content.Append("--------------------------------\r\n");
            if (!string.IsNullOrEmpty(order.Remark))
            {
                content.Append($"<FS2>备注:{order.Remark}</FS2>\r\n");
            }
            content.Append($"<center><FS2>{business.Name}</FS2></center>\r\n");
            content.Append($"下单时间:{order.CreateTime:yyyy-MM-dd HH:mm:ss}\r\n");
            content.Append($"订单编号:{order.OrderCode}\r\n");
            content.Append("--------------------------------");
            if (order.Products == null || order.Products.Count == 0)
            {
                content.Append("无任何商品\r\n");
            }
            else
            {
                content.Append("<table>");
                foreach (var item in order.Products)
                {
                    content.Append($"<tr><td>{item.Name}</td><td>* {item.Quantity}</td><td>{item.Price}</td></tr>");
                }
                content.Append("</table>");
            }
            content.Append("--------------------------------");
            if (order.PackagePrice.HasValue)
            {
                content.Append($"{UtilHelper.PrintLineLeftRight("包装费", Convert.ToDouble(order.PackagePrice.Value) + "")}\n");
            }
            content.Append($"{UtilHelper.PrintLineLeftRight("配送费", Convert.ToDouble(order.Freight.Value) + "")}\n");
            //if (order.SaleCouponUser != null)
            //{
            //    content.Append($"{UtilHelper.PrintLineLeftRight("[" + order.SaleCouponUser.Name + "]", "-¥" + Convert.ToDouble(order.SaleCouponUser.Value) + "")}\n");
            //}
            //if (order.SaleFullReduce != null)
            //{
            //    content.Append($"{UtilHelper.PrintLineLeftRight("[" + order.SaleFullReduce.Name + "]", "-¥" + Convert.ToDouble(order.SaleFullReduce.ReduceMoney) + "")}\n");
            //}
            if (order.OrderActivities != null && order.OrderActivities.Count > 0)
            {
                order.OrderActivities.ForEach(a => content.Append($"{UtilHelper.PrintLineLeftRight(a.Remark, a.Amount + "")}\n"));
            }
            content.Append("--------------------------------");
            if (order.OldPrice.HasValue && order.Price != order.OldPrice)
            {
                content.Append($"<right>原价:{order.OldPrice.Value}元</right>");
            }
            content.Append($"<right>实付:{order.Price}元</right>\r\n");
            content.Append("--------------------------------");
            content.Append($"<FS2>{order.ReceiverAddress}</FS2>\r\n");
            content.Append($"<FS2>{order.Phone}</FS2>\r\n");
            content.Append($"<FS2>{order.GetUserCall()}</FS2>\r\n");
            if (!string.IsNullOrEmpty(business.AppQrCode))
            {
                content.Append($"<QR>{business.AppQrCode}</QR>");
            }
            content.Append("--------------------------------");
            content.Append($"<center><FS2>#{order.Identifier}完</FS2></center>");

            var trans = $"partner={partner}&machine_code={device.Code}&time={time}&sign={sign}&content={content}";

            using (var client = new HttpClient())
            {
                var body = new StringContent(trans);
                body.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                var res = await client.PostAsync(url, body);

                var result = await res.Content.ReadAsStringAsync();

                return(result);
            }
        }
コード例 #11
0
        /// <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());
            }
        }