easyswoole - 基于swoole扩展实现的一款高性能php框架

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
主站蜘蛛池模板: 智能试剂柜-疾控|高校实验室|医院药品智能试剂管理柜-北京晶品赛思 | 耐磨涂料_陶瓷涂料_高温涂料_高硬度耐磨涂料-北京耐默科技 | 全自动贴标机厂家-深圳市优斯迪自动贴标机官网 | 搅拌设备_搅拌器_浓密机_浆式_顶入式_不锈钢「赛鼎机械」 | 汽车漆|汽车油漆|工业油漆涂料|汽车漆加盟-佛山市科涂涂料有限公司 | 免费会员管理系统,会员管理软件,会员卡积分系统—智络软件 | 江寒必恋术在线阅读_江寒必恋术免费下载 - 江寒必恋术电子书 | 行域人才网-垂直行业领域招聘首选的专业人才网,分行业招聘就上行域人才网 | 真空电镀机_镀膜机厂家_离子镀膜机_磁控溅射镀膜设备_镀钛设备-江苏驰诚科技发展有限公司 | 重庆聚成达汽车有限公司-重庆吸污净化车 | 友信京泰-操作台-调度台-控制台-监控台定制厂家 | 厦门,泉州自助餐上门|生日自助餐|婚礼自助餐|公司聚会自助餐|户外烧烤|冷餐|茶歇外卖配送-福建非选餐饮公司 | 停车场收费管理系统,通道闸系统,门禁系统,东莞停车场收费管理系统,东莞通道闸系统,-东莞市骄阳交通设备有限公司 | 免喷涂材料,免喷涂塑料,免喷涂注塑,免喷涂挤塑,免喷涂工艺-中山鸿盛免喷涂 | 制砂机_鹅卵石制砂机_河卵石制砂机_制砂机价格-上海山卓重工机械有限公司 | 全自动拆包机,自动拆包机,全自动逐层拆包机,全自动吨袋拆包机,吨袋拆包机,管链输送机,气流分级机 | 自动缠绕机_帝虎包装设备(上海)有限公司_缠绕包装机 | 压力变送器,差压变送器,液位变送器,温度变送器生产厂家价格-西安仕乐克仪表科技有限公司 | 泊头市鸿海泵业有限公司--导热油泵,高温油泵,沥青保温泵,圆弧泵,齿轮油泵,高粘度泵,自吸离心油泵,罗茨油泵为主的专业生产厂家 | 视频制作_产品宣传片拍摄_二维动画制作公司-深圳火牛传媒 | 郑州润滑油展-第16届中国润滑油、脂及汽车养护展览会-2025年5月27-28日-郑州国际会展中心 | 实木中药柜,实木中药斗,木制中药柜,木制中药柜的价格,实木中草药柜,安国美佳中药柜厂家 | 商标注册_北京商标注册费用_申请商标注册代理机构_北京商标注册公司- | 潍坊亿宏重工机械有限公司,破碎机,高性能立磨机,颚式破碎机,锤式破碎机反击式破碎机,重锤式破碎机,高性能反击式破碎机,圆锥式破碎机,给料机系列,链板给料机系列,简易给料机系列,振动给料机 | 西宁佳越信息科技发展有限公司- 西门子伺服电机维修_西门子变频器维修_西门子伺服驱动器维修_数控系统维修_PL维修-上海仰光电子 | 重庆自考网-重庆自学考试| 展馆周边酒店_会展中心附近酒店_展览旅游酒店预订官网-盟友云 | 曙海培训-仿真培训Linux培训html5培单片机培训PCB培训python培训PLC培训C语言培训android培训物联网培训无线电培训欧姆龙培训工业机器人培训5G培训Hadoop培训CFD培训项目外包开发咨询 | 中国环博会 | 亚洲旗舰环保展 2025.4.21-23 上海新国际博览中心 中国焊接协会网站—中国焊接信息网;焊接行业最权威访问量最大的专业网站:焊接信息、焊接材料,焊接机器,焊接设备,焊机,焊材,辅助设备,焊机配件,仪器仪表,电动工具,钎焊,送丝机,表面处理,自动化专机,焊锡丝,助焊剂 | 酒类灌装机厂家_贴标机_灌装生产线-青州市锦德包装机械有限公司 酒店设计_建筑设计_室内装修装饰-北极点酒店设计公司 | 金雷诺机柜,GLN机柜,户外机柜,电力机柜,服务器机柜 | 泰州阳光会计服务有限公司官网-泰州公司注册|泰州代理记账 | 烘干机|烘干房|网带烘干机|滚筒烘干机|炒货机-河南曼瑞通机械有限公司 | 手动叉车|电动搬运车|电动升降平台-牛力机械制造有限公司官网 | 拓展器材_拓展训练器械_心理行为训练器械_沧州华北特训器械有限公司 | 磐林投资-大宗林产品现货电子交易|林业碳汇|林权交易|农林产品投资 | 洗车机-自动汽车洗车机-全自动洗车设备-全自动电脑洗车机-北京自然绿环境科技发展有限公司 | 天津市金色巨腾科技发展有限公司-天津监控安装,天津弱电工程,天津无线网络覆盖 | 微波烘干设备厂家-微波烘干干燥设备-山东邦普机械设备有限公司 | 智能一体化蒸馏仪_氨氮蒸馏仪_全自动智能蒸馏仪器厂家-那艾 | 妙手网-圆心大药房-广东圆心恒金堂医药连锁有限公司-放心的网上药店_妙手医生旗下正规网上买药平台 |