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

174 lines
3.8 KiB
PHP
Raw Permalink Normal View History

<?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()
{
2018-02-07 08:19:31 +00:00
$this->source = new Source('poles');
}
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);
$userinfo = json_decode($cache_data['userInfo'],true);
if(!is_array($cache_data) || !isset($cache_data['expires_in']) || !isset($userinfo['cstnetId']))
{
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();
}
}