123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- namespace app\admin\model\system;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use think\facade\Session;
- class SystemAdmin extends BaseModel
- {
-
- protected $pk = 'id';
-
- protected $name = 'system_admin';
- use ModelTrait;
- protected $insert = ['add_time'];
- public static function setAddTimeAttr($value)
- {
- return time();
- }
- public static function setRolesAttr($value)
- {
- return is_array($value) ? implode(',', $value) : $value;
- }
-
- public static function login($account, $pwd)
- {
- $adminInfo = self::get(compact('account'));
- if (!$adminInfo) return self::setErrorInfo('登陆的账号不存在!');
- if ($adminInfo['pwd'] != md5(md5($pwd) . md5($adminInfo['salt']))) return self::setErrorInfo('账号或密码错误,请重新输入');
- if (!$adminInfo['status']) return self::setErrorInfo('该账号已被关闭!');
- self::setLoginInfo($adminInfo);
- event('SystemAdminLoginAfter', [$adminInfo]);
- return true;
- }
-
- public static function setLoginInfo($adminInfo)
- {
- Session::set('adminId', $adminInfo['id']);
- Session::set('adminInfo', $adminInfo->toArray());
- Session::save();
- }
-
- public static function clearLoginInfo()
- {
- Session::delete('adminInfo');
- Session::delete('adminId');
- Session::save();
- }
-
- public static function hasActiveAdmin()
- {
- return Session::has('adminId') && Session::has('adminInfo');
- }
-
- public static function activeAdminInfoOrFail()
- {
- $adminInfo = Session::get('adminInfo');
- if (!$adminInfo) exception('请登陆');
- if (!$adminInfo['status']) exception('该账号已被关闭!');
- return $adminInfo;
- }
-
- public static function activeAdminIdOrFail()
- {
- $adminId = Session::get('adminId');
- if (!$adminId) exception('访问用户为登陆登陆!');
- return $adminId;
- }
-
- public static function activeAdminAuthOrFail()
- {
- $adminInfo = self::activeAdminInfoOrFail();
- if (is_object($adminInfo)) $adminInfo = $adminInfo->toArray();
- return $adminInfo['level'] === 0 ? SystemRole::getAllAuth() : SystemRole::rolesByAuth($adminInfo['roles']);
- }
-
- public static function getValidAdminInfoOrFail($id)
- {
- $adminInfo = self::get($id);
- if (!$adminInfo) exception('用户不能存在!');
- if (!$adminInfo['status']) exception('该账号已被关闭!');
- return $adminInfo;
- }
-
- public static function getOrdAdmin($field = 'real_name,id', $level = 0)
- {
- return self::where('level', '>=', $level)->field($field)->select();
- }
- public static function getTopAdmin($field = 'real_name,id')
- {
- return self::where('level', 0)->field($field)->select();
- }
-
- public static function systemPage($where)
- {
- $model = new self;
- if ($where['name'] != '') $model = $model->where('account|real_name', 'LIKE', "%$where[name]%");
- if ($where['roles'] != '') $model = $model->where("CONCAT(',',roles,',') LIKE '%,$where[roles],%'");
- $model = $model->where('level', $where['level'])->where('is_del', 0);
- return self::page($model, function ($admin) {
- $admin->roles = SystemRole::where('id', 'IN', $admin->roles)->column('role_name', 'id');
- }, $where);
- }
- }
|