- 新增聚水潭回调接口及事件处理机制 - 添加聚水潭商品、订单、售后同步任务 - 实现聚水潭订单推送及售后状态同步 - 新增手动同步聚水潭商品功能 - 在各路由组添加聚水潭中间件支持
89 lines
2.9 KiB
PHP
89 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace app\haichao;
|
|
|
|
use think\exception\ValidateException;
|
|
use think\facade\Log;
|
|
|
|
abstract class HaichaoBridge
|
|
{
|
|
private array $api = [
|
|
'appid' => '',
|
|
'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);
|
|
}
|
|
}
|