SDK是用于实现与软件平台连接、调用的类,被需要的适配器引用并实例化。实例化时,会传入基本的连接参数给SDK构造方法。
namespace Adapter\PlatformName\SDK;
class PlatformNameSDK
{
protected $connectorId = 'connectorId';
protected $env = '';
protected $host = '';
protected $login = ['appKey' => 'xxxxxx', 'appSecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',];
protected $token = null;
protected $client = \GuzzleHttp\Client::class;
public function __construct($connectorId, $params, string $env = '')
{
$this->connectorId = $connectorId;
$this->host = $params['host'];
$this->login = $params;
$this->env = $env;
$this->client = new \GuzzleHttp\Client();
}
public function invoke(string $api, $params = [], $method = 'POST')
{
}
public function connection()
{
}
}
实现SDK->connection(),连接到目标平台的方法,主要针对需要token鉴权的平台,用于管理token。
public function connection()
{
$cacheKey = $this->connectorId . $this->env;
$token = Cache::get($cacheKey);
if ($token) {
$this->token = $token;
return ['status' => true, 'token' => $token];
}
$url = $this->host . '/open-apis/auth/v3/tenant_access_token/internal';
$response = $this->client->post($url, ['form_params' => $this->login, 'headers' => ['Content-Type' => 'application/json; charset=utf-8',]]);
$body = $response->getBody();
$arr = json_decode((string)$body, true);
if ($arr['code'] == 0) {
$this->token = $arr['tenant_access_token'];
Cache::put($cacheKey, $this->token, $arr['expire'] - 100);
}
return $arr;
}
实现SDK->invoke(),实现具体接口调用方法。
public function invoke(string $api, $params = [], $method = 'POST')
{
$url = $this->host . $api;
$sign = $this->generateSign($params);
$headers = ['accesstoken' => $this->token, 'sign' => $sign, 'Content-Type' => 'application/json'];
if ($method === 'get' || $method === 'GET') {
$response = $this->client->get($url, ['query' => $params,'http_errors' => false,'headers' => $headers]);
} else {
$response = $this->client->post($url, ['body'=>json_encode($params),'http_errors' => false,'headers' => $headers]);
}
$body = $response->getBody();
$bodyStr = (string)$body;
$arr = json_decode($bodyStr,true);
return $arr;
}
protected function generateSign($params)
{
$jsonStr = json_encode($params).$this->login['appKey'];
return md5($jsonStr);
}
2021-01-09 19:58:20 | |
2021-02-19 05:51:45 | |
2021-01-30 12:31:34 | |
2021-02-20 08:37:03 | |
2021-01-16 20:59:40 | |
2021-01-19 03:24:16 | |
2021-01-18 10:15:53 | |
2021-01-18 22:55:33 | |
2021-02-16 16:44:22 | |
2021-01-12 10:04:27 | |
2021-02-04 22:09:24 | |
2021-02-11 20:54:05 | |
2021-02-10 12:19:12 | |
2021-02-10 23:08:06 | |
2021-01-09 14:36:09 | |
2021-02-19 00:06:41 | |
2021-03-03 10:21:16 | |
2021-02-05 13:42:12 | |
2021-01-24 22:00:13 | |
2021-02-13 05:12:56 | |
2023-08-14 00:27:27 | |
2023-08-12 02:43:54 | |
2023-08-11 03:49:59 | |
2023-08-10 03:03:54 | |
2023-08-09 07:41:34 |
卢剑航 13760755942
数据集成专家 拥有十多年丰富的经验,擅长ERP、MES、数据中台、营销云中台等集成。他能够根据客户需求,为其提供一站式集成解决方案,帮助企业快速实现各类系统数据集成服务。
何海波 18175716035
数据集成顾问 轻易云的技术专家,拥有丰富的数据集成规划经验。他能够为客户提供专业、全面的数据集成规划方案,熟练掌握多种集成技术和工具,帮助企业在数据集成领域得到长远发展。
黄宏棵 13286997615
数据集成顾问 资深系统集成顾问,专长于ERP、电商OMS、钉钉及CRM系统。他能提供高效的集成方案,优化企业运营流程,提升业务效率和决策智能化。