Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

176 linhas
5.3 KiB

  1. <?php
  2. namespace Kuxin;
  3. use Kuxin\Helper\Json;
  4. use Kuxin\Helper\OpenCC;
  5. /**
  6. * Class Block
  7. *
  8. * @package Kuxin
  9. * @author Pakey <pakey@qq.com>
  10. */
  11. class Block
  12. {
  13. /**
  14. * 获取区块
  15. * @param string $name
  16. * @param array|null $param
  17. * @return array|mixed|string
  18. */
  19. public static function show(string $name, ?array $param = [])
  20. {
  21. $cacheKey = md5(self::getUniqId($name, self::getParamId($param)) . '_' . Json::encode($param));
  22. $data = DI::Cache()->debugGet($cacheKey, function ($key) use ($name, $param) {
  23. if (strpos($name, '.')) {
  24. $var = explode('.', $name);
  25. } else {
  26. $var = [$name, 'index'];
  27. }
  28. $method = array_pop($var);
  29. $class = '\\App\\Block\\' . implode('\\', $var);
  30. $block = Loader::instance($class);
  31. if (!$block || !is_callable([$block, $method])) {
  32. trigger_error(sprintf('区块 %s 无法加载', $name), E_USER_ERROR);
  33. }
  34. $data = $block->$method($param);
  35. $cacheTime = $param['cachetime'] ?? $block->getCacheTime();
  36. if ($cacheTime && $data) {
  37. DI::Cache()->set($key, $data, $cacheTime);
  38. }
  39. return $data;
  40. });
  41. //随机数
  42. if (isset($param['randnum'])) {
  43. $randnum = Input::param('randnum', 'int', 10, $param);
  44. if ($randnum > count($data)) {
  45. $list = [];
  46. $keys = array_rand($data, $randnum);
  47. foreach ($keys as $v) {
  48. $list[] = $data[$v];
  49. }
  50. $data = $list;
  51. }
  52. }
  53. // 定义了模板
  54. if (isset($param['template'])) {
  55. View::disableLayout();
  56. $data = View::make($param['template'], $param);
  57. View::enableLayout();
  58. }
  59. if(!in_array(Response::getType(), ['json', 'jsonp', 'xml'])){
  60. switch (Config::get('template.output_charset')) {
  61. case 'gbk':
  62. self::recursionHandleValueCharset($data, 'gbk');
  63. break;
  64. case 'big5':
  65. self::recursionHandleValueCharset($data, 'big5');
  66. break;
  67. }
  68. }
  69. return $data;
  70. }
  71. private static function recursionHandleValueCharset(&$data, $method)
  72. {
  73. foreach ($data as &$datum) {
  74. if (is_string($datum)) {
  75. switch ($method) {
  76. case 'gbk':
  77. $datum = iconv('utf-8', 'gbk', $datum);
  78. break;
  79. case 'big5':
  80. $datum = OpenCC::change($datum);
  81. break;
  82. }
  83. } else if (is_array($datum)) {
  84. self::recursionHandleValueCharset($datum, $method);
  85. }
  86. }
  87. }
  88. /**
  89. * 获取参数中的id值
  90. * @param $param
  91. * @return string
  92. */
  93. protected static function getParamId($param): string
  94. {
  95. if (isset($param['id'])) {
  96. return $param['id'];
  97. } elseif (isset($param['novelid'])) {
  98. return $param['novelid'];
  99. } elseif (isset($param['chapterid'])) {
  100. return $param['chapterid'];
  101. } elseif (isset($param['siteid'])) {
  102. return $param['siteid'];
  103. } elseif (isset($param['authorid'])) {
  104. return $param['authorid'];
  105. } elseif (isset($param['categoryid'])) {
  106. return $param['categoryid'];
  107. } elseif (isset($param['typeid'])) {
  108. return $param['typeid'];
  109. } else {
  110. return '0';
  111. }
  112. }
  113. /**
  114. * 生成区块名的唯一id 用于缓存 便于清理缓存
  115. * @param string $name
  116. * @param int $id
  117. * @return string
  118. */
  119. protected static function getUniqId(string $name, int $id = 0): string
  120. {
  121. static $_cache = [];
  122. $nameUniqId = self::getUniqNameId($name);
  123. $key = 'block_uniq_' . $nameUniqId . '_' . $id;
  124. if (isset($_cache[$key])) {
  125. return $_cache[$key];
  126. }
  127. $data = DI::Cache()->get($key, function ($key) {
  128. $uniqid = uniqid();
  129. DI::Cache()->set($key, $uniqid);
  130. return $uniqid;
  131. });
  132. return $_cache[$key] = $data;
  133. }
  134. /**
  135. * 缓存设计
  136. * @param string $name
  137. * @return mixed
  138. */
  139. protected static function getUniqNameId(string $name)
  140. {
  141. static $_cache = [];
  142. if (isset($_cache['block_uniq_' . $name])) {
  143. return $_cache['block_uniq_' . $name];
  144. }
  145. $key = 'block_uniq_' . $name;
  146. $data = DI::Cache()->get($key, function ($key) {
  147. $uniqid = uniqid();
  148. DI::Cache()->set($key, $uniqid);
  149. return $uniqid;
  150. });
  151. return $_cache['block_uniq_' . $name] = $data;
  152. }
  153. /**
  154. * 更新区块的唯一
  155. * @param string $name
  156. * @param int $id
  157. */
  158. public static function clearCache(string $name, ?int $id = null): void
  159. {
  160. if ($id !== null) {
  161. $nameUniqId = self::getUniqNameId($name);
  162. DI::Cache()->set('block_uniq_' . $nameUniqId . '_' . $id, uniqid() . uniqid());
  163. } else {
  164. DI::Cache()->set('block_uniq_' . $name, uniqid());
  165. }
  166. }
  167. }