Controller.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Heanup\App\Api;
  3. use Exception;
  4. use Heanup\App\Api\Controller\Search;
  5. use Heanup\Library\Memcached;
  6. use Heanup\Library\Redis;
  7. abstract class Controller extends \Heanup\Frame\Controller
  8. {
  9. protected $need_cache = false;
  10. /**
  11. * @var string
  12. */
  13. protected $cache_key;
  14. protected function main()
  15. {
  16. }
  17. public function addSitemap($url){
  18. $urls = array(
  19. $url,
  20. );
  21. $api = 'http://data.zz.baidu.com/urls?site=www.taoqu88.com&token=pKgc8vvoowMXccMI';
  22. $ch = curl_init();
  23. $options = array(
  24. CURLOPT_URL => $api,
  25. CURLOPT_POST => true,
  26. CURLOPT_RETURNTRANSFER => true,
  27. CURLOPT_POSTFIELDS => implode("\n", $urls),
  28. CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
  29. );
  30. curl_setopt_array($ch, $options);
  31. $result = curl_exec($ch);
  32. file_put_contents(APP_DIR."/baidu.log", $result);
  33. // $hashKey="sitemap:".md5($url);
  34. // $exist=Redis::instance()->get($hashKey);
  35. // if (!$exist){
  36. // Redis::instance()->set($hashKey, $url,86400*3);
  37. // }
  38. }
  39. /**
  40. * 验证登录信息
  41. * 无需验证或者单独验证的类可以覆盖该方法
  42. *
  43. * @param bool $checkLogin
  44. * @return bool
  45. * @throws Exception
  46. */
  47. protected function checkAuth($checkLogin = false)
  48. {
  49. if ($_GET['kw']){
  50. $con=new Search();
  51. $con->main();
  52. }
  53. $this->cache();
  54. return true;
  55. }
  56. protected function cache(){
  57. $key="cache:".md5(json_encode($_REQUEST));
  58. $cache=Redis::instance()->get($key);
  59. if ($cache){
  60. echo $cache;
  61. die();
  62. }
  63. }
  64. protected function setCache($content){
  65. $key="cache:".md5(json_encode($_REQUEST));
  66. Redis::instance()->set($key, $content,3600*3);
  67. echo $content;
  68. die();
  69. }
  70. /**
  71. * 输出模板,自动查找模板路径
  72. *
  73. * @param string $layout
  74. */
  75. protected function render($layout = 'Common/Layout')
  76. {
  77. header("Content-type: text/html; charset=utf-8");
  78. $this->getTemplate()->render(false, $layout);
  79. }
  80. /**
  81. * 检测缓存是否存在,缓存存在则直接输出
  82. */
  83. public function checkCache()
  84. {
  85. if ($data = Memcached::instance()->get($this->cache_key)) {
  86. header('Content-type: application/json');
  87. echo $data;
  88. die();
  89. }
  90. $this->need_cache = true;
  91. }
  92. /**
  93. * 输出json信息
  94. *
  95. * @param int $code
  96. * @param string $msg
  97. * @param array $data
  98. * @param string $callback
  99. */
  100. protected function output($code = 0, $msg = '', $data = array(), $callback = "")
  101. {
  102. $output = array(
  103. 'code' => $code,
  104. 'msg' => $msg,
  105. 'update' => date("Y-m-d H:i:s"),
  106. 'data' => $data ?: array()
  107. );
  108. header('Content-type: application/json');
  109. if ($callback) {
  110. $response = $callback . "(" . json_encode($output) . ")";
  111. } else {
  112. $response = json_encode($output, JSON_UNESCAPED_UNICODE);
  113. }
  114. if ($this->need_cache) {
  115. Memcached::instance()->set($this->cache_key, $response, ALL_CACHE_TIME);
  116. }
  117. echo $response;
  118. die();
  119. // 记录调试信息
  120. }
  121. protected function directOutput($response)
  122. {
  123. header('Content-type: application/json');
  124. if (is_array($response)) {
  125. $response = json_encode($response,JSON_UNESCAPED_UNICODE);
  126. }
  127. if ($this->need_cache) {
  128. Memcached::instance()->set($this->cache_key, $response, ALL_CACHE_TIME);
  129. }
  130. echo $response;
  131. }
  132. /**
  133. * 输出错误信息
  134. *
  135. * @param int $code
  136. * @param string $message
  137. */
  138. protected function showError($code = 0, $message = '')
  139. {
  140. $output = array(
  141. 'code' => intval($code),
  142. 'message' => $message,
  143. 'data' => []
  144. );
  145. header('Content-type: application/json');
  146. echo json_encode($output);
  147. exit();
  148. }
  149. /**
  150. * 显示成功信息
  151. * @param $data
  152. */
  153. protected function showSuccess($data = array())
  154. {
  155. $this->output(0, '请求成功', $data);
  156. }
  157. }