优化代码

main
gzydong 2021-08-10 21:21:26 +08:00
parent 0f18655569
commit c1465d9963
4 changed files with 28 additions and 29 deletions

View File

@ -9,7 +9,7 @@ Lumen-IM 是一个网页版在线即时聊天项目,前端使用 Element-ui +
- 基于 Swoole WebSocket 服务做消息即时推送 - 基于 Swoole WebSocket 服务做消息即时推送
- 支持私聊及群聊 - 支持私聊及群聊
- 支持聊天消息类型有文本、代码块、图片及其它类型文件,并支持文件下载 - 支持聊天消息类型有文本、代码块、图片及其它类型文件,并支持文件下载
- 支持聊天消息撤回、删除或批量删除、转发消息(逐条转发、合并转发) - 支持聊天消息撤回、删除或批量删除、转发消息(逐条转发、合并转发)及群投票功能
- 支持编写个人笔记、支持笔记分享(好友或群) - 支持编写个人笔记、支持笔记分享(好友或群)
## 2、项目DEMO ## 2、项目DEMO

View File

@ -35,7 +35,7 @@ class AppExceptionHandler extends ExceptionHandler
$data = json_encode([ $data = json_encode([
'code' => ResponseCode::SERVER_ERROR, 'code' => ResponseCode::SERVER_ERROR,
'error' => $isDev ? $throwable->getMessage() : 'Internal Server Error.', 'error' => $isDev ? $throwable->getMessage() : 'Internal Server Error.',
'traces' => $isDev ? $throwable->getTrace() : [], 'traces' => $isDev ? $throwable->getLine() : [],
], JSON_UNESCAPED_UNICODE); ], JSON_UNESCAPED_UNICODE);
!$isDev && $this->sendAdminEmail($throwable); !$isDev && $this->sendAdminEmail($throwable);

View File

@ -12,7 +12,6 @@ use App\Constants\TalkMessageType;
use App\Constants\TalkModeConstant; use App\Constants\TalkModeConstant;
use App\Event\TalkEvent; use App\Event\TalkEvent;
use App\Model\Group\GroupMember; use App\Model\Group\GroupMember;
use App\Model\Talk\TalkList;
use App\Model\Talk\TalkRecordsCode; use App\Model\Talk\TalkRecordsCode;
use App\Model\Talk\TalkRecordsLogin; use App\Model\Talk\TalkRecordsLogin;
use App\Model\Talk\TalkRecordsVote; use App\Model\Talk\TalkRecordsVote;
@ -32,10 +31,10 @@ class TalkMessageService
* @param array $message * @param array $message
* @return bool * @return bool
*/ */
public function insertTextMessage(array $message) public function insertTextMessage(array $message): bool
{ {
$message['msg_type'] = TalkMessageType::TEXT_MESSAGE; $message['msg_type'] = TalkMessageType::TEXT_MESSAGE;
$message['content'] = htmlspecialchars($message['content']); $message['content'] = htmlspecialchars($message['content']);
$message['created_at'] = date('Y-m-d H:i:s'); $message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s'); $message['updated_at'] = date('Y-m-d H:i:s');
@ -70,11 +69,11 @@ class TalkMessageService
* @param array $code * @param array $code
* @return bool * @return bool
*/ */
public function insertCodeMessage(array $message, array $code) public function insertCodeMessage(array $message, array $code): bool
{ {
Db::beginTransaction(); Db::beginTransaction();
try { try {
$message['msg_type'] = TalkMessageType::CODE_MESSAGE; $message['msg_type'] = TalkMessageType::CODE_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s'); $message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s'); $message['updated_at'] = date('Y-m-d H:i:s');
@ -83,7 +82,7 @@ class TalkMessageService
throw new Exception('插入聊天记录失败...'); throw new Exception('插入聊天记录失败...');
} }
$code['record_id'] = $insert->id; $code['record_id'] = $insert->id;
$code['created_at'] = date('Y-m-d H:i:s'); $code['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsCode::create($code)) { if (!TalkRecordsCode::create($code)) {
throw new Exception('插入聊天记录(代码消息)失败...'); throw new Exception('插入聊天记录(代码消息)失败...');
@ -117,11 +116,11 @@ class TalkMessageService
* @param array $file * @param array $file
* @return bool * @return bool
*/ */
public function insertFileMessage(array $message, array $file) public function insertFileMessage(array $message, array $file): bool
{ {
Db::beginTransaction(); Db::beginTransaction();
try { try {
$message['msg_type'] = TalkMessageType::FILE_MESSAGE; $message['msg_type'] = TalkMessageType::FILE_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s'); $message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s'); $message['updated_at'] = date('Y-m-d H:i:s');
@ -130,8 +129,8 @@ class TalkMessageService
throw new Exception('插入聊天记录失败...'); throw new Exception('插入聊天记录失败...');
} }
$file['record_id'] = $insert->id; $file['record_id'] = $insert->id;
$file['file_type'] = MediaTypeConstant::getMediaType($file['file_suffix']); $file['file_type'] = MediaTypeConstant::getMediaType($file['file_suffix']);
$file['created_at'] = date('Y-m-d H:i:s'); $file['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsFile::create($file)) { if (!TalkRecordsFile::create($file)) {
throw new Exception('插入聊天记录(代码消息)失败...'); throw new Exception('插入聊天记录(代码消息)失败...');
@ -165,27 +164,27 @@ class TalkMessageService
* @param array $vote * @param array $vote
* @return bool * @return bool
*/ */
public function insertVoteMessage(array $message, array $vote) public function insertVoteMessage(array $message, array $vote): bool
{ {
$answer_num = GroupMember::where('group_id', $message['receiver_id'])->where('is_quit', 0)->count(); $answer_num = GroupMember::where('group_id', $message['receiver_id'])->where('is_quit', 0)->count();
Db::beginTransaction(); Db::beginTransaction();
try { try {
$message['msg_type'] = TalkMessageType::VOTE_MESSAGE; $message['msg_type'] = TalkMessageType::VOTE_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s'); $message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s'); $message['updated_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message); $insert = TalkRecords::create($message);
$options = []; $options = [];
foreach ($vote['answer_option'] as $k => $option) { foreach ($vote['answer_option'] as $k => $option) {
$options[chr(65 + $k)] = $option; $options[chr(65 + $k)] = $option;
} }
$vote['record_id'] = $insert->id; $vote['record_id'] = $insert->id;
$vote['answer_option'] = $options; $vote['answer_option'] = $options;
$vote['answer_num'] = $answer_num; $vote['answer_num'] = $answer_num;
$vote['created_at'] = date('Y-m-d H:i:s'); $vote['created_at'] = date('Y-m-d H:i:s');
$vote['updated_at'] = $vote['created_at']; $vote['updated_at'] = $vote['created_at'];
if (!TalkRecordsVote::create($vote)) { if (!TalkRecordsVote::create($vote)) {
throw new Exception('插入聊天记录(投票消息)失败...'); throw new Exception('插入聊天记录(投票消息)失败...');
@ -216,7 +215,7 @@ class TalkMessageService
* *
* @param int $user_id * @param int $user_id
* @param array $params * @param array $params
* @return bool * @return array
*/ */
public function handleVote(int $user_id, array $params): array public function handleVote(int $user_id, array $params): array
{ {
@ -230,7 +229,7 @@ class TalkMessageService
'vote.id as vote_id', 'vote.answer_mode', 'vote.answer_option', 'vote.answer_num', 'vote.status as vote_status' 'vote.id as vote_id', 'vote.answer_mode', 'vote.answer_option', 'vote.answer_num', 'vote.status as vote_status'
]); ]);
if (!$record) return false; if (!$record) return [false, []];
if ($record->msg_type != TalkMessageType::VOTE_MESSAGE) { if ($record->msg_type != TalkMessageType::VOTE_MESSAGE) {
return [false, []]; return [false, []];
@ -294,22 +293,22 @@ class TalkMessageService
* @param array $loginParams * @param array $loginParams
* @return bool * @return bool
*/ */
public function insertLoginMessage(array $message, array $loginParams) public function insertLoginMessage(array $message, array $loginParams): bool
{ {
Db::beginTransaction(); Db::beginTransaction();
try { try {
$message['receiver_id'] = RobotConstant::LOGIN_ROBOT; $message['receiver_id'] = RobotConstant::LOGIN_ROBOT;
$message['talk_type'] = TalkModeConstant::PRIVATE_CHAT; $message['talk_type'] = TalkModeConstant::PRIVATE_CHAT;
$message['msg_type'] = TalkMessageType::USER_LOGIN_MESSAGE; $message['msg_type'] = TalkMessageType::USER_LOGIN_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s'); $message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s'); $message['updated_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message); $insert = TalkRecords::create($message);
if (!$insert) { if (!$insert) {
throw new Exception('插入聊天记录失败...'); throw new Exception('插入聊天记录失败...');
} }
$loginParams['record_id'] = $insert->id; $loginParams['record_id'] = $insert->id;
$loginParams['created_at'] = date('Y-m-d H:i:s'); $loginParams['created_at'] = date('Y-m-d H:i:s');
if (!TalkRecordsLogin::create($loginParams)) { if (!TalkRecordsLogin::create($loginParams)) {

View File

@ -16,7 +16,7 @@ class CreateArticleTable extends Migration
$table->unsignedInteger('id', true)->comment('笔记ID'); $table->unsignedInteger('id', true)->comment('笔记ID');
$table->unsignedInteger('user_id')->default(0)->comment('用户ID'); $table->unsignedInteger('user_id')->default(0)->comment('用户ID');
$table->unsignedInteger('class_id')->default(0)->comment('分类ID'); $table->unsignedInteger('class_id')->default(0)->comment('分类ID');
$table->string('tags_id', 20)->default('')->comment('笔记关联标签'); $table->string('tags_id', 100)->default('')->comment('笔记关联标签');
$table->string('title', 80)->default('')->charset('utf8mb4')->comment('笔记标题'); $table->string('title', 80)->default('')->charset('utf8mb4')->comment('笔记标题');
$table->string('abstract', 200)->default('')->charset('utf8mb4')->comment('笔记摘要'); $table->string('abstract', 200)->default('')->charset('utf8mb4')->comment('笔记摘要');
$table->string('image', 255)->default('')->comment('笔记首图'); $table->string('image', 255)->default('')->comment('笔记首图');