add clear
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount;
|
||||
|
||||
|
||||
use common\HttpClient;
|
||||
|
||||
class AccessToken extends AfficialAccount
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount;
|
||||
|
||||
|
||||
use common\HttpClient;
|
||||
use common\Miniprogarampage;
|
||||
|
||||
abstract class AfficialAccount extends Miniprogarampage
|
||||
{
|
||||
protected static $instance;
|
||||
|
||||
private $url = 'https://api.weixin.qq.com/cgi-bin/token?';
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function generateAccess_token()
|
||||
{
|
||||
if (!empty($this->config->getToken())) {
|
||||
return $this->config->getToken();
|
||||
}
|
||||
$requestParam['grant_type'] = 'client_credential';
|
||||
$requestParam['appid'] = $this->config->getAppid();
|
||||
$requestParam['secret'] = $this->config->getAppsecret();
|
||||
|
||||
$result = HttpClient::NewRequest()->get($this->url, $requestParam);
|
||||
if (!$result->isResultsOK()) {
|
||||
throw new \Exception($result->getMessage(), $result->getCode());
|
||||
}
|
||||
return $result->getData();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount;
|
||||
|
||||
|
||||
use officialaccount\dcaler\SnsInfo;
|
||||
use common\HttpClient;
|
||||
use officialaccount\dcaler\Authorization as DAuth;
|
||||
|
||||
class Authorization extends AfficialAccount
|
||||
{
|
||||
|
||||
private $url = 'https://api.weixin.qq.com/cgi-bin/user/info';
|
||||
|
||||
private $turn = 'https://api.weixin.qq.com/cgi-bin/shorturl?access_token=';
|
||||
|
||||
private $oauth2 = 'https://api.weixin.qq.com/sns/oauth2/access_token';
|
||||
private $snsUserInfo = 'https://api.weixin.qq.com/sns/userinfo';
|
||||
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
* @return array|mixed|\wchat\Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function authorization_user_info($code)
|
||||
{
|
||||
$param = $this->GetAccessTokenByCode($code);
|
||||
if (!$param->isResultsOK()) {
|
||||
throw new \Exception($param->getMessage());
|
||||
}
|
||||
|
||||
$requestParam['access_token'] = $param->getData('access_token');
|
||||
$requestParam['openid'] = $param->getData('openid');
|
||||
$requestParam['lang'] = 'zh_CN';
|
||||
|
||||
$client = HttpClient::NewRequest();
|
||||
$result = $client->get($this->snsUserInfo, $requestParam);
|
||||
if ($result->isResultsOK()) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$userInfo = SnsInfo::instance($result->getData());
|
||||
$result->append('instance', $userInfo);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
* @return array|mixed|\wchat\Result
|
||||
*/
|
||||
private function GetAccessTokenByCode($code)
|
||||
{
|
||||
$requestParam['appid'] = $this->config->getAppid();
|
||||
$requestParam['secret'] = $this->config->getAppsecret();
|
||||
$requestParam['code'] = $code;
|
||||
$requestParam['grant_type'] = 'authorization_code';
|
||||
|
||||
$client = HttpClient::NewRequest();
|
||||
return $client->get($this->oauth2, $requestParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $openid
|
||||
* @return DAuth
|
||||
* @throws \Exception
|
||||
* 获取用户信息
|
||||
*/
|
||||
public function To_grant_authorization($openid)
|
||||
{
|
||||
$requestParam['access_token'] = $this->config->getAccessToken();
|
||||
$requestParam['openid'] = $openid;
|
||||
$requestParam['lang'] = 'zh_CN';
|
||||
|
||||
$authorization = $this->request->get($this->url, $requestParam);
|
||||
if (!$authorization) {
|
||||
throw new \Exception($authorization->getMessage());
|
||||
}
|
||||
|
||||
return DAuth::instance($authorization->getData());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $long_url
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function Turn_to_short_chain($long_url)
|
||||
{
|
||||
$token = $this->config->getAccessToken();
|
||||
$requestParam['action'] = 'long2short';
|
||||
$requestParam['long_url'] = $long_url;
|
||||
|
||||
$result = $this->request->post($this->turn . $token, $requestParam);
|
||||
if (!$result->isResultsOK()) {
|
||||
throw new \Exception($result->getMessage());
|
||||
}
|
||||
return $result->getData('short_url');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount;
|
||||
|
||||
|
||||
use officialaccount\dcaler\Prpcrypt;
|
||||
use officialaccount\dcaler\XMLParse;
|
||||
|
||||
class NewsManager extends AfficialAccount
|
||||
{
|
||||
|
||||
const OK = 0;
|
||||
const ValidateSignatureError = -40001;
|
||||
const ParseXmlError = -40002;
|
||||
const ComputeSignatureError = -40003;
|
||||
const IllegalAesKey = -40004;
|
||||
const ValidateAppidError = -40005;
|
||||
const EncryptAESError = -40006;
|
||||
const DecryptAESError = -40007;
|
||||
const IllegalBuffer = -40008;
|
||||
const EncodeBase64Error = -40009;
|
||||
const DecodeBase64Error = -40010;
|
||||
const GenReturnXmlError = -40011;
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function toError($code)
|
||||
{
|
||||
$messages = [
|
||||
self::ValidateSignatureError => '签名验证错误',
|
||||
self::ParseXmlError => 'xml解析失败',
|
||||
self::ComputeSignatureError => 'sha加密生成签名失败',
|
||||
self::IllegalAesKey => 'encodingAesKey 非法',
|
||||
self::ValidateAppidError => 'appid 校验错误',
|
||||
self::EncryptAESError => 'aes 加密失败',
|
||||
self::DecryptAESError => 'aes 解密失败',
|
||||
self::IllegalBuffer => '解密后得到的buffer非法',
|
||||
self::EncodeBase64Error => 'base64加密失败',
|
||||
self::DecodeBase64Error => 'base64解密失败',
|
||||
self::GenReturnXmlError => '生成xml失败',
|
||||
];
|
||||
return $messages[$code] ?? 'OK';
|
||||
}
|
||||
|
||||
/**
|
||||
* 将公众平台回复用户的消息加密打包.
|
||||
* <ol>
|
||||
* <li>对要发送的消息进行AES-CBC加密</li>
|
||||
* <li>生成安全签名</li>
|
||||
* <li>将消息密文和安全签名打包成xml格式</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param $replyMsg string 公众平台待回复用户的消息,xml格式的字符串
|
||||
* @param $timeStamp string 时间戳,可以自己生成,也可以用URL参数的timestamp
|
||||
* @param $nonce string 随机串,可以自己生成,也可以用URL参数的nonce
|
||||
* @param &$encryptMsg string 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串,
|
||||
* 当return返回0时有效
|
||||
*
|
||||
* @return int 成功0,失败返回对应的错误码
|
||||
*/
|
||||
public function encryptMsg($replyMsg, $timeStamp, $nonce, &$encryptMsg)
|
||||
{
|
||||
$pc = new Prpcrypt($this->config->getEncodingAesKey());
|
||||
|
||||
//加密
|
||||
$array = $pc->encrypt($replyMsg, $this->config->getAppid());
|
||||
$ret = $array[0];
|
||||
if ($ret != 0) {
|
||||
return $ret;
|
||||
}
|
||||
|
||||
if ($timeStamp == null) {
|
||||
$timeStamp = time();
|
||||
}
|
||||
$encrypt = $array[1];
|
||||
|
||||
//生成安全签名
|
||||
$array = $this->getSHA1($this->config->getToken(), $timeStamp, $nonce, $encrypt);
|
||||
$ret = $array[0];
|
||||
if ($ret != 0) {
|
||||
return $ret;
|
||||
}
|
||||
$signature = $array[1];
|
||||
|
||||
//生成发送的xml
|
||||
$xmlparse = new XMLParse;
|
||||
$encryptMsg = $xmlparse->generate($encrypt, $signature, $timeStamp, $nonce);
|
||||
return self::OK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检验消息的真实性,并且获取解密后的明文.
|
||||
* <ol>
|
||||
* <li>利用收到的密文生成安全签名,进行签名验证</li>
|
||||
* <li>若验证通过,则提取xml中的加密消息</li>
|
||||
* <li>对消息进行解密</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param $msgSignature string 签名串,对应URL参数的msg_signature
|
||||
* @param $timestamp string 时间戳 对应URL参数的timestamp
|
||||
* @param $nonce string 随机串,对应URL参数的nonce
|
||||
* @param $postData string 密文,对应POST请求的数据
|
||||
* @param &$msg string 解密后的原文,当return返回0时有效
|
||||
*
|
||||
* @return int 成功0,失败返回对应的错误码
|
||||
*/
|
||||
public function decryptMsg($msgSignature, $timestamp, $nonce, $postData, &$msg)
|
||||
{
|
||||
if (strlen($this->config->getEncodingAesKey()) != 43) {
|
||||
return self::IllegalAesKey;
|
||||
}
|
||||
|
||||
$pc = new Prpcrypt($this->config->getEncodingAesKey());
|
||||
|
||||
//提取密文
|
||||
$xmlparse = new XMLParse();
|
||||
$array = $xmlparse->extract($postData);
|
||||
$ret = $array[0];
|
||||
|
||||
if ($ret != 0) {
|
||||
return $ret;
|
||||
}
|
||||
|
||||
if ($timestamp == null) {
|
||||
$timestamp = time();
|
||||
}
|
||||
|
||||
$encrypt = $array[1];
|
||||
$touser_name = $array[2];
|
||||
|
||||
//验证安全签名
|
||||
$array = $this->getSHA1($this->config->getToken(), $timestamp, $nonce, $encrypt);
|
||||
$ret = $array[0];
|
||||
|
||||
if ($ret != 0) {
|
||||
return $ret;
|
||||
}
|
||||
|
||||
$signature = $array[1];
|
||||
if ($signature != $msgSignature) {
|
||||
return self::ValidateSignatureError;
|
||||
}
|
||||
|
||||
$result = $pc->decrypt($encrypt, $this->config->getAppid());
|
||||
if ($result[0] != 0) {
|
||||
return $result[0];
|
||||
}
|
||||
$msg = $result[1];
|
||||
|
||||
return self::OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $token
|
||||
* @param $timestamp
|
||||
* @param $nonce
|
||||
* @param $encrypt_msg
|
||||
* @return array
|
||||
*/
|
||||
private function getSHA1($token, $timestamp, $nonce, $encrypt_msg)
|
||||
{
|
||||
//排序
|
||||
try {
|
||||
$array = array($encrypt_msg, $token, $timestamp, $nonce);
|
||||
sort($array, SORT_STRING);
|
||||
$str = implode($array);
|
||||
return array(self::OK, sha1($str));
|
||||
} catch (\Exception $e) {
|
||||
//print $e . "\n";
|
||||
return array(self::ComputeSignatureError, null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace officialaccount;
|
||||
|
||||
|
||||
use common\HttpClient;
|
||||
use common\Result;
|
||||
|
||||
class QrCode extends AfficialAccount
|
||||
{
|
||||
|
||||
private $tick_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=';
|
||||
private $temporary_url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode';
|
||||
private $permanent_qr_code_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=';
|
||||
|
||||
/**
|
||||
* @param $scene_id
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function temporaryQrCode($scene_id)
|
||||
{
|
||||
$requestParam['expire_seconds'] = 24 * 3600 * 30;
|
||||
$requestParam['action_name'] = 'QR_SCENE';
|
||||
$requestParam['action_info[scene][scene_id]'] = $scene_id;
|
||||
|
||||
$tick = $this->get_tick($requestParam);
|
||||
|
||||
$client = HttpClient::NewRequest();
|
||||
return $client->get($this->temporary_url, ['ticket' => $tick]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $scene_id
|
||||
* @return array|mixed|\common\Result
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function permanent_qr_code($scene_id)
|
||||
{
|
||||
$requestParam['action_name'] = 'QR_LIMIT_SCENE';
|
||||
$requestParam['action_info[scene][scene_id]'] = $scene_id;
|
||||
|
||||
$tick = $this->get_tick($requestParam);
|
||||
|
||||
$client = HttpClient::NewRequest();
|
||||
return $client->get($this->permanent_qr_code_url, ['ticket' => $tick]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $requestParam
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
* tick
|
||||
*/
|
||||
private function get_tick($requestParam)
|
||||
{
|
||||
$request = HttpClient::NewRequest();
|
||||
$request->setCallback(function ($body) {
|
||||
if (is_array($body)) {
|
||||
$param = ['code' => $body['errcode'], 'message' => $body['errmsg'], 'data' => $body];
|
||||
} else {
|
||||
$param = ['code' => 0, 'data' => json_decode($body, true)];
|
||||
}
|
||||
return new Result($param);
|
||||
});
|
||||
$data = $request->post($this->tick_url . $this->config->getAccessToken(), json_encode($requestParam));
|
||||
if (!$data->isResultsOK()) {
|
||||
throw new \Exception($data->getMessage());
|
||||
}
|
||||
return $data->getData('ticket');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount;
|
||||
|
||||
|
||||
use common\HttpClient;
|
||||
use common\Result;
|
||||
|
||||
class SourceMaterial extends AfficialAccount
|
||||
{
|
||||
|
||||
private $uploadUrl = 'https://api.weixin.qq.com/cgi-bin/media/upload?';
|
||||
private $getUploadUrl = 'https://api.weixin.qq.com/cgi-bin/media/get';
|
||||
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @return bool|mixed|Result
|
||||
*/
|
||||
public function ImageUpload($file)
|
||||
{
|
||||
if (!file_exists($file)) {
|
||||
return $this->sendError('文件不存在.', 404);
|
||||
}
|
||||
return $this->upload($file, 'image');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @return bool|mixed|Result
|
||||
*/
|
||||
public function VoiceUpload($file)
|
||||
{
|
||||
if (!file_exists($file)) {
|
||||
return $this->sendError('文件不存在.', 404);
|
||||
}
|
||||
return $this->upload($file, 'voice');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @return bool|mixed|Result
|
||||
*/
|
||||
public function VideoUpload($file)
|
||||
{
|
||||
if (!file_exists($file)) {
|
||||
return $this->sendError('文件不存在.', 404);
|
||||
}
|
||||
return $this->upload($file, 'video');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @return bool|mixed|Result
|
||||
*/
|
||||
public function ThumbUpload($file)
|
||||
{
|
||||
if (!file_exists($file)) {
|
||||
return $this->sendError('文件不存在.', 404);
|
||||
}
|
||||
return $this->upload($file, 'thumb');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $media_id
|
||||
* @return array|mixed|Result
|
||||
*/
|
||||
public function MediaGet($media_id)
|
||||
{
|
||||
$client = HttpClient::NewRequest();
|
||||
|
||||
$accessToken = $this->config->getAccessToken();
|
||||
|
||||
return $client->get($this->getUploadUrl, ['access_token' => $accessToken, 'media_id' => $media_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @param $type
|
||||
* @return bool|mixed
|
||||
*/
|
||||
private function upload($file, $type)
|
||||
{
|
||||
$uploadInfo['media'] = new \CURLFile($file);
|
||||
$uploadInfo['form-data[filename]'] = $uploadInfo['media']->getFilename();
|
||||
$uploadInfo['form-data[content-type]'] = $uploadInfo['media']->getMimeType();
|
||||
|
||||
$accessToken = $this->config->getAccessToken();
|
||||
|
||||
$url = $this->uploadUrl . 'access_token=' . $accessToken . '&type=' . $type;
|
||||
$result = $this->request->post($url, $uploadInfo);
|
||||
if (!$result->isResultsOK()) {
|
||||
return false;
|
||||
}
|
||||
return $result->getData();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount;
|
||||
|
||||
|
||||
use common\HttpClient;
|
||||
|
||||
class Subscribe extends AfficialAccount
|
||||
{
|
||||
|
||||
private $openid = '';
|
||||
private $template_id = '';
|
||||
private $url = '';
|
||||
private $miniprogram = [];
|
||||
private $scene = '';
|
||||
private $title = '';
|
||||
private $keywords = [];
|
||||
|
||||
private $pushUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=';
|
||||
|
||||
/**
|
||||
* @param string $openid
|
||||
* @return Subscribe
|
||||
*/
|
||||
public function setOpenid(string $openid): Subscribe
|
||||
{
|
||||
$this->openid = $openid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $template_id
|
||||
* @return Subscribe
|
||||
*/
|
||||
public function setTemplateId(string $template_id): Subscribe
|
||||
{
|
||||
$this->template_id = $template_id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @return Subscribe
|
||||
*/
|
||||
public function setUrl(string $url): Subscribe
|
||||
{
|
||||
$this->url = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $appid
|
||||
* @param string $path
|
||||
* @return Subscribe
|
||||
*/
|
||||
public function setMiniprogram(string $appid, string $path): Subscribe
|
||||
{
|
||||
$this->miniprogram['appid'] = $appid;
|
||||
$this->miniprogram['pagepath'] = $path;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $scene
|
||||
* @return Subscribe
|
||||
*/
|
||||
public function setScene(string $scene): Subscribe
|
||||
{
|
||||
$this->scene = $scene;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return Subscribe
|
||||
*/
|
||||
public function setTitle(string $title): Subscribe
|
||||
{
|
||||
$this->title = $title;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $keyword
|
||||
* @param $content
|
||||
* @param null $color
|
||||
*/
|
||||
public function addContent($keyword, $content, $color = null)
|
||||
{
|
||||
$param['value'] = $content;
|
||||
if (!empty($color)) {
|
||||
$param['color'] = $color;
|
||||
}
|
||||
$this->keywords[$keyword] = $param;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|mixed|\common\Result
|
||||
*/
|
||||
public function sendSubscribeMessage()
|
||||
{
|
||||
$requestParam['touser'] = $this->openid;
|
||||
$requestParam['template_id'] = $this->template_id;
|
||||
$requestParam['url'] = $this->url;
|
||||
$requestParam['miniprogram'] = $this->miniprogram;
|
||||
$requestParam['scene'] = $this->scene;
|
||||
$requestParam['title'] = $this->title;
|
||||
$requestParam['data'] = $this->keywords;
|
||||
|
||||
$requestUrl = $this->pushUrl . $this->config->getAccessToken();
|
||||
|
||||
$client = HttpClient::NewRequest();
|
||||
return $client->post($requestUrl, $requestParam);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount;
|
||||
|
||||
|
||||
class Tag extends AfficialAccount
|
||||
{
|
||||
|
||||
private $generate_url = 'https://api.weixin.qq.com/cgi-bin/tags/create?access_token=';
|
||||
|
||||
/**
|
||||
* @param $tagName
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function generate($tagName)
|
||||
{
|
||||
$url['tag[name]'] = $tagName;
|
||||
|
||||
$config = $this->config->getAccessToken();
|
||||
|
||||
$result = $this->request->post($this->generate_url . $config, $url);
|
||||
if (!$result->isResultsOK()) {
|
||||
throw new \Exception($result->getMessage());
|
||||
}
|
||||
return $result->getData();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount;
|
||||
|
||||
|
||||
use common\Config;
|
||||
|
||||
class WxSDK
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
* @return mixed
|
||||
*/
|
||||
public static function authorization(Config $config)
|
||||
{
|
||||
return Authorization::getInstance($config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
* @return mixed
|
||||
*/
|
||||
public static function qrCode(Config $config)
|
||||
{
|
||||
return QrCode::getInstance($config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
* @return mixed
|
||||
*/
|
||||
public static function news(Config $config)
|
||||
{
|
||||
return NewsManager::getInstance($config);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,360 @@
|
||||
<?php
|
||||
|
||||
namespace officialaccount\dcaler;
|
||||
|
||||
|
||||
class Authorization
|
||||
{
|
||||
private $subscribe;
|
||||
private $openid;
|
||||
private $nickname;
|
||||
private $sex;
|
||||
private $language;
|
||||
private $city;
|
||||
private $province;
|
||||
private $country;
|
||||
private $headimgurl;
|
||||
private $subscribe_time;
|
||||
private $unionid;
|
||||
private $remark;
|
||||
private $groupid;
|
||||
private $tagid_list;
|
||||
private $subscribe_scene;
|
||||
private $qr_scene;
|
||||
private $qr_scene_str;
|
||||
|
||||
/**
|
||||
* @param $object
|
||||
* @return Authorization
|
||||
*/
|
||||
public static function instance($object)
|
||||
{
|
||||
$class = new Authorization();
|
||||
$class->setSubscribe($object['subscribe']);
|
||||
$class->setOpenid($object['openid']);
|
||||
$class->setNickname($object['nickname']);
|
||||
$class->setSex($object['sex']);
|
||||
$class->setLanguage($object['language']);
|
||||
$class->setCity($object['city']);
|
||||
$class->setProvince($object['province']);
|
||||
$class->setCountry($object['country']);
|
||||
$class->setHeadimgurl($object['headimgurl']);
|
||||
$class->setSubscribeTime($object['subscribe_time']);
|
||||
$class->setUnionid($object['unionid']);
|
||||
$class->setRemark($object['remark']);
|
||||
$class->setGroupid($object['groupid']);
|
||||
$class->setTagidList($object['tagid_list']);
|
||||
$class->setSubscribeScene($object['subscribe_scene']);
|
||||
$class->setQrScene($object['qr_scene']);
|
||||
$class->setQrSceneStr($object['qr_scene_str']);
|
||||
return $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSubscribe()
|
||||
{
|
||||
return $this->subscribe;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $subscribe
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setSubscribe($subscribe)
|
||||
{
|
||||
$this->subscribe = $subscribe;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOpenid()
|
||||
{
|
||||
return $this->openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $openid
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setOpenid($openid)
|
||||
{
|
||||
$this->openid = $openid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getNickname()
|
||||
{
|
||||
return $this->nickname;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $nickname
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setNickname($nickname)
|
||||
{
|
||||
$this->nickname = $nickname;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSex()
|
||||
{
|
||||
return $this->sex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $sex
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setSex($sex)
|
||||
{
|
||||
$this->sex = $sex;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLanguage()
|
||||
{
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $language
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setLanguage($language)
|
||||
{
|
||||
$this->language = $language;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCity()
|
||||
{
|
||||
return $this->city;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $city
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setCity($city)
|
||||
{
|
||||
$this->city = $city;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getProvince()
|
||||
{
|
||||
return $this->province;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $province
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setProvince($province)
|
||||
{
|
||||
$this->province = $province;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCountry()
|
||||
{
|
||||
return $this->country;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $country
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setCountry($country)
|
||||
{
|
||||
$this->country = $country;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getHeadimgurl()
|
||||
{
|
||||
return $this->headimgurl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $headimgurl
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setHeadimgurl($headimgurl)
|
||||
{
|
||||
$this->headimgurl = $headimgurl;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSubscribeTime()
|
||||
{
|
||||
return $this->subscribe_time;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $subscribe_time
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setSubscribeTime($subscribe_time)
|
||||
{
|
||||
$this->subscribe_time = $subscribe_time;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getUnionid()
|
||||
{
|
||||
return $this->unionid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $unionid
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setUnionid($unionid)
|
||||
{
|
||||
$this->unionid = $unionid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRemark()
|
||||
{
|
||||
return $this->remark;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $remark
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setRemark($remark)
|
||||
{
|
||||
$this->remark = $remark;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getGroupid()
|
||||
{
|
||||
return $this->groupid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $groupid
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setGroupid($groupid)
|
||||
{
|
||||
$this->groupid = $groupid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTagidList()
|
||||
{
|
||||
return $this->tagid_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $tagid_list
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setTagidList($tagid_list)
|
||||
{
|
||||
$this->tagid_list = $tagid_list;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSubscribeScene()
|
||||
{
|
||||
return $this->subscribe_scene;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $subscribe_scene
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setSubscribeScene($subscribe_scene)
|
||||
{
|
||||
$this->subscribe_scene = $subscribe_scene;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getQrScene()
|
||||
{
|
||||
return $this->qr_scene;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $qr_scene
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setQrScene($qr_scene)
|
||||
{
|
||||
$this->qr_scene = $qr_scene;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getQrSceneStr()
|
||||
{
|
||||
return $this->qr_scene_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $qr_scene_str
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setQrSceneStr($qr_scene_str)
|
||||
{
|
||||
$this->qr_scene_str = $qr_scene_str;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount\dcaler;
|
||||
|
||||
|
||||
class PKCS7Encoder
|
||||
{
|
||||
public static $block_size = 32;
|
||||
|
||||
/**
|
||||
* @param $text
|
||||
* @return string
|
||||
*/
|
||||
function encode($text)
|
||||
{
|
||||
$block_size = PKCS7Encoder::$block_size;
|
||||
$text_length = strlen($text);
|
||||
//计算需要填充的位数
|
||||
$amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
|
||||
if ($amount_to_pad == 0) {
|
||||
$amount_to_pad = PKCS7Encoder::$block_size;
|
||||
}
|
||||
//获得补位所用的字符
|
||||
$pad_chr = chr($amount_to_pad);
|
||||
$tmp = "";
|
||||
for ($index = 0; $index < $amount_to_pad; $index++) {
|
||||
$tmp .= $pad_chr;
|
||||
}
|
||||
return $text . $tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $text
|
||||
* @return false|string
|
||||
*/
|
||||
function decode($text)
|
||||
{
|
||||
|
||||
$pad = ord(substr($text, -1));
|
||||
if ($pad < 1 || $pad > 32) {
|
||||
$pad = 0;
|
||||
}
|
||||
return substr($text, 0, (strlen($text) - $pad));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount\dcaler;
|
||||
|
||||
|
||||
use officialaccount\NewsManager;
|
||||
|
||||
class Prpcrypt
|
||||
{
|
||||
public $key;
|
||||
|
||||
/**
|
||||
* Prpcrypt constructor.
|
||||
* @param $k
|
||||
*/
|
||||
public function __construct($k)
|
||||
{
|
||||
$this->key = base64_decode($k . "=");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $text
|
||||
* @param $appid
|
||||
* @return array
|
||||
*/
|
||||
public function encrypt($text, $appid)
|
||||
{
|
||||
|
||||
try {
|
||||
//获得16位随机字符串,填充到明文之前
|
||||
$random = $this->getRandomStr();
|
||||
$text = $random . pack("N", strlen($text)) . $text . $appid;
|
||||
// 网络字节序
|
||||
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
|
||||
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
||||
$iv = substr($this->key, 0, 16);
|
||||
//使用自定义的填充方式对明文进行补位填充
|
||||
$pkc_encoder = new PKCS7Encoder;
|
||||
$text = $pkc_encoder->encode($text);
|
||||
mcrypt_generic_init($module, $this->key, $iv);
|
||||
//加密
|
||||
$encrypted = mcrypt_generic($module, $text);
|
||||
mcrypt_generic_deinit($module);
|
||||
mcrypt_module_close($module);
|
||||
|
||||
//print(base64_encode($encrypted));
|
||||
//使用BASE64对加密后的字符串进行编码
|
||||
return array(NewsManager::OK, base64_encode($encrypted));
|
||||
} catch (\Exception $e) {
|
||||
//print $e;
|
||||
return array(NewsManager::EncryptAESError, null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $encrypted
|
||||
* @param $appid
|
||||
* @return array|string
|
||||
*/
|
||||
public function decrypt($encrypted, $appid)
|
||||
{
|
||||
|
||||
try {
|
||||
//使用BASE64对需要解密的字符串进行解码
|
||||
$ciphertext_dec = base64_decode($encrypted);
|
||||
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
||||
$iv = substr($this->key, 0, 16);
|
||||
mcrypt_generic_init($module, $this->key, $iv);
|
||||
|
||||
//解密
|
||||
$decrypted = mdecrypt_generic($module, $ciphertext_dec);
|
||||
mcrypt_generic_deinit($module);
|
||||
mcrypt_module_close($module);
|
||||
} catch (\Exception $e) {
|
||||
return array(NewsManager::DecryptAESError, null);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
//去除补位字符
|
||||
$pkc_encoder = new PKCS7Encoder;
|
||||
$result = $pkc_encoder->decode($decrypted);
|
||||
//去除16位随机字符串,网络字节序和AppId
|
||||
if (strlen($result) < 16)
|
||||
return "";
|
||||
$content = substr($result, 16, strlen($result));
|
||||
$len_list = unpack("N", substr($content, 0, 4));
|
||||
$xml_len = $len_list[1];
|
||||
$xml_content = substr($content, 4, $xml_len);
|
||||
$from_appid = substr($content, $xml_len + 4);
|
||||
} catch (\Exception $e) {
|
||||
//print $e;
|
||||
return array(NewsManager::IllegalBuffer, null);
|
||||
}
|
||||
if ($from_appid != $appid)
|
||||
return array(NewsManager::ValidateAppidError, null);
|
||||
return array(0, $xml_content);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 随机生成16位字符串
|
||||
* @return string 生成的字符串
|
||||
*/
|
||||
function getRandomStr()
|
||||
{
|
||||
|
||||
$str = "";
|
||||
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
$max = strlen($str_pol) - 1;
|
||||
for ($i = 0; $i < 16; $i++) {
|
||||
$str .= $str_pol[mt_rand(0, $max)];
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount\dcaler;
|
||||
|
||||
|
||||
class SnsInfo
|
||||
{
|
||||
|
||||
private $openid;
|
||||
private $nickname;
|
||||
private $sex;
|
||||
private $province;
|
||||
private $city;
|
||||
private $country;
|
||||
private $headimgurl;
|
||||
private $privilege;
|
||||
private $unionid;
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOpenid()
|
||||
{
|
||||
return $this->openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $openid
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setOpenid($openid)
|
||||
{
|
||||
$this->openid = $openid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getNickname()
|
||||
{
|
||||
return $this->nickname;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $nickname
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setNickname($nickname)
|
||||
{
|
||||
$this->nickname = $nickname;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSex()
|
||||
{
|
||||
return $this->sex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $sex
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setSex($sex)
|
||||
{
|
||||
$this->sex = $sex;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getProvince()
|
||||
{
|
||||
return $this->province;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $province
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setProvince($province)
|
||||
{
|
||||
$this->province = $province;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCity()
|
||||
{
|
||||
return $this->city;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $city
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setCity($city)
|
||||
{
|
||||
$this->city = $city;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCountry()
|
||||
{
|
||||
return $this->country;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $country
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setCountry($country)
|
||||
{
|
||||
$this->country = $country;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getHeadimgurl()
|
||||
{
|
||||
return $this->headimgurl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $headimgurl
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setHeadimgurl($headimgurl)
|
||||
{
|
||||
$this->headimgurl = $headimgurl;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPrivilege()
|
||||
{
|
||||
return $this->privilege;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $privilege
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setPrivilege($privilege)
|
||||
{
|
||||
$this->privilege = $privilege;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getUnionid()
|
||||
{
|
||||
return $this->unionid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $unionid
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setUnionid($unionid)
|
||||
{
|
||||
$this->unionid = $unionid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param $object
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public static function instance($object)
|
||||
{
|
||||
$class = new SnsInfo();
|
||||
$class->setOpenid($object['openid']);
|
||||
$class->setNickname($object['nickname']);
|
||||
$class->setSex($object['sex']);
|
||||
$class->setCity($object['city']);
|
||||
$class->setProvince($object['province']);
|
||||
$class->setCountry($object['country']);
|
||||
$class->setHeadimgurl($object['headimgurl']);
|
||||
$class->setUnionid($object['unionid']);
|
||||
$class->setPrivilege($object['privilege']);
|
||||
return $class;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount\dcaler;
|
||||
|
||||
|
||||
use officialaccount\NewsManager;
|
||||
|
||||
class XMLParse
|
||||
{
|
||||
/**
|
||||
* 提取出xml数据包中的加密消息
|
||||
* @param string $xmltext 待提取的xml字符串
|
||||
* @return array 提取出的加密消息字符串
|
||||
*/
|
||||
public function extract($xmltext)
|
||||
{
|
||||
try {
|
||||
$xml = new \DOMDocument();
|
||||
$xml->loadXML($xmltext);
|
||||
$array_e = $xml->getElementsByTagName('Encrypt');
|
||||
$array_a = $xml->getElementsByTagName('ToUserName');
|
||||
$encrypt = $array_e->item(0)->nodeValue;
|
||||
$tousername = $array_a->item(0)->nodeValue;
|
||||
return [0, $encrypt, $tousername];
|
||||
} catch (\Exception $e) {
|
||||
return [NewsManager::ParseXmlError, null, null];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $encrypt
|
||||
* @param $signature
|
||||
* @param $timestamp
|
||||
* @param $nonce
|
||||
* @return string
|
||||
*/
|
||||
public function generate($encrypt, $signature, $timestamp, $nonce)
|
||||
{
|
||||
$format = "<xml>
|
||||
<Encrypt><![CDATA[%s]]></Encrypt>
|
||||
<MsgSignature><![CDATA[%s]]></MsgSignature>
|
||||
<TimeStamp>%s</TimeStamp>
|
||||
<Nonce><![CDATA[%s]]></Nonce>
|
||||
</xml>";
|
||||
return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user