quanwei
2025-12-05 feda780069d64479c0c20493603717e100655da9
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
 
namespace app\branch\service;
 
 
use think\facade\Cache;
use think\facade\Session;
use app\branch\model\auth\User;
use app\branch\model\auth\UserRole;
use app\branch\model\auth\RoleAccess;
use app\branch\model\branch\Access;
/**
 * 商家后台权限业务
 */
class AuthService
{
    // 存放实例
    static public $instance;
 
    // 商家登录信息
    private $store;
 
    // 商家用户信息
    private $user;
 
    // 权限验证白名单
    protected $allowAllAction = [
        // 用户登录
        '/passport/login',
        /*系统设置*/
        '/index/base',
        // 退出登录
        '/passport/logout',
        // 首页
        '/index/index',
        // 修改当前用户信息
        '/passport/editPass',
        // 图片上传
        '/file/file/*',
        '/file/upload/image',
        // 数据选择
        '/data/*',
        // 添加商品规格
        '/product/spec/*',
        // 用户信息
        '/auth/user/getUserInfo',
        // 角色列表
        '/auth/user/getRoleList',
        // 统计
        '/statistics/sales/order',
        '/statistics/sales/product',
        '/statistics/user/visit'
    ];
 
    /** @var array $accessUrls 商家用户权限url */
    private $accessUrls = [];
 
    /**
     * 公有化获取实例方法
     */
    public static function getInstance()
    {
        if (!(self::$instance instanceof AuthService)) {
            self::$instance = new self;
        }
        return self::$instance;
    }
 
    /**
     * 私有化构造方法
     */
    private function __construct()
    {
        // 商家登录信息
        $this->store = Session::get('jjjshop_branch');
        // 当前用户信息
        $this->user = User::detail($this->store['user']['branch_user_id']);
    }
 
    /**
     * 私有化克隆方法
     */
    private function __clone()
    {
    }
 
    /**
     * 验证指定url是否有访问权限
     */
    public function checkPrivilege($url, $strict = true)
    {
        if (!is_array($url)):
            return $this->checkAccess($url);
        else:
            foreach ($url as $val):
                if ($strict && !$this->checkAccess($val)) {
                    return false;
                }
                if (!$strict && $this->checkAccess($val)) {
                    return true;
                }
            endforeach;
        endif;
        return true;
    }
 
    /**
     * @param string $url
     */
    private function checkAccess($url)
    {
        // 超级管理员无需验证
        if ($this->user['is_super']) {
            return true;
        }
        // 验证当前请求是否在白名单
        if (in_array($url, $this->allowAllAction)) {
            return true;
        }
 
        // 通配符支持
        foreach ($this->allowAllAction as $action) {
            if (strpos($action, '*') !== false
                && preg_match('/^' . str_replace('/', '\/', $action) . '/', $url)
            ) {
                return true;
            }
        }
        // 获取当前用户的权限url列表
        if (!in_array($url, $this->getAccessUrls())) {
            return false;
        }
        return true;
    }
 
    /**
     * 获取当前用户的权限url列表
     */
    private function getAccessUrls()
    {
        if (empty($this->accessUrls)) {
            // 获取当前用户的角色集
            $roleIds = UserRole::getRoleIds($this->user['branch_user_id']);
            // 根据已分配的权限
            $accessIds = RoleAccess::getAccessIds($roleIds);
            // 获取当前角色所有权限链接
            $this->accessUrls = Access::getAccessUrls($accessIds);
        }
        return $this->accessUrls;
    }
 
    public static function getAccessNameByPath($path, $app_id){
        $arr = Cache::get('path_access_' . $app_id);
        if (!$arr) {
            // 查找访问资源
            $list = (new Access())->withoutGlobalScope()->field(['name', 'path'])->select();
            foreach ($list as $access){
                $arr[$access['path']] = $access['name'];
            }
            Cache::tag('cache')->set('path_access_' . $app_id, $arr, 3600);
        }
        $url = isset($arr[$path])?$arr[$path]:'';
        return $url;
    }
}