HomeBaseController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 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 app\admin\model\ThemeModel;
  14. use think\facade\View;
  15. class HomeBaseController extends BaseController
  16. {
  17. protected function initialize()
  18. {
  19. // 监听home_init
  20. hook('home_init');
  21. parent::initialize();
  22. $closeConfig = cmf_get_option('close_config');
  23. $closeConfig = $closeConfig? $closeConfig : config('weixin.site');
  24. $isClose = isset($closeConfig['close'])? $closeConfig['close'] : 2;
  25. $accessIp = isset($closeConfig['access_ip'])? $closeConfig['access_ip'] : '';
  26. $accessIp = $accessIp? explode(',', $accessIp) : [];
  27. $closeMsg = isset($closeConfig['msg'])? $closeConfig['msg'] : '<div style="width: 100%; margin-top: 20%;line-height: 100px; font-size: 24px; text-align: center;"><h2>网站正在升级维护中</h2></div>';
  28. $controllerName = strtolower(request()->controller());
  29. if($controllerName != 'notify' && $isClose==1 && (empty($accessIp) || $accessIp && !in_array(get_client_ip(),$accessIp))){
  30. echo '<meta name="viewport" content="width=device-width, initial-scale=1.0,minimum-scale=1.0,user-scalable=0"><title>站点维护提示</title><div id="close_msg">'.$closeMsg.'</div>'; die();
  31. }
  32. $siteInfo = cmf_get_site_info();
  33. $mapConfig = config('config.map');
  34. $mapKey = isset($mapConfig['key'])? $mapConfig['key'] : '';
  35. $version = config('weixin.version');
  36. $version = $version? $version.'.'.date('Hi') : date('YmdH');
  37. $this->assign('version', $version);
  38. View::share('mapKey', $mapKey);
  39. View::share('site_info', $siteInfo);
  40. }
  41. protected function _initializeView()
  42. {
  43. $cmfThemePath = config('template.cmf_theme_path');
  44. $cmfDefaultTheme = cmf_get_current_theme();
  45. $themePath = "{$cmfThemePath}{$cmfDefaultTheme}";
  46. $root = cmf_get_root();
  47. //使cdn设置生效
  48. $cdnSettings = cmf_get_option('cdn_settings');
  49. if (empty($cdnSettings['cdn_static_root'])) {
  50. $viewReplaceStr = [
  51. '__ROOT__' => $root,
  52. '__TMPL__' => "{$root}/{$themePath}",
  53. '__STATIC__' => "{$root}/static",
  54. '__WEB_ROOT__' => $root
  55. ];
  56. } else {
  57. $cdnStaticRoot = rtrim($cdnSettings['cdn_static_root'], '/');
  58. $viewReplaceStr = [
  59. '__ROOT__' => $root,
  60. '__TMPL__' => "{$cdnStaticRoot}/{$themePath}",
  61. '__STATIC__' => "{$cdnStaticRoot}/static",
  62. '__WEB_ROOT__' => $cdnStaticRoot
  63. ];
  64. }
  65. config('template.view_base', WEB_ROOT . "{$themePath}/");
  66. config('template.tpl_replace_string', $viewReplaceStr);
  67. $themeErrorTmpl = "{$themePath}/error.html";
  68. if (file_exists_case($themeErrorTmpl)) {
  69. config('dispatch_error_tmpl', $themeErrorTmpl);
  70. }
  71. $themeSuccessTmpl = "{$themePath}/success.html";
  72. if (file_exists_case($themeSuccessTmpl)) {
  73. config('dispatch_success_tmpl', $themeSuccessTmpl);
  74. }
  75. }
  76. /**
  77. * 加载模板输出
  78. * @access protected
  79. * @param string $template 模板文件名
  80. * @param array $vars 模板输出变量
  81. * @param array $replace 模板替换
  82. * @param array $config 模板参数
  83. * @return mixed
  84. */
  85. protected function fetch($template = '', $vars = [], $replace = [], $config = [])
  86. {
  87. $template = $this->parseTemplate($template);
  88. $more = $this->getThemeFileMore($template);
  89. $this->assign('theme_vars', $more['vars']);
  90. $this->assign('theme_widgets', $more['widgets']);
  91. $content = parent::fetch($template, $vars, $replace, $config);
  92. $designingTheme = cookie('cmf_design_theme');
  93. if ($designingTheme) {
  94. $app = $this->request->module();
  95. $controller = $this->request->controller();
  96. $action = $this->request->action();
  97. $output = <<<hello
  98. <script>
  99. var _themeDesign=true;
  100. var _themeTest="test";
  101. var _app='{$app}';
  102. var _controller='{$controller}';
  103. var _action='{$action}';
  104. var _themeFile='{$more['file']}';
  105. if(parent && parent.simulatorRefresh){
  106. parent.simulatorRefresh();
  107. }
  108. </script>
  109. hello;
  110. $pos = strripos($content, '</body>');
  111. if (false !== $pos) {
  112. $content = substr($content, 0, $pos) . $output . substr($content, $pos);
  113. } else {
  114. $content = $content . $output;
  115. }
  116. }
  117. return $content;
  118. }
  119. /**
  120. * 自动定位模板文件
  121. * @access private
  122. * @param string $template 模板文件规则
  123. * @return string
  124. */
  125. private function parseTemplate($template)
  126. {
  127. // 分析模板文件规则
  128. $request = $this->request;
  129. // 获取视图根目录
  130. if (strpos($template, '@')) {
  131. // 跨模块调用
  132. list($module, $template) = explode('@', $template);
  133. }
  134. $viewBase = config('template.view_base');
  135. if ($viewBase) {
  136. // 基础视图目录
  137. $module = isset($module) ? $module : $request->module();
  138. $path = $viewBase . ($module ? $module . DIRECTORY_SEPARATOR : '');
  139. } else {
  140. $path = isset($module) ? APP_PATH . $module . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR : config('template.view_path');
  141. }
  142. $depr = config('template.view_depr');
  143. if (0 !== strpos($template, '/')) {
  144. $template = str_replace(['/', ':'], $depr, $template);
  145. $controller = cmf_parse_name($request->controller());
  146. if ($controller) {
  147. if ('' == $template) {
  148. // 如果模板文件名为空 按照默认规则定位
  149. $template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . cmf_parse_name($request->action(true));
  150. } elseif (false === strpos($template, $depr)) {
  151. $template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $template;
  152. }
  153. }
  154. } else {
  155. $template = str_replace(['/', ':'], $depr, substr($template, 1));
  156. }
  157. return $path . ltrim($template, '/') . '.' . ltrim(config('template.view_suffix'), '.');
  158. }
  159. /**
  160. * 获取模板文件变量
  161. * @param string $file
  162. * @param string $theme
  163. * @return array
  164. */
  165. private function getThemeFileMore($file, $theme = "")
  166. {
  167. //TODO 增加缓存
  168. $theme = empty($theme) ? cmf_get_current_theme() : $theme;
  169. // 调试模式下自动更新模板
  170. if (APP_DEBUG) {
  171. $themeModel = new ThemeModel();
  172. $themeModel->updateTheme($theme);
  173. }
  174. $themePath = config('template.cmf_theme_path');
  175. $file = str_replace('\\', '/', $file);
  176. $file = str_replace('//', '/', $file);
  177. $webRoot = str_replace('\\', '/', WEB_ROOT);
  178. $themeFile = str_replace(['.html', '.php', $themePath . $theme . "/", $webRoot], '', $file);
  179. $files = Db::name('theme_file')->field('more')->where('theme', $theme)
  180. ->where(function ($query) use ($themeFile) {
  181. $query->where('is_public', 1)->whereOr('file', $themeFile);
  182. })->select();
  183. $vars = [];
  184. $widgets = [];
  185. foreach ($files as $file) {
  186. $oldMore = json_decode($file['more'], true);
  187. if (!empty($oldMore['vars'])) {
  188. foreach ($oldMore['vars'] as $varName => $var) {
  189. $vars[$varName] = $var['value'];
  190. }
  191. }
  192. if (!empty($oldMore['widgets'])) {
  193. foreach ($oldMore['widgets'] as $widgetName => $widget) {
  194. $widgetVars = [];
  195. if (!empty($widget['vars'])) {
  196. foreach ($widget['vars'] as $varName => $var) {
  197. $widgetVars[$varName] = $var['value'];
  198. }
  199. }
  200. $widget['vars'] = $widgetVars;
  201. //如果重名,则合并配置
  202. if (empty($widgets[$widgetName])) {
  203. $widgets[$widgetName] = $widget;
  204. } else {
  205. foreach ($widgets[$widgetName] as $key => $value) {
  206. if (is_array($widget[$key])) {
  207. $widgets[$widgetName][$key] = array_merge($widgets[$widgetName][$key], $widget[$key]);
  208. } else {
  209. $widgets[$widgetName][$key] = $widget[$key];
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }
  216. return ['vars' => $vars, 'widgets' => $widgets, 'file' => $themeFile];
  217. }
  218. public function checkUserLogin()
  219. {
  220. $userId = cmf_get_current_user_id();
  221. if (empty($userId)) {
  222. if ($this->request->isAjax()) {
  223. $this->error("您尚未登录", cmf_url("user/Login/index"));
  224. } else {
  225. $this->redirect(cmf_url("user/Login/index"));
  226. }
  227. }
  228. }
  229. }