Config.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace app\api\controller;
  3. use think\Request;
  4. use think\Db;
  5. class Config extends Base
  6. {
  7. use PublicApi;
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. $this->check_config();
  12. }
  13. /**
  14. * 解析当前 PC 模板目录(与 Init 行为一致:优先 site_tpl_dir,否则 template_dir)
  15. */
  16. private function resolveTemplateDir()
  17. {
  18. $config = config('maccms');
  19. $site = isset($config['site']) ? $config['site'] : [];
  20. $dir = isset($site['site_tpl_dir']) ? trim((string) $site['site_tpl_dir']) : '';
  21. if ($dir === '') {
  22. $dir = isset($site['template_dir']) ? trim((string) $site['template_dir']) : '';
  23. }
  24. return $dir !== '' ? $dir : 'default';
  25. }
  26. public function get_config(Request $request)
  27. {
  28. $config = config('maccms');
  29. $banners = isset($config['site']['site_banner']) ? $config['site']['site_banner'] : '';
  30. $banner_list = [];
  31. if (!empty($banners)) {
  32. $banner_list = explode("\n", $banners);
  33. foreach ($banner_list as $k => &$v) {
  34. $v = mac_url_img($v);
  35. }
  36. }
  37. $res = [
  38. 'code' => 1,
  39. 'msg' => '获取成功',
  40. 'data' => [
  41. 'site_banner' => $banner_list,
  42. 'site_app_launch_image' => isset($config['site']['site_app_launch_image']) ? mac_url_img($config['site']['site_app_launch_image']) : '',
  43. ]
  44. ];
  45. return json($res)->options(['json_encode_param' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE]);
  46. }
  47. /**
  48. * 获取预留参数
  49. * 后台 网站参数配置 -> 预留参数 分页中的所有设置项
  50. * 包含:播放器排序、加密方式、热门搜索、各类扩展分类/地区/年份/语言、
  51. * 过滤词、自定义参数(extra_var)等
  52. *
  53. * 逗号分隔的字段会自动转为数组
  54. *
  55. * @param Request $request
  56. * @return \think\response\Json
  57. *
  58. * 示例:
  59. * GET /api/config/get_extra_var => 返回预留参数分页的所有配置
  60. */
  61. public function get_extra_var(Request $request)
  62. {
  63. $config = config('maccms');
  64. $app = isset($config['app']) ? $config['app'] : [];
  65. // 将逗号分隔的字符串转为数组,过滤空值
  66. $toArray = function($val) {
  67. if (empty($val)) return [];
  68. return array_values(array_filter(array_map('trim', explode(',', $val)), function($v) {
  69. return $v !== '';
  70. }));
  71. };
  72. // 预留参数分页中的所有字段(逗号分隔的转为数组)
  73. $data = [
  74. 'player_sort' => isset($app['player_sort']) ? $app['player_sort'] : '',
  75. 'encrypt' => isset($app['encrypt']) ? $app['encrypt'] : '0',
  76. 'search_hot' => $toArray(isset($app['search_hot']) ? $app['search_hot'] : ''),
  77. 'art_extend_class' => $toArray(isset($app['art_extend_class']) ? $app['art_extend_class'] : ''),
  78. 'vod_extend_class' => $toArray(isset($app['vod_extend_class']) ? $app['vod_extend_class'] : ''),
  79. 'vod_extend_state' => $toArray(isset($app['vod_extend_state']) ? $app['vod_extend_state'] : ''),
  80. 'vod_extend_version' => $toArray(isset($app['vod_extend_version']) ? $app['vod_extend_version'] : ''),
  81. 'vod_extend_area' => $toArray(isset($app['vod_extend_area']) ? $app['vod_extend_area'] : ''),
  82. 'vod_extend_lang' => $toArray(isset($app['vod_extend_lang']) ? $app['vod_extend_lang'] : ''),
  83. 'vod_extend_year' => $toArray(isset($app['vod_extend_year']) ? $app['vod_extend_year'] : ''),
  84. 'vod_extend_weekday' => $toArray(isset($app['vod_extend_weekday']) ? $app['vod_extend_weekday'] : ''),
  85. 'actor_extend_area' => $toArray(isset($app['actor_extend_area']) ? $app['actor_extend_area'] : ''),
  86. 'filter_words' => $toArray(isset($app['filter_words']) ? $app['filter_words'] : ''),
  87. ];
  88. // 自定义参数(extra_var 解析为 key-value)
  89. $extra = isset($config['extra']) && is_array($config['extra']) ? $config['extra'] : [];
  90. if (empty($extra) && !empty($app['extra_var'])) {
  91. $extra_var = str_replace(array(chr(10), chr(13)), array('', '#'), $app['extra_var']);
  92. $tmp = explode('#', $extra_var);
  93. foreach ($tmp as $a) {
  94. if (!empty($a)) {
  95. $tmp2 = explode('$$$', $a);
  96. if (count($tmp2) >= 2) {
  97. $extra[trim($tmp2[0])] = trim($tmp2[1]);
  98. }
  99. }
  100. }
  101. }
  102. $data['extra_var'] = $extra;
  103. return json([
  104. 'code' => 1,
  105. 'msg' => lang('api/get_ok'),
  106. 'data' => $data,
  107. ])->options(['json_encode_param' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE]);
  108. }
  109. /**
  110. * 获取前台所需的完整配置
  111. * 包含:站点基本信息 + 模板主题配置(tplconfig)
  112. *
  113. * 前端首页初始化时调用一次即可拿到所有配置信息,
  114. * 包括各区块的显示/隐藏开关、标题、数量、导航ID等。
  115. *
  116. * @param Request $request
  117. * @return \think\response\Json
  118. */
  119. public function get_tpl_config(Request $request)
  120. {
  121. $config = config('maccms');
  122. $templateDir = $this->resolveTemplateDir();
  123. // 读取模板目录的 config.json(tplconfig)
  124. $tplconfig = [];
  125. $configFile = './template/' . $templateDir . '/config.json';
  126. if (file_exists($configFile)) {
  127. $content = file_get_contents($configFile);
  128. $tplconfig = json_decode($content, true);
  129. if (!is_array($tplconfig)) {
  130. $tplconfig = [];
  131. }
  132. }
  133. // 站点基本信息
  134. $site = [
  135. 'site_name' => $config['site']['site_name'] ?? '',
  136. 'site_url' => $config['site']['site_url'] ?? '',
  137. 'site_logo' => !empty($config['site']['site_logo']) ? mac_url_img($config['site']['site_logo']) : '',
  138. 'site_wap_logo' => !empty($config['site']['site_wap_logo']) ? mac_url_img($config['site']['site_wap_logo']) : '',
  139. 'site_keywords' => $config['site']['site_keywords'] ?? '',
  140. 'site_description' => $config['site']['site_description'] ?? '',
  141. 'site_icp' => $config['site']['site_icp'] ?? '',
  142. 'site_email' => $config['site']['site_email'] ?? '',
  143. 'template_dir' => $templateDir,
  144. ];
  145. // 功能开关
  146. $features = [
  147. 'user_status' => intval($config['user']['status'] ?? 0),
  148. 'gbook_status' => intval($config['gbook']['status'] ?? 0),
  149. 'comment_status' => intval($config['comment']['status'] ?? 0),
  150. ];
  151. // 搜索相关
  152. $search = [
  153. 'search_hot' => $config['app']['search_hot'] ?? '',
  154. ];
  155. return json([
  156. 'code' => 1,
  157. 'msg' => '获取成功',
  158. 'info' => [
  159. 'site' => $site,
  160. 'features' => $features,
  161. 'search' => $search,
  162. 'tpl_config' => $tplconfig,
  163. ],
  164. ])->options(['json_encode_param' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE]);
  165. }
  166. /**
  167. * 获取与 PC 模板一致的「主题配置」
  168. * GET /api.php/config/get_mctheme
  169. *
  170. * 对应前台 assign 的 $tplconfig 数据源:config('mctheme')(application/extra/mctheme.php + 后台主题落盘)。
  171. * 含 theme.ad_slots、theme.ads 等,供 SPA 渲染广告位、首页模块开关。
  172. */
  173. public function get_mctheme(Request $request)
  174. {
  175. $mctheme = config('mctheme');
  176. if (!is_array($mctheme)) {
  177. $mctheme = [];
  178. }
  179. return json([
  180. 'code' => 1,
  181. 'msg' => '获取成功',
  182. 'info' => [
  183. 'template_dir' => $this->resolveTemplateDir(),
  184. 'mctheme' => $mctheme,
  185. ],
  186. ])->options(['json_encode_param' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE]);
  187. }
  188. /**
  189. * 枚举当前模板「广告目录」下的 *.js 文件及可访问 URL
  190. * GET /api.php/config/get_ads_files
  191. *
  192. * 目录:template/{模板目录}/{ads_dir}/ ,ads_dir 来自站点配置(默认 ads),与 MAC_PATH_ADS 一致。
  193. */
  194. public function get_ads_files(Request $request)
  195. {
  196. $config = config('maccms');
  197. $templateDir = $this->resolveTemplateDir();
  198. $adsDir = !empty($config['site']['ads_dir']) ? trim((string) $config['site']['ads_dir']) : 'ads';
  199. $physical = ROOT_PATH . 'template/' . $templateDir . '/' . $adsDir;
  200. $files = [];
  201. if (is_dir($physical)) {
  202. foreach (glob($physical . DIRECTORY_SEPARATOR . '*.js') ?: [] as $full) {
  203. if (!is_file($full)) {
  204. continue;
  205. }
  206. $name = basename($full);
  207. $rel = 'template/' . $templateDir . '/' . $adsDir . '/' . $name;
  208. $files[] = [
  209. 'name' => $name,
  210. 'path' => $rel,
  211. 'url' => mac_url_img($rel),
  212. ];
  213. }
  214. }
  215. usort($files, function ($a, $b) {
  216. return strcmp($a['name'], $b['name']);
  217. });
  218. return json([
  219. 'code' => 1,
  220. 'msg' => '获取成功',
  221. 'info' => [
  222. 'template_dir' => $templateDir,
  223. 'ads_dir' => $adsDir,
  224. 'files' => $files,
  225. ],
  226. ])->options(['json_encode_param' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE]);
  227. }
  228. }