GenerateToken.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\User;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Auth;
  6. class GenerateToken extends Command
  7. {
  8. /**
  9. * 指令
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'learnku:generate-token';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '快速为用户生成 token';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $userIde = $this->ask('输入用户 id');
  38. $user = User::find($userIde);
  39. if (!$user) {
  40. return $this->error('用户不存在');
  41. }
  42. // 一年后过期
  43. $ttl = 365 * 24 * 60;
  44. $this->info(Auth::guard('api')->setTTl($ttl)->fromUser($user));
  45. }
  46. }