123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- declare (strict_types = 1);
- /**
- * +----------------------------------------------------------------------
- * | 后台登录控制制器
- * +----------------------------------------------------------------------
- */
- namespace app\sys\controller;
- // 引入框架内置类
- use think\facade\Request;
- use think\facade\Event;
- use think\facade\Session;
- use think\facade\View;
- use think\captcha\facade\Captcha;
- use think\facade\Config;
- use app\common\model\SysUser as SysUserModel;
- use app\common\model\SysRole as SysRoleModel;
- class SysUser extends Base
- {
- public function index()
- {
- $roleid = $this->getSysUser()->roleid;
- $list = SysUserModel::queryList($roleid);
- View::assign('list', $list);
- return View::fetch();
- }
- // public function info($id = 0)
- // {
- // if ($id != 0) {
- // $data = $this->model->find($id);
- // } else {
- // $data = ['id'=>0, 'username' => '', 'role_id' => 0, 'truename' => '', 'email' => '', 'note' => ''];
- // }
- // $dataRole = SysRoleModel::column('name','id');
- // return View::fetch('save',[
- // 'data' => $data,
- // 'dataRole' => $dataRole
- // ]);
- // }
- public function save($id = 0)
- {
- if ($this->app->request->isPost()) {
- $params = $this->app->request->param();
- if ($params['username'] =='' || $params['roleid'] =='') {
- $this->error("用户名 or 角色 不能为空!");
- }
- try {
- $id = $params['userid'];
- unset($params['userid']);
- if ($id != 0) {
- if (empty($params['password'])) {
- unset($params['password']);
- } else {
- $params['salt'] = generate_stochastic_string();
- $params['password'] = md5($params['password'].$params['salt']);
- }
- SysUserModel::update($params, ['userid' => $id]);
- } else {
- $params['password'] = empty($params['password']) ? Config::get('app.default_password') : $params['password'];
- $params['salt'] = generate_stochastic_string();
- $params['password'] = md5($params['password'].$params['salt']);
- SysUserModel::create($params);
- }
- } catch (\Exception $e) {
- $msg = $e->getMessage();
- $this->error("错误代码:".$msg);
- }
- $this->success('操作成功', 'sys_user/index');
- } else {
- if ($id != 0) {
- $data = SysUserModel::find($id);
- } else {
- $data = null;
- }
-
- $dataRole = SysRole::column('name','roleid');
- View::assign('data', $data);
- View::assign('dataRole', $dataRole);
-
- return View::fetch();
- }
- }
- public function isAvailable($id = null, $username = '')
- {
- if ($this->app->request->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 ($this->app->request->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 (SysUserModel::destroy($id)) {
- return ['code' => 1,'msg'=>'删除成功'];
- } else {
- return ['code' => 0,'msg'=>'删除失败'];
- }
- }
- }
- // 停用or启用管理员
- public function status($id, $status)
- {
- if ($this->app->request->isAjax()) {
- if ($id == session('uid')) {
- return ['code'=>0,'msg'=>'当前登录用户无法停用'];
- }
- if ($this->model->save(['status' => $status], ['id' => $id])) {
- return ['code'=>1,'msg'=>'操作成功'];
- } else {
- return ['code'=>0,'msg'=>'操作失败'];
- }
- }
- }
- // 修改密码
- public function password()
- {
- if ($this->app->request->isPOST()) {
- $params = $this->app->request->param();
-
- $info = SysUserModel::field('password,salt')->find(session('adminuser.userid'));
- if ($info->password != md5($params['oldpassword'].$info->salt)) {
- $this->error('原密码不正确');
- }
- if ($params['newpassword']!=$params['repassword']) {
- $this->error('两次新密码不一致,请核查');
- }
- $info->salt = generate_stochastic_string();
- $info->password = md5($params['newpassword'] . $info->salt);
- if ($info->save()) {
- session(null);
- $this->success("修改成功,请重新登陆", 'login/index');
- } else {
- $this->error('修改失败,请稍后重试');
- }
- } else {
- return View::fetch();
- }
- }
- }
|