CheckSiteStatus.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\Statistic\Services\SendStatsEmailService;
  10. use DB;
  11. use GuzzleHttp\Client;
  12. use Illuminate\Console\Command;
  13. use Log;
  14. class CheckSiteStatus extends Command
  15. {
  16. /**
  17. * 执行命令 检查站点信息
  18. *
  19. * The name and signature of the console command.
  20. *
  21. * @var string
  22. */
  23. protected $signature = 'check_site_status';
  24. /**
  25. * The console command description.
  26. *
  27. * @var string
  28. */
  29. protected $description = '检查掌中云站点信息';
  30. public $table_name = 'novel_platform_sites';
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. Log::info('CheckSiteStatus start command');
  39. $this->loadSiteIndexInfo();
  40. //$this->loadCompanyName();
  41. Log::info('CheckSiteStatus end command');
  42. }
  43. function loadCompanyName()
  44. {
  45. $pageCount = 0;
  46. $pageSize = 1000;
  47. $client = new Client(['timeout' => 3.0,]);
  48. $totalCount = DB::table($this->table_name)->max('site');
  49. $totalPageCount = ceil($totalCount / $pageSize);
  50. while ($pageCount < $totalPageCount) {
  51. $beginIndex = $pageCount * $pageSize;
  52. $data = DB::table($this->table_name)->offset($beginIndex)->limit($pageSize)->get();
  53. if ($data) {
  54. foreach ($data as $item) {
  55. $gzh_name = trim($item->gzh_name);
  56. $companyInfo = $this->getCompanyInfo($client, $gzh_name);
  57. $companyInfo['updated_at'] = date("Y-m-d H:i:s");
  58. DB::table($this->table_name)->where('id', $item->id)->update($companyInfo);
  59. sleep(1);
  60. }
  61. }
  62. $pageCount++;
  63. }
  64. }
  65. function loadSiteIndexInfo()
  66. {
  67. $trueSiteArray = [];
  68. $client = new Client(['timeout' => 3.0,]);
  69. $lastIndex = DB::table('novel_platform_sites')->max('site');
  70. $index = $lastIndex + 1;
  71. while ($index <= $lastIndex + 1000) {
  72. Log::info('check_site_status index is: ' . $index);
  73. $siteUrl = 'https://c' . $index . '.818tu.com/spa/config?tdsourcetag=s_pcqq_aiomsg';
  74. Log::info('check_site_status siteUrl is: ' . $siteUrl);
  75. try {
  76. $response = $client->request('get', $siteUrl);
  77. $resultCode = $response->getStatusCode();
  78. //成功
  79. if ($resultCode == 200 || $resultCode == 302) {
  80. $content = $response->getBody()->getContents();
  81. //Log::info('content is :' . $content);
  82. if ($content) {
  83. $findStr = "mp:";
  84. $findBegin = strpos($content, $findStr);
  85. if ($findBegin) {
  86. $findBegin = $findBegin + strlen($findStr);
  87. $findEnd = strpos($content, '},', $findBegin);
  88. if ($findEnd) {
  89. $content = substr($content, $findBegin, $findEnd - $findBegin + 1);
  90. Log::info('content is :' . $content);
  91. $content = json_decode($content);
  92. $gzh_name = trim($content->raw_id);
  93. $companyInfo = $this->getCompanyInfo($client, $gzh_name);
  94. $params = [
  95. 'site' => $index,
  96. 'gzh_name' => $gzh_name,
  97. 'platform_name' => '掌中云',
  98. 'appid' => $content->app_id,
  99. 'nickname' => $content->nickname,
  100. 'qrcode' => $companyInfo['qrcode'],
  101. 'company' => $companyInfo['company'],
  102. 'created_at' => date("Y-m-d H:i:s"),
  103. 'updated_at' => date("Y-m-d H:i:s"),
  104. 'fans_num_estimate' => $companyInfo['fans_num_estimate']
  105. ];
  106. $trueSiteArray[] = $params;
  107. DB::table('novel_platform_sites')->insert($params);
  108. }
  109. }
  110. } else {
  111. Log::info('content is :' . $content);
  112. }
  113. } else {
  114. Log::info('CheckSiteStatus load error resultCode is:' . $resultCode);
  115. }
  116. } catch (\Exception $e) {
  117. Log::info('CheckSiteStatus load error');
  118. Log::info($e->getMessage());
  119. }
  120. $index++;
  121. }
  122. if ($trueSiteArray) {
  123. Log::info('CheckSiteStatus need sendEmail');
  124. $this->sendEmail($trueSiteArray);
  125. } else {
  126. Log::info('CheckSiteStatus not need sendEmail');
  127. }
  128. }
  129. function getCompanyInfo($client, $gzh_name)
  130. {
  131. $qrcode = '';
  132. $company = '';
  133. $fans_num_estimate = '';
  134. $time_stamp = time();
  135. $sign_params = 'app_id=364c32166857f0131a0ac35396d6c0&kw=' . $gzh_name . '&page=1&timestamp=' . $time_stamp;
  136. $sign = md5($sign_params . 'sjadf92jf9vh@827*sjfo09n30');
  137. $url = 'https://api.wxb.com/data/search?' . $sign_params . '&sign=' . $sign;
  138. try {
  139. $response = $client->request('get', $url);
  140. } catch (\Exception $e) {
  141. Log::info($e->getMessage());
  142. return compact('qrcode', 'company', 'fans_num_estimate');
  143. }
  144. $resultCode = $response->getStatusCode();
  145. if ($resultCode == 200 || $resultCode == 302) {
  146. $content = $response->getBody()->getContents();
  147. $content = json_decode($content);
  148. $content = $content->data;
  149. if (is_array($content) && count($content) > 0) {
  150. $content = $content[0];
  151. $qrcode = $content->qrcode;
  152. $company = $content->company;
  153. $fans_num_estimate = $content->fans_num_estimate;
  154. }
  155. }
  156. return compact('qrcode', 'company', 'fans_num_estimate');
  157. }
  158. function sendEmail($trueSiteArray)
  159. {
  160. Log::info('CheckSiteStatus start sendEmail');
  161. $to_user = array(
  162. ['address' => 'zhangzg@iqiyoo.com', 'name' => '张总'],
  163. ['address' => 'songdb@iqiyoo.com', 'name' => 'songdb'],
  164. ['address' => 'zhaojp@yqsd.net', 'name' => '赵君平'],
  165. ['address' => 'sijj@yqsd.net', 'name' => 'sijj'],
  166. ['address' => 'qincp@iqiyoo.com', 'name' => '阿才'],
  167. );
  168. $content = "<table border='1' cellpadding='10' cellspacing='0'><tr><td align='center'>序号</td><td align='center'>站点id</td><td align='center'>公众号名称</td><td align='center'>主体公司名称</td></tr>";
  169. $index = 0;
  170. foreach ($trueSiteArray as $item) {
  171. $index++;
  172. $content .= "<tr><td align='center'>{$index}</td><td align='center'>{$item['site']}</td><td align='center'>{$item['nickname']}</td><td align='center'>{$item['company']}</td></tr>";
  173. }
  174. $content .= "</table>";
  175. SendStatsEmailService::SendHtmlEmailWithAcce($to_user, ['subject' => date("Y-m-d", time()) . "掌中云昨天新增站点" . $index . "个", 'body' => $content]);
  176. }
  177. }