add clear
This commit is contained in:
+17
-36
@@ -8,58 +8,39 @@
|
||||
|
||||
namespace wchat\qq;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Decode;
|
||||
use wchat\common\HttpClient;
|
||||
use wchat\common\Result;
|
||||
|
||||
class Account extends SmallProgram
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
* @param string $code
|
||||
* @return Result
|
||||
*/
|
||||
public function login($code)
|
||||
public function login(string $code): Result
|
||||
{
|
||||
$param['appid'] = $this->config->getAppid();
|
||||
$param['secret'] = $this->config->getAppsecret();
|
||||
$param['js_code'] = $code;
|
||||
$param['grant_type'] = 'authorization_code';
|
||||
|
||||
if (empty($code)) {
|
||||
return new Result(['code' => 404, 'message' => '临时登录凭证不能为空.']);
|
||||
$client = new Client('api.q.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->get('sns/jscode2session', $param);
|
||||
$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);
|
||||
}
|
||||
|
||||
$this->request->setHost('api.q.qq.com');
|
||||
$this->request->addHeader('Host', 'api.q.qq.com');
|
||||
$this->request->setMethod(HttpClient::GET);
|
||||
$this->request->setIsSSL(true);
|
||||
$this->request->setUseSwoole($this->config->isUsrSwoole());
|
||||
|
||||
return $this->request->get('sns/jscode2session', $param);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $encryptedData
|
||||
* @param $iv
|
||||
* @param $sessionKey
|
||||
* @param $asArray
|
||||
* @return object|array
|
||||
* @throws
|
||||
*
|
||||
* * <li>-41001: encodingAesKey 非法</li>
|
||||
* <li>-41003: aes 解密失败</li>
|
||||
* <li>-41004: 解密后得到的buffer非法</li>
|
||||
* <li>-41005: base64加密失败</li>
|
||||
* <li>-41016: base64解密失败</li>
|
||||
*/
|
||||
public function decode($encryptedData, $iv, $sessionKey, $asArray = false)
|
||||
{
|
||||
$decode = new Decode();
|
||||
$decode->setAppId($this->config->getAppid());
|
||||
$decode->setIv($iv);
|
||||
$decode->setEncryptedData($encryptedData);
|
||||
$decode->setSessionKey($sessionKey);
|
||||
return $decode->decode($asArray);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user