SystemAdmin.php 4.8 KB

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