Common.php 15.7 KB
Newer Older
D
v1.2.0  
devil_gong 已提交
1 2 3 4
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
devil_gong 已提交
5
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
D
v1.2.0  
devil_gong 已提交
6 7 8 9 10 11 12
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\index\controller;

G
1.3.0  
gongfuxiang 已提交
13
use think\facade\Hook;
D
v1.2.0  
devil_gong 已提交
14
use think\Controller;
D
devil 已提交
15
use app\module\FormHandleModule;
16
use app\service\SystemService;
17 18 19 20 21 22 23
use app\service\GoodsService;
use app\service\NavigationService;
use app\service\BuyService;
use app\service\MessageService;
use app\service\SearchService;
use app\service\ConfigService;
use app\service\LinkService;
D
devil_gong 已提交
24
use app\service\UserService;
D
v1.2.0  
devil_gong 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

/**
 * 前端公共控制器
 * @author   Devil
 * @blog    http://gong.gg/
 * @version 1.0.0
 * @date    2018-11-30
 * @desc    description
 */
class Common extends Controller
{
    // 顶部导航
    protected $nav_header;

    // 底部导航
    protected $nav_footer;

    // 用户信息
    protected $user;

G
gongfuxiang 已提交
45 46 47
    // 请求参数
    protected $params;

D
devil 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    // 当前操作名称
    protected $module_name;
    protected $controller_name;
    protected $action_name;

    // 输入参数 post|get|request
    protected $data_post;
    protected $data_get;
    protected $data_request;

    // 分页信息
    protected $page;
    protected $page_size;

    // 动态表格
    protected $form_table;
    protected $form_where;
    protected $form_params;
    protected $form_error;

D
v1.2.0  
devil_gong 已提交
68 69 70 71 72 73 74 75 76 77 78 79
    /**
     * 构造方法
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-30
     * @desc    description
     */
    public function __construct()
    {
        parent::__construct();

D
devil 已提交
80 81 82 83 84
        // 输入参数
        $this->data_post = input('post.');
        $this->data_get = input('get.');
        $this->data_request = input();

D
v1.2.0  
devil_gong 已提交
85 86 87
        // 系统初始化
        $this->SystemInit();

D
devil 已提交
88 89 90
        // 系统运行开始
        SystemService::SystemBegin();

D
v1.2.0  
devil_gong 已提交
91 92 93 94 95 96 97 98 99 100 101
        // 站点状态校验
        $this->SiteStstusCheck();

        // 公共数据初始化
        $this->CommonInit();

        // 菜单
        $this->NavInit();

        // 视图初始化
        $this->ViewInit();
G
1.3.0  
gongfuxiang 已提交
102

D
devil 已提交
103 104 105
        // 动态表格初始化
        $this->FormTableInit();

G
1.3.0  
gongfuxiang 已提交
106 107 108 109
        // 公共钩子初始化
        $this->CommonPluginsInit();
    }

110 111 112 113 114 115 116 117 118 119 120 121 122 123
    /**
     * 析构函数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-03-18
     * @desc    description
     */
    public function __destruct()
    {
        // 系统运行结束
        SystemService::SystemEnd();
    }

G
1.3.0  
gongfuxiang 已提交
124 125 126 127 128 129 130 131 132 133
    /**
     * 公共钩子初始化
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-07
     * @desc    description
     */
    private function CommonPluginsInit()
    {
G
gongfuxiang 已提交
134
        // css钩子
135
        $this->assign('plugins_css_data', Hook::listen('plugins_css', ['hook_name'=>'plugins_css', 'is_backend'=>false]));
G
gongfuxiang 已提交
136 137

        // js钩子
138
        $this->assign('plugins_js_data', Hook::listen('plugins_js', ['hook_name'=>'plugins_js', 'is_backend'=>false]));
G
gongfuxiang 已提交
139
        
D
devil_gong 已提交
140
        // 公共header内钩子
141
        $this->assign('plugins_common_header_data', Hook::listen('plugins_common_header', ['hook_name'=>'plugins_common_header', 'is_backend'=>false, 'user'=>$this->user]));
D
devil_gong 已提交
142

G
优化  
gongfuxiang 已提交
143
        // 公共页面底部钩子
144
        $this->assign('plugins_common_page_bottom_data', Hook::listen('plugins_common_page_bottom', ['hook_name'=>'plugins_common_page_bottom', 'is_backend'=>false, 'user'=>$this->user]));
D
devil_gong 已提交
145

G
1.3.0  
gongfuxiang 已提交
146
        // 公共顶部钩子
147
        $this->assign('plugins_view_common_top_data', Hook::listen('plugins_view_common_top', ['hook_name'=>'plugins_view_common_top', 'is_backend'=>false, 'user'=>$this->user]));
D
devil_gong 已提交
148 149

        // 公共底部钩子
150
        $this->assign('plugins_view_common_bottom_data', Hook::listen('plugins_view_common_bottom', ['hook_name'=>'plugins_view_common_bottom', 'is_backend'=>false, 'user'=>$this->user]));
D
devil_gong 已提交
151 152

        // 公共顶部小导航钩子-左侧
G
gongfuxiang 已提交
153 154 155 156 157 158 159 160
        $this->assign('plugins_view_header_navigation_top_left_data', Hook::listen('plugins_view_header_navigation_top_left', ['hook_name'=>'plugins_view_header_navigation_top_left', 'is_backend'=>false, 'user'=>$this->user]));

        // 用户登录页面顶部钩子
        $this->assign('plugins_view_user_login_info_top_data', Hook::listen('plugins_view_user_login_info_top', ['hook_name'=>'plugins_view_user_login_info_top', 'is_backend'=>false, 'user'=>$this->user]));

        // 用户注册页面钩子
        $this->assign('plugins_view_user_reg_info_data', Hook::listen('plugins_view_user_reg_info', ['hook_name'=>'plugins_view_user_reg_info', 'is_backend'=>false, 'user'=>$this->user]));

D
devil_gong 已提交
161 162
        // 用户注册页面顶部钩子
        $this->assign('plugins_view_user_reg_info_top_data', Hook::listen('plugins_view_user_reg_info_top', ['hook_name'=>'plugins_view_user_reg_info_top', 'is_backend'=>false, 'user'=>$this->user]));
G
gongfuxiang 已提交
163

D
devil_gong 已提交
164 165
        // 用户注册页面底部钩子
        $this->assign('plugins_view_user_reg_info_bottom_data', Hook::listen('plugins_view_user_reg_info_bottom', ['hook_name'=>'plugins_view_user_reg_info_bottom', 'is_backend'=>false, 'user'=>$this->user]));
D
devil_gong 已提交
166 167 168

        // 底部导航上面钩子
        $this->assign('plugins_view_common_footer_top_data', Hook::listen('plugins_view_common_footer_top', ['hook_name'=>'plugins_view_common_footer_top', 'is_backend'=>false, 'user'=>$this->user]));
D
devil 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

        // 公共表格钩子名称动态处理
        $current = 'plugins_view_index_'.$this->controller_name;
        // 内容外部顶部
        $this->assign('hook_name_content_top', $current.'_content_top');
        // 内容外部底部
        $this->assign('hook_name_content_bottom', $current.'_content_bottom');
        // 内容内部顶部
        $this->assign('hook_name_content_inside_top', $current.'_content_inside_top');
        // 内容内部底部
        $this->assign('hook_name_content_inside_bottom', $current.'_content_inside_bottom');
        // 表格列表顶部操作
        $this->assign('hook_name_form_top_operate', $current.'_top_operate');
        // 表格列表底部操作
        $this->assign('hook_name_form_bottom_operate', $current.'_bottom_operate');
        // 表格列表后面操作栏
        $this->assign('hook_name_form_list_operate', $current.'_list_operate');

        // 公共详情页面钩子名称动态处理
        // 内容外部顶部
        $this->assign('hook_name_detail_top', $current.'_detail_top');
        // 内容外部底部
        $this->assign('hook_name_detail_bottom', $current.'_detail_bottom');
        // 内容内部顶部
        $this->assign('hook_name_detail_inside_top', $current.'_detail_inside_top');
        // 内容内部底部
        $this->assign('hook_name_detail_inside_bottom', $current.'_detail_inside_bottom');
D
v1.2.0  
devil_gong 已提交
196 197 198 199 200 201 202 203 204 205 206
    }

    /**
     * 系统初始化
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-07
     * @desc    description
     */
    private function SystemInit()
D
devil 已提交
207
    {        
D
v1.2.0  
devil_gong 已提交
208 209 210 211 212 213
        // 配置信息初始化
        ConfigService::ConfigInit();
        
        // url模式
        if(MyC('home_seo_url_model', 0) == 0)
        {
D
devil_gong 已提交
214
            \think\facade\Url::root(__MY_ROOT_PUBLIC__.'index.php?s=');
D
v1.2.0  
devil_gong 已提交
215
        }
D
devil 已提交
216 217

        // 推荐人
D
devil 已提交
218
        if(!empty($this->data_request['referrer']))
D
devil 已提交
219
        {
D
devil 已提交
220
            session('share_referrer_id', $this->data_request['referrer']);
D
devil 已提交
221
        }
D
v1.2.0  
devil_gong 已提交
222 223 224
    }

    /**
G
gongfuxiang 已提交
225
     * [CommonInit 公共数据初始化]
D
v1.2.0  
devil_gong 已提交
226 227 228 229 230
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-09T11:43:48+0800
     */
G
gongfuxiang 已提交
231
    private function CommonInit()
D
v1.2.0  
devil_gong 已提交
232
    {
G
gongfuxiang 已提交
233 234
        // 用户数据
        $this->user = UserService::LoginUserInfo();
D
v1.2.0  
devil_gong 已提交
235 236 237
    }

    /**
G
gongfuxiang 已提交
238
     * [IsLogin 登录校验]
D
v1.2.0  
devil_gong 已提交
239 240 241 242 243
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-09T11:43:48+0800
     */
G
gongfuxiang 已提交
244
    protected function IsLogin()
D
v1.2.0  
devil_gong 已提交
245
    {
G
gongfuxiang 已提交
246
        if(empty($this->user))
G
gongfuxiang 已提交
247
        {
G
gongfuxiang 已提交
248 249 250 251 252 253
            if(IS_AJAX)
            {
                exit(json_encode(DataReturn('登录失效,请重新登录', -400)));
            } else {
                return $this->redirect('index/user/logininfo');
            }
G
gongfuxiang 已提交
254
        }
D
v1.2.0  
devil_gong 已提交
255 256 257 258 259 260 261 262 263 264 265
    }

    /**
     * [ViewInit 视图初始化]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-03T12:30:06+0800
     */
    public function ViewInit()
    {
G
gongfuxiang 已提交
266
        // 公共参数
D
devil 已提交
267
        $this->assign('params', $this->data_request);
D
devil_gong 已提交
268

D
devil 已提交
269 270 271
        // 价格符号
        $this->assign('price_symbol', config('shopxo.price_symbol'));

D
devil_gong 已提交
272 273 274
        // 站点类型
        $this->assign('common_site_type', MyC('common_site_type', 0, true));

D
devil_gong 已提交
275 276 277
        // 预约模式
        $this->assign('common_order_is_booking', MyC('common_order_is_booking', 0, true));

D
1.5  
devil_gong 已提交
278 279 280 281
        // 商店信息
        $this->assign('common_customer_store_tel', MyC('common_customer_store_tel'));
        $this->assign('common_customer_store_email', MyC('common_customer_store_email'));
        $this->assign('common_customer_store_address', MyC('common_customer_store_address'));
D
devil_gong 已提交
282
        $this->assign('common_customer_store_qrcode', AttachmentPathViewHandle(MyC('common_customer_store_qrcode')));
D
1.5  
devil_gong 已提交
283
        
D
v1.2.0  
devil_gong 已提交
284 285 286 287
        // 主题
        $default_theme = strtolower(MyC('common_default_theme', 'default', true));
        $this->assign('default_theme', $default_theme);

288
        // 当前操作名称, 兼容插件模块名称
G
gongfuxiang 已提交
289 290 291
        $module_name = strtolower(request()->module());
        $controller_name = strtolower(request()->controller());
        $action_name = strtolower(request()->action());
D
v1.2.0  
devil_gong 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

        // 当前操作名称
        $this->assign('module_name', $module_name);
        $this->assign('controller_name', $controller_name);
        $this->assign('action_name', $action_name);

        // 控制器静态文件状态css,js
        $module_css = $module_name.DS.$default_theme.DS.'css'.DS.$controller_name;
        $module_css .= file_exists(ROOT_PATH.'static'.DS.$module_css.'.'.$action_name.'.css') ? '.'.$action_name.'.css' : '.css';
        $this->assign('module_css', file_exists(ROOT_PATH.'static'.DS.$module_css) ? $module_css : '');

        $module_js = $module_name.DS.$default_theme.DS.'js'.DS.$controller_name;
        $module_js .= file_exists(ROOT_PATH.'static'.DS.$module_js.'.'.$action_name.'.js') ? '.'.$action_name.'.js' : '.js';
        $this->assign('module_js', file_exists(ROOT_PATH.'static'.DS.$module_js) ? $module_js : '');

        // 导航
        $this->assign('nav_header', $this->nav_header);
        $this->assign('nav_footer', $this->nav_footer);

        // 导航/底部默认显示
        $this->assign('is_header', 1);
        $this->assign('is_footer', 1);

        // 图片host地址
316
        $this->assign('attachment_host', config('shopxo.attachment_host'));
D
v1.2.0  
devil_gong 已提交
317

D
devil_gong 已提交
318
        // seo
D
v1.2.0  
devil_gong 已提交
319
        $this->assign('home_seo_site_title', MyC('home_seo_site_title'));
D
devil_gong 已提交
320 321
        $this->assign('home_seo_site_keywords', MyC('home_seo_site_keywords'));
        $this->assign('home_seo_site_description', MyC('home_seo_site_description'));
D
v1.2.0  
devil_gong 已提交
322 323 324 325 326 327 328 329 330 331

        // 页面最大宽度
        $max_width = MyC('home_content_max_width', 0, true);
        $max_width_style = ($max_width == 0) ? '' : 'max-width:'.$max_width.'px;';
        $this->assign('max_width_style', $max_width_style);

        // 用户数据
        $this->assign('user', $this->user);

        // 用户中心菜单
D
devil_gong 已提交
332
        $this->assign('user_left_menu', NavigationService::UsersCenterLeftList());
D
v1.2.0  
devil_gong 已提交
333 334

        // 商品大分类
G
gongfuxiang 已提交
335
        $this->assign('goods_category_list', GoodsService::GoodsCategoryAll());
D
v1.2.0  
devil_gong 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349

        // 搜索框下热门关键字
        $home_search_keywords = [];
        switch(intval(MyC('home_search_keywords_type', 0)))
        {
            case 1 :
                $home_search_keywords = SearchService::SearchKeywordsList();
                break;
            case 2 :
                $home_search_keywords = explode(',', MyC('home_search_keywords'));
                break;
        }
        $this->assign('home_search_keywords', $home_search_keywords);

D
devil_gong 已提交
350
        // 友情链接
D
devil_gong 已提交
351
        $link = LinkService::LinkList(['where'=>['is_enable'=>1]]);
D
devil_gong 已提交
352
        $this->assign('link_list', $link['data']);
G
1.3.0  
gongfuxiang 已提交
353 354 355

        // 开发模式
        $this->assign('shopxo_is_develop', config('shopxo.is_develop'));
D
devil_gong 已提交
356 357 358

        // 顶部右侧导航
        $this->assign('common_nav_top_right_list', NavigationService::HomeHavTopRight(['user'=>$this->user]));
G
gongfuxiang 已提交
359

D
devil_gong 已提交
360 361 362
        // 底部导航
        $this->assign('common_bottom_nav_list', NavigationService::BottomNavigation(['user'=>$this->user]));

G
gongfuxiang 已提交
363
        // 编辑器文件存放地址
D
devil_gong 已提交
364
        $this->assign('editor_path_type', empty($this->user['id']) ? 'public' : 'user-'.$this->user['id']);
D
Devil 已提交
365 366 367 368 369

        // 备案信息
        $this->assign('home_site_icp', MyC('home_site_icp'));
        $this->assign('home_site_security_record_name', MyC('home_site_security_record_name'));
        $this->assign('home_site_security_record_url', MyC('home_site_security_record_url'));
D
devil_gong 已提交
370 371 372

        // 默认不加载百度地图api
        $this->assign('is_load_baidu_map_api', 0);
D
v1.2.0  
devil_gong 已提交
373 374
    }

D
devil 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    /**
     * 动态表格初始化
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2020-06-02
     * @desc    description
     */
    public function FormTableInit()
    {
        // 获取表格模型
        $module = FormModulePath($this->data_request);
        if(!empty($module))
        {
            // 调用表格处理
            $res = (new FormHandleModule())->Run($module, $this->data_request);
            if($res['code'] == 0)
            {
                $this->form_table = $res['data']['table'];
                $this->form_where = $res['data']['where'];
                $this->form_params = $res['data']['params'];

                $this->assign('form_table', $this->form_table);
                $this->assign('form_params', $this->form_params);
            } else {
                $this->form_error = $res['msg'];
                $this->assign('form_error', $this->form_error);
            }
        }
    }

D
v1.2.0  
devil_gong 已提交
406 407 408 409 410 411 412 413 414
    /**
     * [NavInit 导航初始化]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-19T22:41:20+0800
     */
    private function NavInit()
    {
D
devil_gong 已提交
415
        $navigation = NavigationService::Nav();
D
v1.2.0  
devil_gong 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
        $this->nav_header = $navigation['header'];
        $this->nav_footer = $navigation['footer'];
    }

    /**
     * [_empty 空方法操作]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-02-25T15:47:50+0800
     * @param    [string]      $name [方法名称]
     */
    public function _empty($name)
    {
        if(IS_AJAX)
        {
            exit(json_encode(DataReturn($name.' 非法访问', -1000)));
        } else {
            $this->assign('msg', $name.' 非法访问');
G
gongfuxiang 已提交
435
            return $this->fetch('public/tips_error');
D
v1.2.0  
devil_gong 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
        }
    }

    /**
     * [SiteStstusCheck 站点状态校验]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-02-25T21:43:07+0800
     */
    private function SiteStstusCheck()
    {
        if(MyC('home_site_state') != 1)
        {
            // 是否ajax请求
            if(IS_AJAX)
            {
D
devil 已提交
453
                exit(json_encode(DataReturn(MyC('home_site_close_reason', '网站维护中...'), -10000)));
D
v1.2.0  
devil_gong 已提交
454
            } else {
D
devil 已提交
455
                exit('<div style="text-align: center;margin-top: 15%;font-size: 18px;color: #f00;">'.MyC('home_site_close_reason', '网站维护中...', true).'</div>');
D
v1.2.0  
devil_gong 已提交
456 457 458 459 460
            }
        }
    }
}
?>