Files
kiri-wchat/wchat/wx/Account.php
T
2020-11-14 03:04:25 +08:00

201 lines
5.0 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/19 0019
* Time: 16:12
*/
namespace wchat\wx;
use wchat\common\Decode;
use wchat\common\HttpClient;
use wchat\common\Result;
class Account extends SmallProgram
{
private string $wxaqr = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=';
private string $getwxacode = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=';
private string $getwxacodeunlimit = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=';
private string $savePath = __DIR__ . '/../../Users/';
/**
* @param $path
*/
public function setSavePath($path)
{
$this->savePath = $path;
}
/**
* @param $code
* @return Result
*/
public function login($code)
{
$param['appid'] = $this->config->getAppid();
$param['secret'] = $this->config->getAppsecret();
$param['js_code'] = $code;
$param['grant_type'] = 'authorization_code';
$this->request->setMethod(HttpClient::GET);
$this->request->addHeader('Content-Type', 'application/json');
$this->request->setHost('api.weixin.qq.com');
$this->request->addHeader('Host', 'api.weixin.qq.com');
$this->request->setErrorField('errcode');
$this->request->setErrorMsgField('errmsg');
return $this->request->get('sns/jscode2session', $param);
}
/**
* @param $openid
* @return array|mixed|Result
* @throws \Exception
*/
public function getPublicUserInfo($openid)
{
$query = [
'access_token' => $this->getAccessToken(),
'openid' => $openid,
'lang' => 'zh_CN'
];
$client = HttpClient::NewRequest();
$client->setAgent($this->config->getAgent());
$client->setUseSwoole($this->config->isUsrSwoole());
$client->setMethod(HttpClient::GET);
$client->setIsSSL(true);
$client->setHost('api.weixin.qq.com');
$client->addHeader('Host', 'api.weixin.qq.com');
$client->setErrorField('errcode');
$client->setErrorMsgField('errmsg');
return $client->get('cgi-bin/user/info', $query);
}
/**
* @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->setSessionKey($sessionKey);
$decode->setEncryptedData($encryptedData);
$decode->setAppId($this->config->getAppid());
$decode->setIv($iv);
return $decode->decode($asArray);
}
/**
* @param $path
* @param $width
* @return array|mixed|Result
* @throws \Exception
*/
public function createwxaqrcode($path, $width)
{
$url = $this->wxaqr . $this->getAccessToken();
$sendBody['path'] = $path;
$sendBody['width'] = $width;
$this->request->setMethod(HttpClient::POST);
$this->request->setCallback([$this, 'saveByPath']);
$this->request->setErrorField('errcode');
$this->request->setErrorMsgField('errmsg');
return $this->request->post($url, $sendBody);
}
/**
* @param $path
* @param $width
* @param bool $is_hyaline
* @param bool $auto_color
* @param string $line_color
* @return array|mixed|Result
* @throws \Exception
*/
public function getwxacode($path, $width, $is_hyaline = false, $auto_color = false, $line_color = '')
{
$sendBody['path'] = $path;
$sendBody['width'] = $width;
$sendBody['auto_color'] = $auto_color;
$sendBody['is_hyaline'] = $is_hyaline;
if ($auto_color) {
$sendBody['line_color'] = $line_color;
}
$url = $this->getwxacode . $this->getAccessToken();
$this->request->setMethod(HttpClient::POST);
$this->request->setCallback([$this, 'saveByPath']);
$this->request->setErrorField('errcode');
$this->request->setErrorMsgField('errmsg');
return $this->request->post($url, $sendBody);
}
/**
* @param $path
* @param $width
* @param bool $is_hyaline
* @param bool $auto_color
* @param string $line_color
* @return array|mixed|Result
* @throws \Exception
*/
public function getwxacodeunlimit($path, $width, $is_hyaline = false, $auto_color = false, $line_color = '')
{
$sendBody['path'] = $path;
$sendBody['width'] = $width;
$sendBody['auto_color'] = $auto_color;
$sendBody['is_hyaline'] = $is_hyaline;
if ($auto_color) {
$sendBody['line_color'] = $line_color;
}
$url = $this->getwxacodeunlimit . $this->getAccessToken();
$this->request->setMethod(HttpClient::POST);
$this->request->setCallback([$this, 'saveByPath']);
$this->request->setErrorField('errcode');
$this->request->setErrorMsgField('errmsg');
return $this->request->post($url, $sendBody);
}
/**
* @param mixed $body
* @return string
* @throws \Exception
*/
public function saveByPath($body)
{
if (!is_null($json = json_decode($body))) {
throw new \Exception($json['errmsg'], $json['errcode']);
}
$push = md5_file($body) . '.png';
file_put_contents($this->savePath . $push, $this->savePath);
return $this->savePath . $push;
}
}