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

EasySwoole 易聯(lián)云打印 (Printer) 組件

EasySwoole 提供了全協(xié)程支持的云打印機(jī) SDK,易于使用的操作接口和風(fēng)格,輕松推送海量任務(wù)至云打印機(jī)。

目前僅支持易聯(lián)云,歡迎 fork 本項目貢獻(xiàn)您的力量

組件要求

  • 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';
    }

    /**
     * 設(shè)置緩存
     * @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)
    {
        // 如果不設(shè)置時間 默認(rèn) 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;
    }

    /**
     * 批量設(shè)置緩存
     * @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 需要實現(xiàn) \Psr\SimpleCache\CacheInterface 接口,示例僅實現(xiàn)了文件緩存

    $clientId = '您的易聯(lián)云應(yīng)用ID';
    $clientSecret = '您的易聯(lián)云應(yīng)用秘鑰';
    $driver = EasyPrinter::yiLinkCloud($clientId, $clientSecret, $cache);

    // 新建一條命令
    $PrintCommand = new PrintText();
    $PrintCommand->setMachineCode('打印機(jī)編號');
    $PrintCommand->setContent('歡迎使用EasyPrinter!');
    $PrintCommand->setOriginId(md5(microtime()));

    try {
        $response = $driver->sendCommand($PrintCommand);
        var_dump($response);
    } catch (Throwable $throwable) {

    }
});

上述 Cache 參考 Cache 實現(xiàn)僅僅實現(xiàn)了文件緩存,開發(fā)者若想使用其他緩存實現(xiàn),可以自行實現(xiàn) PSR-16 CacheInterface 接口 進(jìn)行調(diào)用。

目前已支持的指令

服務(wù)商 說明 Command
易聯(lián)云 終端授權(quán) (永久授權(quán)) AuthorizePrinter
易聯(lián)云 獲取請求令牌 GetAccessToken
易聯(lián)云 獲取機(jī)型打印寬度 GetPrinterInfo
易聯(lián)云 獲取終端狀態(tài) GetPrinterStatus
易聯(lián)云 添加應(yīng)用菜單 PrinterAddMenu
易聯(lián)云 取消所有未打印訂單 PrinterCancelAll
易聯(lián)云 取消單條未打印訂單 PrinterCancelOne
易聯(lián)云 取消LOGO PrinterDeleteIcon
易聯(lián)云 刪除終端授權(quán) PrinterDeletePrinter
易聯(lián)云 刪除內(nèi)置語音 PrinterDeleteVoice
易聯(lián)云 獲取訂單列表 PrinterGetOrderPagingList
易聯(lián)云 獲取訂單狀態(tài) PrinterGetOrderStatus
易聯(lián)云 獲取機(jī)型軟硬件版本 PrinterGetVersion
易聯(lián)云 設(shè)置打印方式 PrinterSetBtnPrinter
易聯(lián)云 設(shè)置LOGO PrinterSetIcon
易聯(lián)云 接單拒單設(shè)置 PrinterSetIfGetOrder
易聯(lián)云 設(shè)置推送URL PrinterSetPushUrl
易聯(lián)云 聲音調(diào)節(jié) PrinterSetSound
易聯(lián)云 設(shè)置內(nèi)置語音 PrinterSetVoice
易聯(lián)云 關(guān)機(jī)重啟 PrinterShutdownRestart
易聯(lián)云 打印圖片 PrintPicture
易聯(lián)云 打印文字 PrintText
主站蜘蛛池模板: 日韩在线视频观看 | 色先锋资源 | 日韩欧美久久 | 嫩呦国产一区二区三区av | 国产成人免费 | 国产一区二区久久久 | 色婷婷亚洲| 91精品一区二区三区久久久久久 | 欧美日韩亚洲在线 | 午夜影院免费看 | 亚洲一区二区在线 | 国产精久久久久久久妇剪断 | 性处破╳╳╳高清欧美 | 国产精品久久久久久久久小说 | 欧美电影一区 | 亚洲精品午夜国产va久久成人 | 日韩一区二区中文字幕 | 亚洲国产精品成人 | 久久一区二区精品 | 久久综合九色综合欧美狠狠 | 日韩毛片在线观看 | 欧美日韩视频 | 99免费视频 | 国内精品一区二区三区视频 | 日本亚洲最大的色成网站www | 极品毛片 | 一级毛片,一级毛片 | 亚洲www啪成人一区二区 | 91精品国产综合久久久久久丝袜 | 黄色网址av | 精精国产xxxx视频在线 | 久久1区 | 久久免费黄色网址 | a级片在线观看 | 毛片免费看| 亚洲一区二区免费视频 | 狠狠综合久久av一区二区老牛 | 国产高清在线观看 | 久久国| 岛国av在线 | 国产女人爽到高潮免费视频 |