微信小程序:微擎开发飞鹅打印机配置

1、打印机配置,这3个参数在new Feie()时会用到
在这里插入图片描述
2、根据以下方法,可以打印出各订单类型的小票

public function fe_print($id){
        global $_W;
        include_once __DIR__.'/../class/Feie.php';   
        $store = new store();
        $user = new user();
        if (!function_exists('getMoney')) {
            function getMoney($money,$len = 7,$num = 2){
                $money = sprintf("%.{$num}f",$money);
                for ($i = 1;$i<= $len;$i++){
                    $money = ' '.$money;
                }
                return substr($money,-1*$len);
            }
        }   
        if (!function_exists('getName')) {
            function getName($name,$len = 6){
                if(strlen($name)>= $len*3){
                    return $name;
                }
                for ($i = 1;$i<= $len;$i++){
                    $name .= ' ';
                }
                return substr($name,0,$len*3);
            }
        }  
        // $order_data  要打印的订单信息
        $order_data=pdo_get('order',array('id'=>$id));
        // goodses 要打印的订单产品明细
        $goodses=pdo_getall('ordergoods',array('order_id'=>$id));
        if (!$order_data){
            return ['code'=>1];
        }
        $store_data = $store->get_data_by_id($order_data['store_id']);
        $user_data = $user->get_data_by_id($order_data['user_id']);
        if (!$store_data['feie_user']){
            return ['code'=>1,'msg'=>'商家没有设置飞鹅打印信息'];
        }
        $feie = new Feie($store_data['feie_user'],$store_data['feie_ukey'],$store_data['feie_sn']);

        $print_info = "";
        if ($order_data['distribution_type'] == 1){
            $print_info .= '<CB>门店自提单</CB><BR>';
            $print_info .= '提货人:'.$user_data['name'].'<BR>';
            $print_info .= '联系电话:'.$order_data['take_tel'].'<BR>';
            $print_info .= '提货时间:'.$order_data['take_time'].'<BR>';
        }else{
            //门店配送
            $print_info .= '<CB>门店配送单</CB><BR>';
            $print_info .= '收货人:'.$user_data['name'].'<BR>';
            $print_info .= '联系电话:'.$user_data['tel'].'<BR>';
            $print_info .= '地址:'.$order_data['province'].$order_data['city'].$order_data['county'].'<BR>';
            $print_info .= $order_data['address'].'<BR>';
        }
        $print_info .= '<BR>名称      <RIGHT>单价 数量  金额</RIGHT>';
        $print_info .= '--------------------------------<BR>';
        foreach ($goodses as $goods) {
            $print_info .= getName($goods['goods_name'])
                    .'<RIGHT>'
                .getMoney($goods['goods_price'])
                .getMoney($goods['num'],4,0)
                .getMoney($goods['goods_price']*$goods['num'])
                .'</RIGHT><BR>';
        }
        $print_info .= '--------------------------------<BR>';
        $print_info .= '合计:'.$order_data['amount'].'<BR>';
        $print_info .= '单号:'.$order_data['order_number'].'<BR>';
        $print_info .= '日期:'.date("Y-m-d H:i:s",$order_data['time']).'<BR>';
        $ret = $feie->print_fe($print_info);
        return $ret;
    }

Feie.php代码

<?php
/**
 * Created by PhpStorm.
 * User: lts
 * Date: 2018/8/20
 * Time: 18:01
 */
include_once __DIR__.'/HttpClient.class.php';

class Feie
{
    public $ip = 'api.feieyun.cn';
    public $port = 80;
    public $path = '/Api/Open/';
    public $user = '';
    public $ukey = '';
    public $sn = '';
    public $time = 0;
    public $sig = '';

    public function __construct($user,$ukey,$sn)
    {
        $this->user = $user;
        $this->ukey = $ukey;
        $this->sn = $sn;
    }

    //打印
    function print_fe($orderInfo,$times =1){
        $time = time();
        $content = array(
            'user'=>$this->user,
            'stime'=>$time,
            'sig'=>sha1($this->user.$this->ukey.$time),
            'apiname'=>'Open_printMsg',

            'sn'=>$this->sn,
            'content'=>$orderInfo,
            'times'=>$times//打印次数
        );
        $client = new HttpClient($this->ip,$this->port);
        if(!$client->post($this->path,$content)){
            return array('code'=>1,'msg'=>'连接异常');
        }
        //服务器返回的JSON字符串,建议要当做日志记录起来
        $ret = $client->getContent();
        $ret = (array)json_decode($ret);
        if ($ret['msg'] != 'ok'){
            return array('code'=>1,'msg'=>$ret['msg']);
        }
        return array('code'=>0,'msg'=>'操作成功');
    }
}
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读