model = new SysUserModel(); } public function index() { $data = $this->model->where('is_del', 0) ->field('id,username,role_id,truename,email,create_time,status') ->select(); $data_role = model('sys_role')->column('id, name'); $this->assign("data", $data); $this->assign("data_role", $data_role); return $this->fetch(); } public function save(Request $request = null, $id = 0) { if ($request->isPost()) { $param = $request->param(); if ($param['username'] =='' || $param['role_id'] =='') { $this->error("用户名 or 角色 不能为空!"); } try { if ($param['id'] != 0) { $this->model->save( [ 'username' => $param['username'], 'role_id' => intval($param['role_id']), 'truename' => $param['truename'], 'email' => $param['email'], 'note' => $param['note'], ], ['id'=>$id] ); } else { $password = empty($param['password']) ? 123456 : $param['password']; $this->model->save( [ 'username' => $param['username'], 'role_id' => intval($param['role_id']), 'password' => md5($password), 'truename' => $param['truename'], 'email' => $param['email'], 'note' => $param['note'], 'create_time' => time() ] ); } } catch (\Exception $e) { $msg = $e->getMessage(); $this->error("错误代码:".$msg); } $this->success('操作成功', 'admin/sys_user/index'); } else { if ($id != 0) { $data = $this->model->find($id); } else { $data = ['id'=>0, 'username' => '', 'role_id' => 0, 'truename' => '', 'email' => '', 'note' => '',]; } $data_role = model('sys_role')->column('id, name'); $this->assign('data', $data); $this->assign("data_role", $data_role); return $this->fetch(); } } public function isAvailable($id = null, $username = '') { if (Request::instance()->isAjax()) { $data = $this->model->where('username', $username)->find(); if ($data && $data->id != $id) { return ['code' => 2, 'msg'=>'用户名已存在, 请使用其他用户名']; } else { return ['code' => 0, 'msg'=>'用户名可用']; } } } public function delete($id = null) { if (Request::instance()->isAjax()) { if (is_array($id)) { if (in_array(session('uid'), $id)) { return ['code'=>0,'msg'=>'当前登录用户无法删除']; } } else { if ($id == session('uid')) { return ['code'=>0,'msg'=>'当前登录用户无法删除']; } } if ($this->model->destroy($id)) { return ['code' => 1,'msg'=>'删除成功']; } else { return ['code' => 0,'msg'=>'删除失败']; } } } //修改密码 public function modifyPwd() { if (Request::instance()->isPOST()) { $info = $this->model->field(true)->find($this->uid); $pwd = $info->password; $oldpwd = md5(trim(input('post.oldpassword'))); if ($pwd!=$oldpwd) { $this->error('原密码不正确'); } $newpwd = md5(trim(input('post.newpassword'))); $repwd = md5(trim(input('post.repassword'))); if ($newpwd!=$repwd) { $this->error('两次新密码不一致,请核查'); } $info->password = $newpwd; if ($info->save()) { session(null); $this->success("修改成功,请重新登陆", 'login/logout'); } else { $this->error('修改失败,请稍后重试'); } } else { return $this->fetch(); } } }