黄p网站_在线看中文字幕_亚洲电影免费观看_成人激情视频_欧美成视频_中国av在线

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

倉庫地址

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
主站蜘蛛池模板: 一区二区三区回区在观看免费视频 | 日日日操 | 国产成人免费视频网站视频社区 | 日韩一区二区三区在线 | 精品一区二区三区免费视频 | 三区在线 | 午夜精品久久久久久久星辰影院 | 亚洲精品一区在线观看 | 久久久久久九九九 | 国产精品久久久久久一级毛片 | 欧美日韩成人在线 | 日日操天天操 | 国产高清在线精品一区 | 山岸逢花在线观看 | 在线免费观看色视频 | 中文字幕av网 | 久久久久久国产精品美女 | 中文无码日韩欧 | 日韩免费视频 | 日本不卡一区二区 | 久久久久久国产 | 高清成人| 午夜影院免费 | 日韩毛片 | 国产在线免费 | 国产视频二 | 毛片链接| 99热热热| 范冰冰一级做a爰片久久毛片 | 色综合激情 | 精品日韩| 国产一区二区在线看 | 日本在线免费看 | 国产精品久久国产精品 | 一区二区三区回区在观看免费视频 | 男人av网 | 成人小视频在线观看 | 欧洲精品视频在线观看 | 久久久精品 | 久久久久亚洲一区二区三区 | 欧美日韩一区二区三区在线观看 |