diff --git a/SweetChatService/Controllers/UserController.cs b/SweetChatService/Controllers/UserController.cs new file mode 100644 index 0000000..aa98ad9 --- /dev/null +++ b/SweetChatService/Controllers/UserController.cs @@ -0,0 +1,25 @@ +using Microsoft.AspNetCore.Mvc; +using SweetChatService.Dto; +using SweetChatService.Entity; +using SweetChatService.Services; + +namespace SweetChatService.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class UserController : ControllerBase + { + private readonly UserService _userService; + + public UserController(UserService userService) + { + _userService = userService; + } + + [HttpGet("friend/{userId}")] + public IActionResult QueryFriendByUserId(long userId) + { + return Ok(_userService.QueryUserFriends(userId)); + } + } +} diff --git a/SweetChatService/Entity/Friend.cs b/SweetChatService/Entity/Friend.cs new file mode 100644 index 0000000..66e7f2a --- /dev/null +++ b/SweetChatService/Entity/Friend.cs @@ -0,0 +1,30 @@ +using FreeSql.DataAnnotations; + +namespace SweetChatService.Entity +{ + [Table(Name = "friend")] + [Index("uk_user_friend", "UserId,FriendId", true)] + public class Friend + { + [Column(Name = "id", IsPrimary = true, IsIdentity = true)] + public long Id { get; set; } + + [Column(Name = "user_id")] + public long UserId { get; set; } + + [Column(Name = "friend_id")] + public long FriendId { get; set; } + + [Column(Name = "remark", StringLength = 64)] + public string? Remark { get; set; } + + [Column(Name = "status")] + public byte Status { get; set; } = 1; + + [Column(Name = "created_time", ServerTime = DateTimeKind.Local, CanInsert = true, CanUpdate = false)] + public DateTime? CreatedTime { get; set; } + + [Column(Name = "update_time", ServerTime = DateTimeKind.Local)] + public DateTime UpdateTime { get; set; } + } +} \ No newline at end of file diff --git a/SweetChatService/Entity/User.cs b/SweetChatService/Entity/User.cs index 5d39689..57d99c7 100644 --- a/SweetChatService/Entity/User.cs +++ b/SweetChatService/Entity/User.cs @@ -20,7 +20,7 @@ namespace SweetChatService.Entity [Column(Name = "gender")] public byte Gender { get; set; } = 0; - [Column(Name = "create_time", ServerTime = DateTimeKind.Local, CanUpdate = false)] + [Column(Name = "create_time", ServerTime = DateTimeKind.Local, CanInsert = true, CanUpdate = false)] public DateTime CreateTime { get; set; } [Column(Name = "update_time", ServerTime = DateTimeKind.Local)] diff --git a/SweetChatService/Program.cs b/SweetChatService/Program.cs index b074071..82e1cb8 100644 --- a/SweetChatService/Program.cs +++ b/SweetChatService/Program.cs @@ -1,14 +1,9 @@ -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using NLog.Extensions.Hosting; +using NLog.Extensions.Hosting; using SuperSocket.Server.Host; using SuperSocket.WebSocket.Server; using SweetChatService.Config; using SweetChatService.Handlers; using SweetChatService.Services; -using System.IO; var host = Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => diff --git a/SweetChatService/Properties/launchSettings.json b/SweetChatService/Properties/launchSettings.json index 3fc5a9b..0648ee6 100644 --- a/SweetChatService/Properties/launchSettings.json +++ b/SweetChatService/Properties/launchSettings.json @@ -6,7 +6,7 @@ "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, - "applicationUrl": "https://localhost:2835;http://localhost:2836" + "applicationUrl": "https://0.0.0.0:2835;http://0.0.0.0:2836" } } } \ No newline at end of file diff --git a/SweetChatService/Services/UserService.cs b/SweetChatService/Services/UserService.cs index 1cffa95..dcfc2b9 100644 --- a/SweetChatService/Services/UserService.cs +++ b/SweetChatService/Services/UserService.cs @@ -14,5 +14,13 @@ namespace SweetChatService.Services .Where(u => u.Nickname == username) .First(); } + + public List QueryUserFriends(long userId) + { + return _fsql.Select() + .InnerJoin((u, f) => u.Id == f.FriendId) + .Where((u, f) => f.UserId == userId && f.Status == 1) + .ToList((u, f) => u); + } } } diff --git a/SweetChatService/appsettings.json b/SweetChatService/appsettings.json index 38e2d10..011ef2f 100644 --- a/SweetChatService/appsettings.json +++ b/SweetChatService/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "Default": "Server=localhost;Port=3306;Database=sweet_chat;Uid=root;Pwd=estun@medical;Charset=utf8mb4;" + "Default": "Server=localhost;Port=3306;Database=sweet_chat;Uid=root;Pwd=123456;Charset=utf8mb4;" }, "serverOptions": { "WebSocketServer": { diff --git a/SweetChatService/sweet_chat.sql b/SweetChatService/sweet_chat.sql index eac411a..c2454eb 100644 --- a/SweetChatService/sweet_chat.sql +++ b/SweetChatService/sweet_chat.sql @@ -1,141 +1,123 @@ --- MySQL dump 10.13 Distrib 8.0.27, for Win64 (x86_64) --- --- Host: 127.0.0.1 Database: sweet_chat --- ------------------------------------------------------ --- Server version 8.0.27 +/* + Navicat Premium Data Transfer -/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; -/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; -/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; -/*!50503 SET NAMES utf8 */; -/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; -/*!40103 SET TIME_ZONE='+00:00' */; -/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; -/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; -/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; -/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + Source Server : localhost + Source Server Type : MySQL + Source Server Version : 80027 + Source Host : localhost:3306 + Source Schema : sweet_chat --- --- Table structure for table `conversation` --- + Target Server Type : MySQL + Target Server Version : 80027 + File Encoding : 65001 + Date: 17/06/2026 23:41:13 +*/ + +SET NAMES utf8mb4; +SET FOREIGN_KEY_CHECKS = 0; + +-- ---------------------------- +-- Table structure for conversation +-- ---------------------------- DROP TABLE IF EXISTS `conversation`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!50503 SET character_set_client = utf8mb4 */; -CREATE TABLE `conversation` ( +CREATE TABLE `conversation` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '会话ID', `type` tinyint NOT NULL COMMENT '1单聊 2群聊', - `last_msg_id` bigint DEFAULT '0' COMMENT '最后一条消息ID', + `last_msg_id` bigint NULL DEFAULT 0 COMMENT '最后一条消息ID', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', - PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='会话表'; -/*!40101 SET character_set_client = @saved_cs_client */; + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '会话表' ROW_FORMAT = Dynamic; --- --- Dumping data for table `conversation` --- - -LOCK TABLES `conversation` WRITE; -/*!40000 ALTER TABLE `conversation` DISABLE KEYS */; -INSERT INTO `conversation` VALUES (1,1,2,'2026-06-17 13:45:29','2026-06-17 13:50:21'); -/*!40000 ALTER TABLE `conversation` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `conversation_member` --- +-- ---------------------------- +-- Records of conversation +-- ---------------------------- +INSERT INTO `conversation` VALUES (1, 1, 2, '2026-06-17 13:45:29', '2026-06-17 13:50:21'); +-- ---------------------------- +-- Table structure for conversation_member +-- ---------------------------- DROP TABLE IF EXISTS `conversation_member`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!50503 SET character_set_client = utf8mb4 */; -CREATE TABLE `conversation_member` ( +CREATE TABLE `conversation_member` ( `conversation_id` bigint NOT NULL COMMENT '会话ID', `user_id` bigint NOT NULL COMMENT '用户ID', - `unread_count` int DEFAULT '0' COMMENT '未读消息数', - `last_read_msg_id` bigint DEFAULT '0' COMMENT '已读到最后一条消息ID', + `unread_count` int NULL DEFAULT 0 COMMENT '未读消息数', + `last_read_msg_id` bigint NULL DEFAULT 0 COMMENT '已读到最后一条消息ID', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', - PRIMARY KEY (`conversation_id`,`user_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='会话成员表'; -/*!40101 SET character_set_client = @saved_cs_client */; + PRIMARY KEY (`conversation_id`, `user_id`) USING BTREE +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '会话成员表' ROW_FORMAT = Dynamic; --- --- Dumping data for table `conversation_member` --- +-- ---------------------------- +-- Records of conversation_member +-- ---------------------------- +INSERT INTO `conversation_member` VALUES (1, 1, 1, 0, '2026-06-17 13:46:18', '2026-06-17 13:50:36'); +INSERT INTO `conversation_member` VALUES (1, 2, 1, 0, '2026-06-17 13:46:18', '2026-06-17 13:47:27'); -LOCK TABLES `conversation_member` WRITE; -/*!40000 ALTER TABLE `conversation_member` DISABLE KEYS */; -INSERT INTO `conversation_member` VALUES (1,1,1,0,'2026-06-17 13:46:18','2026-06-17 13:50:36'),(1,2,1,0,'2026-06-17 13:46:18','2026-06-17 13:47:27'); -/*!40000 ALTER TABLE `conversation_member` ENABLE KEYS */; -UNLOCK TABLES; +-- ---------------------------- +-- Table structure for friend +-- ---------------------------- +DROP TABLE IF EXISTS `friend`; +CREATE TABLE `friend` ( + `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增主键', + `user_id` bigint UNSIGNED NOT NULL COMMENT '用户ID(主动方)', + `friend_id` bigint UNSIGNED NOT NULL COMMENT '好友用户ID', + `remark` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '好友备注名', + `status` tinyint NULL DEFAULT 1 COMMENT '0=待验证 1=正常 2=拉黑', + `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + `update_time` datetime NOT NULL COMMENT '更新时间', + PRIMARY KEY (`id`) USING BTREE, + UNIQUE INDEX `uk_user_friend`(`user_id` ASC, `friend_id` ASC) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '好友关系表' ROW_FORMAT = Dynamic; --- --- Table structure for table `message` --- +-- ---------------------------- +-- Records of friend +-- ---------------------------- +INSERT INTO `friend` VALUES (1, 1, 2, NULL, 1, '2026-06-17 21:04:33', '2026-06-17 21:12:40'); +INSERT INTO `friend` VALUES (2, 2, 1, NULL, 1, '2026-06-17 21:06:59', '2026-06-17 21:12:43'); +-- ---------------------------- +-- Table structure for message +-- ---------------------------- DROP TABLE IF EXISTS `message`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!50503 SET character_set_client = utf8mb4 */; -CREATE TABLE `message` ( +CREATE TABLE `message` ( `msg_id` bigint NOT NULL AUTO_INCREMENT COMMENT '消息ID', `conversation_id` bigint NOT NULL COMMENT '会话ID', `sender_id` bigint NOT NULL COMMENT '发送者ID', - `content` text NOT NULL COMMENT '消息内容', - `msg_type` tinyint DEFAULT '1' COMMENT '1文本 2图片 3语音', + `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '消息内容', + `msg_type` tinyint NULL DEFAULT 1 COMMENT '1文本 2图片 3语音', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', - PRIMARY KEY (`msg_id`) -) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='消息表'; -/*!40101 SET character_set_client = @saved_cs_client */; + PRIMARY KEY (`msg_id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '消息表' ROW_FORMAT = Dynamic; --- --- Dumping data for table `message` --- - -LOCK TABLES `message` WRITE; -/*!40000 ALTER TABLE `message` DISABLE KEYS */; -INSERT INTO `message` VALUES (1,1,1,'张三,在吗?',1,'2026-06-17 13:46:35','2026-06-17 13:46:35'),(2,1,2,'在,咋了?',1,'2026-06-17 13:49:48','2026-06-17 13:49:48'); -/*!40000 ALTER TABLE `message` ENABLE KEYS */; -UNLOCK TABLES; - --- --- Table structure for table `user` --- +-- ---------------------------- +-- Records of message +-- ---------------------------- +INSERT INTO `message` VALUES (1, 1, 1, '张三,在吗?', 1, '2026-06-17 13:46:35', '2026-06-17 13:46:35'); +INSERT INTO `message` VALUES (2, 1, 2, '在,咋了?', 1, '2026-06-17 13:49:48', '2026-06-17 13:49:48'); +-- ---------------------------- +-- Table structure for user +-- ---------------------------- DROP TABLE IF EXISTS `user`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!50503 SET character_set_client = utf8mb4 */; -CREATE TABLE `user` ( +CREATE TABLE `user` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '用户ID', - `phone` varchar(20) NOT NULL COMMENT '手机号', - `nickname` varchar(50) NOT NULL COMMENT '昵称', - `avatar_url` varchar(255) DEFAULT '' COMMENT '头像', - `gender` tinyint DEFAULT '0' COMMENT '0未知 1男 2女', + `phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '手机号', + `nickname` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '昵称', + `avatar_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '' COMMENT '头像', + `gender` tinyint NULL DEFAULT 0 COMMENT '0未知 1男 2女', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', - PRIMARY KEY (`id`), - UNIQUE KEY `phone` (`phone`) -) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户表'; -/*!40101 SET character_set_client = @saved_cs_client */; + PRIMARY KEY (`id`) USING BTREE, + UNIQUE INDEX `phone`(`phone` ASC) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '用户表' ROW_FORMAT = Dynamic; --- --- Dumping data for table `user` --- +-- ---------------------------- +-- Records of user +-- ---------------------------- +INSERT INTO `user` VALUES (1, '13800000001', 'Cxx', 'https://images.xxapi.cn/images/head/7679300522.jpg', 1, '2026-06-17 13:42:21', '2026-06-17 21:30:49'); +INSERT INTO `user` VALUES (2, '13800000002', 'Pjm', 'https://images.xxapi.cn/images/head/5963813620.jpg', 1, '2026-06-17 13:42:21', '2026-06-17 21:30:46'); -LOCK TABLES `user` WRITE; -/*!40000 ALTER TABLE `user` DISABLE KEYS */; -INSERT INTO `user` VALUES (1,'13800000001','Cxx','',1,'2026-06-17 13:42:21','2026-06-17 13:42:21'),(2,'13800000002','Pjm','',1,'2026-06-17 13:42:21','2026-06-17 18:18:16'); -/*!40000 ALTER TABLE `user` ENABLE KEYS */; -UNLOCK TABLES; -/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; - -/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; -/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; -/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; -/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; -/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; -/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; -/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; - --- Dump completed on 2026-06-17 19:53:25 +SET FOREIGN_KEY_CHECKS = 1;