quanwei
2025-11-01 121b714d710cf3c865f4a1b5efe81abec11056d1
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
166
167
168
169
170
171
172
173
174
175
<?php
 
namespace app\branch\model\auth;
 
use app\common\model\branch\User as BranchUserModel;
use app\common\model\user\User as UserModel;
 
/**
 * 角色模型
 */
class User extends BranchUserModel
{
 
    public function getList($limit = 20,$branch_id)
    {
        return $this->with(['userRole.role'])->where('is_delete', '=', 0)
                    ->where('branch_id','=',$branch_id)
                    ->order(['create_time' => 'desc'])
                    ->paginate($limit);
    }
 
    /**
     * 获取所有上级id集
     */
    public function getTopRoleIds($role_id, &$all = null)
    {
        static $ids = [];
        is_null($all) && $all = $this->getAll();
        foreach ($all as $item) {
            if ($item['role_id'] == $role_id && $item['parent_id'] > 0) {
                $ids[] = $item['parent_id'];
                $this->getTopRoleIds($item['parent_id'], $all);
            }
        }
        return $ids;
    }
 
    /**
     * 获取所有角色
     */
    private function getAll()
    {
        $data = $this->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
        return $data ? $data->toArray() : [];
    }
 
    public function add($data)
    {
        $this->startTrans();
        try {
            // 用户是否已绑定
            $user = null;
            if($data['user_id'] > 0){
                $user = UserModel::detail($data['user_id']);
                if($user['user_type'] != 1){
                    $this->error = '该用户已绑定,或绑定的商户正在审核';
                    return false;
                }
            }
            $arr = [
                'user_name' => trim($data['user_name']),
                'password' => salt_hash($data['password']),
                'real_name' => trim($data['real_name']),
                'user_id' => $data['user_id'],
                'app_id' => self::$app_id,
                'branch_id' => $data['branch_id']
            ];
            $res = self::create($arr);
            $add_arr = [];
            $model = new UserRole();
            foreach ($data['access_id'] as $val) {
                $add_arr[] = [
                    'branch_user_id' => $res['branch_user_id'],
                    'role_id' => $val,
                    'app_id' => self::$app_id,
                ];
            }
            $model->saveAll($add_arr);
            // 后台添加的直接算审核通过
            if($user){
                $user->save([
                    'user_type' => 2
                ]);
            }
            // 事务提交
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
 
    }
 
    public function getUserName($where)
    {
        return $this->where($where)->count();
    }
 
 
    public function edit($data)
    {
        $this->startTrans();
        try {
            // 用户是否已绑定
            $user = null;
            $old_user_id = 0;
            if($this['user']){
                $old_user_id = $this['user']['user_id'];
            }
            $userChange = false;
            if($this['user'] && $data['user_id'] > 0 && $data['user_id'] != $this['user']['user_id']){
                $user = UserModel::detail($data['user_id']);
                if($user['user_type'] != 1){
                    $this->error = '该用户已绑定,或绑定的商户正在审核';
                    return false;
                }
                $userChange = true;
            }
 
            $arr = [
                'user_name' => $data['user_name'],
                'user_id' => $data['user_id'],
                'real_name' => $data['real_name'],
            ];
            if (!empty($data['password'])) {
                $arr['password'] = salt_hash($data['password']);
            }
            $this->save($arr);
 
            $model = new UserRole();
            $where['branch_user_id'] = $data['branch_user_id'];
            UserRole::destroy($where);
            $add_arr = [];
            foreach ($data['access_id'] as $val) {
                $add_arr[] = [
                    'branch_user_id' => $data['branch_user_id'],
                    'role_id' => $val,
                    'app_id' => self::$app_id
                ];
            }
            $model->saveAll($add_arr);
            // 后台添加的直接算审核通过
            if($userChange){
                $user->save([
                    'user_type' => 2
                ]);
                //取消原来的
                if ($old_user_id > 0){
                    (new UserModel())->where('user_id', '=', $old_user_id)->update([
                        'user_type' => 1
                    ]);
                }
            }
            // 事务提交
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    public function getChild($where)
    {
        return $this->where($where)->count();
    }
 
    public function del($where)
    {
        return self::update(['is_delete' => 1], $where);
    }
}