From 1796639fad7d0834be93a4557fe3b7cfe4a650ee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dear=E7=81=AC=E9=AD=84?= <15033850348@163.com>
Date: Wed, 10 Jul 2024 16:17:51 +0800
Subject: [PATCH] =?UTF-8?q?=E8=81=8A=E5=A4=A9=E5=AE=8C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
core/app.js | 29 +++-
core/module/module.js | 25 +++-
core/routes/index.js | 124 ++++++++----------
core/routes/sokio.js | 12 +-
yunyou_pavilion/src/components/Tabber.vue | 2 +-
yunyou_pavilion/src/router/index.js | 15 ++-
yunyou_pavilion/src/views/Chitchat/Chat.vue | 101 +++++++++++++-
yunyou_pavilion/src/views/Chitchat/socket.vue | 36 ++---
8 files changed, 216 insertions(+), 128 deletions(-)
diff --git a/core/app.js b/core/app.js
index 96bdedd..48046d2 100644
--- a/core/app.js
+++ b/core/app.js
@@ -7,10 +7,15 @@ var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
var cors = require("cors")
+const {
+ UserModel,
+ FriendModel, //好友
+ RecordModel, //记录
+} = require('./module/module')
const bodyParser = require('body-parser')
// app.use('/upload',express.static(__dirname,'upload'))
// app.use('/upload',express.static(__dirname+'public'))
-app.use(bodyParser.urlencoded({extends:true}))
+app.use(bodyParser.urlencoded({ extends: true }))
app.use(bodyParser.json())
// app.post('/upload',function(req,res){
// const form = new multiparty.Form({uploadDir:'temp'})
@@ -28,7 +33,7 @@ app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
-app.use('/upload',express.static(path.join(__dirname, 'upload')));
+app.use('/upload', express.static(path.join(__dirname, 'upload')));
app.use(auth) //将中间件使用
app.use('/', indexRouter);
app.use('/users', usersRouter);
@@ -48,6 +53,7 @@ app.use(function (err, req, res, next) {
//聊天部分
const { createServer } = require("http");
+const { time } = require('console');
const httpServer = createServer();
// socket.getSocket(httpServer);
const io = require("socket.io")(httpServer, {
@@ -57,15 +63,26 @@ const io = require("socket.io")(httpServer, {
io.on('connection', (socket) => {
// console.log('socket.handshake.headers ===',socket.handshake.headers.user_id);
socket.join(socket.handshake.headers.user_id)
- console.log(123123213);
+ console.log(socket.handshake.headers.user_id);
+ // console.log(123123213);
+ // console.log('socket.rooms === ',socket.rooms);
+ console.log("成功连接");
+ socket.join(socket.handshake.headers.user_id)
// console.log('socket.rooms === ',socket.rooms);
//接收前端发送的socket请求
socket.on('sendMsg', (data) => {
- socket.to(data.firend_id).emit('receiveMsg', data)
- // chat.create(data);
+ socket.to(data.firend_id).emit('receiveMsg', data)
+ // chat.create(data);
})
socket.on('chat_send', (chat_value, firend_id, req) => {
- // console.log(chat_value,firend_id);
+ // console.log(chat_value,firend_id);
+ })
+ socket.on('send', async (chat_value) => {
+ //添加
+ console.log(chat_value);
+ console.log(chat_value.user_id);
+ await RecordModel.create({ user_id: chat_value.user_id, friend_id: chat_value.friend_id, content: chat_value.message, time: chat_value.time });
+ console.log('添加消息成功');
})
})
httpServer.listen(4000)
diff --git a/core/module/module.js b/core/module/module.js
index 2562229..c433b88 100644
--- a/core/module/module.js
+++ b/core/module/module.js
@@ -115,14 +115,29 @@ const CommentSchema = new mongoose.Schema({
})
//好友
const friendSchema = new mongoose.Schema({
- user_id: String, //用户id
- friend_id: String, //好友id
+ user_id: {
+ type: mongoose.Types.ObjectId,
+ ref: 'user'
+ }, //用户id
+ friend_id: {
+ type: mongoose.Types.ObjectId,
+ ref: 'user'
+ }, //好友id
})
//聊天记录
const recordSchema = new mongoose.Schema({
- user_id: String, //用户id
- friend_id: String, //好友id
- content: String //聊天记录
+ user_id: {
+ type: mongoose.Types.ObjectId,
+ ref: 'user'
+ }, //用户id
+ friend_id: {
+ type: mongoose.Types.ObjectId,
+ ref: 'user'
+ }, //好友id
+ content: String, //聊天记录
+ time: String, //时间
+ user_avatar: String, //头像
+ friend_avatar: String, //头像
})
diff --git a/core/routes/index.js b/core/routes/index.js
index 1b6915e..ae1477e 100644
--- a/core/routes/index.js
+++ b/core/routes/index.js
@@ -8,7 +8,7 @@ const fse = require('fs-extra')
const path = require('path')
const fs = require('fs')
-const {
+const {
UserModel,
ConcernModel, //关注
CollectionModel, //收藏
@@ -142,7 +142,7 @@ router.post('/login', async (req, res) => {
if (user.length !== 0) {
res.send({
code: 200,
- msg: '登录成功',
+ msg: '登录成功',
user_id: user[0]._id,
accessToken: accessToken({ phone: phone }),
refreshToken: refreshToken({ phone: phone })
@@ -188,54 +188,54 @@ router.get('/refresh', async (req, res) => {
//添加用户数据
-router.post('/user_add',function(req,res){
+router.post('/user_add', function (req, res) {
let user_la = req.body;
UserModel.create(user_la)
res.send({
- code:200,
- msg:'添加成功'
+ code: 200,
+ msg: '添加成功'
})
})
// 添加视频数据
-router.post('/video_add',function(req,res){
+router.post('/video_add', function (req, res) {
let video_la = req.body;
VideoModel.create(video_la)
res.send({
- code:200,
- msg:'添加成功'
+ code: 200,
+ msg: '添加成功'
})
})
// 添加评论数据
-router.post('/comment_add',function(req,res){
+router.post('/comment_add', function (req, res) {
let comment_la = req.body;
CommentModel.create(comment_la)
res.send({
- code:200,
- msg:'添加成功'
+ code: 200,
+ msg: '添加成功'
})
})
//添加关注数据
-router.post('/concern_add',function(req,res){
+router.post('/concern_add', function (req, res) {
let concern_la = req.body;
ConcernModel.create(concern_la)
res.send({
- code:200,
- msg:'添加成功'
+ code: 200,
+ msg: '添加成功'
})
})
//添加收藏数据
-router.post('/collect_add',function(req,res){
+router.post('/collect_add', function (req, res) {
let collect_la = req.body;
CollectionModel.create(collect_la)
res.send({
- code:200,
- msg:'添加成功'
+ code: 200,
+ msg: '添加成功'
})
})
@@ -243,33 +243,33 @@ router.post('/collect_add',function(req,res){
//获取视频数据
-router.get('/video_list',async function(req,res){
+router.get('/video_list', async function (req, res) {
let video_list = await VideoModel.find();
res.send({
- code:200,
- msg:'获取成功',
- data:video_list
+ code: 200,
+ msg: '获取成功',
+ data: video_list
})
})
// 存放路径
-const UPLOAD_DIR = path.resolve(__dirname,'../upload')
-let cun
+const UPLOAD_DIR = path.resolve(__dirname, '../upload')
+let cun
//上传文件 上传视频文件
-router.post('/upload',function(req,res){
- const form = new multiparty.Form({uploadDir:'temp'})
+router.post('/upload', function (req, res) {
+ const form = new multiparty.Form({ uploadDir: 'temp' })
// cun = new multiparty.Form()
// form.parse(req)
form.parse(req)
-
-form.baseUrl = '/upload'
+
+ form.baseUrl = '/upload'
- form.on('file',async(name,chunk)=>{
- console.log(chunk,'sdfsdfsssssssdfsdfsdds');
+ form.on('file', async (name, chunk) => {
+ console.log(chunk, 'sdfsdfsssssssdfsdfsdds');
// 存放接片目录 存放的路径
@@ -285,19 +285,19 @@ form.baseUrl = '/upload'
// size: 673821
// } sdfsdfsssssssdfsdfsdds
// 判断目录是否存在
- if(!fse.existsSync(chunkDir)){
+ if (!fse.existsSync(chunkDir)) {
await fse.mkdirs(chunkDir);
}
// 原文件名。index.ext 移动到的文件 要移动的文件
var dPath = path.join(chunkDir, chunk.originalFilename.split('.')[1])
// 保存文件
- await fse.move(chunk.path,dPath,{overwrite:true})
+ await fse.move(chunk.path, dPath, { overwrite: true })
res.send('上传成功')
})
-
+
})
@@ -362,44 +362,44 @@ router.post('/getImg', (req, res) => {
})
//合并分片
-router.post('/merge',async(req,res)=>{
+router.post('/merge', async (req, res) => {
//根据传递的文件名进行合并 完整的文件名
let name = req.body.name
-
+
//文件名 没有后缀
let fname = name.split('.')[0]
// 获取文件名的所有分片
let chunkDir = path.join(UPLOAD_DIR, fname)
- console.log(chunkDir,'这个是文件的所有切片目录');
- // C:\Users\HP\Desktop\vue3\yunyou-pavilion\core\routes\public\upload\李宁岩效果(2) 这个是文件的所有切片
+ console.log(chunkDir, '这个是文件的所有切片目录');
+ // C:\Users\HP\Desktop\vue3\yunyou-pavilion\core\routes\public\upload\李宁岩效果(2) 这个是文件的所有切片
let chunks = await fse.readdir(chunkDir)
- console.log(chunks,'这个是文件的所有切片123123123');
- // [
- // '0', '1', '2', '3',
- // '4', '5', '6', '7',
- // '8', '9'
- // ] 这个是文件的所有切片123123123
+ console.log(chunks, '这个是文件的所有切片123123123');
+ // [
+ // '0', '1', '2', '3',
+ // '4', '5', '6', '7',
+ // '8', '9'
+ // ] 这个是文件的所有切片123123123
// 排序
- chunks.sort((a,b)=>a-b).map(chunkPath=>{
+ chunks.sort((a, b) => a - b).map(chunkPath => {
//合并文件
fs.appendFileSync(
- path.join(UPLOAD_DIR,name),
+ path.join(UPLOAD_DIR, name),
fs.readFileSync(`${chunkDir}/${chunkPath}`)
)
})
- let la = path.join(UPLOAD_DIR,name)
- console.log(la,'lalalala');
+ let la = path.join(UPLOAD_DIR, name)
+ console.log(la, 'lalalala');
fse.removeSync(chunkDir)
res.send({
- msg:'合并成功',
- url:`http://localhost:3000/upload/${name}`
+ msg: '合并成功',
+ url: `http://localhost:3000/upload/${name}`
})
})
@@ -408,33 +408,24 @@ router.post('/merge',async(req,res)=>{
// 查询好友
router.post('/selectfriend', async (req, res) => {
const { user_id } = req.body
- console.log(user_id);
+ console.log(req.body);
//查询好友表
- const friends = await FriendModel.find({ user_id })
- console.log(friends.length);
- const list = []
- for (let i = 0; i < friends.length; i++) {
- list.push(friends[i].friend_id)
- }
+ const friends = await FriendModel.find({ user_id: user_id }).populate('friend_id')
res.send({
code: 200,
msg: '查询成功',
- list: list
+ friends
})
})
-// 查询好友信息
-router.post('/selectfriendmessage', async (req, res) => {
- // console.log(req.body.friendList);
- const fendlist = []
- for (let i = 0; i < req.body.friendList.length; i++) {
- const friend = await UserModel.find({ _id: req.body.friendList[i] })
- fendlist.push(friend)
- }
- console.log(fendlist);
+
+router.post('/record', async (req, res) => {
+ const { user_id, friend_id } = req.body
+ const list = await RecordModel.find({ user_id:user_id, friend_id:friend_id }).populate('user_id').populate('friend_id').sort({time:1})
+
res.send({
code: 200,
msg: '查询成功',
- fendlist
+ list
})
})
@@ -463,6 +454,7 @@ router.post('/selectfriendmessage', async (req, res) => {
+
module.exports = router;
diff --git a/core/routes/sokio.js b/core/routes/sokio.js
index bdbdf13..dc213ba 100644
--- a/core/routes/sokio.js
+++ b/core/routes/sokio.js
@@ -9,17 +9,7 @@ function getSocket(http) {
//socketio接收
io.on('connection', (socket) => {
// console.log('socket.handshake.headers ===',socket.handshake.headers.user_id);
- socket.join(socket.handshake.headers.user_id)
- // console.log('socket.rooms === ',socket.rooms);
- //接收前端发送的socket请求
- socket.on('sendMsg', (data) => {
- socket.to(data.firend_id).emit('receiveMsg', data)
- // chat.create(data);
- })
-
- socket.on('chat_send', (chat_value, firend_id, req) => {
- // console.log(chat_value,firend_id);
- })
+
})
}
diff --git a/yunyou_pavilion/src/components/Tabber.vue b/yunyou_pavilion/src/components/Tabber.vue
index bd4bef0..af011d4 100644
--- a/yunyou_pavilion/src/components/Tabber.vue
+++ b/yunyou_pavilion/src/components/Tabber.vue
@@ -45,7 +45,7 @@ const tabberArr = [
name: "消息",
icon: "/Tabber/chat.svg",
on_icon: "/Tabber/onChat.svg",
- path: flag ? '/chat' : '/unchat',
+ path: flag ? '/socket' : '/unchat',
},
{
id: 4,
diff --git a/yunyou_pavilion/src/router/index.js b/yunyou_pavilion/src/router/index.js
index 1b1aa7a..1bdb7a5 100644
--- a/yunyou_pavilion/src/router/index.js
+++ b/yunyou_pavilion/src/router/index.js
@@ -196,7 +196,12 @@ const router = createRouter({
{
path: '',
redirect: '/home'
- }
+ },
+ {
+ path:'/socket',
+ name:'socket',
+ component:Socket
+ },
]
},
@@ -204,14 +209,10 @@ const router = createRouter({
{ path: '/add', component: Add },
//好友页面
- {
- path:'/socket',
- name:'socket',
- component:Socket
- },
+
//聊天页面
{
- path: '/chat/:id',
+ path: '/chat/:id/:name',
name: 'chat',
component: Chat
},
diff --git a/yunyou_pavilion/src/views/Chitchat/Chat.vue b/yunyou_pavilion/src/views/Chitchat/Chat.vue
index bfc9f12..1506323 100644
--- a/yunyou_pavilion/src/views/Chitchat/Chat.vue
+++ b/yunyou_pavilion/src/views/Chitchat/Chat.vue
@@ -1,17 +1,106 @@
+
{{ item.time }}
+{{ item.friend_id.time }}