易联云小票打印机对接

官网地址

https://www.yilianyun.net/

SDK

https://gitee.com/diycp/openapi-sdk

相关代码
where('id',1)->find();
        self::$app_id = $config['app_id'];
        self::$app_secret = $config['app_secret'];
        self::$terminal_no = $config['terminal_no'];
        self::$terminal_secret = $config['terminal_secret'];

        $map['app_id'] = self::$app_id;
        $map['expires_time'] = array('lt',time());
        $printer_token = Db::table("printer_token")->where($map)->find();
        self::$config = new YlyConfig(self::$app_id, self::$app_secret);
        if($printer_token){
            self::$access_token = $printer_token['access_token'];
        }else{
            $client = new YlyOauthClient(self::$config);
            $token = $client->getToken();
            Db::table("printer_token")->where(['app_id'=>self::$app_id])->update(['access_token'=>$token->access_token,'refresh_token'=>$token->refresh_token,'expires_time'=>time+$token->expires_in]);
            self::$access_token = $token->access_token;
        }
        //授权打印机
        $printer = new PrinterService(self::$access_token, self::$config);
        $printer_status = $printer->addPrinter(self::$terminal_no, self::$terminal_secret, '', '');
        if($printer_status->error){
            self::printerLog('授权打印机失败:'.$printer_status->error_description);
        }else{
                $res = self::text();
                return $res;
        }
    }

    //打印机日志
    private static function printerLog($content)
    {
        $data['terminal_no'] = self::$terminal_no;
        $data['content'] = $content;
        $data['created_time'] = date("Y-m-d H:i:s");
        Db::table("printer_log")->insertGetId($data);
    }

    //打印文本
    public static function text()
    {
        $print = new PrintService(self::$access_token, self::$config);
        //58mm排版 排版指令详情请看 http://doc2.10ss.net/332006
        $content = "
**#1 美团**
"; $content .= str_repeat('.', 32); $content .= "
--在线支付--
"; $content .= "
张周兄弟烧烤
"; $content .= "订单时间:". date("Y-m-d H:i") . "\n"; $content .= "订单编号:40807050607030\n"; $content .= str_repeat('*', 14) . "商品" . str_repeat("*", 14); $content .= ""; $content .= ""; $content .= ""; $content .= ""; $content .= ""; $content .= ""; $content .= "
烤土豆(超级辣)x35.96
烤豆干(超级辣)x23.88
烤鸡翅(超级辣)x317.96
烤排骨(香辣)x312.44
烤韭菜(超级辣)x38.96
"; $content .= str_repeat('.', 32); $content .= "这是二维码内容"; $content .= "小计:¥82\n"; $content .= "折扣:¥4 \n"; $content .= str_repeat('*', 32); $content .= "订单总价:¥78 \n"; $content .= "
**#1 完**
"; try{ $result = $print->index(self::$terminal_no, $content, $serial_number); if($result->error){ self::printerLog("批次:".$serial_number." 打印成失败。".$result->error_description); }else{ self::printerLog("批次:".$serial_number." 打印成功"); } }catch (Exception $e) { $result = $e->getMessage(); self::printerLog("批次:".$serial_number." 打印成失败。".$result); } } }

你可能感兴趣的:(易联云小票打印机对接)