// +---------------------------------------------------------------------- namespace crmeb\services\printer\storage; use crmeb\services\printer\BasePrinter; /** * Class YiLianYun * @package crmeb\services\printer\storage */ class YiLianYun extends BasePrinter { /** * 初始化 * @param array $config * @return mixed|void */ protected function initialize(array $config) { } /** * 开始打印 * @return bool|mixed|string * @throws \Exception */ public function startPrinter() { if (!$this->printerContent) { return $this->setError('Missing print'); } $request = $this->accessToken->postRequest('https://open-api.10ss.net/print/index', [ 'client_id' => $this->accessToken->clientId, 'access_token' => $this->accessToken->getAccessToken(), 'machine_code' => $this->accessToken->machineCode, 'content' => $this->printerContent, 'origin_id' => 'crmeb' . time(), 'sign' => strtolower(md5($this->accessToken->clientId . time() . $this->accessToken->apiKey)), 'id' => $this->accessToken->createUuid(), 'timestamp' => time() ]); if ($request === false) { return $this->setError('request was aborted'); } $request = is_string($request) ? json_decode($request, true) : $request; if (isset($request['error']) && in_array($request['error'], [18, 14])) { return $this->setError('Accesstoken has expired'); } return $request; } /** * 设置打印内容 * @param array $config * @return YiLianYun */ public function setPrinterContent(array $config): self { $printTime = date('Y-m-d H:i:s', time()); $goodsStr = ''; $product = $config['product']; foreach ($product as $item) { $goodsStr .= ''; $price = bcmul((string)$item['cart_num'], (string)$item['truePrice'], 2); $goodsStr .= ""; $goodsStr .= ''; unset($price); } $goodsStr .= '
商品名称数量单价金额
{$item['productInfo']['store_name']} | {$item['productInfo']['attrInfo']['suk']}{$item['cart_num']}{$item['truePrice']}{$price}
'; $orderInfo = $config['orderInfo']; $orderTime = date('Y-m-d H:i:s',$orderInfo['pay_time']); $name = $config['name']; $this->printerContent = <<
** {$name} **
---------------- 订单编号:{$orderInfo['order_id']}\r 打印时间: {$printTime} \r 付款时间: {$orderTime}\r 姓 名: {$orderInfo['real_name']}\r 电 话: {$orderInfo['user_phone']}\r 地 址: {$orderInfo['user_address']}\r 赠送积分: {$orderInfo['gain_integral']}\r 订单备注:{$orderInfo['mark']}\r *************商品***************\r {$goodsStr} ********************************\r 合计:¥{$orderInfo['total_price']},优惠: ¥{$orderInfo['coupon_price']} 邮费:¥{$orderInfo['pay_postage']},抵扣:¥{$orderInfo['deduction_price']} 实际支付:¥{$orderInfo['pay_price']}
** 完 **
CONTENT; return $this; } }