quanwei
2025-12-25 a47b138c7455dee981af9b4fac431a16c0eee675
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
 
namespace app\shop\controller;
 
use app\common\exception\BaseException;
use app\common\model\settings\Setting;
use app\common\model\shop\OptLog as OptLogModel;
use app\JjjController;
use app\shop\service\AuthService;
use app\common\model\supplier\Supplier as SupplierModel;
 
/**
 * 商户后台控制器基类
 */
class Controller extends JjjController
{
    /** @var array $store 商家登录信息 */
    protected $store;
 
    /** @var string $route 当前控制器名称 */
    protected $controller = '';
 
    /** @var string $route 当前方法名称 */
    protected $action = '';
 
    /** @var string $route 当前路由uri */
    protected $routeUri = '';
 
    /** @var string $route 当前路由:分组名称 */
    protected $group = '';
 
    /** @var string $route 当前路由:分组名称 */
    protected $menu = '';
    /** @var array $allowAllAction 登录验证白名单 */
    protected $allowAllAction = [
        // 登录页面
        '/passport/login',
        /*登录信息*/
        '/index/base'
    ];
 
    /**
     * 后台初始化
     */
    public function initialize()
    {
        // 商家登录信息
        $this->store = session('jjjshop_store');
        // 当前路由信息
        $this->getRouteinfo();
        //  验证登录状态
        $this->checkLogin();
        // 写入操作日志
        $this->saveOptLog();
        // 验证当前页面权限
        $this->checkPrivilege();
    }
 
    /**
     * 验证当前页面权限
     */
    private function checkPrivilege()
    {
        if($this->store == null){
            return false;
        }
        if (!AuthService::getInstance()->checkPrivilege($this->routeUri)) {
            throw new BaseException(['msg' => '很抱歉,没有访问权限']);
        }
        return true;
    }
 
    /**
     * 解析当前路由参数 (分组名称、控制器名称、方法名)
     */
    protected function getRouteinfo()
    {
        // 控制器名称
        $this->controller = strtolower($this->request->controller());
        $this->controller = str_replace(".","/",$this->controller);
        // 方法名称
        $this->action = Request()->action();
        // 控制器分组 (用于定义所属模块)
        $groupstr = strstr($this->controller, '.', true);
        $this->group = $groupstr !== false ? $groupstr : $this->controller;
        // 当前uri
        $this->routeUri =  '/' . $this->controller . '/' . $this->action;
    }
 
    /**
     * 验证登录状态
     */
    private function checkLogin()
    {
        // 验证当前请求是否在白名单
        if (in_array($this->routeUri, $this->allowAllAction)) {
            return true;
        }
        // 验证登录状态
        if ($this->store != null && $this->store['is_login'] == 1) {
            return true;
        }
        $url = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER['SERVER_NAME']."/shop";
        Header("Location:$url");
        //throw new BaseException(['code' => -1, 'msg' => 'not_login']);
    }
 
    /**
     * 操作日志
     */
    private function saveOptLog(){
        if($this->store == null){
            return;
        }
        $shop_user_id = $this->store['user']['shop_user_id'];
        if(!$shop_user_id){
            return;
        }
        // 如果不记录查询日志
        $config = Setting::getItem('store');
        if(!$config || !$config['is_get_log']){
            return;
        }
        $model = new OptLogModel();
        $model->save([
            'shop_user_id' => $shop_user_id,
            'ip' => \request()->ip(),
            'request_type' => $this->request->isGet()?'Get':'Post',
            'url' => $this->routeUri,
            'content' => json_encode($this->request->param(), JSON_UNESCAPED_UNICODE),
            'browser' => get_client_browser(),
            'agent' => $_SERVER['HTTP_USER_AGENT'],
            'title' => AuthService::getAccessNameByPath($this->routeUri, $this->store['app']['app_id']),
            'app_id' => $this->store['app']['app_id']
        ]);
    }
 
    /**
     * 获取自营供应商id
     */
    protected function getSupplierId(){
        return SupplierModel::getSupplierSelfSupport();
    }
 
}