SystemAdmin.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\models\system;
  3. use crmeb\services\CacheService;
  4. use crmeb\traits\JwtAuthModelTrait;
  5. use crmeb\traits\ModelTrait;
  6. use crmeb\basic\BaseModel;
  7. use think\facade\Cache;
  8. /**
  9. * Class SystemAdmin
  10. * @package app\models\system
  11. */
  12. class SystemAdmin extends BaseModel
  13. {
  14. /**
  15. * 数据表主键
  16. * @var string
  17. */
  18. protected $pk = 'id';
  19. /**
  20. * 模型名称
  21. * @var string
  22. */
  23. protected $name = 'system_admin';
  24. /**
  25. * token令牌桶名
  26. * @var string
  27. */
  28. protected static $tokenPrefix = 'admin_token';
  29. use ModelTrait;
  30. use JwtAuthModelTrait;
  31. protected $insert = ['add_time'];
  32. /**
  33. * 获取token缓存前缀
  34. * @return string
  35. */
  36. public static function getTokenPrefix()
  37. {
  38. return self::$tokenPrefix;
  39. }
  40. public static function getRolesAttr($value)
  41. {
  42. return explode(',', $value);
  43. }
  44. /**
  45. * 获取token并放入令牌桶
  46. * @param SystemAdmin $adminInfo
  47. * @param $type
  48. * @return array|bool
  49. * @throws \Psr\SimpleCache\InvalidArgumentException
  50. */
  51. public static function createToken(SystemAdmin $adminInfo, string $type, array $params = [])
  52. {
  53. $tokenInfo = $adminInfo->getToken($type, $params);
  54. $exp = (int)bcadd($tokenInfo['params']['exp'] - $tokenInfo['params']['iat'], 60, 0);
  55. $res = CacheService::setTokenBucket($tokenInfo['token'], ['token' => $tokenInfo['token'], 'exp' => $exp], (int)$exp);
  56. if (!$res) return self::setErrorInfo('保存token失败');
  57. return $tokenInfo;
  58. }
  59. /**
  60. * 从令牌桶中删除token
  61. * @param string $token
  62. * @param string $tokenPrefix
  63. * @throws \Psr\SimpleCache\InvalidArgumentException
  64. */
  65. public static function delTokenBucket(string $token)
  66. {
  67. if (Cache::has($token)) {
  68. return Cache::delete($token);
  69. }
  70. return true;
  71. }
  72. /**
  73. * 用户登陆
  74. * @param $account
  75. * @param $pwd
  76. * @return bool
  77. */
  78. public static function login($account, $pwd)
  79. {
  80. $adminInfo = self::get(compact('account'));
  81. if (!$adminInfo) return self::setErrorInfo('登陆的账号不存在!');
  82. if (!password_verify($pwd, $adminInfo['pwd'])) return self::setErrorInfo('账号或密码错误,请重新输入');
  83. if (!$adminInfo['status']) return self::setErrorInfo('该账号已被关闭!');
  84. event('SystemAdminLoginAfter', [$adminInfo]);
  85. return $adminInfo;
  86. }
  87. /**
  88. * @return array|null
  89. * @throws \Exception
  90. */
  91. public static function activeAdminAuthOrFail($adminInfo)
  92. {
  93. if (is_object($adminInfo)) $adminInfo = $adminInfo->toArray();
  94. return $adminInfo['level'] === 0 ? SystemRole::getAllRule() : SystemRole::rolesByAuth($adminInfo['roles']);
  95. }
  96. /**
  97. * 获取当前管理员等级下的所有管理员
  98. * @param $name
  99. * @param $level
  100. * @param $roles
  101. * @param $page
  102. * @param $limit
  103. * @param int $mer_id
  104. * @return array
  105. */
  106. public static function getAdminList($name, $level, $roles, $page, $limit, $mer_id = 0)
  107. {
  108. $model = self::where('level', $level)->where('is_del', 0);
  109. if ($name) $model = $model->where('account|real_name', 'LIKE', "%$name%");
  110. if ($roles) $model = $model->where("CONCAT(',',roles,',') LIKE '%,$roles,%'");
  111. if ($mer_id) $model = $model->where("mer_id", 'in', [0, $mer_id]);
  112. $list = $model->hidden(['pwd'])->page($page, $limit)->select();
  113. $list = count($list) ? $list->toArray() : [];
  114. foreach ($list as &$item) {
  115. $roles = SystemRole::whereIn('id', $item['roles'])->column('role_name');
  116. $item['roles'] = implode(',', $roles);
  117. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  118. $item['_last_time'] = $item['last_time'] ? date('Y-m-d H:i:s', $item['last_time']) : '';
  119. }
  120. return $list;
  121. }
  122. /**
  123. * @param string $field
  124. * @param int $level
  125. * @return \think\Collection
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. * @throws \think\exception\DbException
  129. */
  130. public static function getOrdAdmin($field = 'real_name,id', $level = 0)
  131. {
  132. return self::where('level', '>=', $level)->field($field)->select();
  133. }
  134. }