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
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
<?php
 
namespace app\common\model\branch;
 
use app\common\model\BaseModel;
 
/**
 * 分会管理员模型
 */
class User extends BaseModel
{
    protected $name = 'branch_user';
    protected $pk = 'branch_user_id';
 
    /**
     * 关联应用表
     */
    public function app()
    {
        return $this->belongsTo('app\\common\\model\\app\\App', 'app_id', 'app_id');
    }
 
    /**
     * 关联用户角色表表
     */
    public function role()
    {
        return $this->belongsToMany('app\\common\\model\\auth\\Role', 'app\\common\\model\\auth\\UserRole');
    }
 
    public function userRole()
    {
        return $this->hasMany('app\\common\\model\\branch\\UserRole', 'branch_user_id', 'branch_user_id');
    }
 
    /**
     * 关联应用表
     */
    public function user()
    {
        return $this->belongsTo('app\\common\\model\\user\\User', 'user_id', 'user_id');
    }
 
    /**
     * 验证用户名是否重复
     */
    public static function checkExist($user_name)
    {
        return !!static::withoutGlobalScope()
            ->where('user_name', '=', $user_name)
            ->value('branch_user_id');
    }
 
    /**
     * 分会用户详情
     */
    public static function detail($where, $with = [])
    {
        !is_array($where) && $where = ['branch_user_id' => (int)$where];
        return (new static())->where(array_merge(['is_delete' => 0], $where))->with($with)->find();
    }
 
    /**
     * 分会用户详情
     */
    public static function detailByUserId($user_id)
    {
        return (new static())->where('is_delete', '=', 0)
            ->where('user_id', '=', $user_id)
            ->where('branch_id', '>', 0)
            ->find();
    }
    /**
     * 保存登录状态
     */
    public function loginState($user)
    {
        $app = $user['app'];
        // 保存登录状态
        $session = array(
            'user' => [
                'branch_user_id' => $user['branch_user_id'],
                'user_name' => $user['user_name'],
                'branch_id' => $user['branch_id'],
                'app_id' => $user['app_id'],
                'user_id' => $user['user_id'],
                'branch_name' => $user['branch_name'],
                'branch_type' => $user['branch_type'],
            ],
            'app' => $app->toArray(),
            'is_login' => true,
        );
        session('jjjshop_branch', $session);
    }
 
    /**
     * 是否是管理员
     */
    public static function isManager($user_id)
    {
        $user = (new static())->where('user_id', '=', $user_id)->order('is_delete')->find();
        return !!$user && !$user['is_delete'];
    }
 
}