ProcessBrokerMessage.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\MicroApi\Items\UserItem;
  4. use App\MicroApi\Services\UserService;
  5. use App\Services\Broker\BrokerService;
  6. use Illuminate\Console\Command;
  7. class ProcessBrokerMessage extends Command
  8. {
  9. protected $signature = 'process:broker-message';
  10. protected $description = 'Subscribe and process message from micro broker';
  11. /**
  12. * @var UserService
  13. */
  14. protected $userService;
  15. /**
  16. * Create a new command instance.
  17. * @return void
  18. */
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->userService = resolve("microUserService");
  23. }
  24. /**
  25. * Execute the console command.
  26. *
  27. * @return mixed
  28. */
  29. public function handle()
  30. {
  31. $broker = new BrokerService();
  32. $broker->subscribe('password.reset', function ($message) {
  33. // 解析消息数据
  34. $message = $message->getBody();
  35. $passwordReset = json_decode(base64_decode($message['Body']));
  36. $email = $passwordReset->email;
  37. $token = $passwordReset->token;
  38. // 发送重置邮件
  39. $user = $this->userService->getByEmail($email);
  40. if ($user) {
  41. $model = new UserItem();
  42. $model->fillAttributes($user);
  43. $model->sendPasswordResetNotification($token);
  44. $this->info('密码重置邮件已发送[email:' . $email . ']');
  45. } else {
  46. $this->error('指定用户不存在[email:' . $email . ']');
  47. }
  48. });
  49. $broker->wait();
  50. }
  51. }