官网地址
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);
}
}
}