westdc-zf1/application/module/Open/Client.php

172 lines
3.6 KiB
PHP

<?php
namespace Open;
use \Helpers\View as view;
use \Helpers\Curl;
use \Open\Source;
use \Open\Listener\ClientListener as Listener;
class Client extends \Zend_Controller_Plugin_Abstract
{
public $db;
public $config;
public $table;
private $source = NULL;
private $target;
public $events;
public function __construct($target = NULL){
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
$this->table = new \Helpers\Table();
$this->target = $target;
$Listener = new Listener();
@$this->events()->attachAggregate($Listener);
}
public function events(\Zend_EventManager_EventCollection $events = NULL)
{
if ($events !== NULL) {
$this->events = $events;
} elseif ($this->events === NULL) {
$this->events = new \Zend_EventManager_EventManager(__CLASS__);
}
return $this->events;
}
public function initSource()
{
$this->source = new Source('heihedata');
}
public function getSource()
{
return $this->source;
}
//创建获取Code的URL
public function makeRequestCodeUrl($target = NULL)
{
if(empty($this->source))
{
$this->initSource();
}
if(empty($target))
{
$target = $this->target;
}
$main_target = $this->source->getTarget($target);
if($main_target === false)
{
return "此登录接口可能不存在";
}
$source = $this->source->getSource($target);
if($source === false)
{
return "此登录接口暂不可用";
}
$main_url = $main_target['code'];
$param = array(
$source->param['id'] => $source->config['id'],
$source->param['secret'] => $source->config['secret'],
$source->param['callback'] => $source->config['callback'],
$source->param['code_response'] => $main_target['code_response']
);
if(isset($source->config['other']))
{
$param = array_merge($param,$source->config['other']);
}
$url = $main_url."?".http_build_query($param);
return $url;
}
//获得token
public function requestToken($code,$target = NULL)
{
if(empty($target))
{
$target = $this->target;
}
if(empty($code))
{
return "未获得您的授权码";
}
$main_target = $this->source->getTarget($target);
$client = $this->source->getSource($target);
if($client === false)
{
return "请求发生错误,登录接口不存在或者可能不再适用";
}
$main_url = $main_target['token'];
$param = array(
$client->param['id'] => $client->config['id'],
$client->param['secret'] => $client->config['secret'],
$client->param['grant_type'] => $main_target['grant_type'],
$client->param['callback'] => $client->config['callback'],
$client->param['code'] => $code
);
$curl = new Curl();
$curl->port = 443;
$curl->initOptions(array('verifyssl'=>false));
$data = $curl->request($main_url,$param,"POST");
$cache_data = json_decode($data['response'],true);
if(!isset($cache_data['expires_in']))
{
return "未获得授权信息,请重试链接";
}
return $cache_data;
}
//储存token信息
public function storageTokenData($type,$token)
{
if(empty($type))
{
return "接口类型错误";
}
if(empty($token))
{
return "登录信息有误,请重新登录";
}
$param = compact("type","token");
$results = $this->events()->trigger('tokenStorage', $this, $param);
return $cache_data = $results->bottom();
}
}