commit 5eefef9b19a7e40027a99f9c07c7e012de7532a0 Author: X14XA\shengli Date: Wed May 7 10:51:21 2025 +0800 MQTT 管理客户端 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2f4ab1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +composer.lock +.idea/ +vendor/ +tests/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..583e331 --- /dev/null +++ b/composer.json @@ -0,0 +1,30 @@ +{ + "name": "maiyoule/mqttclient_author", + "type": "library", + "description": "MQTT管理模块操作库", + "authors": [ + { + "name": "Shengli", + "email": "mothz@126.com", + "role": "developer" + } + ], + "require": { + "php": ">=8.0", + "guzzlehttp/guzzle": "~6.0", + "phpseclib/phpseclib": "~3.0" + }, + "require-dev": { + "phpunit/phpunit": "^11.0" + }, + "autoload": { + "psr-4": { + "cn\\com\\maiyoule\\mqttclient\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "cn\\com\\maiyoule\\mqttclient\\test\\": "tests" + } + } +} \ No newline at end of file diff --git a/src/IRequest.php b/src/IRequest.php new file mode 100644 index 0000000..1217977 --- /dev/null +++ b/src/IRequest.php @@ -0,0 +1,20 @@ +privateKey; + } + + public function setPrivateKey(string $privateKey): void + { + $this->privateKey = $privateKey; + } + + public function getType(): string + { + return $this->type; + } + + public function setType(string $type): void + { + $this->type = $type; + } + + public function getAppId(): string + { + return $this->appId; + } + + public function setAppId(string $appId): void + { + $this->appId = $appId; + } + + public function getApi(): string + { + return $this->api; + } + + public function setApi(string $api): void + { + $this->api = $api; + } + + public function isDebug(): bool + { + return $this->debug; + } + + public function setDebug(bool $debug): void + { + $this->debug = $debug; + } + + private ?Client $client = null; + + private function getHttpClient(): Client + { + if ($this->client == null) { + $this->client = new Client([ + 'base_uri' => $this->api, + 'allow_redirects' => [ + 'max' => 2 + ], + 'debug' => $this->debug, + 'headers' => [ + 'user-agent' => 'MaiYouLeMQTTClientAuthor/' . self::VERSION, + 'Accept' => 'application/json' + ] + ]); + } + return $this->client; + } + + + private function sign(string $str) + { + $prikey = RSA::loadPrivateKey($this->privateKey); + $result = $prikey->sign($str); + return base64_encode($result); + } + + /** + * @param IRequest $request + * @return BizResponse + * @throws ApiException + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function exec(IRequest $request): BizResponse + { + //生成签名 + $url = sprintf('%s%s', $this->api, $request->path()); + $urls = parse_url($url); + + $path = $urls['path']; + $body = $request->body(); + + $body['appid'] = $this->getAppId(); + $body['type'] = $this->getType(); + + ksort($body, SORT_NATURAL); + + $waitItem = []; + foreach ($body as $key => $value) { + $waitItem[] = $key . '=' . urlencode($value); + } + $waitItem[] = urlencode($path); + $str = join('&', $waitItem); + $sign = $this->sign($str); + $body['sign'] = $sign; + $http = $this->getHttpClient(); + + try { + $result = $http->request('POST', $path, [ + 'form_params' => $body + ]); + $content = $result->getBody()->getContents(); + if (empty($content)) { + //没有反馈内容 + return new BizResponse(); + } + $data = json_decode($content, true); + return new BizResponse($data['code'] ?? 0, $data['message'] ?? '', $data['data'] ?? []); + + } catch (ClientException $e) { + $body = $e->getResponse()->getBody()->getContents(); + $message = ''; + if (!empty($body)) { + $data = json_decode($body, true); + $message = $data['message'] ?? ''; + } + throw new ApiException($message, $e->getResponse()->getStatusCode()); + } + + } +} \ No newline at end of file diff --git a/src/biz/AppUserCreateRequest.php b/src/biz/AppUserCreateRequest.php new file mode 100644 index 0000000..a7d2a77 --- /dev/null +++ b/src/biz/AppUserCreateRequest.php @@ -0,0 +1,74 @@ + $this->getPassword(), + 'fettle' => $this->getFettle(), + 'role' => $this->getRole(), + 'biz' => join(',', $this->getBiz()) + ]; + } + + private array $biz = []; + + public function getBiz(): array + { + return $this->biz; + } + + public function setBiz(array $biz): void + { + $this->biz = $biz; + } + + private string $password = ''; + private string $fettle = ''; + private string $role = 'user'; + + + public function getRole(): string + { + return $this->role; + } + + public function setRole(string $role): void + { + $this->role = $role; + } + + + public function getPassword(): string + { + return $this->password; + } + + public function setPassword(string $password): void + { + $this->password = $password; + } + + public function getFettle(): string + { + return $this->fettle; + } + + public function setFettle(string $fettle): void + { + $this->fettle = $fettle; + } + + +} \ No newline at end of file diff --git a/src/biz/AppUserDeleteRequest.php b/src/biz/AppUserDeleteRequest.php new file mode 100644 index 0000000..01270a8 --- /dev/null +++ b/src/biz/AppUserDeleteRequest.php @@ -0,0 +1,39 @@ + $this->getUsername() + ]; + } + + + private string $username; + + public function getUsername(): string + { + return $this->username; + } + + public function setUsername(string $username): void + { + $this->username = $username; + } + + +} \ No newline at end of file diff --git a/src/biz/AppUserUpdateRequest.php b/src/biz/AppUserUpdateRequest.php new file mode 100644 index 0000000..eb30636 --- /dev/null +++ b/src/biz/AppUserUpdateRequest.php @@ -0,0 +1,110 @@ + $this->getUsername(), + 'state' => $this->isState() ? 0 : 1, + 'role' => $this->getRole() + ]; + + if (!is_null($this->password)) { + $data['password'] = $this->password; + } + if (!is_null($this->fettle)) { + $data['fettle'] = $this->fettle; + } + if (!is_null($this->biz)) { + $data['biz'] = join(',', $this->biz); + } + + return $data; + } + + private ?array $biz = null; + + public function getBiz(): array + { + return $this->biz; + } + + public function setBiz(array $biz): void + { + $this->biz = $biz; + } + + private string $username; + private ?string $password = null; + private bool $state = true; + private ?string $fettle = null; + + private string $role = ''; + + public function getRole(): string + { + return $this->role; + } + + public function setRole(string $role): void + { + $this->role = $role; + } + + + public function getUsername(): string + { + return $this->username; + } + + public function setUsername(string $username): void + { + $this->username = $username; + } + + public function getPassword(): string + { + return $this->password; + } + + public function setPassword(string $password): void + { + $this->password = $password; + } + + public function isState(): bool + { + return $this->state; + } + + public function setState(bool $state): void + { + $this->state = $state; + } + + public function getFettle(): string + { + return $this->fettle; + } + + public function setFettle(string $fettle): void + { + $this->fettle = $fettle; + } + + +} \ No newline at end of file diff --git a/src/exception/ApiException.php b/src/exception/ApiException.php new file mode 100644 index 0000000..18cd2ba --- /dev/null +++ b/src/exception/ApiException.php @@ -0,0 +1,14 @@ +code = $code; + $this->message = $message; + $this->data = $data; + } + + public function isSuccess(): bool + { + return $this->code == 1; + } + + public function getCode(): int + { + return $this->code; + } + + public function setCode(int $code): void + { + $this->code = $code; + } + + public function getMessage(): string + { + return $this->message; + } + + public function setMessage(string $message): void + { + $this->message = $message; + } + + /** + * @return mixed + */ + public function getData() + { + return $this->data; + } + + /** + * @param mixed $data + */ + public function setData($data): void + { + $this->data = $data; + } + + +} \ No newline at end of file