测试
This commit is contained in:
35
app/Enum/MessageTypeEnum.php
Normal file
35
app/Enum/MessageTypeEnum.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enum;
|
||||
|
||||
enum MessageTypeEnum: int
|
||||
{
|
||||
// 消息类型 0:文本 1:图片 2:音频 3:视频 4:处方 5:病例 6:视频通话
|
||||
case Text = 0;
|
||||
case Image = 1;
|
||||
case Audio = 2;
|
||||
case Video = 3;
|
||||
case Prescription = 4;
|
||||
case Case = 5;
|
||||
case VideoCall = 6;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
* @return string
|
||||
*/
|
||||
public function description(int $value): string
|
||||
{
|
||||
return match ($value) {
|
||||
self::Text->value => '文本',
|
||||
self::Image->value => '图片',
|
||||
self::Audio->value => '音频',
|
||||
self::Video->value => '视频',
|
||||
self::Prescription->value => '处方',
|
||||
self::Case->value => '病例',
|
||||
self::VideoCall->value => '视频通话',
|
||||
default => '未知',
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\GatewayWorker\ImService;
|
||||
use App\Services\GatewayWorker\UserService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ImController extends Controller
|
||||
@@ -25,4 +26,20 @@ class ImController extends Controller
|
||||
return jerr($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
* @Method POST
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function sendMessage()
|
||||
{
|
||||
$messageType = request()->post('message_type');
|
||||
$data = request()->post('data');
|
||||
try {
|
||||
return jok($this->service->sendMessage($data, $messageType));
|
||||
} catch (\Exception $e) {
|
||||
return jerr($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,19 @@
|
||||
|
||||
namespace App\Services\Base;
|
||||
|
||||
use App\Models\UserModel;
|
||||
|
||||
class BaseService
|
||||
{
|
||||
protected $userId;
|
||||
protected $userInfo;
|
||||
protected static $instance;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$token = request()->header('token');
|
||||
$this->userId = $token;
|
||||
$this->userInfo = UserModel::where('id', $token)->first();
|
||||
}
|
||||
|
||||
// 单例
|
||||
|
||||
@@ -21,7 +21,6 @@ class GatewayClientService
|
||||
{
|
||||
// ds(Gateway::$registerAddress = config('gateway.register_address'));
|
||||
Gateway::$registerAddress = config('gateway.register_address');
|
||||
ds(Gateway::getAllUidCount());
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -72,9 +71,9 @@ class GatewayClientService
|
||||
/**
|
||||
* 发送消息给指定用户
|
||||
*/
|
||||
public function sendToUser(int $toUserId, int $fromUserId, string $fromUserName, string $text): void
|
||||
public function sendToUser(int $toUserId, int $fromUserId, string $fromUserName, string $msgData): void
|
||||
{
|
||||
$message = $this->buildMessage($fromUserId, $fromUserName, $text);
|
||||
$message = $this->buildMessage($fromUserId, $fromUserName, $msgData);
|
||||
Gateway::sendToUid($toUserId, $message);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services\GatewayWorker;
|
||||
|
||||
use App\Enum\MessageTypeEnum;
|
||||
use App\Services\Base\BaseService;
|
||||
|
||||
class ImService extends BaseService
|
||||
@@ -17,4 +18,16 @@ class ImService extends BaseService
|
||||
|
||||
return ['绑定成功'];
|
||||
}
|
||||
|
||||
public function sendMessage($data, $type = MessageTypeEnum::Text->value)
|
||||
{
|
||||
$data['message_type'] = $type;
|
||||
GatewayClientService::getInstance()->init()->sendToUser(
|
||||
$this->userId,
|
||||
$this->userId === 1? 2: 1,
|
||||
$this->userInfo['nick_name'],
|
||||
json_encode($data),
|
||||
);
|
||||
return ['发送成功'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -658,24 +658,52 @@
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
let token = sessionStorage.getItem('token');
|
||||
|
||||
if (socket && socket.readyState === WebSocket.OPEN) {
|
||||
socket.send(JSON.stringify(message));
|
||||
axios.post('/api/b', message, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`, // 假设使用 Bearer token
|
||||
'Content-Type': 'application/json' // 通常也需要指定内容类型
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
console.log('请求成功:', response.data);
|
||||
// 添加消息到UI(自己的消息)
|
||||
addMessage({
|
||||
userId: currentUser.id,
|
||||
name: currentUser.name,
|
||||
avatarColor: currentUser.avatarColor,
|
||||
text: text,
|
||||
timestamp: new Date(),
|
||||
type: 'sent'
|
||||
});
|
||||
// 清空输入框
|
||||
messageInput.value = '';
|
||||
|
||||
// 添加消息到UI(自己的消息)
|
||||
addMessage({
|
||||
userId: currentUser.id,
|
||||
name: currentUser.name,
|
||||
avatarColor: currentUser.avatarColor,
|
||||
text: text,
|
||||
timestamp: new Date(),
|
||||
type: 'sent'
|
||||
// 重新聚焦输入框
|
||||
messageInput.focus();
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('请求出错:', error);
|
||||
});
|
||||
|
||||
// 清空输入框
|
||||
messageInput.value = '';
|
||||
|
||||
// 重新聚焦输入框
|
||||
messageInput.focus();
|
||||
// socket.send(JSON.stringify(message));
|
||||
//
|
||||
// // 添加消息到UI(自己的消息)
|
||||
// addMessage({
|
||||
// userId: currentUser.id,
|
||||
// name: currentUser.name,
|
||||
// avatarColor: currentUser.avatarColor,
|
||||
// text: text,
|
||||
// timestamp: new Date(),
|
||||
// type: 'sent'
|
||||
// });
|
||||
//
|
||||
// // 清空输入框
|
||||
// messageInput.value = '';
|
||||
//
|
||||
// // 重新聚焦输入框
|
||||
// messageInput.focus();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ Route::group([
|
||||
'prefix' => '',
|
||||
], function () {
|
||||
cc_auto_route_register([
|
||||
'auth' => \App\Http\Controllers\UserController::class
|
||||
'im' => \App\Http\Controllers\ImController::class,
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user