新增模板消息部分操作

This commit is contained in:
TOP糯米 2019-07-19 11:04:42 +08:00
parent 441645115c
commit dfe4863dd4
1 changed files with 198 additions and 56 deletions

View File

@ -6,10 +6,10 @@ namespace top\extend\wechat;
* 微信API
* Class WeChatAPI
* @package top\extend\wechat
* @author TOP糯米
*/
class WeChatAPI
{
/**
* @var null|string 当前页面的URL
*/
@ -21,12 +21,17 @@ class WeChatAPI
private $error = null;
/**
* @var string 获取ACCESS_TOKEN的接口
* @var array 微信配置
*/
private $config = [];
/**
* @var string 获取access_token的接口
*/
private $accessTokenAPI = 'https://api.weixin.qq.com/cgi-bin/token?';
/**
* @var string 获取OAuth ACCESS_TOKEN的接口
* @var string 获取OAuth access_token的接口
*/
private $oauthAccessTokenAPI = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
@ -48,22 +53,34 @@ class WeChatAPI
/**
* @var string 自定义菜单创建接口
*/
private $menuAPI = 'https://api.weixin.qq.com/cgi-bin/menu/create';
private $menuAPI = 'https://api.weixin.qq.com/cgi-bin/menu';
/**
* @var array 微信配置
* @var string 获取自定义菜单配置接口
*/
private $config = [];
private $currentSelfmenuAPI = 'https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?';
public function __construct($config = [])
/**
* @var string 模版消息接口
*/
private $templateAPI = 'https://api.weixin.qq.com/cgi-bin/template/';
/**
* @var string 语言
*/
private $lang = null;
public function __construct($config = [], $lang = null)
{
$this->url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$this->config = $config;
$this->lang = $lang ? $lang : 'zh_CN';
}
/**
* 获取ACCESS_TOKEN
* @return bool|mixed
* 获取access_token
* @return mixed
* @throws WeChatAPIException
*/
private function getAccessToken()
{
@ -76,16 +93,14 @@ class WeChatAPI
@unlink($file);
return $this->getAccessToken();
}
return $result;
} else {
$api = $this->accessTokenAPI . "grant_type=client_credential&appid={$this->config['appid']}&secret={$this->config['appsecret']}";
$json = create_http_request($api);
$result = json_decode($json, true);
if (isset($result['errcode']) && $result['errcode'] != 0) {
throw new WeChatAPIException('code:' . $result['errcode'] . ',' . $result['errmsg']);
}
file_put_contents($file, $json);
$api = $this->accessTokenAPI . "grant_type=client_credential&appid={$this->config['appid']}";
$api .= "&secret={$this->config['appsecret']}";
$result = $this->createHttpRequest($api, null, true);
file_put_contents($file, $result[0]);
return $result[1];
}
return $result;
}
/**
@ -95,70 +110,70 @@ class WeChatAPI
private function getCode($scope)
{
$redirect = $this->url;
$api = $this->codeAPI . "appid={$this->config['appid']}&redirect_uri={$redirect}&response_type=code&scope={$scope}&state=0#wechat_redirect";
header('location:' . $api);
$api = $this->codeAPI . "appid={$this->config['appid']}&redirect_uri={$redirect}";
$api .= "&response_type=code&scope={$scope}&state=0#wechat_redirect";
exit(header('location:' . $api));
}
/**
* 获取OAuth的ACCESS_TOKEN
* 获取OAuth的access_token
* @param string $scope
* @return bool|mixed|void
* @return mixed
* @throws WeChatAPIException
*/
private function getOAuthAccessToken($scope = 'snsapi_base')
{
$file = './oauth_access_token.json';
if (file_exists($file)) {
$content = file_get_contents($file);
if (isset($_SESSION['oauth_access_token']) && !empty($_SESSION['oauth_access_token'])) {
$session = $_SESSION['oauth_access_token'];
$content = $session['content'];
$result = json_decode($content, true);
$expires = $result['expires_in'] - (time() - filemtime($file));
$expires = $result['expires_in'] - (time() - $session['time']);
if ($expires <= 5) {
@unlink($file);
unset($_SESSION['oauth_access_token']);
return $this->getOAuthAccessToken();
}
return $result;
} else {
$code = isset($_GET['code']) ? $_GET['code'] : null;
if (!$code) {
return $this->getCode($scope);
$this->getCode($scope);
}
$api = $this->oauthAccessTokenAPI . "appid={$this->config['appid']}&secret={$this->config['appsecret']}&code={$code}&grant_type=authorization_code";
$json = create_http_request($api);
$result = json_decode($json, true);
if (isset($result['errcode']) && $result['errcode'] != 0) {
throw new WeChatAPIException('code:' . $result['errcode'] . ',' . $result['errmsg']);
}
file_put_contents($file, $json);
$api = $this->oauthAccessTokenAPI . "appid={$this->config['appid']}";
$api .= "&secret={$this->config['appsecret']}&code={$code}&grant_type=authorization_code";
$result = $this->createHttpRequest($api, null, true);
$_SESSION['oauth_access_token'] = [
'time' => time(),
'content' => $result[0]
];
return $result[1];
}
return $result;
}
/**
* 拉取用户信息
* @param string $openid
* @param null $openid
* @return bool|mixed
* @throws WeChatAPIException
*/
public function getUserInfo($openid = null)
{
$postData = [];
if ($openid) {
$accessToken = $this->getAccessToken();
$api = $this->userinfoUnionIdAPI . "access_token={$accessToken['access_token']}";
if (is_array($openid)) {
$postData = [
$postData = json_encode([
'user_list' => $openid
];
$api = $this->userinfoUnionIdAPI . "access_token={$accessToken['access_token']}";
]);
} else {
$api = $this->userinfoUnionIdAPI . "access_token={$accessToken['access_token']}&openid={$openid}&lang=zh_CN";
$api .= "&openid={$openid}&lang={$this->lang}";
}
} else {
$accessToken = $this->getOAuthAccessToken('snsapi_userinfo');
$api = $this->userinfoAPI . "access_token={$accessToken['access_token']}&openid={$accessToken['openid']}&lang=zh_CN";
}
$json = create_http_request($api, json_encode($postData));
$result = json_decode($json, true);
if (isset($result['errcode']) && $result['errcode'] != 0) {
$this->error = 'code:' . $result['errcode'] . ',' . $result['errmsg'];
return false;
$api = $this->userinfoAPI . "access_token={$accessToken['access_token']}";
$api .= "&openid={$accessToken['openid']}&lang={$this->lang}";
}
$result = $this->createHttpRequest($api, $postData);
return $result;
}
@ -188,22 +203,149 @@ class WeChatAPI
* ]
* ]
* @param array $menu
* @param array $matchrule
* @return bool
* @throws WeChatAPIException
*/
public function createMenu($menu = [])
public function createMenu($menu = [], $matchrule = [])
{
// 公众号菜单
$menu = [
'button' => $menu
];
// 个性化菜单
$matchrule = (!empty($matchrule)) ? [
'matchrule' => $matchrule
] : [];
$menu = array_merge($menu, $matchrule);
$menuJson = json_encode($menu, JSON_UNESCAPED_UNICODE);
$type = (!empty($matchrule)) ? 'addconditional' : 'create';
return $this->menuAction($type, $menuJson);
}
/**
* 获取公众号菜单
* @return mixed
* @throws WeChatAPIException
*/
public function getMenu()
{
return $this->menuAction('get');
}
/**
* 删除公众号自定义菜单
* @return mixed
* @throws WeChatAPIException
*/
public function deleteMenu($menuid = null)
{
$json = null;
if ($menuid) {
$json = json_encode([
'menuid' => $menuid
]);
}
$type = ($menuid) ? 'delconditional' : 'delete';
return $this->menuAction($type, $json, $menuid);
}
/**
* 公众号菜单的基础操作
* @param null $type
* @param null $postData
* @return bool|mixed
* @throws WeChatAPIException
*/
private function menuAction($type = null, $postData = null, $menuid = null)
{
$typePool = ['create', 'get', 'delete', 'addconditional', 'delconditional'];
if (in_array($type, $typePool)) {
$accessToken = $this->getAccessToken();
$api = $this->menuAPI . "/{$type}?access_token={$accessToken['access_token']}";
$result = $this->createHttpRequest($api, $postData);
return ($type == 'get' || $type == 'addconditional') ? $result : true;
} else {
throw new WeChatAPIException('对公众号菜单的操作不在允许列表');
}
}
/**
* 获取自定义菜单配置
* @return mixed
* @throws WeChatAPIException
*/
public function getCurrentSelfmenuInfo()
{
$accessToken = $this->getAccessToken();
$api = $this->menuAPI . "?access_token={$accessToken['access_token']}";
$menu = json_encode([
'button' => $menu
], JSON_UNESCAPED_UNICODE);
$json = create_http_request($api, $menu);
$api = $this->currentSelfmenuAPI . "access_token={$accessToken['access_token']}";
$result = $this->createHttpRequest($api);
return $result;
}
/**
* 设置所属行业
* post数据示例
* {
* "industry_id1":"1",
* "industry_id2":"4"
* }
* @param $id1
* @param $id2
* @return bool
* @throws WeChatAPIException
*/
public function setIndustry($id1, $id2)
{
$accessToken = $this->getAccessToken();
$api = $this->templateAPI . "api_set_industry?access_token={$accessToken['access_token']}";
$postData = json_encode([
'industry_id1' => $id1,
'industry_id2' => $id2
]);
$this->createHttpRequest($api, $postData);
return true;
}
/**
* 获取设置的行业信息
* @return mixed
*/
public function getIndustry()
{
$accessToken = $this->getAccessToken();
$api = $this->templateAPI . "get_industry?access_token={$accessToken['access_token']}";
$result = $this->createHttpRequest($api);
return $result;
}
/**
* 获取模板列表
* @return mixed
*/
public function getTemplateList()
{
$accessToken = $this->getAccessToken();
$api = $this->templateAPI . "get_all_private_template?access_token={$accessToken['access_token']}";
$result = $this->createHttpRequest($api);
return $result;
}
/**
* 创建HTTP请求
* @param $api
* @param array $data
* @param bool $includeJson
* @return mixed
*/
private function createHttpRequest($api, $data = [], $includeJson = false)
{
$json = create_http_request($api, $data);
$result = json_decode($json, true);
if (isset($result['errcode']) && $result['errcode'] != 0) {
$this->error = 'code:' . $result['errcode'] . ',' . $result['errmsg'];
return false;
throw new WeChatAPIException("code:{$result['errcode']}, {$result['errmsg']}");
}
return true;
return ($includeJson) ? [$json, $result] : $result;
}
/**