2019-10-25 15:20:09 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
2020-03-05 12:41:49 +08:00
|
|
|
namespace wchat\qq;
|
2019-10-25 15:20:09 +08:00
|
|
|
|
|
|
|
|
|
2022-09-09 16:42:55 +08:00
|
|
|
use Kiri\Client;
|
|
|
|
|
use wchat\common\Result;
|
2019-10-25 15:20:09 +08:00
|
|
|
|
2022-09-09 16:42:55 +08:00
|
|
|
class Token extends SmallProgram
|
2019-10-25 15:20:09 +08:00
|
|
|
{
|
|
|
|
|
|
2022-09-09 16:42:55 +08:00
|
|
|
/**
|
|
|
|
|
* @return Result
|
|
|
|
|
*/
|
|
|
|
|
public function token(): Result
|
|
|
|
|
{
|
|
|
|
|
$query = [
|
|
|
|
|
'grant_type' => 'client_credential',
|
|
|
|
|
'appid' => $this->config->getAppid(),
|
|
|
|
|
'secret' => $this->config->getAppsecret()
|
|
|
|
|
];
|
|
|
|
|
$client = new Client('api.q.qq.com', 443, true);
|
|
|
|
|
$client->withHeader(['Content-Type' => 'application/json']);
|
|
|
|
|
$client->get('/api/getToken', $query);
|
|
|
|
|
$client->close();
|
|
|
|
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
|
|
|
|
return new Result(code: 505, message: 'network error.');
|
|
|
|
|
}
|
|
|
|
|
$body = json_decode($client->getBody(), true);
|
|
|
|
|
if (isset($body['errcode']) && $body['errcode'] != 0) {
|
|
|
|
|
return new Result(code: $body['errcode'], message: $body['errmsg']);
|
|
|
|
|
} else {
|
|
|
|
|
return new Result(code: 0, data: $body);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-25 15:20:09 +08:00
|
|
|
|
|
|
|
|
}
|