SystemAdmin.php 4.4 KB

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