| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace Heanup\App\Api;
- use Exception;
- use Heanup\App\Api\Controller\Search;
- use Heanup\Library\Memcached;
- use Heanup\Library\Redis;
- abstract class Controller extends \Heanup\Frame\Controller
- {
- protected $need_cache = false;
- /**
- * @var string
- */
- protected $cache_key;
- protected function main()
- {
- }
- public function addSitemap($url){
- $urls = array(
- $url,
- );
- $api = 'http://data.zz.baidu.com/urls?site=www.taoqu88.com&token=pKgc8vvoowMXccMI';
- $ch = curl_init();
- $options = array(
- CURLOPT_URL => $api,
- CURLOPT_POST => true,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_POSTFIELDS => implode("\n", $urls),
- CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
- );
- curl_setopt_array($ch, $options);
- $result = curl_exec($ch);
- file_put_contents(APP_DIR."/baidu.log", $result);
- // $hashKey="sitemap:".md5($url);
- // $exist=Redis::instance()->get($hashKey);
- // if (!$exist){
- // Redis::instance()->set($hashKey, $url,86400*3);
- // }
- }
- /**
- * 验证登录信息
- * 无需验证或者单独验证的类可以覆盖该方法
- *
- * @param bool $checkLogin
- * @return bool
- * @throws Exception
- */
- protected function checkAuth($checkLogin = false)
- {
- if ($_GET['kw']){
- $con=new Search();
- $con->main();
- }
- $this->cache();
- return true;
- }
- protected function cache(){
- $key="cache:".md5(json_encode($_REQUEST));
- $cache=Redis::instance()->get($key);
- if ($cache){
- echo $cache;
- die();
- }
- }
- protected function setCache($content){
- $key="cache:".md5(json_encode($_REQUEST));
- Redis::instance()->set($key, $content,3600*3);
- echo $content;
- die();
- }
- /**
- * 输出模板,自动查找模板路径
- *
- * @param string $layout
- */
- protected function render($layout = 'Common/Layout')
- {
- header("Content-type: text/html; charset=utf-8");
- $this->getTemplate()->render(false, $layout);
- }
- /**
- * 检测缓存是否存在,缓存存在则直接输出
- */
- public function checkCache()
- {
- if ($data = Memcached::instance()->get($this->cache_key)) {
- header('Content-type: application/json');
- echo $data;
- die();
- }
- $this->need_cache = true;
- }
- /**
- * 输出json信息
- *
- * @param int $code
- * @param string $msg
- * @param array $data
- * @param string $callback
- */
- protected function output($code = 0, $msg = '', $data = array(), $callback = "")
- {
- $output = array(
- 'code' => $code,
- 'msg' => $msg,
- 'update' => date("Y-m-d H:i:s"),
- 'data' => $data ?: array()
- );
- header('Content-type: application/json');
- if ($callback) {
- $response = $callback . "(" . json_encode($output) . ")";
- } else {
- $response = json_encode($output, JSON_UNESCAPED_UNICODE);
- }
- if ($this->need_cache) {
- Memcached::instance()->set($this->cache_key, $response, ALL_CACHE_TIME);
- }
- echo $response;
- die();
- // 记录调试信息
- }
- protected function directOutput($response)
- {
- header('Content-type: application/json');
- if (is_array($response)) {
- $response = json_encode($response,JSON_UNESCAPED_UNICODE);
- }
- if ($this->need_cache) {
- Memcached::instance()->set($this->cache_key, $response, ALL_CACHE_TIME);
- }
- echo $response;
- }
- /**
- * 输出错误信息
- *
- * @param int $code
- * @param string $message
- */
- protected function showError($code = 0, $message = '')
- {
- $output = array(
- 'code' => intval($code),
- 'message' => $message,
- 'data' => []
- );
- header('Content-type: application/json');
- echo json_encode($output);
- exit();
- }
- /**
- * 显示成功信息
- * @param $data
- */
- protected function showSuccess($data = array())
- {
- $this->output(0, '请求成功', $data);
- }
- }
|