diff --git a/app/Constants/ResponseCode.php b/app/Constants/ResponseCode.php index 8f1e02b..317a6b4 100644 --- a/app/Constants/ResponseCode.php +++ b/app/Constants/ResponseCode.php @@ -2,24 +2,24 @@ namespace App\Constants; -use Hyperf\Constants\AbstractConstants; -use Hyperf\Constants\Annotation\Constants; - /** - * @Constants + * HTTP 响应状态码枚举 + * + * Class ResponseCode + * @package App\Constants */ -class ResponseCode extends AbstractConstants +class ResponseCode { const SUCCESS = 200; // 接口处理成功 const FAIL = 305; // 接口处理失败 /** - * @Message("Server Error!") + * Server Error! */ const SERVER_ERROR = 500; /** - * @Message("请求数据验证失败!") + * 请求数据验证失败! */ const VALIDATION_ERROR = 301; } diff --git a/app/Constants/SocketConstants.php b/app/Constants/SocketConstants.php index 091a1c8..c526170 100644 --- a/app/Constants/SocketConstants.php +++ b/app/Constants/SocketConstants.php @@ -39,4 +39,18 @@ class SocketConstants * WebSocket 消息消费队列交换机名称 */ const CONSUMER_MESSAGE_EXCHANGE = 'im.message.fanout'; + + /** + * @return array + */ + public static function getMap(): array + { + return [ + self::EVENT_TALK => '对话消息通知', + self::EVENT_KEYBOARD => '键盘输入事件通知', + self::EVENT_ONLINE_STATUS => '用户在线状态通知', + self::EVENT_REVOKE_TALK => '聊天消息撤销通知', + self::EVENT_FRIEND_APPLY => '好友申请消息通知' + ]; + } } diff --git a/app/Controller/Api/V1/ArticleController.php b/app/Controller/Api/V1/ArticleController.php index 6b4d24e..a0e54ee 100644 --- a/app/Controller/Api/V1/ArticleController.php +++ b/app/Controller/Api/V1/ArticleController.php @@ -1,5 +1,4 @@ producer->produce( - new ChatMessageProducer('event_talk', [ + new ChatMessageProducer(SocketConstants::EVENT_TALK, [ 'sender' => $user_id, //发送者ID 'receive' => intval($data['group_id']), //接收者ID 'source' => 2, //接收者类型 1:好友;2:群组 @@ -146,7 +147,7 @@ class GroupController extends CController // ...消息推送队列 $this->producer->produce( - new ChatMessageProducer('event_talk', [ + new ChatMessageProducer(SocketConstants::EVENT_TALK, [ 'sender' => $user_id, //发送者ID 'receive' => intval($params['group_id']), //接收者ID 'source' => 2, //接收者类型 1:好友;2:群组 @@ -180,7 +181,7 @@ class GroupController extends CController // ...消息推送队列 $this->producer->produce( - new ChatMessageProducer('event_talk', [ + new ChatMessageProducer(SocketConstants::EVENT_TALK, [ 'sender' => $user_id, //发送者ID 'receive' => intval($params['group_id']), //接收者ID 'source' => 2, //接收者类型 1:好友;2:群组 @@ -246,7 +247,7 @@ class GroupController extends CController // ...消息推送队列 $this->producer->produce( - new ChatMessageProducer('event_talk', [ + new ChatMessageProducer(SocketConstants::EVENT_TALK, [ 'sender' => $user_id, //发送者ID 'receive' => intval($params['group_id']), //接收者ID 'source' => 2, //接收者类型 1:好友;2:群组 diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index d771de3..5093c85 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -21,6 +21,7 @@ use App\Service\UploadService; use App\Amqp\Producer\ChatMessageProducer; use App\Cache\LastMsgCache; use App\Cache\UnreadTalkCache; +use App\Constants\SocketConstants; /** * Class TalkController @@ -229,7 +230,7 @@ class TalkController extends CController [$isTrue, $message,] = $this->talkService->revokeRecord($this->uid(), $params['record_id']); if ($isTrue) { $this->producer->produce( - new ChatMessageProducer('event_revoke_talk', [ + new ChatMessageProducer(SocketConstants::EVENT_REVOKE_TALK, [ 'record_id' => $params['record_id'] ]) ); @@ -326,7 +327,7 @@ class TalkController extends CController // ...消息推送队列 foreach ($ids as $value) { $this->producer->produce( - new ChatMessageProducer('event_talk', [ + new ChatMessageProducer(SocketConstants::EVENT_TALK, [ 'sender' => $user_id, //发送者ID 'receive' => intval($value['receive_id']), //接收者ID 'source' => intval($value['source']), //接收者类型 1:好友;2:群组 @@ -526,7 +527,7 @@ class TalkController extends CController // ...消息推送队列 $this->producer->produce( - new ChatMessageProducer('event_talk', [ + new ChatMessageProducer(SocketConstants::EVENT_TALK, [ 'sender' => $user_id, //发送者ID 'receive' => intval($params['receive_id']), //接收者ID 'source' => intval($params['source']), //接收者类型 1:好友;2:群组 @@ -571,7 +572,7 @@ class TalkController extends CController // ...消息推送队列 $this->producer->produce( - new ChatMessageProducer('event_talk', [ + new ChatMessageProducer(SocketConstants::EVENT_TALK, [ 'sender' => $user_id, //发送者ID 'receive' => intval($params['receive_id']), //接收者ID 'source' => intval($params['source']), //接收者类型 1:好友;2:群组 @@ -586,6 +587,9 @@ class TalkController extends CController * 发送文件消息 * * @RequestMapping(path="send-file", methods="post") + * + * @param UploadService $uploadService + * @return ResponseInterface */ public function sendFile(UploadService $uploadService) { @@ -633,7 +637,7 @@ class TalkController extends CController // ...消息推送队列 $this->producer->produce( - new ChatMessageProducer('event_talk', [ + new ChatMessageProducer(SocketConstants::EVENT_TALK, [ 'sender' => $user_id, //发送者ID 'receive' => intval($params['receive_id']), //接收者ID 'source' => intval($params['source']), //接收者类型 1:好友;2:群组 @@ -661,7 +665,7 @@ class TalkController extends CController $user_id = $this->uid(); $emoticon = EmoticonDetail::where('id', $params['emoticon_id'])->where('user_id', $user_id)->first([ - 'url','file_suffix','file_size' + 'url', 'file_suffix', 'file_size' ]); if (!$emoticon) { @@ -688,7 +692,7 @@ class TalkController extends CController // ...消息推送队列 $this->producer->produce( - new ChatMessageProducer('event_talk', [ + new ChatMessageProducer(SocketConstants::EVENT_TALK, [ 'sender' => $user_id, //发送者ID 'receive' => intval($params['receive_id']), //接收者ID 'source' => intval($params['source']), //接收者类型 1:好友;2:群组 diff --git a/app/Controller/Api/V1/UsersController.php b/app/Controller/Api/V1/UsersController.php index d391cf8..0005fb9 100644 --- a/app/Controller/Api/V1/UsersController.php +++ b/app/Controller/Api/V1/UsersController.php @@ -2,6 +2,7 @@ namespace App\Controller\Api\V1; +use App\Constants\SocketConstants; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; @@ -268,7 +269,7 @@ class UsersController extends CController //判断对方是否在线。如果在线发送消息通知 if ($this->socketClientService->isOnlineAll((int)$params['friend_id'])) { $this->producer->produce( - new ChatMessageProducer('event_friend_apply', [ + new ChatMessageProducer(SocketConstants::EVENT_FRIEND_APPLY, [ 'sender' => $user_id, 'receive' => (int)$params['friend_id'], 'type' => 1, @@ -304,7 +305,7 @@ class UsersController extends CController if ($this->socketClientService->isOnlineAll((int)$params['friend_id'])) { // 待修改 $this->producer->produce( - new ChatMessageProducer('event_friend_apply', [ + new ChatMessageProducer(SocketConstants::EVENT_FRIEND_APPLY, [ 'sender' => $user_id, 'receive' => (int)$params['friend_id'], 'type' => 1, diff --git a/app/Helper/StringHelper.php b/app/Helper/StringHelper.php new file mode 100644 index 0000000..74bce09 --- /dev/null +++ b/app/Helper/StringHelper.php @@ -0,0 +1,50 @@ + 'integer', 'record_id' => 'integer', 'user_id' => 'integer', 'created_at' => 'datetime']; + protected $casts = [ + 'id' => 'integer', + 'record_id' => 'integer', + 'user_id' => 'integer', + 'created_at' => 'datetime' + ]; } diff --git a/app/Model/FileSplitUpload.php b/app/Model/FileSplitUpload.php index 4966dd7..13d0af2 100644 --- a/app/Model/FileSplitUpload.php +++ b/app/Model/FileSplitUpload.php @@ -50,7 +50,6 @@ class FileSplitUpload extends BaseModel 'upload_at' ]; - /** * The attributes that should be cast to native types. * diff --git a/app/Model/Group/UsersGroup.php b/app/Model/Group/UsersGroup.php index 7008ea1..ed48a83 100644 --- a/app/Model/Group/UsersGroup.php +++ b/app/Model/Group/UsersGroup.php @@ -69,7 +69,8 @@ class UsersGroup extends BaseModel * @param int $group_id 群ID * @return mixed */ - public static function isManager(int $user_id,int $group_id){ + public static function isManager(int $user_id, int $group_id) + { return self::where('id', $group_id)->where('user_id', $user_id)->exists(); } @@ -82,6 +83,6 @@ class UsersGroup extends BaseModel */ public static function isMember(int $group_id, int $user_id) { - return UsersGroupMember::where('group_id', $group_id)->where('user_id', $user_id)->where('status', 0)->exists() ? true : false; + return UsersGroupMember::where('group_id', $group_id)->where('user_id', $user_id)->where('status', 0)->exists(); } } diff --git a/app/Model/UsersChatList.php b/app/Model/UsersChatList.php index 4f5ff4e..eb81605 100644 --- a/app/Model/UsersChatList.php +++ b/app/Model/UsersChatList.php @@ -118,7 +118,10 @@ class UsersChatList extends BaseModel */ public static function topItem(int $user_id, int $list_id, $is_top = true) { - return (bool)self::where('id', $list_id)->where('uid', $user_id)->update(['is_top' => $is_top ? 1 : 0, 'updated_at' => date('Y-m-d H:i:s')]); + return (bool)self::where('id', $list_id)->where('uid', $user_id)->update([ + 'is_top' => $is_top ? 1 : 0, + 'updated_at' => date('Y-m-d H:i:s') + ]); } /** @@ -131,12 +134,13 @@ class UsersChatList extends BaseModel */ public static function delItem(int $user_id, int $id, $type = 1) { + $data = ['status' => 0, 'updated_at' => date('Y-m-d H:i:s')]; if ($type == 1) { - return (bool)self::where('id', $id)->where('uid', $user_id)->update(['status' => 0, 'updated_at' => date('Y-m-d H:i:s')]); + return (bool)self::where('id', $id)->where('uid', $user_id)->update($data); } else if ($type == 2) { - return (bool)self::where('uid', $user_id)->where('friend_id', $id)->update(['status' => 0, 'updated_at' => date('Y-m-d H:i:s')]); + return (bool)self::where('uid', $user_id)->where('friend_id', $id)->update($data); } else { - return (bool)self::where('uid', $user_id)->where('group_id', $id)->update(['status' => 0, 'updated_at' => date('Y-m-d H:i:s')]); + return (bool)self::where('uid', $user_id)->where('group_id', $id)->update($data); } } diff --git a/app/Model/UsersFriend.php b/app/Model/UsersFriend.php index 1d92bf4..4d7a0b1 100644 --- a/app/Model/UsersFriend.php +++ b/app/Model/UsersFriend.php @@ -76,7 +76,7 @@ class UsersFriend extends BaseModel SELECT id as rid,user2 as uid,user1_remark as friend_remark from {$prefix}users_friends where user1 = {$uid} and `status` = 1 UNION all SELECT id as rid,user1 as uid,user2_remark as friend_remark from {$prefix}users_friends where user2 = {$uid} and `status` = 1 - ) tmp_table on tmp_table.uid = users.id order by tmp_table.rid desc + ) tmp_table on tmp_table.uid = users.id SQL; $rows = Db::select($sql); diff --git a/app/Service/TalkService.php b/app/Service/TalkService.php index f3fcc38..e6da1ac 100644 --- a/app/Service/TalkService.php +++ b/app/Service/TalkService.php @@ -240,7 +240,7 @@ class TalkService extends BaseService * @param int $record_id 上一次查询的聊天记录ID * @param int $limit 查询数据长度 * @param array $msg_type 消息类型 - * @return mixed + * @return array */ public function getChatRecords(int $user_id, int $receive_id, int $source, int $record_id, $limit = 30, $msg_type = []) { @@ -417,7 +417,6 @@ class TalkService extends BaseService * @param int $record_id 转发消息的记录ID * @param array $receive_ids 接受者数组 例如:[['source' => 1,'id' => 3045],['source' => 1,'id' => 3046],['source' => 1,'id' => 1658]] 二维数组 * @return array - * @throws \Exception */ public function forwardRecords(int $user_id, int $record_id, array $receive_ids) { @@ -477,7 +476,7 @@ class TalkService extends BaseService 'save_dir' => $fileInfo->save_dir, 'created_at' => date('Y-m-d H:i:s') ])) { - throw new \Exception('插入文件消息记录失败'); + throw new Exception('插入文件消息记录失败'); } } else if ($result->msg_type == 5) { if (!ChatRecordsCode::create([ @@ -487,7 +486,7 @@ class TalkService extends BaseService 'code' => $codeBlock->code, 'created_at' => date('Y-m-d H:i:s') ])) { - throw new \Exception('插入代码消息记录失败'); + throw new Exception('插入代码消息记录失败'); } } } @@ -696,17 +695,17 @@ class TalkService extends BaseService $insert = ChatRecord::create($message); if (!$insert) { - throw new \Exception('插入聊天记录失败...'); + throw new Exception('插入聊天记录失败...'); } $fileInfo['record_id'] = $insert->id; $fileInfo['created_at'] = date('Y-m-d H:i:s'); if (!ChatRecordsFile::create($fileInfo)) { - throw new \Exception('插入聊天记录(文件消息)失败...'); + throw new Exception('插入聊天记录(文件消息)失败...'); } Db::commit(); - } catch (\Exception $e) { + } catch (Exception $e) { Db::rollBack(); return false; } @@ -728,17 +727,17 @@ class TalkService extends BaseService $message['created_at'] = date('Y-m-d H:i:s'); $insert = ChatRecord::create($message); if (!$insert) { - throw new \Exception('插入聊天记录失败...'); + throw new Exception('插入聊天记录失败...'); } $codeBlock['record_id'] = $insert->id; $codeBlock['created_at'] = date('Y-m-d H:i:s'); if (!ChatRecordsCode::create($codeBlock)) { - throw new \Exception('插入聊天记录(代码消息)失败...'); + throw new Exception('插入聊天记录(代码消息)失败...'); } Db::commit(); - } catch (\Exception $e) { + } catch (Exception $e) { Db::rollBack(); return false; } @@ -760,17 +759,17 @@ class TalkService extends BaseService $message['created_at'] = date('Y-m-d H:i:s'); $insert = ChatRecord::create($message); if (!$insert) { - throw new \Exception('插入聊天记录失败...'); + throw new Exception('插入聊天记录失败...'); } $emoticon['record_id'] = $insert->id; $emoticon['created_at'] = date('Y-m-d H:i:s'); if (!ChatRecordsFile::create($emoticon)) { - throw new \Exception('插入聊天记录(代码消息)失败...'); + throw new Exception('插入聊天记录(代码消息)失败...'); } Db::commit(); - } catch (\Exception $e) { + } catch (Exception $e) { Db::rollBack(); return false; } @@ -792,17 +791,17 @@ class TalkService extends BaseService $message['created_at'] = date('Y-m-d H:i:s'); $insert = ChatRecord::create($message); if (!$insert) { - throw new \Exception('插入聊天记录失败...'); + throw new Exception('插入聊天记录失败...'); } $emoticon['record_id'] = $insert->id; $emoticon['created_at'] = date('Y-m-d H:i:s'); if (!ChatRecordsFile::create($emoticon)) { - throw new \Exception('插入聊天记录(代码消息)失败...'); + throw new Exception('插入聊天记录(代码消息)失败...'); } Db::commit(); - } catch (\Exception $e) { + } catch (Exception $e) { Db::rollBack(); return false; } diff --git a/app/helper.php b/app/helper.php index 1f3ec3f..6a2eaa0 100644 --- a/app/helper.php +++ b/app/helper.php @@ -38,7 +38,7 @@ function redis() } /** - * server 实例 基于 swoole server + * Server 实例 基于 Swoole Server * * @return \Swoole\Coroutine\Server|\Swoole\Server */ @@ -48,7 +48,9 @@ function server() } /** - * websocket frame 实例 + * WebSocket frame 实例 + * + * @return mixed|Frame */ function frame() { @@ -56,7 +58,9 @@ function frame() } /** - * websocket 实例 + * WebSocketServer 实例 + * + * @return mixed|WebSocketServer */ function websocket() { @@ -65,6 +69,8 @@ function websocket() /** * 缓存实例 简单的缓存 + * + * @return mixed|\Psr\SimpleCache\CacheInterface */ function cache() { @@ -73,6 +79,8 @@ function cache() /** * 控制台日志 + * + * @return StdoutLoggerInterface|mixed */ function stdout_log() { @@ -112,7 +120,9 @@ function response() /** * 从HTML文本中提取所有图片 - * @param $content + * + * @param string $content HTML文本 + * * @return array */ function get_html_images($content) @@ -133,8 +143,9 @@ function get_html_images($content) /** * 获取两个日期相差多少天 * - * @param $day1 - * @param $day2 + * @param string $day1 日期1 + * @param string $day2 日期2 + * * @return float|int */ function diff_date($day1, $day2) @@ -153,6 +164,7 @@ function diff_date($day1, $day2) * 获取媒体文件url * * @param string $path 文件相对路径 + * * @return string */ function get_media_url(string $path) @@ -166,6 +178,7 @@ function get_media_url(string $path) * @param string $ext 图片后缀名 * @param int $width 图片宽度 * @param int $height 图片高度 + * * @return string */ function create_image_name(string $ext, int $width, int $height) @@ -176,7 +189,8 @@ function create_image_name(string $ext, int $width, int $height) /** * 替换文本中的url 为 a标签 * - * @param string $str + * @param string $str 字符串 + * * @return null|string|string[] */ function replace_url_link(string $str) diff --git a/migrations/2020_11_04_153553_create_users_friends_table.php b/migrations/2020_11_04_153553_create_users_friends_table.php index a1f6396..ac0dacd 100644 --- a/migrations/2020_11_04_153553_create_users_friends_table.php +++ b/migrations/2020_11_04_153553_create_users_friends_table.php @@ -4,6 +4,7 @@ use Hyperf\Database\Schema\Schema; use Hyperf\Database\Schema\Blueprint; use Hyperf\Database\Migrations\Migration; use Hyperf\DbConnection\Db; + class CreateUsersFriendsTable extends Migration { /** @@ -27,6 +28,7 @@ class CreateUsersFriendsTable extends Migration $table->engine = 'InnoDB'; $table->index(['user1', 'user2'], 'idx_user1_user2'); + $table->index(['user2', 'user1'], 'idx_user2_user1'); }); $prefix = config('databases.default.prefix');