SystemAdmin.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/11
  5. */
  6. namespace app\admin\model\system;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. use think\facade\Session;
  10. /**
  11. * Class SystemAdmin
  12. * @package app\admin\model\system
  13. */
  14. class SystemAdmin extends BaseModel
  15. {
  16. /**
  17. * 数据表主键
  18. * @var string
  19. */
  20. protected $pk = 'id';
  21. /**
  22. * 模型名称
  23. * @var string
  24. */
  25. protected $name = 'system_admin';
  26. use ModelTrait;
  27. protected $insert = ['add_time'];
  28. public static function setAddTimeAttr($value)
  29. {
  30. return time();
  31. }
  32. public static function setRolesAttr($value)
  33. {
  34. return is_array($value) ? implode(',', $value) : $value;
  35. }
  36. /**
  37. * 用户登陆
  38. * @param $account
  39. * @param $pwd
  40. * @return bool
  41. */
  42. public static function login($account,$pwd)
  43. {
  44. $adminInfo = self::get(compact('account'));
  45. if(!$adminInfo) return self::setErrorInfo('登陆的账号不存在!');
  46. if($adminInfo['pwd'] != md5($pwd)) return self::setErrorInfo('账号或密码错误,请重新输入');
  47. if(!$adminInfo['status']) return self::setErrorInfo('该账号已被关闭!');
  48. self::setLoginInfo($adminInfo);
  49. event('SystemAdminLoginAfter',[$adminInfo]);
  50. return true;
  51. }
  52. /**
  53. * 保存当前登陆用户信息
  54. */
  55. public static function setLoginInfo($adminInfo)
  56. {
  57. Session::set('adminId',$adminInfo['id']);
  58. Session::set('adminInfo',$adminInfo->toArray());
  59. }
  60. /**
  61. * 清空当前登陆用户信息
  62. */
  63. public static function clearLoginInfo()
  64. {
  65. Session::delete('adminInfo');
  66. Session::delete('adminId');
  67. Session::clear();
  68. }
  69. /**
  70. * 检查用户登陆状态
  71. * @return bool
  72. */
  73. public static function hasActiveAdmin()
  74. {
  75. return Session::has('adminId') && Session::has('adminInfo');
  76. }
  77. /**
  78. * 获得登陆用户信息
  79. * @return mixed
  80. * @throws \Exception
  81. */
  82. public static function activeAdminInfoOrFail()
  83. {
  84. $adminInfo = Session::get('adminInfo');
  85. if(!$adminInfo) exception('请登陆');
  86. if(!$adminInfo['status']) exception('该账号已被关闭!');
  87. return $adminInfo;
  88. }
  89. /**
  90. * 获得登陆用户Id 如果没有直接抛出错误
  91. * @return mixed
  92. * @throws \Exception
  93. */
  94. public static function activeAdminIdOrFail()
  95. {
  96. $adminId = Session::get('adminId');
  97. if(!$adminId) exception('访问用户为登陆登陆!');
  98. return $adminId;
  99. }
  100. /**
  101. * @return array|null
  102. * @throws \Exception
  103. */
  104. public static function activeAdminAuthOrFail()
  105. {
  106. $adminInfo = self::activeAdminInfoOrFail();
  107. if(is_object($adminInfo)) $adminInfo = $adminInfo->toArray();
  108. return $adminInfo['level'] === 0 ? SystemRole::getAllAuth() : SystemRole::rolesByAuth($adminInfo['roles']);
  109. }
  110. /**
  111. * 获得有效管理员信息
  112. * @param $id
  113. * @return mixed
  114. * @throws \Exception
  115. */
  116. public static function getValidAdminInfoOrFail($id)
  117. {
  118. $adminInfo = self::get($id);
  119. if(!$adminInfo) exception('用户不能存在!');
  120. if(!$adminInfo['status']) exception('该账号已被关闭!');
  121. return $adminInfo;
  122. }
  123. /**
  124. * @param string $field
  125. * @param int $level
  126. * @return \think\Collection
  127. * @throws \think\db\exception\DataNotFoundException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. * @throws \think\exception\DbException
  130. */
  131. public static function getOrdAdmin($field = 'real_name,id',$level = 0){
  132. return self::where('level','>=',$level)->field($field)->select();
  133. }
  134. public static function getTopAdmin($field = 'real_name,id')
  135. {
  136. return self::where('level',0)->field($field)->select();
  137. }
  138. /**
  139. * @param $where
  140. * @return array
  141. */
  142. public static function systemPage($where){
  143. $model = new self;
  144. if($where['name'] != '') $model = $model->where('account|real_name','LIKE',"%$where[name]%");
  145. if($where['roles'] != '') $model = $model->where("CONCAT(',',roles,',') LIKE '%,$where[roles],%'");
  146. $model = $model->where('level',$where['level'])->where('is_del',0);
  147. return self::page($model,function($admin){
  148. $admin->roles = SystemRole::where('id','IN',$admin->roles)->column('role_name','id');
  149. },$where);
  150. }
  151. }