Operator.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\common\library;
  3. use app\admin\model\Admin;
  4. use app\common\model\User;
  5. class Operator
  6. {
  7. const OPER_TYPE = [
  8. 'admin' => '管理员',
  9. 'user' => '用户',
  10. 'system' => '系统'
  11. ];
  12. /**
  13. * 获取操作人
  14. */
  15. public static function get($user = NULL)
  16. {
  17. if ($user === NULL) {
  18. // 自动获取操作人
  19. $user = self::getDefaultOper();
  20. }
  21. if ($user instanceof Admin) {
  22. $oper = [
  23. 'id' => $user->id,
  24. 'name' => $user->nickname,
  25. 'avatar' => $user->avatar,
  26. 'type' => 'admin',
  27. 'type_text' => (self::OPER_TYPE)['admin']
  28. ];
  29. } elseif ($user instanceof User) {
  30. $oper = [
  31. 'id' => $user->id,
  32. 'name' => $user->nickname,
  33. 'avatar' => $user->avatar,
  34. 'type' => 'user',
  35. 'type_text' => (self::OPER_TYPE)['user']
  36. ];
  37. } else {
  38. $oper = [
  39. 'id' => 0,
  40. 'name' => '',
  41. 'avatar' => '',
  42. 'type' => 'system',
  43. 'type_text' => (self::OPER_TYPE)['system']
  44. ];
  45. }
  46. return $oper;
  47. }
  48. /**
  49. * 解析操作人信息
  50. */
  51. public static function info($type, $user = NULL)
  52. {
  53. return [
  54. 'id' => $user['id'] ?? 0,
  55. 'name' => $user['nickname'] ?? '',
  56. 'avatar' => $user['avatar'] ?? '',
  57. 'type' => $type,
  58. 'type_text' => (self::OPER_TYPE)[$type]
  59. ];
  60. }
  61. /**
  62. * 获取默认操作人
  63. */
  64. private static function getDefaultOper()
  65. {
  66. $user = NULL;
  67. if (!request()->isCli()) {
  68. // 检测管理员登陆
  69. $user = auth_admin();
  70. if (!$user) {
  71. // 检测用户登陆
  72. $user = auth_user();
  73. }
  74. }
  75. return $user;
  76. }
  77. }