123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- /**
- * Created by PhpStorm.
- * User: tandunzhao
- * Date: 2017/11/20
- * Time: 下午5:26
- */
- namespace App\Console\Commands;
- use App\Modules\Book\Models\BookConfig;
- use App\Modules\Statistic\Services\SendStatsEmailService;
- use GuzzleHttp\Client;
- use Illuminate\Console\Command;
- use Log;
- class CheckBookCover extends Command
- {
- /**
- * 执行命令 检查图书封面
- *
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'check_book_cover_status';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '检查图书封面';
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- Log::info('CheckBookCover start command');
- $this->loadBookCoverInfo();
- Log::info('CheckBookCover end command');
- }
- function loadBookCoverInfo()
- {
- $pageCount = 0; //当前的页数(默认从0开始)
- $pageSize = 200;//每页的条数
- $totalCount = BookConfig::getBooksCount(); //总条数
- $totalPageCount = Ceil($totalCount / $pageSize); ////总页数
- Log::info('CheckBookCover totalCount is ' . $totalCount);
- Log::info('CheckBookCover totalPageCount is ' . $totalPageCount);
- $errorBookCoverArray = [];
- $client = new Client(['timeout' => 3.0,]);
- while ($pageCount < $totalPageCount) {
- Log::info('CheckBookCover currentPageCount is: ' . $pageCount);
- $books = BookConfig::getBookCoverInfos(false, $pageSize, $pageCount);
- foreach ($books as $bookItem) {
- $coverUrl = $bookItem->cover;
- try {
- $response = $client->request('get', $coverUrl);
- $resultCode = $response->getStatusCode();
- //成功
- if ($resultCode == 200 || $resultCode == 302) {
- } else {
- Log::info('CheckBookCover load error');
- $errorBookCoverArray[] = $bookItem;
- }
- } catch (\Exception $e) {
- Log::info('CheckBookCover load error');
- $errorBookCoverArray[] = $bookItem;
- }
- }
- $pageCount++;
- }
- if ($errorBookCoverArray) {
- Log::info('CheckBookCover need sendEmail');
- $this->sendEmail($errorBookCoverArray);
- } else {
- Log::info('CheckBookCover not need sendEmail');
- }
- }
- function sendEmail($errorBookCoverArray)
- {
- Log::info('CheckBookCover start sendEmail');
- $to_user = array(
- ['address'=>'songdb@iqiyoo.com','name'=>'songdb'],
- ['address'=>'zhaojp@yqsd.net','name'=>'赵君平'],
- ['address' => 'zhoulj@iqiyoo.com', 'name' => '周灵杰'],
- );
- $content = "<table border='1' cellpadding='10' cellspacing='0'><tr><td align='center'>id</td><td align='center'>书名</td><td align='center'>作者</td></tr>";
- foreach ($errorBookCoverArray as $item) {
- $content .= "<tr><td align='center'>{$item->bid}</td><td align='center'>{$item->book_name}</td><td align='center'>{$item->author}</td></tr>";
- }
- $content .= "</table>";
- SendStatsEmailService::SendHtmlEmailWithAcce($to_user, ['subject' => date("Y-m-d", time()) . "追书云错误封面的书籍信息", 'body' => $content]);
- }
- }
|