<?php
/**
* 项目api接口类文件
*/
class ApiModel extends Cola_Model
{
/*
* 表名
* @var Cola_Model|null
*/
protected $_table = '';
/**
* 是否开启文件缓存[全局]
*
* @var bool
*/
protected $_fileCache = false;
/**
* baseUrl
*
* @var string
*/
protected $baseUrl = '';
//保留
protected $appKey = '';
protected $loginAppId = '';
protected $accessToken = '';
/**
* 初始化
*
* @param string $baseUrl 请求接口地址
*/
public function __construct($baseUrl = '')
{
if (!empty($baseUrl)) {
$this->baseUrl = $baseUrl;
} else {
$this->baseUrl = Cola::config('_bigDataApiURL');
}
$u2Config = Cola::config('_OAuthU2Config');//从配置文件中获取验证信息
$this->loginAppId = $u2Config['AppId'];//获取登录用户的信息
$this->accessToken = 'ef4e3a2b-ae21-4cad-ada8-86ec46a8a83g';//获取登录用户信息
}
/**
* 设置url
*
* @param string $url url地址
*/
public function setBaseUrl($url)
{
$this->baseUrl = $url;
}
/**
* 设置AccessToken
*
* @param void
*/
public function setAccessToken($token)
{
$this->accessToken = $token;
}
/**
* 开启缓存
*
* @param bool $bool
*/
public function openCache($bool = true)
{
$this->_fileCache = $bool;
}
/**
* 获取数据
*
* @param $method 方法名
* @param array $params 参数
* @param int $isPost 0 GET,1 POST请求
* @param bool $cache 缓存
*
* @return mixed
*/
public function getData($method, $params = null, $isPost = 0, $cache = false)
{
if (!is_null($isPost)) {
$params['accessToken'] = $this->accessToken;
$params['appId'] = $this->loginAppId;
}
$url = $this->_getUrl($method, $params, $isPost);
if (true == $cache || true == $this->_fileCache) {
return $this->_getCache($url, $params, $isPost);
}
return $this->_curlRequest($url, $params, $isPost);
}
/**
* 删除缓存
*
* @param $method 方法名
* @param null $params 参数
* @param int $isPost 0 GET,1 POST请求
*
* @return bool
*/
public function deleteCache($method, $params = null, $isPost = 0)
{
$url = $this->_getUrl($method, $params, $isPost);
$key = $this->_cacheKey($url, $params, $isPost);
$file = Cola_Com_Cache::factory(Cola::config('_fileCache'));
return $file->delete($key);
}
/**
* 获取缓存key文件名
*
* @param $method
* @param null $params
* @param int $isPost
*
* @return mixed
*/
public function getCacheFile($method, $params = null, $isPost = 0)
{
$url = $this->_getUrl($method, $params, $isPost);
$file = Cola_Com_Cache::factory(Cola::config('_fileCache'));
$key = $this->_cacheKey($url, $params, $isPost);
return $file->getFile($key);
}
/**
* 请求接口URL
*
* @param $method
* @param null $params
* @param int $isPost
*
* @return string
*/
public function getUrl($method, $params = null, $isPost = 0)
{
if (!is_null($isPost)) {
$params['accessToken'] = $this->accessToken;
$params['appId'] = $this->loginAppId;
}
return $this->_getUrl($method, $params, $isPost);
}
/**
* 获取拼接后的请求地址【缓存的key】
*
* @param $method 方法名
* @param $params 参数
* @param $isPost 0 GET,1 POST请求
*
* @return string
*/
private function _getUrl($method, $params, $isPost)
{
$url = $this->baseUrl . $method;
if ($isPost == 0) {
if (is_array($params) && !empty($params)) {
strpos($url, '?') === false ? $tmpParams = "?" : $tmpParams = "&";
foreach ($params as $key => $value) {
$tmpParams .= $key . "=" . urlencode($value) . "&";
}
$url .= rtrim($tmpParams, '&');
}
}
return $url;
}
/**
* 获取缓存数据
*
* @param string $url url
* @param array $params 参数
* @param int $isPost 0 GET,1 POST请求
*
* @return mixed
*/
private function _getCache($url, $params, $isPost)
{
$file = Cola_Com_Cache::factory(Cola::config('_fileCache'));
//缓存的key
$sKey = $this->_cacheKey($url, $params, $isPost);
$res = $file->get($sKey);
if (!$res) {
$res = $this->_curlRequest($url, $params, $isPost);
if (false !== $res) {
$file->set($sKey, $res);
}
}
return $res;
}
/**
* 设置缓存key
*
* @param $url
* @param $params
* @param $isPost
*
* @return string
*/
private function _cacheKey($url, $params, $isPost)
{
return $url . @json_encode($params) . $isPost;
}
/**
* json转换为数组
*
* @param string $string
*
* @return mixed
*/
private function _json2array($string)
{
if (@$json = @json_decode($string, true)) {
return $json;
} else {
return $string;
}
}
/**
* curl 请求
*
* @param string $url 请求地址
* @param array $params 参数【!!仅当POST时有用】
* @param int $isPost 0 GET,1 POST请求
*
* @return mixed
*/
private function _curlRequest($url, $params = array(), $isPost = 0)
{
header("Content-type: text/html; charset=utf-8");
set_time_limit(0);
// echo $url ; echo "<br>";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($curl , CURLOPT_TIMEOUT , 0 );
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36SE 2.X MetaSr 1.0');
if ($isPost == 1) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);//所需传的数组用http_bulid_query()函数处理一下,就ok了
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$result = curl_exec($curl);
$result = $this->_json2array($result);
return $this->_checkRequestStatus($result, $url, $params);
}
/**
* 检查请求状态
*
* 参考地址:http://scmgit.staff.xdf.cn/bigdata/mdm/wikis/error_code
*
* @param $result
* @param $url
* @param $params
*
* @return bool
*/
private function _checkRequestStatus($result, $url, $params)
{
//错误验证
if (empty($result) || in_array(@$result['status'], array(400, 404, 500))) {
//写入日志
$logFile = WEB_ROOT . '../app/' . 'log/api' . DS . date('Y') . DS . date('m') . DS . date('d') . '-error.log';
$log = Cola_Com::log(array('adapter' => 'File', 'params' => array('file' => $logFile, 'mode' => 0755)));
$log->log("请求地址:" . $url . " 参数:" . json_encode($params) . " 错误信息:" . json_encode($result));
return false;
}
else{
//写入日志
$arr = explode('/', $url);
if(in_array('rule', $arr)){
$logFile = WEB_ROOT . '../app/' . 'log/api' . DS . date('Y') . DS . date('m') . DS . date('d') . '-su.log';
$log = Cola_Com::log(array('adapter' => 'File', 'params' => array('file' => $logFile, 'mode' => 0755)));
$log->log("请求地址:" . $url . " 参数:" . json_encode($params) . " 错误信息:" . json_encode($result));
}
}
return $result;
}
}
로그인 후 복사