文本聊天已完成
This commit is contained in:
@@ -39,9 +39,10 @@ class AuthController extends Controller
|
||||
$username = request()->input('username');
|
||||
$password = request()->input('password');
|
||||
$nickName = request()->input('nickname');
|
||||
$avatar = request()->input('avatar');
|
||||
|
||||
return jok(
|
||||
$this->service->register($username, $password, $nickName),
|
||||
$this->service->register($username, $password, $nickName, $avatar),
|
||||
'注册成功'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -36,15 +36,25 @@ class ImController extends Controller
|
||||
{
|
||||
$messageType = request()->post('message_type');
|
||||
$data = request()->post('data');
|
||||
$toUserId = request()->post('to_user_id');
|
||||
$chatRoomId = request()->post('chat_room_id');
|
||||
$replayId = request()->post('replay_id', 0);
|
||||
|
||||
try {
|
||||
return jok($this->service->sendMessage($data, $messageType));
|
||||
return jok($this->service->sendMessage($data, $toUserId, $chatRoomId, $replayId, $messageType));
|
||||
} catch (\Exception $e) {
|
||||
return jerr($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function getMessageByChatRoomId()
|
||||
{
|
||||
$chatRoomId = request()->input('chat_room_id');
|
||||
try {
|
||||
return jok(
|
||||
$this->service->getMessageByChatRoomId($chatRoomId)
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
ds([
|
||||
'message' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'trace' => $e->getTrace(),
|
||||
]);
|
||||
return jerr($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,25 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\GatewayWorker\UserService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
//
|
||||
/**
|
||||
* 获取用户列表
|
||||
* @Method GET
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function userList()
|
||||
{
|
||||
try {
|
||||
return jok(
|
||||
(new UserService())->userList()
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
return jerr($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
23
app/Models/MessageModel.php
Normal file
23
app/Models/MessageModel.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class MessageModel extends Model
|
||||
{
|
||||
//
|
||||
protected $table = 'nl_messages';
|
||||
protected $primaryKey = 'id';
|
||||
public $timestamps = false;
|
||||
protected $guarded = [];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->hasOne(UserModel::class, 'id', 'user_id');
|
||||
}
|
||||
public function toUser()
|
||||
{
|
||||
return $this->hasOne(UserModel::class, 'id', 'to_user_id');
|
||||
}
|
||||
}
|
||||
14
app/Models/im/ChatRoomModel.php
Normal file
14
app/Models/im/ChatRoomModel.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\im;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ChatRoomModel extends Model
|
||||
{
|
||||
//
|
||||
protected $table = 'nl_chat_room';
|
||||
protected $primaryKey = 'id';
|
||||
public $timestamps = false;
|
||||
protected $guarded = [];
|
||||
}
|
||||
14
app/Models/im/ChatRoomUserModel.php
Normal file
14
app/Models/im/ChatRoomUserModel.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\im;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ChatRoomUserModel extends Model
|
||||
{
|
||||
//
|
||||
protected $table = 'nl_chat_room_user';
|
||||
protected $primaryKey = 'id';
|
||||
public $timestamps = false;
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -17,7 +17,7 @@ class BaseService
|
||||
public function __construct()
|
||||
{
|
||||
$token = request()->header('token');
|
||||
$this->userId = $token;
|
||||
$this->userId = (int)$token;
|
||||
$this->userInfo = UserModel::where('id', $token)->first();
|
||||
}
|
||||
|
||||
|
||||
@@ -122,4 +122,10 @@ class GatewayClientService
|
||||
Gateway::sendToAll($message);
|
||||
}
|
||||
}
|
||||
|
||||
// 判断uid是否在线
|
||||
public function isUidOnline(int $uid): bool
|
||||
{
|
||||
return Gateway::isUidOnline($uid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Services\GatewayWorker;
|
||||
|
||||
use App\Enum\MessageTypeEnum;
|
||||
use App\Models\im\ChatRoomUserModel;
|
||||
use App\Models\MessageModel;
|
||||
use App\Services\Base\BaseService;
|
||||
use GatewayClient\Gateway;
|
||||
|
||||
@@ -26,7 +28,36 @@ class ImService extends BaseService
|
||||
return ['绑定成功'];
|
||||
}
|
||||
|
||||
public function sendMessage($data, $type = MessageTypeEnum::Text->value)
|
||||
public function getMessageByChatRoomId($chatRoomId)
|
||||
{
|
||||
$message = MessageModel::with([
|
||||
'user',
|
||||
'toUser'
|
||||
])->where('chat_room_id', $chatRoomId)->get();
|
||||
|
||||
if (empty($message)) {
|
||||
return [];
|
||||
}
|
||||
$result = [];
|
||||
$message = $message->toArray();
|
||||
foreach ($message as $v) {
|
||||
$result[] = [
|
||||
'userId' => $v['user_id'],
|
||||
'l' => $v['user_id'] === $this->userId? 'user' : 'to_user',
|
||||
'message_type' => MessageTypeEnum::from($v['type'])->description(),
|
||||
'content' => $v['content'],
|
||||
'nick_name' => $v['user']['nick_name'],
|
||||
'avatar' => $v['user']['avatar'],
|
||||
'timestamp' => $v['created_at'],
|
||||
'type' => $v['user_id'] === $this->userId? 'sent' : 'received',
|
||||
'user' => $v['user'],
|
||||
'to_user' => $v['to_user'],
|
||||
];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function sendMessage($data, $toUserId, $chatRoomId, $replayId, $type = MessageTypeEnum::Text->value)
|
||||
{
|
||||
// $uids = [1,2,3];
|
||||
// $uidsC = [];
|
||||
@@ -36,12 +67,22 @@ class ImService extends BaseService
|
||||
// ds($uidsC);
|
||||
$data['message_type'] = MessageTypeEnum::from($type)->description();
|
||||
$this->gatewayClient->sendToUser(
|
||||
$this->userId === 1? 2: 1,
|
||||
$toUserId,
|
||||
$this->userId,
|
||||
$data['avatarColor'],
|
||||
$this->userInfo['nick_name'],
|
||||
'单聊'. $data['content'],
|
||||
$data['content'],
|
||||
);
|
||||
|
||||
MessageModel::create([
|
||||
'user_id' => $this->userId,
|
||||
'chat_room_id' => $chatRoomId,
|
||||
'to_user_id' => $toUserId,
|
||||
'replay_id' => $replayId,
|
||||
'type' => $type,
|
||||
'content' => $data['content'],
|
||||
'created_at' => time(),
|
||||
]);
|
||||
// $this->gatewayClient->broadcastMessage(
|
||||
// $this->userId,
|
||||
// $data['avatarColor'],
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace App\Services\GatewayWorker;
|
||||
|
||||
use App\Models\im\ChatRoomUserModel;
|
||||
use App\Models\UserModel;
|
||||
use App\Services\Base\BaseService;
|
||||
|
||||
class UserService
|
||||
class UserService extends BaseService
|
||||
{
|
||||
public function login($username, $password)
|
||||
{
|
||||
@@ -23,10 +25,11 @@ class UserService
|
||||
}
|
||||
}
|
||||
|
||||
public function register($username, $password, $nickName)
|
||||
public function register($username, $password, $nickName, $avatar)
|
||||
{
|
||||
$userModel = UserModel::create([
|
||||
'username' => $username,
|
||||
'$avatar' => $$avatar,
|
||||
'password' => md5($password),
|
||||
'nick_name' => $nickName,
|
||||
'created_at' => time()
|
||||
@@ -37,4 +40,23 @@ class UserService
|
||||
'user_info' => $userModel
|
||||
];
|
||||
}
|
||||
|
||||
public function userList()
|
||||
{
|
||||
$userModel = UserModel::where('id', '<>', $this->userId)->get();
|
||||
if (empty($userModel)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$myRooms = ChatRoomUserModel::where('user_id', $this->userId)->pluck('chat_room_id');
|
||||
$allRooms = ChatRoomUserModel::where('user_id', '<>', $this->userId)->whereIn('chat_room_id', $myRooms)->get();
|
||||
$roomsMap = array_column($allRooms->toArray(), 'chat_room_id', 'user_id');
|
||||
$userModel = $userModel->toArray();
|
||||
$gatewayClient = GatewayClientService::getInstance()->init();
|
||||
foreach ($userModel as &$v) {
|
||||
$v['is_uid_online'] = $gatewayClient->isUidOnline($v['id']);
|
||||
$v['chat_room_id'] = !empty($roomsMap[$v['id']])? $roomsMap[$v['id']] : 0;
|
||||
}
|
||||
return $userModel;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user