partner = $system['develop_id'] ?? ''; $this->apiKey = $system['printing_api_key'] ?? ''; $this->client_id = $system['printing_client_id'] ?? ''; $this->terminal = $system['terminal_number'] ?? ''; $this->getAccessToken(); } /** * 获取AccessToken * */ protected function getAccessToken() { $this->access_token = CacheModel::getDbCache('YLY_access_token', function () { $request = self::postRequest($this->apiUrl . 'oauth/oauth', [ 'client_id' => $this->client_id, 'grant_type' => 'client_credentials', 'sign' => strtolower(md5($this->client_id . time() . $this->apiKey)), 'scope' => 'all', 'timestamp' => time(), 'id' => $this->createUuid(), ]); $request = json_decode($request, true); $request['error'] = $request['error'] ?? 0; $request['error_description'] = $request['error_description'] ?? ''; if ($request['error'] == 0 && $request['error_description'] == 'success') { return $request['body']['access_token'] ?? ''; } return ''; }, 20 * 86400); if (!$this->access_token) throw new AuthException('获取access_token获取失败'); } /** * 生成UUID4 * @return string */ protected function createUuid() { return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); } /** * 开始打印订单 * @param $content * @param string $order_id * @return bool|mixed */ public function orderPrinting(string $order_id = '', int $errorCount = 0) { $request = self::postRequest($this->apiUrl . 'print/index', [ 'client_id' => $this->client_id, 'access_token' => $this->access_token, 'machine_code' => $this->terminal, 'content' => $this->content, 'origin_id' => $order_id ? $order_id : $this->order_id, 'sign' => strtolower(md5($this->client_id . time() . $this->apiKey)), 'id' => $this->createUuid(), 'timestamp' => time() ]); if ($request === false) return false; $request = json_decode($request, true); if (isset($request['error']) && in_array($request['error'], [18, 14]) && $errorCount == 0) { CacheModel::delectDbCache('YLY_access_token'); $this->getAccessToken(); return $this->orderPrinting($order_id, 1); } return $request; } /** * 删除授权终端 * @param $machine_code * @return bool|mixed4 */ public function deletePrinter($machine_code) { $request = self::postRequest($this->apiUrl . 'printer/deleteprinter', [ 'client_id' => $this->client_id, 'access_token' => $this->access_token, 'machine_code' => $machine_code, 'sign' => strtolower(md5($this->client_id . time() . $this->apiKey)), 'id' => $this->createUuid(), 'timestamp' => time(), ]); if ($request === false) return false; return json_decode($request, true); } /** * 设置订单打印模板 * @param string $name 商家名称 * @param array $orderInfo 下单时间 * @param array $product 订单详情 * @return $this */ public function setContent(string $name, array $orderInfo, array $product) { $timeYmd = date('Y-m-d', time()); $timeHis = date('H:i:s', time()); $goodsStr = ''; foreach ($product as $item) { $goodsStr .= ''; $sumPrice = bcmul($item['truePrice'], $item['cart_num'], 2); $goodsStr .= ""; $goodsStr .= ''; } $goodsStr .= '
商品名称数量金额小计
{$item['productInfo']['store_name']}{$item['cart_num']}{$item['truePrice']}{$sumPrice}
'; $this->order_id = $orderInfo['order_id']; $count = <<
** {$name} **
---------------- 订单编号:{$orderInfo['order_id']}\r 日 期: {$timeYmd} \r 时 间: {$timeHis}\r 姓 名: {$orderInfo['real_name']}\r 赠送积分: {$orderInfo['gain_integral']}\r 电 话: {$orderInfo['user_phone']}\r 地 址: {$orderInfo['user_address']}\r 订单备注: {$orderInfo['mark']}\r *************商品***************\r {$goodsStr} ********************************\r 合计:¥{$orderInfo['total_price']},优惠: ¥{$orderInfo['coupon_price']} 邮费:¥{$orderInfo['pay_postage']},抵扣:¥{$orderInfo['deduction_price']} 实际支付:¥{$orderInfo['pay_price']}
** 完 **
CONTENT; $this->content = $count; return $this; } /** * 获取打印内容 * @return string */ public function getContent() { return $this->content; } }