quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
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
<?php
 
namespace app\common\model\plus\operations;
 
use app\common\model\BaseModel;
 
/**
 * 运营中心角色权限关系模型
 */
class RoleAccess extends BaseModel
{
    protected $name = 'operations_role_access';
    protected $pk = 'id';
 
    /**
     * 获取用户的权限ID列表
     * @param int $userId 用户ID
     * @return array
     */
    public static function getUserAccessIds($userId)
    {
        // 通过用户ID获取角色,再通过角色获取权限ID
        $userRoles = UserRole::where('shop_user_id', '=', $userId)->column('role_id');
        
        if (empty($userRoles)) {
            return [];
        }
 
        return self::where('role_id', 'in', $userRoles)->column('access_id');
    }
 
    /**
     * 获取用户的所有角色
     * @param int $userId 用户ID
     * @return array
     */
    public static function getUserRoles($userId)
    {
        $roleIds = UserRole::where('shop_user_id', '=', $userId)->column('role_id');
        
        if (empty($roleIds)) {
            return [];
        }
 
        return Role::where('role_id', 'in', $roleIds)->select();
    }
 
    /**
     * 批量保存角色权限
     * @param int $roleId 角色ID
     * @param array $accessIds 权限ID数组
     * @return bool
     */
    public static function saveRoleAccess($roleId, $accessIds)
    {
        // 先删除旧的权限关系
        self::where('role_id', '=', $roleId)->delete();
 
        // 批量插入新的权限关系
        $data = [];
        $time = time();
        foreach ($accessIds as $accessId) {
            $data[] = [
                'role_id' => $roleId,
                'access_id' => $accessId,
                'app_id' => self::$app_id,
                'create_time' => $time,
                'client_id' => 0
            ];
        }
 
        if (!empty($data)) {
            (new self())->saveAll($data);
        }
 
        return true;
    }
}