EasySwoole 易聯云打印 (Printer) 組件
EasySwoole
提供了全協程支持的云打印機 SDK
,易于使用的操作接口和風格,輕松推送海量任務至云打印機。
目前僅支持易聯云,歡迎
fork
本項目貢獻您的力量
組件要求
- php: >= 7.1
- ext-swoole: >= 4.4.23
- easyswoole/spl: ^1.4
- easyswoole/http-client: ^1.5
- psr/simple-cache: 1.0
- easyswoole/utility: ^1.2
- ext-json: *
安裝方法
composer require easyswoole/easy-printer
倉庫地址
基本使用
<?php
use EasySwoole\EasyPrinter\Commands\YiLinkCloud\PrintText;
use EasySwoole\EasyPrinter\EasyPrinter;
use EasySwoole\Utility\FileSystem;
use EasySwoole\Utility\File;
require_once __DIR__ . '/vendor/autoload.php';
class CacheConfig
{
protected $driver;
protected $dir;
protected $prefix;
public function setDriver(string $driver)
{
$this->driver = $driver;
}
public function getDriver()
{
return $this->driver;
}
public function setDir(string $dir)
{
$this->dir = $dir;
}
public function getDir()
{
return $this->dir;
}
public function setPrefix(string $prefix)
{
$this->prefix = $prefix;
}
public function getPrefix()
{
return $this->prefix;
}
}
/**
* 文件緩存
* Class FileDriver
*/
class FileDriver implements \Psr\SimpleCache\CacheInterface
{
/** @var string $dir */
protected $dir;
/** @var FileSystem $fileSystem */
protected $fileSystem;
/** @var string $prefix */
protected $prefix;
public function __construct($dir, $prefix)
{
if (empty($dir)) {
$this->dir = sys_get_temp_dir();
}
if (empty($prefix)) {
$this->prefix = 'easyswoole_cache:';
}
$this->fileSystem = new FileSystem();
File::createDirectory($this->dir);
}
/**
* @return string
*/
protected function getPrefix(): string
{
return $this->dir . DIRECTORY_SEPARATOR . $this->prefix;
}
/**
* 獲取緩存的 key
* @param string $key
* @return string
*/
public function getCacheKey(string $key)
{
return $this->getPrefix() . $key . '.cache';
}
/**
* 設置緩存
* @param string $key
* @param mixed $value
* @param null $ttl
* @return bool
*/
public function set($key, $value, $ttl = null)
{
$file = $this->getCacheKey($key);
$data = serialize($value);
$this->fileSystem->put($file, $data);
if ($ttl < time()) {
$ttl = $this->getTtlTime($ttl);
}
return touch($file, $ttl);
}
/**
* 獲取緩存
* @param string $key
* @param null $default
* @return mixed|null
* @throws Exception
*/
public function get($key, $default = null)
{
$file = $this->getCacheKey($key);
if ($this->fileSystem->missing($file)) {
return $default;
}
if ($this->fileSystem->lastModified($file) < time()) {
return $default;
}
return unserialize($this->fileSystem->get($file));
}
/**
* 獲取緩存過期時間
* @param null $ttl
* @return float|int|null
*/
public function getTtlTime($ttl = null)
{
// 如果不設置時間 默認 100 年
if (is_null($ttl)) {
$ttl = 3600 * 24 * 30 * 12 * 100;
}
$ttl = $ttl + time();
return $ttl;
}
/**
* 刪除緩存
* @param string $key
* @return bool
*/
public function delete($key)
{
$file = $this->getCacheKey($key);
return $this->fileSystem->delete($file);
}
/**
* 清空緩存
* @return bool|void
*/
public function clear()
{
$files = glob($this->getPrefix() . '*');
foreach ($files as $file) {
if (is_dir($file)) {
continue;
}
unlink($file);
}
}
/**
* 批量讀取緩存
* @param iterable $keys
* @param null $default
* @return array|iterable
* @throws Exception
*/
public function getMultiple($keys, $default = null)
{
if (!is_array($keys)) {
$keys = [$keys];
}
$result = [];
foreach ($keys as $i => $key) {
$result[$key] = $this->get($key, $default);
}
return $result;
}
/**
* 批量設置緩存
* @param iterable $values
* @param null $ttl
* @return bool>
*/
public function setMultiple($values, $ttl = null)
{
if (!is_array($values)) {
$values = [$values];
}
$ttl = $this->getTtlTime($ttl);
foreach ($values as $key => $value) {
$this->set($key, $value, $ttl);
}
return true;
}
/**
* 批量刪除緩存
* @param iterable $keys
* @return bool
*/
public function deleteMultiple($keys)
{
if (!is_array($keys)) {
$keys = [$keys];
}
foreach ($keys as $index => $key) {
$this->delete($key);
}
return true;
}
/**
* 緩存是否存在
* @param string $key
* @return bool
*/
public function has($key)
{
$file = $this->getCacheKey($key);
return file_exists($file);
}
}
class Cache implements \Psr\SimpleCache\CacheInterface
{
protected $driver;
public function __construct(CacheConfig $cacheConfig)
{
$driver = $cacheConfig->getDriver() ?: FileDriver::class;
$this->driver = new $driver($cacheConfig->getDir(), $cacheConfig->getPrefix());
}
public function __call($name, $arguments)
{
return $this->driver->{$name}(...$arguments);
}
public function set($key, $value, $ttl = null)
{
return $this->__call(__FUNCTION__, func_get_args());
}
public function setMultiple($values, $ttl = null)
{
return $this->__call(__FUNCTION__, func_get_args());
}
public function delete($key)
{
return $this->__call(__FUNCTION__, func_get_args());
}
public function has($key)
{
return $this->__call(__FUNCTION__, func_get_args());
}
public function get($key, $default = null)
{
return $this->__call(__FUNCTION__, func_get_args());
}
public function deleteMultiple($keys)
{
return $this->__call(__FUNCTION__, func_get_args());
}
public function clear()
{
return $this->__call(__FUNCTION__, func_get_args());
}
public function getMultiple($keys, $default = null)
{
return $this->__call(__FUNCTION__, func_get_args());
}
}
go(function () {
$cacheConfig = new CacheConfig();
$cache = new Cache($cacheConfig); // Cache 需要實現 \Psr\SimpleCache\CacheInterface 接口,示例僅實現了文件緩存
$clientId = '您的易聯云應用ID';
$clientSecret = '您的易聯云應用秘鑰';
$driver = EasyPrinter::yiLinkCloud($clientId, $clientSecret, $cache);
// 新建一條命令
$PrintCommand = new PrintText();
$PrintCommand->setMachineCode('打印機編號');
$PrintCommand->setContent('歡迎使用EasyPrinter!');
$PrintCommand->setOriginId(md5(microtime()));
try {
$response = $driver->sendCommand($PrintCommand);
var_dump($response);
} catch (Throwable $throwable) {
}
});
上述
Cache
參考 Cache 實現僅僅實現了文件緩存,開發者若想使用其他緩存實現,可以自行實現 PSR-16 CacheInterface 接口 進行調用。
目前已支持的指令
服務商 | 說明 | Command |
---|---|---|
易聯云 | 終端授權 (永久授權) | AuthorizePrinter |
易聯云 | 獲取請求令牌 | GetAccessToken |
易聯云 | 獲取機型打印寬度 | GetPrinterInfo |
易聯云 | 獲取終端狀態 | GetPrinterStatus |
易聯云 | 添加應用菜單 | PrinterAddMenu |
易聯云 | 取消所有未打印訂單 | PrinterCancelAll |
易聯云 | 取消單條未打印訂單 | PrinterCancelOne |
易聯云 | 取消LOGO | PrinterDeleteIcon |
易聯云 | 刪除終端授權 | PrinterDeletePrinter |
易聯云 | 刪除內置語音 | PrinterDeleteVoice |
易聯云 | 獲取訂單列表 | PrinterGetOrderPagingList |
易聯云 | 獲取訂單狀態 | PrinterGetOrderStatus |
易聯云 | 獲取機型軟硬件版本 | PrinterGetVersion |
易聯云 | 設置打印方式 | PrinterSetBtnPrinter |
易聯云 | 設置LOGO | PrinterSetIcon |
易聯云 | 接單拒單設置 | PrinterSetIfGetOrder |
易聯云 | 設置推送URL | PrinterSetPushUrl |
易聯云 | 聲音調節 | PrinterSetSound |
易聯云 | 設置內置語音 | PrinterSetVoice |
易聯云 | 關機重啟 | PrinterShutdownRestart |
易聯云 | 打印圖片 | PrintPicture |
易聯云 | 打印文字 | PrintText |