SystemAdmin.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. Session::save();
  60. }
  61. /**
  62. * 清空当前登陆用户信息
  63. */
  64. public static function clearLoginInfo()
  65. {
  66. Session::delete('adminInfo');
  67. Session::delete('adminId');
  68. Session::save();
  69. }
  70. /**
  71. * 检查用户登陆状态
  72. * @return bool
  73. */
  74. public static function hasActiveAdmin()
  75. {
  76. return Session::has('adminId') && Session::has('adminInfo');
  77. }
  78. /**
  79. * 获得登陆用户信息
  80. * @return mixed
  81. * @throws \Exception
  82. */
  83. public static function activeAdminInfoOrFail()
  84. {
  85. $adminInfo = Session::get('adminInfo');
  86. if (!$adminInfo) exception('请登陆');
  87. if (!$adminInfo['status']) exception('该账号已被关闭!');
  88. return $adminInfo;
  89. }
  90. /**
  91. * 获得登陆用户Id 如果没有直接抛出错误
  92. * @return mixed
  93. * @throws \Exception
  94. */
  95. public static function activeAdminIdOrFail()
  96. {
  97. $adminId = Session::get('adminId');
  98. if (!$adminId) exception('访问用户为登陆登陆!');
  99. return $adminId;
  100. }
  101. /**
  102. * @return array|null
  103. * @throws \Exception
  104. */
  105. public static function activeAdminAuthOrFail()
  106. {
  107. $adminInfo = self::activeAdminInfoOrFail();
  108. if (is_object($adminInfo)) $adminInfo = $adminInfo->toArray();
  109. return $adminInfo['level'] === 0 ? SystemRole::getAllAuth() : SystemRole::rolesByAuth($adminInfo['roles']);
  110. }
  111. /**
  112. * 获得有效管理员信息
  113. * @param $id
  114. * @return mixed
  115. * @throws \Exception
  116. */
  117. public static function getValidAdminInfoOrFail($id)
  118. {
  119. $adminInfo = self::get($id);
  120. if (!$adminInfo) exception('用户不能存在!');
  121. if (!$adminInfo['status']) exception('该账号已被关闭!');
  122. return $adminInfo;
  123. }
  124. /**
  125. * @param string $field
  126. * @param int $level
  127. * @return \think\Collection
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. * @throws \think\exception\DbException
  131. */
  132. public static function getOrdAdmin($field = 'real_name,id', $level = 0)
  133. {
  134. return self::where('level', '>=', $level)->field($field)->select();
  135. }
  136. public static function getTopAdmin($field = 'real_name,id')
  137. {
  138. return self::where('level', 0)->field($field)->select();
  139. }
  140. /**
  141. * @param $where
  142. * @return array
  143. */
  144. public static function systemPage($where)
  145. {
  146. $model = new self;
  147. if ($where['name'] != '') $model = $model->where('account|real_name', 'LIKE', "%$where[name]%");
  148. if ($where['roles'] != '') $model = $model->where("CONCAT(',',roles,',') LIKE '%,$where[roles],%'");
  149. $model = $model->where('level', $where['level'])->where('is_del', 0);
  150. return self::page($model, function ($admin) {
  151. $admin->roles = SystemRole::where('id', 'IN', $admin->roles)->column('role_name', 'id');
  152. }, $where);
  153. }
  154. }