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
<?php
 
namespace app\operations\model\plus\region;
 
use app\common\model\plus\operations\UserLogin as UserLoginModel;
 
/**
 * 区域代理用户模型
 */
class UserLogin extends UserLoginModel
{
    public function getList($params)
    {
        return $this->with(['roles.role'])->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate($params);
    }
 
    /**
     * 获取所有角色
     */
    private function getAll()
    {
        $data = $this->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
        return $data ? $data->toArray() : [];
    }
 
    public function add($data)
    {
        $this->startTrans();
        try {
            $arr = [
                'user_name' => trim($data['user_name']),
                'password' => salt_hash($data['password']),
                'real_name' => trim($data['real_name']),
                'mobile' => $data['mobile'] ?? '',
                'user_id' => session('jjjshop_operations')['user']['user_id'],
                'app_id' => self::$app_id,
                'create_time' => time(),
                'update_time' => time()
            ];
 
            self::create($arr);
            // 支持role_id或access_id字段
            $roleIds = isset($data['role_id']) ? $data['role_id'] : (isset($data['access_id']) ? $data['access_id'] : []);
            if (!empty($roleIds)) {
                $this->addRole($roleIds);
            }
            
            // 事务提交
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    public function getUserName($where, $operations_user_id = 0)
    {
        if ($operations_user_id > 0) {
            return $this->where($where)->where('operations_user_id', '<>', $operations_user_id)->count();
        }
        return $this->where($where)->count();
    }
 
    public function edit($data)
    {
        $this->startTrans();
        try {
            $arr = [
                'user_name' => $data['user_name'],
                'real_name' => $data['real_name'],
                'mobile' => $data['mobile'] ?? '',
                'update_time' => time()
            ];
            
            if (!empty($data['password'])) {
                $arr['password'] = salt_hash($data['password']);
            }
            $this->save($arr);
            // 支持role_id或access_id字段
            $roleIds = isset($data['role_ids']) ? $data['role_ids'] : (isset($data['access_id']) ? $data['access_id'] : []);
            if (!empty($roleIds)) {
                $this->addRole($roleIds);
            }
            
            // 事务提交
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
    public function del($where)
    {
        self::update(['is_delete' => 1, 'update_time' => time()], $where);
        return UserRole::where('operations_user_id', '=', $where['user_id'])->delete();
    }
}