*/ class YiLianYunService extends Service { /** * @var YlyConfig */ protected $config; /** * 打印数量 * * @var int */ protected $printNum; /** * 机器码 * * @var string */ protected $machineCode; public function initConfig(YiLianYun $config) { $this->machineCode = $config->terminal_number; $this->printNum = $config->print_num; $this->config = new YlyConfig($config->app_id, $config->app_secret_key); // 授权打印机(自有型应用使用,开放型应用请跳过该步骤) // $printer = new PrinterService($this->token->access_token, $this->config); // $data = $printer->addPrinter('你的机器码', '你的机器密钥'); parent::init(); } /** * @param $data * * 58mm排版 排版指令详情请看 http://doc2.10ss.net/332006 * * $content = "
**#1 美团**
"; * $content .= str_repeat('.', 32); * $content .= "
--在线支付--
"; * $content .= "
张周兄弟烧烤
"; * $content .= "订单时间:". date("Y-m-d H:i") . "\n"; * $content .= "订单编号:40807050607030\n"; * $content .= str_repeat('*', 14) . "商品" . str_repeat("*", 14); * $content .= ""; * $content .= ""; * $content .= ""; * $content .= ""; * $content .= ""; * $content .= ""; * $content .= "
烤土豆(超级辣)x35.96
烤豆干(超级辣)x23.88
烤鸡翅(超级辣)x317.96
烤排骨(香辣)x312.44
烤韭菜(超级辣)x38.96
"; * $content .= str_repeat('.', 32); * $content .= "这是二维码内容"; * $content .= "小计:¥82\n"; * $content .= "折扣:¥4 \n"; * $content .= str_repeat('*', 32); * $content .= "订单总价:¥78 \n"; * $content .= "
**#1 完**
"; * * @param string $uuid * @param YiLianYun|array $config */ public function text($content, $uuid = '', $config = []) { if (!empty($config)) { $this->initConfig($config); } // 格式化内容 $content = $this->formattedContent($content); $token = $this->getToken(); empty($uuid) && $uuid = StringHelper::random(32, true); $print = new PrintService($token['access_token'], $this->config); $data = $print->index($this->machineCode, $content, $uuid); if ($data->error != 0) { throw new UnprocessableEntityHttpException($data->error_description); } return $data->body; } /** * @param $data * @param string $uuid * @param YiLianYun|array $config */ public function image($data, $uuid = '', $config = []) { if (!empty($config)) { $this->initConfig($config); } !$uuid && $uuid == StringHelper::random(32, true); $token = $this->getToken(); $print = new PicturePrintService($token['access_token'], $this->config); $data = $print->index($this->machineCode, '打印内容排版可看Demo下的callback.php', $uuid); var_dump($data); } protected function formattedContent($data) { if (!is_array($data)) { return $data; } $content = "{$this->printNum}"; $content .= "
****** {$data['title']} ******
"; $content .= str_repeat('.', 32); $content .= "
---{$data['payType']}---
"; // $content .= "
{$data['merchantTitle']}
"; $content .= "打印时间:" . Yii::$app->formatter->asDatetime(time()) . "\n"; $content .= "下单时间:" . $data['orderTime'] . "\n"; $content .= str_repeat('*', 13) . " 商品 " . str_repeat("*", 13); $content .= ""; $content .= ""; foreach ($data['products'] as $product) { $content .= ""; } $content .= "
商品名称数量金额
{$product['title']}x{$product['num']}¥{$product['price']}
"; $content .= str_repeat('*', 32); $content .= ""; $content .= ""; foreach ($data['marketingDetails'] as $marketingDetail) { $content .= ""; } $data['pointMoney'] > 0 && $content .= ""; $data['taxMoney'] > 0 && $content .= ""; $content .= ""; $content .= ""; $content .= ""; $content .= "
商品总价¥{$data['productOriginalMoney']}
{$marketingDetail['marketing_name']}-¥{$marketingDetail['discount_money']}
积分抵扣-¥{$data['pointMoney']}
发票税额-¥{$data['taxMoney']}
配送费¥{$data['shippingMoney']}
应付金额¥{$data['payMoney']}
"; $content .= str_repeat('.', 32); $content .= "昵称: " . $data['nickname'] . "\n"; isset($data['receiverName']) && $content .= "客户: " . $data['receiverName'] . "\n"; isset($data['receiverMobile']) && $content .= "电话: " . StringHelper::hideStr($data['receiverMobile'], 3) . "\n"; isset($data['receiverAddress']) && $content .= "地址: " . $data['receiverRegionName'] . $data['receiverAddress'] . "\n"; isset($data['buyerMessage']) && $content .= "备注: " . !empty($data['buyerMessage']) ? $data['buyerMessage'] : '无' . "\n"; if (!empty($data['qr'])) { $content .= str_repeat('.', 32); $content .= "
二维码
"; $content .= "{$data['qr']}"; } else { $content .= str_repeat('.', 32); $content .= "
二维码
"; $content .= "{$data['orderSn']}"; } $content .= str_repeat('.', 32); $content .= "
订单号
"; $content .= "{$data['orderSn']}"; $content .= "
"; $content .= "
****** 完 ******
"; return $content; } /** * @var string */ protected $cachePrefix = 'printer.yiLianYun.token.'; /** * @return array */ public function getRefreshedToken(): array { return $this->getToken(true); } /** * @param bool $refresh * @return array|object * @throws NotFoundHttpException */ public function getToken(bool $refresh = false): array { $cacheKey = $this->getCacheKey(); if (!$refresh && Yii::$app->cache->exists($cacheKey)) { return Yii::$app->cache->get($cacheKey); } $token = $this->requestToken(); $this->setToken($token->access_token, $token->refresh_token, $token->expires_in); return ArrayHelper::toArray($token); } /** * @param string $token * @param int $lifetime * @return $this * @throws NotFoundHttpException */ public function setToken(string $token, string $refresh_token, int $lifetime = 7200) { $status = Yii::$app->cache->set($this->getCacheKey(), [ 'access_token' => $token, 'refresh_token' => $refresh_token, 'expireIn' => $lifetime - 500, ], $lifetime); if ($status == false || !Yii::$app->cache->exists($this->getCacheKey())) { throw new NotFoundHttpException('Failed to cache cloud token.'); } return $this; } /** * @return $this */ public function refresh() { $this->getToken(true); return $this; } /** * @return mixed|object */ public function requestToken() { $client = new YlyOauthClient($this->config); return $client->getToken(); } /** * @return string */ protected function getCacheKey() { return $this->cachePrefix; } }