日韩在线观看-日韩在线成人-日韩在线不卡视频-日韩在线不卡视频-国产精品99-国产精品99

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
主站蜘蛛池模板: 齐士龙| 美女网站视频免费| 宫心计1电视剧全集免费高清国语| 喂找谁呀 电影| 小马宝莉名字对照图片| 菊花开| 抖音网页版登录入口| 儿媳妇电视剧免费| 久草电影| 来5566最新av在线电影| 卡通图片头像| 聊斋之千年灵狐3姐妹完整版免费观看| 黄造时曹查理隔世情电影| 未来少年柯南 动漫| 喜羊羊牛气冲天| 追凶电影| 西部往事 电影| 梦桐| 电影《重生》| 艳女十八式无删减版| 栏目大全| cctv17农业农村频道在线直播| 少年王演员表全部| 四川经济频道节目表| 乱世危情电视剧演员表| 卢宇静| 《ulises》完整版在线观看| 哈尔移动的城堡 在线观看| 唐砖演员表| 误杀2演员名单| 巩俐吻戏| 日韩 欧美 视频| 漂流者| 原纱央莉| 豪斯医生第六季| 孤战迷城电视剧剧情介绍| 白培中| 抖音火山版| 体温单的绘制及图解| 郁芳最经典的三部电影| 刘德华神雕侠侣|