// +---------------------------------------------------------------------- namespace crmeb\services\printer\storage; use crmeb\services\PrinterService; use crmeb\services\printer\AccessToken; use Exception; use think\facade\Cache; /** * Class YiLianYun * @package crmeb\services\printer\storage */ class YiLianYun extends PrinterService { /** * 初始化 * @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'); } $arr = [ '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() ]; $request = $this->accessToken->postRequest($this->accessToken->getApiUrl('print/index'), $arr); $info = \Qiniu\json_decode($request); if($info->error == 18){ Cache::delete('YLY_access_token_'.$this->accessToken->clientId); $arr['access_token'] = $this->accessToken->getAccessToken(); $request = $this->accessToken->postRequest($this->accessToken->getApiUrl('print/index'), $arr); } 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 { $timeYmd = date('Y-m-d H:i:s', time()); $goodsStr = '
商品名称 | 数量 | 单价 | 金额 |
{$item['store_name']} | {$item['product_num']} | {$item['price']} | {$item['product_price']} | "; $goodsStr .= '