1,添加聊天信息保存功能
2,加载获取最近十条信息
This commit is contained in:
parent
be5f74a57a
commit
373c69799c
2
app.js
2
app.js
@ -7,6 +7,7 @@ var bodyParser = require('body-parser');
|
||||
|
||||
var index = require('./routes/index');
|
||||
var users = require('./routes/users');
|
||||
var message = require('./routes/message');
|
||||
|
||||
var app = express();
|
||||
|
||||
@ -24,6 +25,7 @@ app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
app.use('/', index);
|
||||
app.use('/users', users);
|
||||
app.use('/message', message);
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function(req, res, next) {
|
||||
|
6
io/io.js
6
io/io.js
@ -8,6 +8,7 @@ var msgType = require('./messageTpye');
|
||||
var ioSvc = require('./ioHelper').ioSvc;
|
||||
var AppConfig = require('../config');
|
||||
var Common = require('../utils/common');
|
||||
var msgModel = require('../model/message');
|
||||
|
||||
//服务端连接
|
||||
function ioServer(io) {
|
||||
@ -192,6 +193,11 @@ function ioServer(io) {
|
||||
|
||||
//监听客户端发送的信息,实现消息转发到各个其他客户端
|
||||
socket.on('message',function(msg){
|
||||
msgModel.add(msg.from_uid,msg.uid,msg.content,function (err) {
|
||||
if(err){
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
if(msg.type == msgType.messageType.public){
|
||||
var mg = {
|
||||
"uid" : msg.from_uid ,
|
||||
|
40
model/message.js
Normal file
40
model/message.js
Normal file
@ -0,0 +1,40 @@
|
||||
var mongoose = require('../utils/mongoose').mongoose;
|
||||
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
var MessageSchema = new Schema({
|
||||
from_uid : { type:String ,index: true},
|
||||
to_uid : { type:String ,index: true},
|
||||
content : { type:String },
|
||||
time : { type:Date, default:Date.now }
|
||||
});
|
||||
|
||||
var MessageModel = mongoose.model("message", MessageSchema);
|
||||
|
||||
function add(from_uid,to_uid,content,callback) {
|
||||
var info = {
|
||||
"from_uid" : from_uid,
|
||||
"to_uid" : to_uid,
|
||||
"content" : content
|
||||
};
|
||||
var msgModel = new MessageModel(info);
|
||||
msgModel.save(function(err, res){
|
||||
return callback(err,res);
|
||||
});
|
||||
}
|
||||
|
||||
function query(page,size,uid,callback) {
|
||||
var query = MessageModel.find({});
|
||||
var condition = [];
|
||||
if(uid){
|
||||
condition.push({"from_uid":uid});
|
||||
condition.push({"to_uid":uid});
|
||||
}
|
||||
|
||||
var skip = (page - 1) * size;
|
||||
query.or(condition).skip(skip).limit(size).sort({"time":-1}).exec(callback);
|
||||
}
|
||||
|
||||
|
||||
exports.add = add;
|
||||
exports.query = query;
|
47
model/users.js
Normal file
47
model/users.js
Normal file
@ -0,0 +1,47 @@
|
||||
var mongoose = require('../mongoose').mongoose;
|
||||
var crypto = require('crypto');
|
||||
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
var UsersSchema = new Schema({
|
||||
username : { type:String },
|
||||
password : { type:String },
|
||||
time : { type:Date, default:Date.now }
|
||||
});
|
||||
|
||||
var UsersModel = mongoose.model("users", UsersSchema);
|
||||
|
||||
function login(username,password,callback) {
|
||||
var md5 = crypto.createHash('md5');
|
||||
password = md5.update(password).digest('hex');
|
||||
var condition = {'username' : username,'password':password};
|
||||
|
||||
UsersModel.findOne(condition, function(err, res){
|
||||
var _err = null;
|
||||
if (err) {
|
||||
_err = err;
|
||||
}
|
||||
if(!res){
|
||||
_err = '用户名密码不正确';
|
||||
}
|
||||
return callback(_err,res);
|
||||
})
|
||||
}
|
||||
|
||||
function reset_psw(username,psw_old,psw_new,callback) {
|
||||
psw_old = crypto.createHash('md5').update(psw_old).digest('hex');
|
||||
UsersModel.find({username:username,password:psw_old},function (err,info) {
|
||||
if (err) {
|
||||
return callback(err,null);
|
||||
}
|
||||
|
||||
if(!info || info.length == 0){
|
||||
return callback('原密码不正确',null);
|
||||
}
|
||||
psw_new = crypto.createHash('md5').update(psw_new).digest('hex');
|
||||
UsersModel.findOneAndUpdate({username:username,password:psw_old}, {password:psw_new}, callback);
|
||||
});
|
||||
}
|
||||
|
||||
exports.login = login;
|
||||
exports.reset_psw = reset_psw;
|
@ -15,6 +15,7 @@
|
||||
"serve-favicon": "~2.4.2",
|
||||
"socket.io":"~2.0.4",
|
||||
"socket.io-client":"^2.0.4",
|
||||
"redis":"^2.8.0"
|
||||
"redis":"^2.8.0",
|
||||
"mongoose":"^4.12.5"
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,15 @@
|
||||
$(function(){
|
||||
//Socket.IO 连接
|
||||
var socket = io.connect('http://'+document.domain+':9010');
|
||||
var socket = io.connect('http://'+document.domain+':9010',{
|
||||
"transports":['websocket', 'polling']
|
||||
});
|
||||
var uuid = '';
|
||||
|
||||
function insert_client_html(time,content){
|
||||
function insert_client_html(content,datetime){
|
||||
var time = dateFormat();
|
||||
if(datetime){
|
||||
time = dateFormat("yyyy-MM-dd hh:mm:ss",new Date(datetime));
|
||||
}
|
||||
var tpl = '<div class="msg-box">'+
|
||||
'<div class="msg-client">'+
|
||||
'<div class="date">' + time + '</div>'+
|
||||
@ -17,7 +23,11 @@ $(function(){
|
||||
$(".msg-container").append(tpl);
|
||||
}
|
||||
|
||||
function insert_agent_html(time,content){
|
||||
function insert_agent_html(content,datetime){
|
||||
var time = dateFormat();
|
||||
if(datetime){
|
||||
time = dateFormat("yyyy-MM-dd hh:mm:ss",new Date(datetime));
|
||||
}
|
||||
var tpl = '<div class="msg-box">'+
|
||||
'<div class="msg-agent">'+
|
||||
'<div class="agent-avatar">'+
|
||||
@ -40,9 +50,25 @@ $(function(){
|
||||
div.scrollTop = div.scrollHeight;
|
||||
}
|
||||
|
||||
//获取最新的五条数据
|
||||
function get_message(uid) {
|
||||
$.get('/message?uid='+uid,function (data) {
|
||||
if(data.code == 200){
|
||||
data.data.reverse().forEach(function (msg) {
|
||||
if(msg.from_uid == uid){
|
||||
insert_client_html(msg.content,msg.time);
|
||||
}else{
|
||||
insert_agent_html(msg.content,msg.time);
|
||||
}
|
||||
|
||||
scrollToBottom();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
$("#btnSend").click(function(){
|
||||
var date = dateFormat();
|
||||
var msg = $("#textarea").val();
|
||||
if(msg){
|
||||
var msg_sender = {
|
||||
@ -52,7 +78,7 @@ $(function(){
|
||||
"from_uid":uuid
|
||||
};
|
||||
socket.emit('message', msg_sender);
|
||||
insert_client_html(date,msg);
|
||||
insert_client_html(msg);
|
||||
scrollToBottom();
|
||||
$("#textarea").val('');
|
||||
}
|
||||
@ -71,7 +97,7 @@ $(function(){
|
||||
"ip" : ip
|
||||
};
|
||||
socket.emit('login', msg);
|
||||
|
||||
get_message(uuid);
|
||||
});
|
||||
|
||||
// /* 后端推送来消息时
|
||||
@ -80,8 +106,7 @@ $(function(){
|
||||
// content 消息
|
||||
// */
|
||||
socket.on('message', function(msg){
|
||||
insert_agent_html(dateFormat(),msg.content);
|
||||
insert_agent_html(msg.content);
|
||||
scrollToBottom();
|
||||
});
|
||||
|
||||
});
|
@ -6,7 +6,9 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
||||
|
||||
var currentUUID = '';
|
||||
var uuid = '';
|
||||
var socket = io.connect('http://'+document.domain+':9010');
|
||||
var socket = io.connect('http://'+document.domain+':9010',{
|
||||
"transports":['websocket', 'polling']
|
||||
});
|
||||
|
||||
var uuids = [];
|
||||
var online_num = 0;
|
||||
@ -33,28 +35,35 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
||||
function insert_section(uid) {
|
||||
var html = '<section class="user-section" style="display:none;" id="section-'+ uid +'"></section>';
|
||||
$(".message-container").append(html);
|
||||
get_message(uid);
|
||||
}
|
||||
|
||||
function insert_agent_html(content){
|
||||
var date = dateFormat();
|
||||
function insert_agent_html(uid,content,datetime){
|
||||
var time = dateFormat();
|
||||
if(datetime){
|
||||
time = dateFormat("yyyy-MM-dd hh:mm:ss",new Date(datetime));
|
||||
}
|
||||
var html = ' <div class="message-agent">\n' +
|
||||
' <div class="message-agent-time-sender message-time-sender">\n' +
|
||||
' <span class="message-agent-time">' + date + '</span>\n' +
|
||||
' <span class="message-agent-time">' + time + '</span>\n' +
|
||||
' <span class="">我</span>\n' +
|
||||
' </div>\n' +
|
||||
' <div class="message-agent-content message-content">\n' +
|
||||
' <div>' + content + '</div>\n' +
|
||||
' </div>\n' +
|
||||
' </div>';
|
||||
$('#section-'+currentUUID).append(html);
|
||||
$('#section-'+uid).append(html);
|
||||
}
|
||||
|
||||
|
||||
function insert_client_html(uid,content){
|
||||
var date = dateFormat();
|
||||
function insert_client_html(uid,content,datetime){
|
||||
var time = dateFormat();
|
||||
if(datetime){
|
||||
time = dateFormat("yyyy-MM-dd hh:mm:ss",new Date(datetime));
|
||||
}
|
||||
var html = '<div class="message-client">\n' +
|
||||
' <div class="message-time-sender">\n' +
|
||||
' <span class="message-client-time">' + date + '</span>\n' +
|
||||
' <span class="message-client-time">' + time + '</span>\n' +
|
||||
' <span class="">客户</span>\n' +
|
||||
' </div>\n' +
|
||||
' <div class="message-client-content message-content">\n' +
|
||||
@ -104,7 +113,8 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
||||
$(".friend-head-right").html( online_num + ' / ' + num + ' 人' );
|
||||
}
|
||||
|
||||
function getUsers() {
|
||||
//获取在线用户
|
||||
function get_users() {
|
||||
$.get('/users',function (data) {
|
||||
if(data.code == 200){
|
||||
$('.chat-user').html('');
|
||||
@ -131,6 +141,23 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
||||
});
|
||||
}
|
||||
|
||||
//获取最新的五条数据
|
||||
function get_message(uid) {
|
||||
$.get('/message?uid='+uid,function (data) {
|
||||
if(data.code == 200){
|
||||
data.data.reverse().forEach(function (msg) {
|
||||
if(msg.from_uid == uid){
|
||||
insert_client_html(msg.from_uid,msg.content,msg.time);
|
||||
}else{
|
||||
insert_agent_html(msg.to_uid,msg.content,msg.time);
|
||||
}
|
||||
|
||||
scrollToBottom();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$(".btnMsgSend").click(function(){
|
||||
var msg = $("#msg-send-textarea").val();
|
||||
if(msg){
|
||||
@ -141,7 +168,7 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
||||
"from_uid":uuid
|
||||
};
|
||||
socket.emit('message', msg_sender);
|
||||
insert_agent_html(msg);
|
||||
insert_agent_html(currentUUID,msg);
|
||||
scrollToBottom();
|
||||
$("#msg-send-textarea").val('');
|
||||
}
|
||||
@ -221,5 +248,5 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
||||
});
|
||||
|
||||
init();
|
||||
getUsers();
|
||||
get_users();
|
||||
});
|
23
routes/message.js
Normal file
23
routes/message.js
Normal file
@ -0,0 +1,23 @@
|
||||
var express = require('express');
|
||||
var router = express.Router();
|
||||
var msgModel = require('../model/message');
|
||||
|
||||
router.get('/', function(req, res, next) {
|
||||
var page = req.query.page || 1;
|
||||
var size = req.query.size || 10;
|
||||
var uid = req.query.uid;
|
||||
|
||||
if(!uid){
|
||||
return res.send({code:500,msg:"参数不全"});
|
||||
}
|
||||
|
||||
msgModel.query(page,size,uid,function (err,data) {
|
||||
if(err){
|
||||
console.error(err);
|
||||
return res.send({code:400,msg:"系统错误"});
|
||||
}
|
||||
return res.send({code:200,msg:"获取成功",data:data});
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
26
utils/mongoose.js
Normal file
26
utils/mongoose.js
Normal file
@ -0,0 +1,26 @@
|
||||
var mongoose = require("mongoose");
|
||||
const DB_URL = 'mongodb://127.0.0.1:27017/kefu';
|
||||
mongoose.connect(DB_URL);
|
||||
|
||||
/**
|
||||
* 连接成功
|
||||
*/
|
||||
mongoose.connection.on('connected', function () {
|
||||
console.log('Mongoose connection open to ' + DB_URL);
|
||||
});
|
||||
|
||||
/**
|
||||
* 连接异常
|
||||
*/
|
||||
mongoose.connection.on('error',function (err) {
|
||||
console.log('Mongoose connection error: ' + err);
|
||||
});
|
||||
|
||||
/**
|
||||
* 连接断开
|
||||
*/
|
||||
mongoose.connection.on('disconnected', function () {
|
||||
console.log('Mongoose connection disconnected');
|
||||
});
|
||||
|
||||
exports.mongoose = mongoose;
|
Loading…
x
Reference in New Issue
Block a user