partner = $system['develop_id'] ?? ''; $this->apiKey = $system['printing_api_key'] ?? ''; $this->client_id = $system['printing_client_id'] ?? ''; $this->terminal = $system['terminal_number'] ?? ''; $this->getAccessToken(); } public function selectStore($store_id) { $this->store_id = $store_id; $terminal_number = SystemStore::where('id', $store_id)->value('terminal_number'); $this->terminal = $terminal_number; $this->getAccessToken($store_id); return $this; } /** * 获取AccessToken * */ protected function getAccessToken($store_id = 0) { $this->access_token = CacheModel::getDbCache('YLY_access_token' . $store_id, 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->store_id); $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 = '
商品名称 | 数量 | 金额 | 小计 |
{$item['productInfo']['store_name']} | {$item['cart_num']} | {$item['truePrice']} | {$sumPrice} | "; $goodsStr .= '