CheckBookCover.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/11/20
  6. * Time: 下午5:26
  7. */
  8. namespace App\Console\Commands;
  9. use App\Modules\Book\Models\BookConfig;
  10. use App\Modules\Statistic\Services\SendStatsEmailService;
  11. use GuzzleHttp\Client;
  12. use Illuminate\Console\Command;
  13. use Log;
  14. class CheckBookCover extends Command
  15. {
  16. /**
  17. * 执行命令 检查图书封面
  18. *
  19. * The name and signature of the console command.
  20. *
  21. * @var string
  22. */
  23. protected $signature = 'check_book_cover_status';
  24. /**
  25. * The console command description.
  26. *
  27. * @var string
  28. */
  29. protected $description = '检查图书封面';
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. Log::info('CheckBookCover start command');
  38. $this->loadBookCoverInfo();
  39. Log::info('CheckBookCover end command');
  40. }
  41. function loadBookCoverInfo()
  42. {
  43. $pageCount = 0; //当前的页数(默认从0开始)
  44. $pageSize = 200;//每页的条数
  45. $totalCount = BookConfig::getBooksCount(); //总条数
  46. $totalPageCount = Ceil($totalCount / $pageSize); ////总页数
  47. Log::info('CheckBookCover totalCount is ' . $totalCount);
  48. Log::info('CheckBookCover totalPageCount is ' . $totalPageCount);
  49. $errorBookCoverArray = [];
  50. $client = new Client(['timeout' => 3.0,]);
  51. while ($pageCount < $totalPageCount) {
  52. Log::info('CheckBookCover currentPageCount is: ' . $pageCount);
  53. $books = BookConfig::getBookCoverInfos(false, $pageSize, $pageCount);
  54. foreach ($books as $bookItem) {
  55. $coverUrl = $bookItem->cover;
  56. try {
  57. $response = $client->request('get', $coverUrl);
  58. $resultCode = $response->getStatusCode();
  59. //成功
  60. if ($resultCode == 200 || $resultCode == 302) {
  61. } else {
  62. Log::info('CheckBookCover load error');
  63. $errorBookCoverArray[] = $bookItem;
  64. }
  65. } catch (\Exception $e) {
  66. Log::info('CheckBookCover load error');
  67. $errorBookCoverArray[] = $bookItem;
  68. }
  69. }
  70. $pageCount++;
  71. }
  72. if ($errorBookCoverArray) {
  73. Log::info('CheckBookCover need sendEmail');
  74. $this->sendEmail($errorBookCoverArray);
  75. } else {
  76. Log::info('CheckBookCover not need sendEmail');
  77. }
  78. }
  79. function sendEmail($errorBookCoverArray)
  80. {
  81. Log::info('CheckBookCover start sendEmail');
  82. $to_user = array(
  83. ['address'=>'songdb@iqiyoo.com','name'=>'songdb'],
  84. ['address'=>'zhaojp@yqsd.net','name'=>'赵君平'],
  85. ['address' => 'zhoulj@iqiyoo.com', 'name' => '周灵杰'],
  86. );
  87. $content = "<table border='1' cellpadding='10' cellspacing='0'><tr><td align='center'>id</td><td align='center'>书名</td><td align='center'>作者</td></tr>";
  88. foreach ($errorBookCoverArray as $item) {
  89. $content .= "<tr><td align='center'>{$item->bid}</td><td align='center'>{$item->book_name}</td><td align='center'>{$item->author}</td></tr>";
  90. }
  91. $content .= "</table>";
  92. SendStatsEmailService::SendHtmlEmailWithAcce($to_user, ['subject' => date("Y-m-d", time()) . "追书云错误封面的书籍信息", 'body' => $content]);
  93. }
  94. }