我是靠谱客的博主 重要战斗机,最近开发中收集的这篇文章主要介绍PHP易联云打印机实现打印小票,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

开发文档地址:开发文档地址
各编程语言SDK:
pythonSDK
phpSDK
javaSDK
goSDK
C#SDK
nodejsSDK

本文档用PHP语言,简要说明使用方式
首先下载php的SDK,放到extend目录下,修改各个文件的命名空间
在这里插入图片描述
在这里插入图片描述
其余的内容暂时不需要改
接下来创建model文件,放在commonmodel目录下,代码如下

<?php


namespace appcommonmodel;


use printsApiPrinterService;
use printsApiPrintService;
use printsConfigYlyConfig;
use printsOauthYlyOauthClient;
use thinkDb;
use thinkException;

class YLPrint
{
    //调用打印机
    public function callPrint($content)
    {
        $config = Db::name('print_config')->find();

        $app_id = $config['app_id'];
        $app_secret = $config['app_secret'];
        $terminal_no = $config['terminal_no'];
        $terminal_secret = $config['terminal_secret'];

        $map[] = ['time','gt', time()];
        //由于每日只有20次机会获取授权,因此要access_token存到表中,当过期时间大于当前时间,可以直接使用,否则就更新
        $print_token = Db::name("print_token")->where($map)->find();

        $config = new YlyConfig($app_id, $app_secret);

        if ($print_token) {
            $access_token = $print_token['access_token'];
        } else {
            try {
                $client = new YlyOauthClient($config);
                $token = $client->getToken();

                Db::name("print_token")->where('id', 1)->update(['access_token' => $token->access_token, 'refresh_token' => $token->refresh_token,'expire_time'=>$token->expires_in, 'create_time' => date('Y-m-d H:i:s'), 'time' => time() + $token->expires_in]);
                $access_token = $token->access_token;
            } catch (Exception $e) {
                $msg = $e->getMessage();
                $msg = json_decode($msg);
                return ['msg' => 'errorCode:' . $msg->error . ',错误信息:' . $msg->error_description, 'code' => 0];
            }

        }
        //授权打印机
        $printer = new PrinterService($access_token, $config);
        $printer_status = $printer->addPrinter($terminal_no, $terminal_secret, '', '');
        if ($printer_status->error) {
            $this->printerLog('授权打印机失败:' . $printer_status->error_description);
        } else {
            $res = $this->text($access_token, $config, $terminal_no, $content);
            if (!$res) {
                return ['msg' => '请求失败', 'code' => 0];
            }
            return ['msg' => '请求成功', 'code' => 1];
        }
    }

    //打印机日志
    private function printerLog($content)
    {
        $data['content'] = $content;
        $data['create_time'] = date("Y-m-d H:i:s");
        Db::name("print_log")->insertGetId($data);
    }

    //打印文本
    public function text($access_token, $config, $terminal_no, $content = '')
    {
        $print = new PrintService($access_token, $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 .= "订单编号:40807050607030n";
        $content .= str_repeat('*', 14) . "商品" . str_repeat("*", 14);
        $content .= "<table>";
        $content .= "<tr><td>APP开发</td><td>x3</td><td>99999</td></tr>";
        $content .= "<tr><td>小程序开发</td><td>x2</td><td>88888</td></tr>";
        $content .= "<tr><td>公众号开发</td><td>x3</td><td>77777</td></tr>";
        $content .= "<tr><td>公司企业站</td><td>x3</td><td>66666</td></tr>";
        $content .= "</table>";
        $content .= str_repeat('.', 32);
        $content .= "<QR>http://www.baidu.com</QR>";
        $content .= "小计:¥333330n";
        $content .= "折扣:¥30 n";
        $content .= str_repeat('*', 32);
        $content .= "订单总价:¥333300 n";
        $content .= "<FS2><center>**#1 完**</center></FS2>";*/


        $serial_number = time();
        $result = $print->index($terminal_no, $content, $serial_number);
        if ($result->error) {
            $this->printerLog("批次:" . $serial_number . " 打印成失败。" . $result->error_description);
            return false;
        } else {
            $this->printerLog("批次:" . $serial_number . " 打印成功");
            return true;
        }

    }
}

调用

	public function prints(){
 		$print = new YLPrint();
        $content = $this->getText($order);
        $res=$print->callPrint($content);
        if($res['code']!==1){
            echo "<script>alert('{$res['msg']}');</script>";
        }
     }

    private function getText($order)
    {
        $shukuan = isset(session('admin_user')['nick']) ? session('admin_user')['nick'] : '未知';
        $total = 0;
        $content = "<FS><center>**#{$order['cname']}缴费凭证**</center></FS>";
        $content .= str_repeat('.', 32);
        $content .= '姓名:' . $order['username'] . "t";
        $content .= '单位:' . $order['company'] . "n";
        $content .= '缴费时间:' . date('Y.m.d H:i') . "t";
        $content .= '收款人:' . $shukuan . "n";
        $content .= str_repeat('*', 12) . "费用明细" . str_repeat("*", 12)."n";
        $content .= "<table>";
        $content .= "<tr><td>项目</td><td></td><td>小计</td></tr>";
        //判断缴费类型
        if ($order['request'] == '' || $order['request'] == 'train') {
            $content .= "<tr><td><FB>培训费</FB></td><td></td><td>{$order['train']}</td></tr>";
            $content .= "<tr><td>费用:</td><td>{$order['train']}元/人</td><td></td></tr>";
            $content .= "<tr><td>人数:</td><td>1</td><td></td></tr>";
            $content .= "<tr></tr>";
            $total += $order['train'];
        }
        if ($order['request'] == '' || $order['request'] == 'hotel') {
            $content .= "<tr><td><FB>酒店住宿</FB></td><td></td><td>{$order['hotel_total']}</td></tr>";
            $content .= "<tr><td>酒店:</td><td>{$order['hotel_name']}</td><td></td></tr>";
            $content .= "<tr><td>住房类型</td><td>{$order['hotel_type']}({$order['hotel_price']}元/天)</td><td></td></tr>";
            $content .= "<tr><td>时间</td><td>共{$order['hotel_day']}天</td><td></td></tr>";
            $content .= "<tr></tr>";
            $total += $order['hotel_total'];
        }
        if ($order['request'] == '' || $order['request'] == 'food') {
            $content .= "<tr><td><FB>餐饮费用</FB></td><td></td><td>{$order['food_total']}</td></tr>";
            $content .= "<tr><td>就餐点:</td><td>{$order['area']}</td><td></td></tr>";
            $content .= "<tr><td>中餐:</td><td>共{$order['noon_num']}餐,{$order['noon_price']}元/餐</td><td></td></tr>";
            $content .= "<tr><td>晚餐:</td><td>共{$order['dinner_num']}餐,{$order['dinner_price']}元/餐</td><td></td></tr>";
            $content .= "<tr></tr>";
            $total += $order['food_total'];
        }
        if ($order['request'] == '' || $order['request'] == 'tour') {
            $content .= "<tr><td><FB>考察费用</FB></td><td></td><td>{$order['tour_price']}</td></tr>";
            $content .= "<tr><td>考察线路:</td><td>{$order['tour_line']}</td><td></td></tr>";
            $content .= "<tr><td>考察项目</td><td>{$order['tour_project']}</td><td></td></tr>";
            $content .= "<tr></tr>";
            $total += $order['tour_price'];
        }
        $content .= "</table>";
        $content .= str_repeat('.', 32);
//        $content .= "<QR>http://www.baidu.com</QR>";
        $content .= "<FB><FS>合计:¥{$total}</FS></FB>t";
        $content .= "<FB><FS>实收:¥{$total}</FS></FB> n";
        $content .= str_repeat('*', 32);
        $content .= "<FS2><center>**#1 完**</center></FS2>";
        return $content;
    }

     ```


附上表结构

```sql
CREATE TABLE `meet_print_token`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `access_token` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `refresh_token` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `expire_time` datetime(0) NULL DEFAULT NULL COMMENT '有效期',
  `time` int(11) NULL DEFAULT NULL COMMENT '过期时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = 'token表' ROW_FORMAT = Compact;

CREATE TABLE `meet_print_config`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `app_id` int(11) NOT NULL,
  `app_secret` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `terminal_no` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `terminal_secret` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '打印机配置表' ROW_FORMAT = Compact;

CREATE TABLE `meet_print_log`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `create_time` datetime(0) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '打印日志表' ROW_FORMAT = Compact;

最后

以上就是重要战斗机为你收集整理的PHP易联云打印机实现打印小票的全部内容,希望文章能够帮你解决PHP易联云打印机实现打印小票所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部