add clear
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user