BaseController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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: Powerless < wzxaini9@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace battery\portal\controller;
  12. use battery\portal\model\RedisModel;
  13. use think\Controller;
  14. use think\View;
  15. use think\Request;
  16. use think\Config;
  17. class BaseController extends Controller
  18. {
  19. public function __construct(Request $request = null)
  20. {
  21. if (is_null($request)) {
  22. $request = Request::instance();
  23. }
  24. $this->request = $request;
  25. $this->_initializeView();
  26. $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
  27. // 控制器初始化
  28. $this->_initialize();
  29. // 前置操作方法
  30. if ($this->beforeActionList) {
  31. foreach ($this->beforeActionList as $method => $options) {
  32. is_numeric($method) ?
  33. $this->beforeAction($options) :
  34. $this->beforeAction($method, $options);
  35. }
  36. }
  37. }
  38. public function setTitleName($title='默认'){
  39. $this->assign('title_name',$title);
  40. }
  41. public function _initialize()
  42. {
  43. // 监听home_init
  44. hook('home_init');
  45. parent::_initialize();
  46. $siteInfo = cmf_get_site_info();
  47. View::share('site_info', $siteInfo);
  48. }
  49. public function _initializeView()
  50. {
  51. $cmfThemePath = config('cmf_theme_path');
  52. $cmfDefaultTheme = cmf_get_current_theme();
  53. $themePath = "{$cmfThemePath}{$cmfDefaultTheme}";
  54. $root = cmf_get_root();
  55. //使cdn设置生效
  56. $cdnSettings = cmf_get_option('cdn_settings');
  57. if (empty($cdnSettings['cdn_static_root'])) {
  58. $viewReplaceStr = [
  59. '__ROOT__' => $root,
  60. '__TMPL__' => "{$root}/{$themePath}",
  61. '__STATIC__' => "{$root}/static",
  62. '__WEB_ROOT__' => $root
  63. ];
  64. } else {
  65. $cdnStaticRoot = rtrim($cdnSettings['cdn_static_root'], '/');
  66. $viewReplaceStr = [
  67. '__ROOT__' => $root,
  68. '__TMPL__' => "{$cdnStaticRoot}/{$themePath}",
  69. '__STATIC__' => "{$cdnStaticRoot}/static",
  70. '__WEB_ROOT__' => $cdnStaticRoot
  71. ];
  72. }
  73. $viewReplaceStr = array_merge(config('view_replace_str'), $viewReplaceStr);
  74. config('template.view_base', "{$themePath}/");
  75. config('view_replace_str', $viewReplaceStr);
  76. $themeErrorTmpl = "{$themePath}/error.html";
  77. if (file_exists_case($themeErrorTmpl)) {
  78. config('dispatch_error_tmpl', $themeErrorTmpl);
  79. }
  80. $themeSuccessTmpl = "{$themePath}/success.html";
  81. if (file_exists_case($themeSuccessTmpl)) {
  82. config('dispatch_success_tmpl', $themeSuccessTmpl);
  83. }
  84. }
  85. /**
  86. * 加载模板输出
  87. * @access protected
  88. * @param string $template 模板文件名
  89. * @param array $vars 模板输出变量
  90. * @param array $replace 模板替换
  91. * @param array $config 模板参数
  92. * @return mixed
  93. */
  94. protected function fetch($template = '', $vars = [], $replace = [], $config = [])
  95. {
  96. $template = $this->parseTemplate($template);
  97. $more = $this->getThemeFileMore($template);
  98. $this->assign('theme_vars', $more['vars']);
  99. $this->assign('theme_widgets', $more['widgets']);
  100. $content = parent::fetch($template, $vars, $replace, $config);
  101. $designingTheme = session('admin_designing_theme');
  102. if ($designingTheme) {
  103. $app = $this->request->module();
  104. $controller = $this->request->controller();
  105. $action = $this->request->action();
  106. $output = <<<hello
  107. <script>
  108. var _themeDesign=true;
  109. var _themeTest="test";
  110. var _app='{$app}';
  111. var _controller='{$controller}';
  112. var _action='{$action}';
  113. var _themeFile='{$more['file']}';
  114. parent.simulatorRefresh();
  115. </script>
  116. hello;
  117. $pos = strripos($content, '</body>');
  118. if (false !== $pos) {
  119. $content = substr($content, 0, $pos) . $output . substr($content, $pos);
  120. } else {
  121. $content = $content . $output;
  122. }
  123. }
  124. return $content;
  125. }
  126. /**
  127. * 自动定位模板文件
  128. * @access private
  129. * @param string $template 模板文件规则
  130. * @return string
  131. */
  132. private function parseTemplate($template)
  133. {
  134. // 分析模板文件规则
  135. $request = $this->request;
  136. // 获取视图根目录
  137. if (strpos($template, '@')) {
  138. // 跨模块调用
  139. list($module, $template) = explode('@', $template);
  140. }
  141. $viewBase = config('template.view_base');
  142. if ($viewBase) {
  143. // 基础视图目录
  144. $module = isset($module) ? $module : $request->module();
  145. $path = $viewBase . ($module ? $module . DS : '');
  146. } else {
  147. $path = isset($module) ? APP_PATH . $module . DS . 'view' . DS : config('template.view_path');
  148. }
  149. $depr = config('template.view_depr');
  150. if (0 !== strpos($template, '/')) {
  151. $template = str_replace(['/', ':'], $depr, $template);
  152. $controller = cmf_parse_name($request->controller());
  153. if ($controller) {
  154. if ('' == $template) {
  155. // 如果模板文件名为空 按照默认规则定位
  156. $template = str_replace('.', DS, $controller) . $depr . cmf_parse_name($request->action(true));
  157. } elseif (false === strpos($template, $depr)) {
  158. $template = str_replace('.', DS, $controller) . $depr . $template;
  159. }
  160. }
  161. } else {
  162. $template = str_replace(['/', ':'], $depr, substr($template, 1));
  163. }
  164. return $path . ltrim($template, '/') . '.' . ltrim(config('template.view_suffix'), '.');
  165. }
  166. /**
  167. * 获取模板文件变量
  168. * @param string $file
  169. * @param string $theme
  170. * @return array
  171. */
  172. private function getThemeFileMore($file, $theme = "")
  173. {
  174. //TODO 增加缓存
  175. $theme = empty($theme) ? cmf_get_current_theme() : $theme;
  176. $themePath = config('cmf_theme_path');
  177. $file = str_replace('\\', '/', $file);
  178. $file = str_replace('//', '/', $file);
  179. $themeFile = str_replace(['.html', '.php', $themePath . $theme . "/"], '', $file);
  180. $vars = [];
  181. $widgets = [];
  182. return ['vars' => $vars, 'widgets' => $widgets, 'file' => $themeFile];
  183. }
  184. public function isLogin()
  185. {
  186. if (hcw_is_user_login()) {
  187. } else {
  188. $code=10001;
  189. $this->result(null,$code,getErrCode($code),'JSON');
  190. }
  191. }
  192. //设置缓存
  193. public function setRedis($tag,$value,$extime=0){
  194. $redisModel=new RedisModel();
  195. $redisModel->set($tag,$value,$extime);
  196. }
  197. public function jsonSuccess($data,$msg='请求成功'){
  198. $this->result($data,1,$msg,'JSON');
  199. }
  200. public function jsonError($data,$msg='请求失败'){
  201. $this->result($data,0,$msg,'JSON');
  202. }
  203. // public function check
  204. }