亚洲精品成人_精品成人一区_999视频在线播放_免费黄色在线_亚洲成人久久久_久久www免费视频

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
主站蜘蛛池模板: 香蕉婷婷| 77成人影视| 亚洲免费在线观看视频 | 天堂最新地址 | 国产精品女同一区二区 | av在线免费网站 | 国产一级高清 | 色视频在线观看免费 | 日韩欧美亚洲精品 | 亚洲高清视频一区 | 欧美日韩在线播放 | 在线观看一区二区三区四区 | 亚洲视频手机在线观看 | 国产成人小视频在线观看 | 欧美日韩在线中文字幕 | 激情伊人网 | 久久久精品综合 | 亚洲一区二区精品视频 | 人人爽人人爽人人片av免费 | 国产精品成人免费一区二区视频 | 在线观看黄色片网站 | 国产三级毛片视频 | 成年人视频在线观看免费 | 全部孕妇丰满孕妇孕交 | 欧美成人性生交大片免费观看 | 久久国产香蕉 | 成人在线视频网 | 力建数控母线加工机 | 先锋影音在线播放 | 一区二区三区视频在线观看 | 亚洲精品成人在线播放 | 亚洲精品国产一区 | 亚洲免费影视 | 99视频网站| 午夜免费播放观看在线视频 | 久久综合成人精品亚洲另类欧美 | 美国av在线免费观看 | 日韩三级视频在线播放 | a毛片免费精品一区二区三区 | 日本三级在线电影 | 国产一区二区网站 |