SiteTitle.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Console\Commands\Channel;
  3. use App\Modules\OfficialAccount\Models\OfficialAccount;
  4. use Illuminate\Console\Command;
  5. use Redis;
  6. class SiteTitle extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'SetSiteTitle';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '设置站点名称';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $channel_ids = $this->getChannelIds();
  37. foreach ($channel_ids as $channel_id) {
  38. $key = sprintf('channel:setting:%s', $channel_id);
  39. $hkey = 'title';
  40. $count = $this->getChannelOfficialAccountCount($channel_id);
  41. if ($count > 1) {
  42. Redis::hDel($key, $hkey);
  43. } else if ($count == 1) {
  44. $official_account = $this->getOfficialAccount($channel_id);
  45. Redis::hSet($key, $hkey, $official_account->nickname);
  46. }
  47. }
  48. }
  49. private function getChannelIds()
  50. {
  51. return OfficialAccount::select('distribution_channel_id')->groupBy('distribution_channel_id')->pluck('distribution_channel_id')->all();
  52. }
  53. private function getOfficialAccount(int $channel_id)
  54. {
  55. return OfficialAccount::where('distribution_channel_id', $channel_id)->first();
  56. }
  57. private function getChannelOfficialAccountCount(int $channel_id)
  58. {
  59. return OfficialAccount::where('distribution_channel_id', $channel_id)->count();
  60. }
  61. }