206 lines
4.9 KiB
PHP
206 lines
4.9 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: whwyy
|
|
* Date: 2018/4/19 0019
|
|
* Time: 16:12
|
|
*/
|
|
|
|
namespace wchat;
|
|
class Account extends Miniprogarampage
|
|
{
|
|
|
|
private $wxaqr = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=';
|
|
private $getwxacode = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=';
|
|
private $publicInfo = 'https://api.weixin.qq.com/cgi-bin/user/info?';
|
|
private $getwxacodeunlimit = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=';
|
|
|
|
private $savePath = __DIR__ . '/../../../';
|
|
|
|
|
|
private $OK = 0;
|
|
private $IllegalAesKey = -41001;
|
|
private $IllegalIv = -41002;
|
|
private $IllegalBuffer = -41003;
|
|
private $DecodeBase64Error = -41004;
|
|
|
|
/**
|
|
* @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(WxClient::GET);
|
|
$this->request->addHeader('Content-Type', 'text/xml');
|
|
|
|
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'
|
|
];
|
|
|
|
$this->request->setMethod(WxClient::GET);
|
|
$this->request->setIsSSL(true);
|
|
return $this->request->get($this->publicInfo, $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)
|
|
{
|
|
$config = Wx::getMiniProGaRamPage()->getConfig();
|
|
if (strlen($sessionKey) != 24) {
|
|
throw new \Exception('encodingAesKey 非法', $this->IllegalAesKey);
|
|
}
|
|
|
|
$aesKey = base64_decode($sessionKey);
|
|
if (strlen($iv) != 24) {
|
|
throw new \Exception('base64解密失败', $this->IllegalIv);
|
|
}
|
|
|
|
$aesIV = base64_decode($iv);
|
|
$aesCipher = base64_decode($encryptedData);
|
|
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV);
|
|
if ($result === false) {
|
|
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
|
|
}
|
|
|
|
$dataObj = json_decode($result);
|
|
if ($dataObj->watermark->appid != $config->getAppid()) {
|
|
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
|
|
}
|
|
|
|
if ($asArray) {
|
|
return get_object_vars($dataObj);
|
|
}
|
|
|
|
return $dataObj;
|
|
}
|
|
|
|
|
|
/**
|
|
* @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(WxClient::POST);
|
|
$this->request->setCallback([$this, 'saveByPath']);
|
|
|
|
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(WxClient::POST);
|
|
$this->request->setCallback([$this, 'saveByPath']);
|
|
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(WxClient::POST);
|
|
$this->request->setCallback([$this, 'saveByPath']);
|
|
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;
|
|
}
|
|
}
|