易联云小票打印机对接

官网地址

https://www.yilianyun.net/

SDK

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

相关代码
<?php
namespace app\work\logic;

use App\Config\YlyConfig;
use App\Oauth\YlyOauthClient;
use App\Api\PrinterService;
use App\Api\PrintService;
use App\Api\PicturePrintService;

use think\Db;

//易联云小票打印机
class PrinterApi extends \app\common\controller\Base
{

    //应用ID
    private static $app_id;
    //应用密钥
    private static $app_secret;
    //打印机编号
    private static $terminal_no;
    //打印机密钥
    private static $terminal_secret;
    //打印机配置
    private static $config;
    //打印机access_token
    private static $access_token;


    //调用打印机
    public static function callPrint()
    {
        $config = Db::table('shop_printer')->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 = "<FS2><center>**#1 美团**</center></FS2>";
        $content .= str_repeat('.', 32);
        $content .= "<FS2><center>--在线支付--</center></FS2>";
        $content .= "<FS><center>张周兄弟烧烤</center></FS>";
        $content .= "订单时间:". date("Y-m-d H:i") . "\n";
        $content .= "订单编号:40807050607030\n";
        $content .= str_repeat('*', 14) . "商品" . str_repeat("*", 14);
        $content .= "<table>";
        $content .= "<tr><td>烤土豆(超级辣)</td><td>x3</td><td>5.96</td></tr>";
        $content .= "<tr><td>烤豆干(超级辣)</td><td>x2</td><td>3.88</td></tr>";
        $content .= "<tr><td>烤鸡翅(超级辣)</td><td>x3</td><td>17.96</td></tr>";
        $content .= "<tr><td>烤排骨(香辣)</td><td>x3</td><td>12.44</td></tr>";
        $content .= "<tr><td>烤韭菜(超级辣)</td><td>x3</td><td>8.96</td></tr>";
        $content .= "</table>";
        $content .= str_repeat('.', 32);
        $content .= "<QR>这是二维码内容</QR>";
        $content .= "小计:¥82\n";
        $content .= "折扣:¥4 \n";
        $content .= str_repeat('*', 32);
        $content .= "订单总价:¥78 \n";
        $content .= "<FS2><center>**#1 完**</center></FS2>";

        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);
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    小迈克阅读 8,150评论 1 3
  • 今早4:00起床,打座练功,学习了足少阴肾经(太谿、阴谷),足太阳膀胱经(束骨、委中)和手少阴心经(神门、少海)手...
    1bf5c78057cf阅读 3,038评论 0 0
  • 我本来想当一个谦谦君子,但是在收款这方面,被拖赖的那些人或公司逼成了土匪。。。。 刚开始催款时,我的步骤比较慢...
    木质为坚阅读 1,885评论 2 0
  • 此生难看透,红尘未堪休,一语犹未尽,字未休。万般独困惑,难明画中眸,纸纸零零,泠泠然, 景中游。 千痕万道落,...
    胥淏頔阅读 984评论 0 1
  • 再次见到亓蓁,她人瘦了一些,但笑容还是那么明媚。 咖啡室里的冷气劲力十足,我努力忍着才没有打出喷嚏。还没有坐下来,...
    林慕言阅读 3,352评论 0 0