SyncTencentIm.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Managers\IMManager;
  4. use App\Utils\ImUtil;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Console\Command;
  7. use Throwable;
  8. class SyncTencentIm extends Command
  9. {
  10. protected $signature = 'sync:tencent-im';
  11. protected $description = '从腾讯云下im聊天记录的命令';
  12. protected $date;
  13. protected $compressFiles = [];
  14. protected $jsonFiles = [];
  15. protected $imManager;
  16. public function __construct(IMManager $imManager)
  17. {
  18. parent::__construct();
  19. $this->imManager = $imManager;
  20. }
  21. /**
  22. * 每天凌晨
  23. *
  24. *
  25. */
  26. public function handle()
  27. {
  28. try {
  29. $this->downloadFromTencent();
  30. $this->unCompressFile();
  31. $this->readFromFile();
  32. } catch (Throwable $exception) {
  33. throw $exception;
  34. }
  35. }
  36. public function downloadFromTencent()
  37. {
  38. $im = new ImUtil();
  39. // 昨天的数据
  40. $endTime = strtotime(date('Ymd'));
  41. $startTime = $endTime - 86400;
  42. for ($step = 0; $step < 24; $step++) {
  43. $timeString = date('Ymd', $startTime) . sprintf('%02d', $step);
  44. $message = $im->downloadMessages($timeString);
  45. $message = json_decode($message);
  46. if (!$message->ErrorCode) {
  47. $i = 0;
  48. foreach ($message->File as $file) {
  49. $filePath = storage_path("app/{$timeString}_{$i}_" . uniqid() . '.gz');
  50. file_put_contents($filePath, '');
  51. $url = $file->URL;
  52. $client = new Client();
  53. $response = $client->request('GET', $url, ['stream' => true]);
  54. $body = $response->getBody();
  55. while (!$body->eof()) {
  56. file_put_contents($filePath, $body->read(1024), FILE_APPEND);
  57. }
  58. $this->compressFiles[$filePath] = $timeString;
  59. $i++;
  60. }
  61. }
  62. }
  63. }
  64. public function unCompressFile()
  65. {
  66. $i = 0;
  67. foreach ($this->compressFiles as $compressFile => $timeString) {
  68. $jsonPath = storage_path("app/{$timeString}_{$i}_" . uniqid() . '.json');
  69. file_put_contents($jsonPath, '');
  70. $cfp = gzopen($compressFile, 'rb');
  71. while (!gzeof($cfp)) {
  72. file_put_contents($jsonPath, gzread($cfp, 1024), FILE_APPEND);
  73. }
  74. gzclose($cfp);
  75. $this->jsonFiles[$jsonPath] = $timeString;
  76. $i++;
  77. }
  78. }
  79. public function readFromFile()
  80. {
  81. foreach ($this->jsonFiles as $jsonFile => $timeString) {
  82. $fp = fopen($jsonFile, 'r');
  83. while (!feof($fp)) {
  84. $content = fgets($fp);
  85. $message = json_decode($content, true);
  86. if ($message) {
  87. $fromAccount = $message['From_Account'];
  88. $toAccount = $message['To_Account'];
  89. $msgRandom = $message['MsgRandom'];
  90. $msgTime = $message['MsgTimestamp'];
  91. $msgSeq = $message['MsgSeq'];
  92. $this->imManager->createUserMessageByAccount(
  93. $fromAccount,
  94. $fromAccount,
  95. $toAccount,
  96. $msgTime,
  97. $msgRandom,
  98. $msgSeq,
  99. $message
  100. );
  101. $this->imManager->createUserMessageByAccount(
  102. $toAccount,
  103. $fromAccount,
  104. $toAccount,
  105. $msgTime,
  106. $msgRandom,
  107. $msgSeq,
  108. $message
  109. );
  110. }
  111. }
  112. }
  113. }
  114. }