123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: Powerless < wzxaini9@gmail.com>
- // +----------------------------------------------------------------------
- namespace battery\portal\controller;
- use battery\portal\model\RedisModel;
- use think\Controller;
- use think\View;
- use think\Request;
- use think\Config;
- class BaseController extends Controller
- {
- public function __construct(Request $request = null)
- {
- if (is_null($request)) {
- $request = Request::instance();
- }
- $this->request = $request;
- $this->_initializeView();
- $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
- // 控制器初始化
- $this->_initialize();
- // 前置操作方法
- if ($this->beforeActionList) {
- foreach ($this->beforeActionList as $method => $options) {
- is_numeric($method) ?
- $this->beforeAction($options) :
- $this->beforeAction($method, $options);
- }
- }
- }
- public function setTitleName($title='默认'){
- $this->assign('title_name',$title);
- }
- public function _initialize()
- {
- // 监听home_init
- hook('home_init');
- parent::_initialize();
- $siteInfo = cmf_get_site_info();
- View::share('site_info', $siteInfo);
- }
- public function _initializeView()
- {
- $cmfThemePath = config('cmf_theme_path');
- $cmfDefaultTheme = cmf_get_current_theme();
- $themePath = "{$cmfThemePath}{$cmfDefaultTheme}";
- $root = cmf_get_root();
- //使cdn设置生效
- $cdnSettings = cmf_get_option('cdn_settings');
- if (empty($cdnSettings['cdn_static_root'])) {
- $viewReplaceStr = [
- '__ROOT__' => $root,
- '__TMPL__' => "{$root}/{$themePath}",
- '__STATIC__' => "{$root}/static",
- '__WEB_ROOT__' => $root
- ];
- } else {
- $cdnStaticRoot = rtrim($cdnSettings['cdn_static_root'], '/');
- $viewReplaceStr = [
- '__ROOT__' => $root,
- '__TMPL__' => "{$cdnStaticRoot}/{$themePath}",
- '__STATIC__' => "{$cdnStaticRoot}/static",
- '__WEB_ROOT__' => $cdnStaticRoot
- ];
- }
- $viewReplaceStr = array_merge(config('view_replace_str'), $viewReplaceStr);
- config('template.view_base', "{$themePath}/");
- config('view_replace_str', $viewReplaceStr);
- $themeErrorTmpl = "{$themePath}/error.html";
- if (file_exists_case($themeErrorTmpl)) {
- config('dispatch_error_tmpl', $themeErrorTmpl);
- }
- $themeSuccessTmpl = "{$themePath}/success.html";
- if (file_exists_case($themeSuccessTmpl)) {
- config('dispatch_success_tmpl', $themeSuccessTmpl);
- }
- }
- /**
- * 加载模板输出
- * @access protected
- * @param string $template 模板文件名
- * @param array $vars 模板输出变量
- * @param array $replace 模板替换
- * @param array $config 模板参数
- * @return mixed
- */
- protected function fetch($template = '', $vars = [], $replace = [], $config = [])
- {
- $template = $this->parseTemplate($template);
- $more = $this->getThemeFileMore($template);
- $this->assign('theme_vars', $more['vars']);
- $this->assign('theme_widgets', $more['widgets']);
- $content = parent::fetch($template, $vars, $replace, $config);
- $designingTheme = session('admin_designing_theme');
- if ($designingTheme) {
- $app = $this->request->module();
- $controller = $this->request->controller();
- $action = $this->request->action();
- $output = <<<hello
- <script>
- var _themeDesign=true;
- var _themeTest="test";
- var _app='{$app}';
- var _controller='{$controller}';
- var _action='{$action}';
- var _themeFile='{$more['file']}';
- parent.simulatorRefresh();
- </script>
- hello;
- $pos = strripos($content, '</body>');
- if (false !== $pos) {
- $content = substr($content, 0, $pos) . $output . substr($content, $pos);
- } else {
- $content = $content . $output;
- }
- }
- return $content;
- }
- /**
- * 自动定位模板文件
- * @access private
- * @param string $template 模板文件规则
- * @return string
- */
- private function parseTemplate($template)
- {
- // 分析模板文件规则
- $request = $this->request;
- // 获取视图根目录
- if (strpos($template, '@')) {
- // 跨模块调用
- list($module, $template) = explode('@', $template);
- }
- $viewBase = config('template.view_base');
- if ($viewBase) {
- // 基础视图目录
- $module = isset($module) ? $module : $request->module();
- $path = $viewBase . ($module ? $module . DS : '');
- } else {
- $path = isset($module) ? APP_PATH . $module . DS . 'view' . DS : config('template.view_path');
- }
- $depr = config('template.view_depr');
- if (0 !== strpos($template, '/')) {
- $template = str_replace(['/', ':'], $depr, $template);
- $controller = cmf_parse_name($request->controller());
- if ($controller) {
- if ('' == $template) {
- // 如果模板文件名为空 按照默认规则定位
- $template = str_replace('.', DS, $controller) . $depr . cmf_parse_name($request->action(true));
- } elseif (false === strpos($template, $depr)) {
- $template = str_replace('.', DS, $controller) . $depr . $template;
- }
- }
- } else {
- $template = str_replace(['/', ':'], $depr, substr($template, 1));
- }
- return $path . ltrim($template, '/') . '.' . ltrim(config('template.view_suffix'), '.');
- }
- /**
- * 获取模板文件变量
- * @param string $file
- * @param string $theme
- * @return array
- */
- private function getThemeFileMore($file, $theme = "")
- {
- //TODO 增加缓存
- $theme = empty($theme) ? cmf_get_current_theme() : $theme;
- $themePath = config('cmf_theme_path');
- $file = str_replace('\\', '/', $file);
- $file = str_replace('//', '/', $file);
- $themeFile = str_replace(['.html', '.php', $themePath . $theme . "/"], '', $file);
- $vars = [];
- $widgets = [];
- return ['vars' => $vars, 'widgets' => $widgets, 'file' => $themeFile];
- }
- public function isLogin()
- {
- if (hcw_is_user_login()) {
- } else {
- $code=10001;
- $this->result(null,$code,getErrCode($code),'JSON');
- }
- }
- //设置缓存
- public function setRedis($tag,$value,$extime=0){
- $redisModel=new RedisModel();
- $redisModel->set($tag,$value,$extime);
- }
- public function jsonSuccess($data,$msg='请求成功'){
- $this->result($data,1,$msg,'JSON');
- }
- public function jsonError($data,$msg='请求失败'){
- $this->result($data,0,$msg,'JSON');
- }
- // public function check
- }
|