'', 'appSecret' => '', 'api' => 'https://openapi.jushuitan.com/', 'access_token' => '', 'shopId' => '', ]; public function __construct() { $this->api['appid'] = env('JST_APP_KEY', ''); $this->api['appSecret'] = env('JST_APP_SECRET', ''); $this->api['api'] = env('JST_API', 'https://openapi.jushuitan.com/'); $this->api['access_token'] = env('JST_ACCESS_TOKEN', ''); $this->api['shopId'] = (string)env('JST_SHOP_ID', ''); } public function getShopId(): string { return $this->api['shopId']; } /** * 聚水潭开放平台请求封装 * @param string $api * @param array $data * @param array $addonBody * @return array */ protected function post(string $api, array $data = [], array $addonBody = []): array { if (!$this->api['appid'] || !$this->api['appSecret'] || !$this->api['access_token']) { throw new ValidateException('JST config missing'); } $time = time(); $baseBody = [ 'access_token' => $this->api['access_token'], 'app_key' => $this->api['appid'], ]; $baseBody = array_merge($baseBody, $addonBody); $baseBody['biz'] = json_encode($data, JSON_UNESCAPED_UNICODE); $baseBody['charset'] = 'utf-8'; $baseBody['timestamp'] = $time; $baseBody['version'] = '2'; $baseBody['sign'] = $this->sign($baseBody); $options['http'] = [ 'timeout' => 60, 'method' => 'POST', 'header' => 'Content-Type: application/json;charset=utf-8', 'content' => http_build_query($baseBody) ]; $rt = json_decode(file_get_contents("{$this->api['api']}$api", false, stream_context_create($options)), true); if (!is_array($rt) || !array_key_exists('code', $rt)) { Log::error('JST response invalid ' . json_encode([ 'api' => $api, ], JSON_UNESCAPED_UNICODE)); throw new ValidateException('JST response invalid'); } if ($rt['code'] != 0) { Log::error('JST request failed ' . json_encode([ 'api' => $api, 'code' => $rt['code'], 'msg' => (string)($rt['msg'] ?? ''), ], JSON_UNESCAPED_UNICODE)); throw new ValidateException($rt['msg'] ?? 'JST error'); } return $rt; } private function sign(array $data): string { $computerStr = ""; foreach ($data as $k => $v) { $computerStr .= $k . $v; } $computerStr = $this->api['appSecret'] . $computerStr; return md5($computerStr); } }