Files
zh-chat-server/app/Jobs/ImportFriends.php
2022-11-01 11:07:41 +08:00

81 lines
2.1 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\User;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ImportFriends implements ShouldQueue
{
use Dispatchable, Queueable, SerializesModels;
protected User $user;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Execute the job.
*
* @return void
* @throws GuzzleException
*/
public function handle(): void
{
$user = $this->user;
$params = [
'jsonrpc' => '2.0',
'id' => 1,
'method' => 'Chain33.Query',
'params' => [
[
'execer' => 'chat',
'funcName' => 'GetFriends',
'payload' => [
'mainAddress' => $user->username,
'count' => 2000,
]
]
],
];
$client = new Client([
'base_uri' => '152.136.224.167:8901'
]);
$result = $client->post('', [
'body' => json_encode($params),
]);
$json = json_decode($result->getBody()->getContents(), true);
$collect = collect($json['result']['friends']);
$AddFriendItem = [];
foreach ($collect as $friend) {
$target = User::firstOrCreate(['username' => $friend['friendAddress'],]);
$AddFriendItem[] = [
"To_Account" => (string) $target->id,
"AddSource" => "AddSource_Type_IMPORT"
];
}
if (! blank($AddFriendItem)) {
$imParams = [
'From_Account' => (string) $user->id,
'AddFriendItem' => $AddFriendItem,
];
app('im')->send('sns', 'friend_import', $imParams);
}
}
}