Files
kiri-wchat/wchat/wx/Account.php
T

201 lines
5.6 KiB
PHP
Raw Normal View History

2018-07-19 12:10:22 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/19 0019
* Time: 16:12
*/
2019-07-12 19:25:47 +08:00
2020-03-05 12:41:49 +08:00
namespace wchat\wx;
2019-11-11 18:14:47 +08:00
2022-09-09 16:42:55 +08:00
use Exception;
use Kiri\Client;
2020-03-05 12:41:49 +08:00
use wchat\common\Decode;
use wchat\common\HttpClient;
use wchat\common\Result;
2019-11-11 18:14:47 +08:00
class Account extends SmallProgram
2018-07-19 12:10:22 +08:00
{
2019-07-12 19:25:47 +08:00
2018-07-19 12:10:22 +08:00
/**
* @param $code
* @return Result
*/
2022-09-09 16:42:55 +08:00
public function login($code): Result
2018-07-19 12:10:22 +08:00
{
2019-07-17 17:17:37 +08:00
$param['appid'] = $this->config->getAppid();
$param['secret'] = $this->config->getAppsecret();
$param['js_code'] = $code;
$param['grant_type'] = 'authorization_code';
2022-09-09 16:42:55 +08:00
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->get('sns/jscode2session', $param);
$client->close();
2019-07-17 17:17:37 +08:00
2022-09-09 16:42:55 +08:00
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-07-17 17:17:37 +08:00
}
2019-07-19 19:11:12 +08:00
/**
* @param $openid
2022-09-09 16:42:55 +08:00
* @return Result
* @throws Exception
2019-07-19 19:11:12 +08:00
*/
2022-09-09 16:42:55 +08:00
public function getPublicUserInfo($openid): Result
2019-07-19 19:11:12 +08:00
{
$query = [
2023-07-13 08:59:23 +08:00
'access_token' => $this->config->getAccessToken(),
2019-11-11 18:14:47 +08:00
'openid' => $openid,
'lang' => 'zh_CN'
2019-07-19 19:11:12 +08:00
];
2022-09-09 16:42:55 +08:00
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->get('cgi-bin/user/info', $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']);
}
return new Result(code: 0, data: $body);
2019-07-19 19:11:12 +08:00
}
2019-07-17 17:17:37 +08:00
2019-07-17 18:56:16 +08:00
/**
* @param $encryptedData
* @param $iv
* @param $sessionKey
2022-09-09 16:42:55 +08:00
* @param bool $asArray
2019-07-17 18:56:16 +08:00
* @return object|array
* @throws
*
* * <li>-41001: encodingAesKey 非法</li>
* <li>-41003: aes 解密失败</li>
* <li>-41004: 解密后得到的buffer非法</li>
* <li>-41005: base64加密失败</li>
* <li>-41016: base64解密失败</li>
*/
2022-09-09 16:42:55 +08:00
public function decode($encryptedData, $iv, $sessionKey, bool $asArray = false): object|array
2019-07-17 18:56:16 +08:00
{
2019-11-11 18:14:47 +08:00
$decode = new Decode();
$decode->setSessionKey($sessionKey);
$decode->setEncryptedData($encryptedData);
$decode->setAppId($this->config->getAppid());
$decode->setIv($iv);
2019-07-17 18:56:16 +08:00
2019-11-11 18:14:47 +08:00
return $decode->decode($asArray);
2019-07-17 18:56:16 +08:00
}
2019-07-17 17:17:37 +08:00
/**
* @param $path
* @param $width
* @return array|mixed|Result
2022-09-09 16:42:55 +08:00
* @throws Exception
2019-07-17 17:17:37 +08:00
*/
2022-09-09 16:42:55 +08:00
public function createwxaqrcode($path, $width): mixed
2019-07-17 17:17:37 +08:00
{
2022-09-09 16:42:55 +08:00
$url = 'cgi-bin/wxaapp/createwxaqrcode?access_token=';
2019-07-17 17:17:37 +08:00
$sendBody['path'] = $path;
$sendBody['width'] = $width;
2022-09-09 16:42:55 +08:00
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
$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 (!is_null($body)) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $client->getBody());
}
2019-07-17 17:17:37 +08:00
}
/**
* @param $path
* @param $width
* @param bool $is_hyaline
* @param bool $auto_color
2022-09-09 16:42:55 +08:00
* @param string $line_color
* @return Result
2019-07-17 17:17:37 +08:00
*/
2022-09-09 16:42:55 +08:00
public function getwxacode($path, $width, bool $is_hyaline = false, bool $auto_color = false, string $line_color = ''): Result
2019-07-17 17:17:37 +08:00
{
$sendBody['path'] = $path;
$sendBody['width'] = $width;
$sendBody['auto_color'] = $auto_color;
$sendBody['is_hyaline'] = $is_hyaline;
if ($auto_color) {
$sendBody['line_color'] = $line_color;
}
2022-09-09 16:42:55 +08:00
$url = 'wxa/getwxacode?access_token=' . $this->getConfig()->getAccessToken();
2019-07-17 17:17:37 +08:00
2022-09-09 16:42:55 +08:00
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
$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 (!is_null($body)) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $client->getBody());
}
2018-07-19 12:10:22 +08:00
}
2019-07-12 19:25:47 +08:00
2019-07-17 17:17:37 +08:00
/**
* @param $path
* @param $width
* @param bool $is_hyaline
* @param bool $auto_color
2022-09-09 16:42:55 +08:00
* @param string $line_color
* @return Result
2019-07-17 17:17:37 +08:00
*/
2022-09-09 16:42:55 +08:00
public function getwxacodeunlimit($path, $width, bool $is_hyaline = false, bool $auto_color = false, string $line_color = ''): Result
2019-07-17 17:17:37 +08:00
{
$sendBody['path'] = $path;
$sendBody['width'] = $width;
$sendBody['auto_color'] = $auto_color;
$sendBody['is_hyaline'] = $is_hyaline;
if ($auto_color) {
$sendBody['line_color'] = $line_color;
}
2022-09-09 16:42:55 +08:00
$url = 'wxa/getwxacodeunlimit?access_token=' . $this->getConfig()->getAccessToken();
2019-07-17 17:17:37 +08:00
2022-09-09 16:42:55 +08:00
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->post($url . $this->getConfig()->getAccessToken(), $sendBody);
$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 (!is_null($body)) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $client->getBody());
2019-07-17 17:17:37 +08:00
}
}
2022-09-09 16:42:55 +08:00
2019-07-12 18:51:12 +08:00
}