AuthKey.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Models\User;
  3. use App\Exceptions\AlertException;
  4. use Tymon\JWTAuth\Contracts\JWTSubject;
  5. use Illuminate\Notifications\Notifiable;
  6. use Illuminate\Foundation\Auth\User as Authenticatable;
  7. /**
  8. * App\Models\User\AuthKey
  9. *
  10. * @property int $id 主键
  11. * @property int $uid 用户id
  12. * @property string $auth_key 认证key
  13. * @property string $auth_type 认证类型
  14. * @property \Illuminate\Support\Carbon $created_at 绑定时间
  15. * @property \Illuminate\Support\Carbon $updated_at 登录时间
  16. * @property int $login_at 上次登录时间
  17. * @property string|null $attach 附加信息
  18. * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
  19. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey newModelQuery()
  20. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey newQuery()
  21. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey query()
  22. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereAttach($value)
  23. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereAuthKey($value)
  24. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereAuthType($value)
  25. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereCreatedAt($value)
  26. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereId($value)
  27. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereLoginAt($value)
  28. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereUid($value)
  29. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\AuthKey whereUpdatedAt($value)
  30. * @mixin \Eloquent
  31. */
  32. class AuthKey extends Authenticatable implements JWTSubject
  33. {
  34. use Notifiable;
  35. protected $table = 'kdgx_user_auth_key';
  36. protected $fillable = [
  37. 'uid',
  38. 'auth_key',
  39. 'auth_type',
  40. 'attach'
  41. ];
  42. protected $dateFormat = 'U';
  43. protected $primaryKey = 'id';
  44. /**
  45. * 获取用户的密码。
  46. *
  47. * @return string
  48. */
  49. public function getAuthPassword()
  50. {
  51. $hash = new \Illuminate\Hashing\BcryptHasher();
  52. return $hash->make($this->auth_key);
  53. }
  54. /**
  55. * 获取将存储在JWT主题声明中的标识符。
  56. *
  57. * @return mixed
  58. */
  59. public function getJWTIdentifier()
  60. {
  61. return $this->auth_key;
  62. }
  63. public function getAuthIdentifierName()
  64. {
  65. return 'auth_key';
  66. }
  67. /**
  68. * 返回一个键值数组,其中包含要添加到JWT的任何自定义声明。
  69. *
  70. * @return array
  71. */
  72. public function getJWTCustomClaims()
  73. {
  74. $user = UserModel::find($this->uid);
  75. return [
  76. 'nickname' => $user->nickname,
  77. 'headimgurl' => $user->headimgurl,
  78. 'phone' => $user->phone,
  79. 'uid' => $this->uid
  80. ];
  81. }
  82. public function validateCredentials($auth_key)
  83. {
  84. $data = $this->where(['auth_key' => $auth_key])->first()->toArray();
  85. if (empty($data)) {
  86. return false;
  87. } else {
  88. return true;
  89. }
  90. }
  91. /**
  92. * 获取某种登录方式的key
  93. * @param int $uid 用户id
  94. * @param string $auth_type 登陆方式
  95. * @return string auth_key
  96. * @throws AlertException
  97. */
  98. public function getKeyOfType(int $uid, string $auth_type)
  99. {
  100. $auth = $this->where(array(['uid', $uid], ['auth_type', $auth_type]))->first();
  101. if (collect($auth)->isEmpty()) {
  102. throw new AlertException("未找到登录方式", 404);
  103. } else {
  104. return $auth->auth_key;
  105. }
  106. }
  107. /**
  108. * 获取用户uid
  109. * @param string $auth_type
  110. * @return array
  111. * @throws AlertException
  112. */
  113. public function getUidByKey(string $auth_type)
  114. {
  115. $auth = $this->where('auth_key', $auth_type)->first();
  116. if (collect($auth)->isEmpty()) {
  117. throw new AlertException("未绑定", 404);
  118. } else {
  119. return $auth->uid;
  120. }
  121. }
  122. }