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
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\common\model\plus\operations;
 
use app\common\model\BaseModel;
 
/**
 * 运营中心管理员模型
 */
class User extends BaseModel
{
    protected $name = 'operations_user';
    protected $pk = 'operations_user_id';
 
    /**
     * 关联角色
     * @return \think\model\relation\HasMany
     */
    public function roles()
    {
        return $this->hasMany('UserRole', 'operations_user_id', 'operations_user_id');
    }
 
    /**
     * 获取管理员列表
     */
    public function getList($params)
    {
        $model = $this->alias('user')
            ->field('user.*')
            ->where('user.is_delete', '=', 0);
 
        // 搜索条件
        if (!empty($params['search'])) {
            $model = $model->where('user.user_name|user.real_name', 'like', '%' . $params['search'] . '%');
        }
 
        $list = $model->order(['user.operations_user_id' => 'desc'])
            ->paginate($params['limit'] ?? 15);
 
        return $list;
    }
 
    /**
     * 获取详情
     */
    public static function detail($where, $with = [])
    {
        if(is_array($where)){
            return (new static())->where($where)->with($with)->find();
        } else{
            return (new static())->where('operations_user_id', '=', $where)->with($with)->find();
        }
    }
 
    /**
     * 添加管理员
     */
    public function add($data)
    {
        $this->startTrans();
        try {
            // 检查用户名是否已存在
            $exists = $this->where('user_name', '=', $data['user_name'])->find();
            if ($exists) {
                $this->error = '用户名已存在';
                return false;
            }
 
            // 准备数据
            $saveData = [
                'user_name' => $data['user_name'],
                'password' => password_hash($data['password'], PASSWORD_DEFAULT),
                'real_name' => $data['real_name'],
                'real_password' => $data['password'] ?? '',
                'is_super' => $data['is_super'] ?? 0,
                'is_delete' => 0,
                'user_id' => $data['user_id'] ?? 0,
                'app_id' => self::$app_id,
                'create_time' => time(),
                'update_time' => time(),
                'client_id' => 0
            ];
 
            $res = $this->save($saveData);
 
            // 保存角色关系
            if (isset($data['role_id']) && !empty($data['role_id'])) {
                $this->addRole($data['role_id']);
            }
 
            $this->commit();
            return $res !== false;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 编辑管理员
     */
    public function edit($data)
    {
        $saveData = [
            'real_name' => $data['real_name'],
            'update_time' => time()
        ];
 
        // 如果修改了密码
        if (!empty($data['password'])) {
            $saveData['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
            $saveData['real_password'] = $data['password'];
        }
 
        // 如果有角色ID
        if (isset($data['role_ids'])) {
           $this->addRole($data['role_ids']);
        }
 
        return $this->save($saveData) !== false;
    }
 
    public function addRole($data)
    {
        $this->roles()->where('operations_user_id', '=', $this->operations_user_id)->delete();
        $list = array_map(function($val) use ($data) {
            return [
                'role_id' => $val,
                'app_id' => self::$app_id
            ];
        }, $data);
        $this->roles()->saveAll($list);
    }
    /**
     * 删除管理员
     */
    public function setDelete()
    {
        return $this->save([
            'is_delete' => 1,
            'update_time' => time()
        ]);
    }
 
    /**
     * 验证登录
     */
    public static function login($userName, $password)
    {
        $user = self::where('user_name', '=', $userName)
            ->where('is_delete', '=', 0)
            ->find();
        if (!$user) {
            return false;
        }
 
        if (salt_hash($password) != $user['password']) {
            return false;
        }
 
        return $user;
    }
}