secretKey = $secretKey; $this->httpClient = new Client($this->guzzleOptions); $this->callback = $callback; } public function getHttpClient(): Client { return $this->httpClient; } /** * 请求参数签名 * @param $params * @return string */ private function generateSignature($params): string { return (new Sign())->generateHmacSignature($this->secretKey, $params); } /** * 接口请求 * @param $api * @param $data * @return mixed|string * @throws HttpException|GuzzleException */ private function sendRequest($api, $data) { $url = self::API_URL . $api; // 附加参数 $params = array_merge([ 'times' => time(), 'output' => self::DEFAULT_FORMAT, 'extensions' => self::DEFAULT_TYPE, ], $data); $signature = $this->generateSignature($params); // 请求参数 $body = [ 'signature' => $signature, 'data' => $params ]; if ($this->callback != '') { $body = array_merge(['callback' => $this->callback], $body); } $params = [ 'headers' => ['content-type' => 'application/json', 'accept' => 'application/json', 'user-agent' => 'mes sdk'], 'body' => json_encode($body), 'http_errors' => false ]; try { $response = $this->httpClient ->request('POST', $url, $params) ->getBody() ->getContents(); return 'json' === self::DEFAULT_FORMAT ? json_decode($response, true) : $response; } catch (\Exception $e) { throw new HttpException($e->getMessage(), $e->getCode(), $e); } } /** * 获取工单信息 * @param $order_sn * @return mixed|string * @throws HttpException|GuzzleException */ public function getProductOrder($order_sn) { $data = [ 'order_sn' => $order_sn ]; return $this->sendRequest('production.status', $data); } /** * 加工工单创建 * @return mixed|string * @throws HttpException|GuzzleException */ public function createProductOrder($data) { return $this->sendRequest('production.create', $data); } }