Controller.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. return true;
  54. }
  55. /**
  56. * 输出模板,自动查找模板路径
  57. *
  58. * @param string $layout
  59. */
  60. protected function render($layout = 'Common/Layout')
  61. {
  62. header("Content-type: text/html; charset=utf-8");
  63. $this->getTemplate()->render(false, $layout);
  64. }
  65. /**
  66. * 检测缓存是否存在,缓存存在则直接输出
  67. */
  68. public function checkCache()
  69. {
  70. if ($data = Memcached::instance()->get($this->cache_key)) {
  71. header('Content-type: application/json');
  72. echo $data;
  73. die();
  74. }
  75. $this->need_cache = true;
  76. }
  77. /**
  78. * 输出json信息
  79. *
  80. * @param int $code
  81. * @param string $msg
  82. * @param array $data
  83. * @param string $callback
  84. */
  85. protected function output($code = 0, $msg = '', $data = array(), $callback = "")
  86. {
  87. $output = array(
  88. 'code' => $code,
  89. 'msg' => $msg,
  90. 'update' => date("Y-m-d H:i:s"),
  91. 'data' => $data ?: array()
  92. );
  93. header('Content-type: application/json');
  94. if ($callback) {
  95. $response = $callback . "(" . json_encode($output) . ")";
  96. } else {
  97. $response = json_encode($output, JSON_UNESCAPED_UNICODE);
  98. }
  99. if ($this->need_cache) {
  100. Memcached::instance()->set($this->cache_key, $response, ALL_CACHE_TIME);
  101. }
  102. echo $response;
  103. die();
  104. // 记录调试信息
  105. }
  106. protected function directOutput($response)
  107. {
  108. header('Content-type: application/json');
  109. if (is_array($response)) {
  110. $response = json_encode($response,JSON_UNESCAPED_UNICODE);
  111. }
  112. if ($this->need_cache) {
  113. Memcached::instance()->set($this->cache_key, $response, ALL_CACHE_TIME);
  114. }
  115. echo $response;
  116. }
  117. /**
  118. * 输出错误信息
  119. *
  120. * @param int $code
  121. * @param string $message
  122. */
  123. protected function showError($code = 0, $message = '')
  124. {
  125. $output = array(
  126. 'code' => intval($code),
  127. 'message' => $message,
  128. 'data' => []
  129. );
  130. header('Content-type: application/json');
  131. echo json_encode($output);
  132. exit();
  133. }
  134. /**
  135. * 显示成功信息
  136. * @param $data
  137. */
  138. protected function showSuccess($data = array())
  139. {
  140. $this->output(0, '请求成功', $data);
  141. }
  142. }