| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 | <?php// +—————————————————————————————————————————————————————————————————————// | Created by Yunbao// +—————————————————————————————————————————————————————————————————————// | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.// +—————————————————————————————————————————————————————————————————————// | Author: https://gitee.com/yunbaokeji// +—————————————————————————————————————————————————————————————————————// | Date: 2022-04-30// +—————————————————————————————————————————————————————————————————————namespace app\admin\controller;use cmf\controller\AdminBaseController;use think\Db;use think\db\Query;/** * Class UserController * @package app\admin\controller * @adminMenuRoot( *     'name'   => '管理组', *     'action' => 'default', *     'parent' => 'user/AdminIndex/default', *     'display'=> true, *     'order'  => 10000, *     'icon'   => '', *     'remark' => '管理组' * ) */class UserController extends AdminBaseController{    /**     * 管理员列表     * @adminMenu(     *     'name'   => '管理员',     *     'parent' => 'default',     *     'display'=> true,     *     'hasView'=> true,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '管理员管理',     *     'param'  => ''     * )     * @throws \think\exception\DbException     */    public function index(){        $content = hook_one('admin_user_index_view');        if (!empty($content)) {            return $content;        }        /**搜索条件**/        $userLogin = $this->request->param('user_login');        $userEmail = trim($this->request->param('user_email'));        $users = Db::name('user')            ->where('user_type', 1)            ->where(function (Query $query) use ($userLogin, $userEmail) {                if ($userLogin) {                    $query->where('user_login', 'like', "%$userLogin%");                }                if ($userEmail) {                    $query->where('user_email', 'like', "%$userEmail%");                }            })            ->order("id DESC")            ->paginate(10);        $users->appends(['user_login' => $userLogin, 'user_email' => $userEmail]);        // 获取分页显示        $page = $users->render();        $rolesSrc = Db::name('role')->select();        $roles    = [];        foreach ($rolesSrc as $r) {            $roleId           = $r['id'];            $roles["$roleId"] = $r;        }        $this->assign("page", $page);        $this->assign("roles", $roles);        $this->assign("users", $users);        return $this->fetch();    }    /**     * 管理员添加     * @adminMenu(     *     'name'   => '管理员添加',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> true,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '管理员添加',     *     'param'  => ''     * )     */    public function add(){        $content = hook_one('admin_user_add_view');        if (!empty($content)) {            return $content;        }        $roles = Db::name('role')->where('status', 1)->order("id DESC")->select();        $this->assign("roles", $roles);        return $this->fetch();    }    /**     * 管理员添加提交     * @adminMenu(     *     'name'   => '管理员添加提交',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> false,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '管理员添加提交',     *     'param'  => ''     * )     */    public function addPost(){        if ($this->request->isPost()) {            if (!empty($_POST['role_id']) && is_array($_POST['role_id'])) {                $role_ids = $_POST['role_id'];                unset($_POST['role_id']);                $result = $this->validate($this->request->param(), 'User');                if ($result !== true) {                    $this->error($result);                } else {                    $_POST['user_pass'] = cmf_password($_POST['user_pass']);                    $result             = DB::name('user')->insertGetId($_POST);                    if ($result !== false) {                        foreach ($role_ids as $role_id) {                            if (cmf_get_current_admin_id() != 1 && $role_id == 1) {                                $this->error("为了网站的安全,非网站创建者不可创建超级管理员!");                            }                            Db::name('RoleUser')->insert(["role_id" => $role_id, "user_id" => $result]);                        }                        $this->success("添加成功!", url("user/index"));                    } else {                        $this->error("添加失败!");                    }                }            } else {                $this->error("请为此用户指定角色!");            }        }    }    /**     * 管理员编辑     * @adminMenu(     *     'name'   => '管理员编辑',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> true,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '管理员编辑',     *     'param'  => ''     * )     */    public function edit(){        $content = hook_one('admin_user_edit_view');        if (!empty($content)) {            return $content;        }        $id    = $this->request->param('id', 0, 'intval');        $roles = DB::name('role')->where('status', 1)->order("id DESC")->select();        $this->assign("roles", $roles);        $role_ids = DB::name('RoleUser')->where("user_id", $id)->column("role_id");        $this->assign("role_ids", $role_ids);        $user = DB::name('user')->where("id", $id)->find();        $this->assign($user);        return $this->fetch();    }    /**     * 管理员编辑提交     * @adminMenu(     *     'name'   => '管理员编辑提交',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> false,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '管理员编辑提交',     *     'param'  => ''     * )     */    public function editPost(){        if ($this->request->isPost()) {            if (!empty($_POST['role_id']) && is_array($_POST['role_id'])) {                if (empty($_POST['user_pass'])) {                    unset($_POST['user_pass']);                } else {                    $_POST['user_pass'] = cmf_password($_POST['user_pass']);                }                $role_ids = $this->request->param('role_id/a');                unset($_POST['role_id']);                $result = $this->validate($this->request->param(), 'User.edit');                if ($result !== true) {                    // 验证失败 输出错误信息                    $this->error($result);                } else {                    $result = DB::name('user')->update($_POST);                    if ($result !== false) {                        $uid = $this->request->param('id', 0, 'intval');                        DB::name("RoleUser")->where("user_id", $uid)->delete();                        foreach ($role_ids as $role_id) {                            if (cmf_get_current_admin_id() != 1 && $role_id == 1) {                                $this->error("为了网站的安全,非网站创建者不可创建超级管理员!");                            }                            DB::name("RoleUser")->insert(["role_id" => $role_id, "user_id" => $uid]);                        }                        $this->success("保存成功!");                    } else {                        $this->error("保存失败!");                    }                }            } else {                $this->error("请为此用户指定角色!");            }        }    }    /**     * 管理员个人信息修改     * @adminMenu(     *     'name'   => '个人信息',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> true,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '管理员个人信息修改',     *     'param'  => ''     * )     */    public function userInfo(){        $id   = cmf_get_current_admin_id();        $user = Db::name('user')->where("id", $id)->find();        $this->assign($user);        return $this->fetch();    }    /**     * 管理员个人信息修改提交     * @adminMenu(     *     'name'   => '管理员个人信息修改提交',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> false,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '管理员个人信息修改提交',     *     'param'  => ''     * )     */    public function userInfoPost(){        if ($this->request->isPost()) {            $data             = $this->request->post();            $data['birthday'] = strtotime($data['birthday']);            $data['id']       = cmf_get_current_admin_id();            $create_result    = Db::name('user')->update($data);;            if ($create_result !== false) {                $this->success("保存成功!");            } else {                $this->error("保存失败!");            }        }    }    /**     * 管理员删除     * @adminMenu(     *     'name'   => '管理员删除',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> false,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '管理员删除',     *     'param'  => ''     * )     */    public function delete(){        $id = $this->request->param('id', 0, 'intval');        if ($id == 1) {            $this->error("最高管理员不能删除!");        }        if (Db::name('user')->delete($id) !== false) {            Db::name("RoleUser")->where("user_id", $id)->delete();            $this->success("删除成功!");        } else {            $this->error("删除失败!");        }    }    /**     * 停用管理员     * @adminMenu(     *     'name'   => '停用管理员',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> false,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '停用管理员',     *     'param'  => ''     * )     */    public function ban(){        $id = $this->request->param('id', 0, 'intval');        if (!empty($id)) {            $result = Db::name('user')->where(["id" => $id, "user_type" => 1])->setField('user_status', '0');            if ($result !== false) {                $this->success("管理员停用成功!", url("user/index"));            } else {                $this->error('管理员停用失败!');            }        } else {            $this->error('数据传入失败!');        }    }    /**     * 启用管理员     * @adminMenu(     *     'name'   => '启用管理员',     *     'parent' => 'index',     *     'display'=> false,     *     'hasView'=> false,     *     'order'  => 10000,     *     'icon'   => '',     *     'remark' => '启用管理员',     *     'param'  => ''     * )     */    public function cancelBan(){        $id = $this->request->param('id', 0, 'intval');        if (!empty($id)) {            $result = Db::name('user')->where(["id" => $id, "user_type" => 1])->setField('user_status', '1');            if ($result !== false) {                $this->success("管理员启用成功!", url("user/index"));            } else {                $this->error('管理员启用失败!');            }        } else {            $this->error('数据传入失败!');        }    }}
 |