-
Notifications
You must be signed in to change notification settings - Fork 952
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
234 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
/** | ||
* App\Models\MeetingMsg | ||
* | ||
* @property int $id | ||
* @property string|null $meetingid 会议ID | ||
* @property int|null $dialog_id 对话ID | ||
* @property int|null $msg_id 消息ID | ||
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelAppend() | ||
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelHidden() | ||
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel change($array) | ||
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel getKeyValue() | ||
* @method static \Illuminate\Database\Eloquent\Builder|MeetingMsg newModelQuery() | ||
* @method static \Illuminate\Database\Eloquent\Builder|MeetingMsg newQuery() | ||
* @method static \Illuminate\Database\Eloquent\Builder|MeetingMsg query() | ||
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove() | ||
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore() | ||
* @method static \Illuminate\Database\Eloquent\Builder|MeetingMsg whereDialogId($value) | ||
* @method static \Illuminate\Database\Eloquent\Builder|MeetingMsg whereId($value) | ||
* @method static \Illuminate\Database\Eloquent\Builder|MeetingMsg whereMeetingid($value) | ||
* @method static \Illuminate\Database\Eloquent\Builder|MeetingMsg whereMsgId($value) | ||
* @mixin \Eloquent | ||
*/ | ||
class MeetingMsg extends AbstractModel | ||
{ | ||
function __construct(array $attributes = []) | ||
{ | ||
parent::__construct($attributes); | ||
$this->timestamps = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
namespace App\Tasks; | ||
|
||
use App\Models\Meeting; | ||
use App\Models\WebSocketDialog; | ||
use App\Module\Base; | ||
use Carbon\Carbon; | ||
use App\Models\WebSocketDialogMsg; | ||
use Illuminate\Support\Facades\Cache; | ||
|
||
@error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING); | ||
|
||
class CloseMeetingRoomTask extends AbstractTask | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function start() | ||
{ | ||
// 10分钟执行一次 | ||
$time = intval(Cache::get("CloseMeetingRoomTask:Time")); | ||
if (time() - $time < 600) { | ||
return; | ||
} | ||
Cache::put("CloseMeetingRoomTask:Time", time(), Carbon::now()->addMinutes(10)); | ||
// 判断参数 | ||
$setting = Base::setting('meetingSetting'); | ||
if ($setting['open'] !== 'open') { | ||
return; | ||
} | ||
if (empty($setting['appid']) ||empty($setting['api_key']) || empty($setting['api_secret'])) { | ||
return; | ||
} | ||
$credentials = $setting['api_key'] . ":" . $setting['api_secret']; | ||
$base64Credentials = base64_encode($credentials); | ||
$arrHeader = [ | ||
"Accept: application/json", | ||
"Authorization: Basic " . $base64Credentials | ||
]; | ||
// 获取10分钟未更新的会议 | ||
$meetings = Meeting::whereNull('end_at') | ||
->where('updated_at', '<', Carbon::now()->subMinutes(10)) | ||
->take(100) | ||
->get(); | ||
$dialogIds = []; | ||
/** @var Meeting $meeting */ | ||
foreach ($meetings as $meeting) { | ||
if (!$this->isEmptyChannel($setting['appid'], $meeting->channel, $arrHeader)) { | ||
$meeting->updated_at = Carbon::now(); | ||
$meeting->save(); | ||
continue; | ||
} | ||
$meeting->end_at = Carbon::now(); | ||
$meeting->save(); | ||
// 更新消息 | ||
$newMsg = $meeting->toArray(); | ||
$newMsg['end_at'] = $meeting->end_at->toDateTimeString(); | ||
WebSocketDialogMsg::select(['web_socket_dialog_msgs.*', 'm.meetingid']) | ||
->join("meeting_msgs as m", "m.msg_id", "=", "web_socket_dialog_msgs.id") | ||
->where('m.meetingid', $meeting->meetingid) | ||
->chunk(100, function ($msgs) use ($newMsg, &$dialogIds) { | ||
/** @var WebSocketDialogMsg $msg */ | ||
foreach ($msgs as $msg) { | ||
$msg->msg = Base::array2json($newMsg); | ||
$msg->save(); | ||
// | ||
if (!isset($dialogIds[$msg->dialog_id])) { | ||
$dialogIds[$msg->dialog_id] = []; | ||
} | ||
$dialogIds[$msg->dialog_id][] = [ | ||
'id' => $msg->id, | ||
'msg' => $msg->msg, | ||
]; | ||
} | ||
}); | ||
} | ||
// 推送更新 | ||
foreach ($dialogIds as $dialogId => $datas) { | ||
$dialog = WebSocketDialog::find($dialogId); | ||
if (empty($dialog)) { | ||
continue; | ||
} | ||
foreach ($datas as $data) { | ||
$dialog->pushMsg('update', $data); | ||
} | ||
} | ||
} | ||
|
||
public function end() | ||
{ | ||
} | ||
|
||
/** | ||
* 是否空频道 | ||
* @param $appid | ||
* @param $channel | ||
* @param $arrHeader | ||
* @return bool | ||
*/ | ||
private function isEmptyChannel($appid, $channel, $arrHeader) | ||
{ | ||
$curl = curl_init(); | ||
curl_setopt_array($curl, [ | ||
CURLOPT_URL => "https://api.sd-rtn.com/dev/v1/channel/user/{$appid}/{$channel}", | ||
CURLOPT_RETURNTRANSFER => true, | ||
CURLOPT_ENCODING => "", | ||
CURLOPT_MAXREDIRS => 10, | ||
CURLOPT_TIMEOUT => 30, | ||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | ||
CURLOPT_CUSTOMREQUEST => "GET", | ||
CURLOPT_HTTPHEADER => $arrHeader, | ||
]); | ||
$response = curl_exec($curl); | ||
$err = curl_error($curl); | ||
curl_close($curl); | ||
if ($err) { | ||
return false; // 错误 | ||
} | ||
$data = Base::json2array($response); | ||
if (!$data['success']) { | ||
return false; // 失败 | ||
} | ||
if ($data['data']['channel_exist']) { | ||
return false; // 有人 | ||
} | ||
return true; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
database/migrations/2024_04_04_082823_create_meeting_msgs_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
use App\Models\WebSocketDialogMsg; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateMeetingMsgsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('meeting_msgs', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('meetingid')->nullable()->default('')->unique()->comment('会议ID'); | ||
$table->bigInteger('dialog_id')->nullable()->default(0)->comment('对话ID'); | ||
$table->bigInteger('msg_id')->nullable()->default(0)->comment('消息ID'); | ||
}); | ||
WebSocketDialogMsg::whereType('meeting')->chunk(100, function ($msgs) { | ||
/** @var WebSocketDialogMsg $msg */ | ||
foreach ($msgs as $msg) { | ||
$meetingid = $msg->msg['meetingid']; | ||
$dialog_id = $msg->dialog_id; | ||
$msg_id = $msg->id; | ||
\DB::table('meeting_msgs')->insert(compact('meetingid', 'dialog_id', 'msg_id')); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('meeting_msgs'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters