TplConfig.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\admin\controller;
  3. class TplConfig extends Base
  4. {
  5. /**
  6. * 默认模板主题配置
  7. */
  8. public function theme()
  9. {
  10. if (request()->isPost()) {
  11. $tplconfig = input();
  12. if (empty($tplconfig) || !isset($tplconfig['theme'])) {
  13. return $this->error(lang('param_err'));
  14. }
  15. $tplconfig = $tplconfig['theme'];
  16. if (!isset($tplconfig['banner']) || !is_array($tplconfig['banner'])) {
  17. $tplconfig['banner'] = [];
  18. }
  19. $stylePc = isset($tplconfig['banner']['style_pc']) ? (string)$tplconfig['banner']['style_pc'] : '1';
  20. $styleH5 = isset($tplconfig['banner']['style_h5']) ? (string)$tplconfig['banner']['style_h5'] : '1';
  21. $tplconfig['banner']['style_pc'] = in_array($stylePc, ['1', '2', '3'], true) ? $stylePc : '1';
  22. $tplconfig['banner']['style_h5'] = in_array($styleH5, ['1', '2', '3'], true) ? $styleH5 : '1';
  23. if (isset($tplconfig['fnav']['ym']) && is_array($tplconfig['fnav']['ym'])) {
  24. $tplconfig['fnav']['ym'] = join('|', $tplconfig['fnav']['ym']);
  25. }
  26. if (isset($tplconfig['show']['filter']) && is_array($tplconfig['show']['filter'])) {
  27. $tplconfig['show']['filter'] = join('|', $tplconfig['show']['filter']);
  28. }
  29. if (isset($tplconfig['ad_slots'])) {
  30. $tplconfig['ad_slots'] = $this->normalizeAdSlots($tplconfig['ad_slots']);
  31. }
  32. if (isset($tplconfig['type']['hom']) && is_array($tplconfig['type']['hom'])) {
  33. foreach ($tplconfig['type']['hom'] as $hk => $homRow) {
  34. if (!is_array($homRow)) {
  35. continue;
  36. }
  37. $cv = isset($homRow['cover']) ? (string) $homRow['cover'] : 'v';
  38. $tplconfig['type']['hom'][$hk]['cover'] = ($cv === 'h') ? 'h' : 'v';
  39. }
  40. }
  41. if (!isset($tplconfig['list_cover']) || !is_array($tplconfig['list_cover'])) {
  42. $tplconfig['list_cover'] = [];
  43. }
  44. $vodRows = [];
  45. if (isset($tplconfig['list_cover']['vod']) && is_array($tplconfig['list_cover']['vod'])) {
  46. $vodRows = $tplconfig['list_cover']['vod'];
  47. } elseif (isset($tplconfig['type']['list_cover']) && is_array($tplconfig['type']['list_cover'])) {
  48. // 兼容旧结构,避免历史配置迁移期间丢失
  49. $vodRows = $tplconfig['type']['list_cover'];
  50. }
  51. $listCoverVod = [];
  52. foreach ($vodRows as $row) {
  53. if (!is_array($row) || !isset($row['id']) || (string) $row['id'] === '') {
  54. continue;
  55. }
  56. $cv = isset($row['cover']) ? (string) $row['cover'] : 'v';
  57. $listCoverVod[] = [
  58. 'id' => (string) (int) $row['id'],
  59. 'cover' => ($cv === 'h') ? 'h' : 'v',
  60. ];
  61. }
  62. $tplconfig['list_cover']['vod'] = $listCoverVod;
  63. unset($tplconfig['type']['list_cover']);
  64. if (isset($tplconfig['manga']['hbtn'])) {
  65. $tplconfig['manga']['hbtn'] = ((string) $tplconfig['manga']['hbtn'] === '1') ? '1' : '0';
  66. }
  67. if (isset($tplconfig['manga']['hnum'])) {
  68. $tplconfig['manga']['hnum'] = ((string) $tplconfig['manga']['hnum'] === '12') ? '12' : '6';
  69. }
  70. $mangaCoverRaw = isset($tplconfig['list_cover']['manga'])
  71. ? (string) $tplconfig['list_cover']['manga']
  72. : (isset($tplconfig['manga']['cover']) ? (string) $tplconfig['manga']['cover'] : 'v');
  73. $tplconfig['list_cover']['manga'] = ($mangaCoverRaw === 'h') ? 'h' : 'v';
  74. if (isset($tplconfig['manga']['cover'])) {
  75. unset($tplconfig['manga']['cover']);
  76. }
  77. if (isset($tplconfig['art']['hbtn'])) {
  78. $tplconfig['art']['hbtn'] = ((string) $tplconfig['art']['hbtn'] === '1') ? '1' : '0';
  79. }
  80. if (isset($tplconfig['art']['hnum'])) {
  81. $tplconfig['art']['hnum'] = ((string) $tplconfig['art']['hnum'] === '12') ? '12' : '6';
  82. }
  83. $artCoverRaw = isset($tplconfig['list_cover']['art'])
  84. ? (string) $tplconfig['list_cover']['art']
  85. : (isset($tplconfig['art']['cover']) ? (string) $tplconfig['art']['cover'] : 'v');
  86. $tplconfig['list_cover']['art'] = ($artCoverRaw === 'h') ? 'h' : 'v';
  87. if (isset($tplconfig['art']['cover'])) {
  88. unset($tplconfig['art']['cover']);
  89. }
  90. if (isset($tplconfig['topic']['hbtn'])) {
  91. $tplconfig['topic']['hbtn'] = ((string) $tplconfig['topic']['hbtn'] === '1') ? '1' : '0';
  92. }
  93. unset($tplconfig['topic']['hnum']);
  94. unset($tplconfig['topic']['htitle']);
  95. $topicCellsIn = isset($tplconfig['topic']['cells']) && is_array($tplconfig['topic']['cells'])
  96. ? $tplconfig['topic']['cells'] : [];
  97. $topicCellsNorm = [];
  98. for ($ti = 0; $ti < 5; $ti++) {
  99. $trow = isset($topicCellsIn[$ti]) && is_array($topicCellsIn[$ti]) ? $topicCellsIn[$ti] : [];
  100. $tidRaw = isset($trow['topic_id']) ? (int) $trow['topic_id'] : 0;
  101. $topicCellsNorm[] = [
  102. 'topic_id' => (string) ($tidRaw > 0 ? $tidRaw : 0),
  103. 'title' => isset($trow['title']) ? trim((string) $trow['title']) : '',
  104. 'sub' => isset($trow['sub']) ? trim((string) $trow['sub']) : '',
  105. ];
  106. }
  107. $tplconfig['topic']['cells'] = $topicCellsNorm;
  108. if (isset($tplconfig['contact']) && is_array($tplconfig['contact'])) {
  109. unset($tplconfig['contact']['sdk_js']);
  110. }
  111. $tplconfig_new = ['theme' => $tplconfig];
  112. $tplconfig_old = isset($GLOBALS['mctheme']) && is_array($GLOBALS['mctheme']) ? $GLOBALS['mctheme'] : [];
  113. $tplconfig_new = array_merge($tplconfig_old, $tplconfig_new);
  114. $res = mac_save_config_data(APP_PATH . 'extra/mctheme.php', $tplconfig_new);
  115. if ($res === false) {
  116. return $this->error(lang('save_err'));
  117. }
  118. // 与 ThinkPHP 配置、$GLOBALS 同步,当前请求内模板与 mac_tpl_* 立即生效
  119. \think\Config::set($tplconfig_new, 'mctheme');
  120. $GLOBALS['mctheme'] = $tplconfig_new;
  121. return $this->success(lang('save_ok'));
  122. }
  123. $tplconfig = isset($GLOBALS['mctheme']) ? $GLOBALS['mctheme'] : (config('mctheme') ?: ['theme' => []]);
  124. $this->assign('tplconfig', $tplconfig);
  125. $this->assign('title', lang('menu/theme/config'));
  126. return $this->fetch('admin@tplconfig/theme');
  127. }
  128. /**
  129. * 广告位配置规范化(固定结构):
  130. * slot => ['ad_type_id' => ?, 'ad_data' => [ ... ]]
  131. */
  132. protected function normalizeAdSlots($adSlots)
  133. {
  134. if (!is_array($adSlots)) {
  135. return [];
  136. }
  137. $slotMeta = [
  138. 'banner1' => ['ad_type_id' => 1, 'area' => 'hengfu1'],
  139. 'banner_swiper' => ['ad_type_id' => 4, 'area' => 'lunbo'],
  140. 'icon1' => ['ad_type_id' => 2, 'area' => 'tubiao1'],
  141. 'bottom' => ['ad_type_id' => 3, 'area' => 'dipiao'],
  142. 'sitejs' => ['ad_type_id' => null, 'area' => 'js'],
  143. ];
  144. $fields = ['id', 'area', 'order', 'name', 'url', 'image', 'height', 'text', 'code', 'active', 'date', 'note'];
  145. $normalized = [];
  146. foreach ($slotMeta as $slot => $meta) {
  147. $normalized[$slot] = ['ad_type_id' => $meta['ad_type_id'], 'ad_data' => []];
  148. if (empty($adSlots[$slot]) || !is_array($adSlots[$slot]) || empty($adSlots[$slot]['ad_data']) || !is_array($adSlots[$slot]['ad_data'])) {
  149. continue;
  150. }
  151. if (array_key_exists('ad_type_id', $adSlots[$slot])) {
  152. $normalized[$slot]['ad_type_id'] = $adSlots[$slot]['ad_type_id'] === '' ? null : $adSlots[$slot]['ad_type_id'];
  153. }
  154. foreach ($adSlots[$slot]['ad_data'] as $row) {
  155. if (!is_array($row)) {
  156. continue;
  157. }
  158. $item = [];
  159. foreach ($fields as $field) {
  160. $value = isset($row[$field]) ? trim((string)$row[$field]) : '';
  161. if ($field === 'order') {
  162. $value = (string)intval($value);
  163. } elseif ($field === 'active') {
  164. $value = strtolower($value) === 'on' ? 'on' : 'off';
  165. }
  166. $item[$field] = $value;
  167. }
  168. $item['area'] = $meta['area'];
  169. // 至少有一个有效业务字段才保留,避免空行写入配置
  170. $hasPayload = false;
  171. $payloadFields = ['url', 'image', 'code', 'text', 'name', 'note'];
  172. foreach ($payloadFields as $k) {
  173. if (!empty($item[$k])) { $hasPayload = true; break; }
  174. }
  175. if ($hasPayload) {
  176. $normalized[$slot]['ad_data'][] = $item;
  177. }
  178. }
  179. }
  180. return $normalized;
  181. }
  182. }