Controller.php 4.4 KB

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