chengkun
2025-09-12 b21e53f16f228d3192eb54178f081395878b2406
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
176
177
178
179
180
181
182
183
184
185
186
<?php
 
namespace app\admin\controller;
 
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
 
class Administrators extends Common {
 
    public function index() {
        ////////
        View::assign('menuitem', strtolower('administrators-index'));
 
        $other['title'] = '管理员';
        $guide['one']['text'] = '安全中心';
        $guide['two']['text'] = '管理员';
        $guide['two']['url'] = url('/admin/administrators/index')->build();
        View::assign('guide', $guide);
 
 
        View::assign('other', $other);
        return View::fetch('index');
    }
 
    public function adminlist() {
        if (Request::isPost()) {
            $kw = input('kw');
            $condition = [];
            if (!empty($kw)) {
                $condition[] = array('user_name|real_name', 'like', "%$kw%");
            }
            $p = input('p', 1);
            $_GET['p'] = $p;
            $other['page_size'] = input('page_size', 20);
            $order = 'initialize desc,id asc';
            $count = Db::name('administrators')->where($condition)->count();
            $list = Db::name('administrators')->field("id,user_name,real_name,ban_access,phone,initialize,login_time,login_ip")->where($condition)->order($order)->page($p, $other['page_size'])->select()->toArray();
            $other['count'] = $count;
            if (!$list) {
                $list = [];
            }
            $other['admin_info'] = $this->cinfo;
            $d['code'] = 200;
            $d['list'] = $list;
            $d['other'] = $other;
            return $d;
        }
    }
 
    public function update_ban_access() {
        header('Content-Type:text/html;charset=utf-8');
        if (Request::isPost()) {
 
            $id = input('id');
            $sub_ban_access = input('ban_access');
            $info = Db::name('administrators')->field('id')->where('id', $id)->find();
            if (empty($info)) {
                $d['code'] = 400;
                $d['message'] = '信息不存在';
                return $d;
            }
            ///
 
            if (is_numeric($id)) {
                if ($sub_ban_access == '1') {
                    $dd['ban_access'] = 1;
                    $d['ban_access'] = 1;
                    $d['message'] = '允许登录';
                } else {
                    $dd['ban_access'] = 0;
                    $d['ban_access'] = 0;
                    $d['message'] = '禁止登录';
                }
                $backval = Db::name('administrators')->where('id', $id)->save($dd);
                if ($backval === FALSE) {
                    $d['code'] = 400;
                } else {
                    $d['code'] = 200;
                }
            } else {
                $d['code'] = 400;
            }
        } else {
            $d['code'] = 400;
        }
        return $d;
    }
 
 
    public function save_admin() {
        if (Request::isPost()) {
            header('Content-Type:text/html;charset=utf-8');
            $data = Request::post();
            $id = input('post.id');
            $password = input('pwd');
            if (empty($password)) {
            } else {
                $data['password'] = joinmd5($password);
            }
 
            if (is_numeric($id)) {//更新数据
                /////验证密码复杂度///////////
                $validate = new \app\admin\validate\Administrators();
                $result = $validate->scene('edit')->check($data);
                if (!$result) {
                    $d['code'] = 400;
                    $d['message'] = $validate->getError();
                    return $d;
                }
 
                $backval = Db::name('administrators')->where('id', $id)->withoutField('id')->save($data);
                if ($backval === false) {
                    $d['code'] = 400;
                    $d['message'] = '编辑失败';
                } else {
                    ////增加系统操作记录//
                    $d['code'] = 200;
                    $d['message'] = '编辑成功';
                }
            } else {
                unset($data['id']);
                $data['add_time'] = time();
                $data['start_time'] = time();
                $data['end_time'] = time() + 3600 * 24 * 7;
                /////验证试卷名称是否存在///////////
                $validate = new \app\admin\validate\Administrators();
                $result = $validate->check($data);
                if (!$result) {
                    $d['code'] = 400;
                    $d['message'] = $validate->getError();
                    return $d;
                }
                //添加数据
                $result = Db::name('administrators')->insertGetId($data);
                if ($result !== false) {
                    $d['code'] = 200;
                    $d['message'] = '添加成功';
                } else {
                    $d['code'] = 400;
                    $d['message'] = '添加失败';
                }
            }
        } else {
            $d['code'] = 400;
            $d['message'] = '非法请求';
        }
        return $d;
    }
 
    public function delete_admin() {
        if (Request::isPost()) {
            $id = input('id');
            if (is_numeric($id)) {
                $condition['id'] = $id;
                $info = Db::name('administrators')->field('id,initialize')->where($condition)->find();
                if ($info) {
                    ///////////////////
                    if ($info['initialize'] == '1') {
                        $d['code'] = 400;
                        $d['message'] = '该账号不能删除!';
                        return $d;
                    }
                    if ($this->cinfo['initialize'] == 1 || $this->admin_id == $info['id']) {
                        Db::name('administrators')->where('id', $id)->delete();
                        /////////////////////////////
                        $d['code'] = 200;
                        $d['message'] = '删除成功!';
                    } else {
                        $d['code'] = 400;
                        $d['message'] = '您没有删除权限';
                    }
                } else {
                    $d['code'] = 400;
                    $d['message'] = '子账号不存在';
                }
            } else {
                $d['code'] = 400;
                $d['message'] = '参数有误!';
            }
            return $d;
        }
    }
 
 
}