Skip to content

Commit

Permalink
perf: 优化会议室
Browse files Browse the repository at this point in the history
  • Loading branch information
kuaifan committed Apr 4, 2024
1 parent 540bff8 commit 93c8d86
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 6 deletions.
7 changes: 6 additions & 1 deletion app/Http/Controllers/Api/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ public function umeng__alias()
public function meeting__open()
{
$type = trim(Request::input('type'));
$meetingid = trim(Request::input('meetingid'));
$meetingid = str_replace(' ', '', trim(Request::input('meetingid')));
$name = trim(Request::input('name'));
$userids = Request::input('userids');
$sharekey = trim(Request::input('sharekey'));
Expand All @@ -1163,6 +1163,11 @@ public function meeting__open()
if (empty($meeting)) {
return Base::retError('频道ID不存在');
}
if ($meeting->end_at) {
return Base::retError('会议已结束');
}
$meeting->updated_at = Carbon::now();
$meeting->save();
} elseif ($type === 'create') {
$meetingid = strtoupper(Base::generatePassword(11, 1));
$name = $name ?: "{$user?->nickname} 发起的会议";
Expand Down
9 changes: 6 additions & 3 deletions app/Http/Controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
use App\Module\Doo;
use App\Models\File;
use App\Module\Base;
use App\Tasks\LoopTask;
use App\Module\Extranet;
use App\Tasks\AppPushTask;
use App\Module\RandomColor;
use App\Tasks\LoopTask;
use App\Tasks\AppPushTask;
use App\Tasks\JokeSoupTask;
use App\Tasks\DeleteTmpTask;
use App\Tasks\EmailNoticeTask;
use App\Tasks\AutoArchivedTask;
use App\Tasks\DeleteBotMsgTask;
use App\Tasks\CheckinRemindTask;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use App\Tasks\CloseMeetingRoomTask;
use App\Tasks\UnclaimedTaskRemindTask;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use LasseRafn\InitialAvatarGenerator\InitialAvatar;


Expand Down Expand Up @@ -165,6 +166,8 @@ public function crontab()
Task::deliver(new JokeSoupTask());
// 未领取任务通知
Task::deliver(new UnclaimedTaskRemindTask());
// 关闭会议室
Task::deliver(new CloseMeetingRoomTask());

return "success";
}
Expand Down
34 changes: 34 additions & 0 deletions app/Models/MeetingMsg.php
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;
}
}
131 changes: 131 additions & 0 deletions app/Tasks/CloseMeetingRoomTask.php
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;
}
}
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');
}
}
7 changes: 5 additions & 2 deletions resources/assets/js/pages/manage/components/DialogView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</div>
<!--会议-->
<div v-else-if="msgData.type === 'meeting'" class="content-meeting no-dark-content">
<ul class="dialog-meeting">
<ul class="dialog-meeting" :class="{'meeting-end':!!msgData.msg.end_at}">
<li>
<em>{{$L('会议主题')}}</em>
{{msgData.msg.name}}
Expand All @@ -67,7 +67,10 @@
<em>{{$L('频道ID')}}</em>
{{msgData.msg.meetingid.replace(/^(.{3})(.{3})(.*)$/, '$1 $2 $3')}}
</li>
<li class="meeting-operation" @click="openMeeting">
<li v-if="msgData.msg.end_at" class="meeting-operation">
{{$L('会议已结束')}}
</li>
<li v-else class="meeting-operation" @click="openMeeting">
{{$L('点击加入会议')}}
<i class="taskfont">&#xe68b;</i>
</li>
Expand Down
9 changes: 9 additions & 0 deletions resources/assets/sass/pages/components/dialog-wrapper.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,15 @@
color: $primary-title-color;
.dialog-meeting {
min-width: 220px;
&.meeting-end {
opacity: .7;
> li {
&.meeting-operation {
justify-content: center;
cursor: default;
}
}
}
> li {
list-style: none;
display: flex;
Expand Down

0 comments on commit 93c8d86

Please sign in to comment.