HomeBaseController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +---------------------------------------------------------------------
  9. // | Author: Dean <zxxjjforever@163.com>
  10. // +----------------------------------------------------------------------
  11. namespace cmf\controller;
  12. use think\Db;
  13. use admin\admin\model\ThemeModel;
  14. use think\View;
  15. class HomeBaseController extends BaseController
  16. {
  17. public function _initialize()
  18. {
  19. // 监听home_init
  20. hook('home_init');
  21. parent::_initialize();
  22. $siteInfo = cmf_get_site_info();
  23. View::share('site_info', $siteInfo);
  24. }
  25. public function _initializeView()
  26. {
  27. $cmfThemePath = config('cmf_theme_path');
  28. $cmfDefaultTheme = cmf_get_current_theme();
  29. $themePath = "{$cmfThemePath}{$cmfDefaultTheme}";
  30. $root = cmf_get_root();
  31. //使cdn设置生效
  32. $cdnSettings = cmf_get_option('cdn_settings');
  33. if (empty($cdnSettings['cdn_static_root'])) {
  34. $viewReplaceStr = [
  35. '__ROOT__' => $root,
  36. '__TMPL__' => "{$root}/{$themePath}",
  37. '__STATIC__' => "{$root}/static",
  38. '__WEB_ROOT__' => $root
  39. ];
  40. } else {
  41. $cdnStaticRoot = rtrim($cdnSettings['cdn_static_root'], '/');
  42. $viewReplaceStr = [
  43. '__ROOT__' => $root,
  44. '__TMPL__' => "{$cdnStaticRoot}/{$themePath}",
  45. '__STATIC__' => "{$cdnStaticRoot}/static",
  46. '__WEB_ROOT__' => $cdnStaticRoot
  47. ];
  48. }
  49. $viewReplaceStr = array_merge(config('view_replace_str'), $viewReplaceStr);
  50. config('template.view_base', "{$themePath}/");
  51. config('view_replace_str', $viewReplaceStr);
  52. $themeErrorTmpl = "{$themePath}/error.html";
  53. if (file_exists_case($themeErrorTmpl)) {
  54. config('dispatch_error_tmpl', $themeErrorTmpl);
  55. }
  56. $themeSuccessTmpl = "{$themePath}/success.html";
  57. if (file_exists_case($themeSuccessTmpl)) {
  58. config('dispatch_success_tmpl', $themeSuccessTmpl);
  59. }
  60. }
  61. /**
  62. * 加载模板输出
  63. * @access protected
  64. * @param string $template 模板文件名
  65. * @param array $vars 模板输出变量
  66. * @param array $replace 模板替换
  67. * @param array $config 模板参数
  68. * @return mixed
  69. */
  70. protected function fetch($template = '', $vars = [], $replace = [], $config = [])
  71. {
  72. $template = $this->parseTemplate($template);
  73. $more = $this->getThemeFileMore($template);
  74. $this->assign('theme_vars', $more['vars']);
  75. $this->assign('theme_widgets', $more['widgets']);
  76. return parent::fetch($template, $vars, $replace, $config);
  77. }
  78. /**
  79. * 自动定位模板文件
  80. * @access private
  81. * @param string $template 模板文件规则
  82. * @return string
  83. */
  84. private function parseTemplate($template)
  85. {
  86. // 分析模板文件规则
  87. $request = $this->request;
  88. // 获取视图根目录
  89. if (strpos($template, '@')) {
  90. // 跨模块调用
  91. list($module, $template) = explode('@', $template);
  92. }
  93. $viewBase = config('template.view_base');
  94. if ($viewBase) {
  95. // 基础视图目录
  96. $module = isset($module) ? $module : $request->module();
  97. $path = $viewBase . ($module ? $module . DS : '');
  98. } else {
  99. $path = isset($module) ? APP_PATH . $module . DS . 'view' . DS : config('template.view_path');
  100. }
  101. $depr = config('template.view_depr');
  102. if (0 !== strpos($template, '/')) {
  103. $template = str_replace(['/', ':'], $depr, $template);
  104. $controller = cmf_parse_name($request->controller());
  105. if ($controller) {
  106. if ('' == $template) {
  107. // 如果模板文件名为空 按照默认规则定位
  108. $template = str_replace('.', DS, $controller) . $depr . $request->action();
  109. } elseif (false === strpos($template, $depr)) {
  110. $template = str_replace('.', DS, $controller) . $depr . $template;
  111. }
  112. }
  113. } else {
  114. $template = str_replace(['/', ':'], $depr, substr($template, 1));
  115. }
  116. return $path . ltrim($template, '/') . '.' . ltrim(config('template.view_suffix'), '.');
  117. }
  118. /**
  119. * 获取模板文件变量
  120. * @param string $file
  121. * @param string $theme
  122. * @return array
  123. */
  124. private function getThemeFileMore($file, $theme = "")
  125. {
  126. //TODO 增加缓存
  127. $theme = empty($theme) ? cmf_get_current_theme() : $theme;
  128. // 调试模式下自动更新模板
  129. if (APP_DEBUG) {
  130. $themeModel = new ThemeModel();
  131. $themeModel->updateTheme($theme);
  132. }
  133. $themePath = config('cmf_theme_path');
  134. $file = str_replace('\\', '/', $file);
  135. $file = str_replace('//', '/', $file);
  136. $file = str_replace(['.html', '.php', $themePath . $theme . "/"], '', $file);
  137. $files = Db::name('theme_file')->field('more')->where(['theme' => $theme])->where(function ($query) use ($file) {
  138. $query->where(['is_public' => 1])->whereOr(['file' => $file]);
  139. })->select();
  140. $vars = [];
  141. $widgets = [];
  142. foreach ($files as $file) {
  143. $oldMore = json_decode($file['more'], true);
  144. if (!empty($oldMore['vars'])) {
  145. foreach ($oldMore['vars'] as $varName => $var) {
  146. $vars[$varName] = $var['value'];
  147. }
  148. }
  149. if (!empty($oldMore['widgets'])) {
  150. foreach ($oldMore['widgets'] as $widgetName => $widget) {
  151. $widgetVars = [];
  152. if (!empty($widget['vars'])) {
  153. foreach ($widget['vars'] as $varName => $var) {
  154. $widgetVars[$varName] = $var['value'];
  155. }
  156. }
  157. $widget['vars'] = $widgetVars;
  158. $widgets[$widgetName] = $widget;
  159. }
  160. }
  161. }
  162. return ['vars' => $vars, 'widgets' => $widgets];
  163. }
  164. public function checkUserLogin()
  165. {
  166. $userId = cmf_get_current_user_id();
  167. if (empty($userId)) {
  168. if ($this->request->isAjax()) {
  169. $this->error("您尚未登录", cmf_url("user/Login/index"));
  170. } else {
  171. $this->redirect(cmf_url("user/Login/index"));
  172. }
  173. }
  174. }
  175. }