SystemAdmin.php 4.2 KB

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