From c8c614c47be181d78a14460df1f7e4a61245105b Mon Sep 17 00:00:00 2001 From: callmeyan Date: Sat, 28 Sep 2019 23:29:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=80=E6=9C=89=E7=9A=84=E9=80=9A=E8=AE=AF?= =?UTF-8?q?=E6=89=93=E9=80=9A=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.js | 2 +- io/io.js | 196 +++++++++++++++++++------------------ public/js/client/client.js | 8 +- public/js/server/index.js | 7 +- routes/index.js | 15 ++- utils/mongoose.js | 2 +- views/server/footer.ejs | 31 +++++- views/server/header.ejs | 3 +- views/server/setup.ejs | 13 ++- 9 files changed, 168 insertions(+), 109 deletions(-) diff --git a/config.js b/config.js index 27da586..570b289 100644 --- a/config.js +++ b/config.js @@ -1,5 +1,5 @@ const APP = { - "KEFUUUID":'chat-kefu-admin', + "KF_PREFIX":'chat-admin-', "QINIU":{ "accessKey":"your access key", "secretKey":"your secret key" diff --git a/io/io.js b/io/io.js index 41dbbaa..937c99c 100644 --- a/io/io.js +++ b/io/io.js @@ -19,13 +19,13 @@ function ioServer(io) { var __uuids = []; //初始化连接人数 - redis.set('online_count',0,null,function (err,ret) { - if(err){ + redis.set('online_count', 0, null, function (err, ret) { + if (err) { console.error(err); } }); - Array.prototype.remove = function(val) { + Array.prototype.remove = function (val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); @@ -43,59 +43,63 @@ function ioServer(io) { console.log(uid + '登录成功'); //通知用户上线 - if(uid != AppConfig.KEFUUUID){ // 通知给管理员 - redis.get(AppConfig.KEFUUUID,function (err,sid) { - if(err){ + if (!uid.toString().startsWith(AppConfig.KF_PREFIX)) { + let gongHao = msg.gongHao; + console.log('gongHao=>',gongHao) + // 给管理员发送通知 + redis.get(AppConfig.KF_PREFIX + gongHao, function (err, sid) { + if (err) { console.error(err); } - if(sid){ - redis.get('online_count',function (err,val) { - if(err){ + console.log('sid=>',sid) + if (sid) { + redis.get('online_count', function (err, val) { + if (err) { console.error(err); } - if(!val){ + if (!val) { val = 0; } - if(typeof val == 'string'){ + if (typeof val == 'string') { val = parseInt(val); } //var ip = socket.request.connection.remoteAddress; //此处获取IP可能会有延迟,建议改成自己的IP库 - Common.getIpLocation(msg.ip,function (err,location) { - if(err){ + Common.getIpLocation(msg.ip, function (err, location) { + if (err) { location = ''; } var info = { - "uid":uid, - "name":location + ' 客户', - "type":'online' + "uid": uid, + "name": location + ' 客户', + "type": 'online' }; - - redis.get('user-uuids',function (err,uuids) { - if(err){ + console.log(info); + redis.get('user-uuids', function (err, uuids) { + if (err) { console.error(err); } - if(uuids){ - uuids =JSON.parse(uuids); - }else{ + if (uuids) { + uuids = JSON.parse(uuids); + } else { uuids = []; } - if(__uuids.indexOf(uid) == -1){ + if (__uuids.indexOf(uid) == -1) { __uuids.push(uid); - var d_user = {"uid":uid,"name":location + ' 客户'}; + var d_user = {"uid": uid, "name": location + ' 客户'}; uuids.push(d_user); uuids = JSON.stringify(uuids); - redis.set('user-uuids',uuids,null,function (err,ret) { - if(err){ + redis.set('user-uuids', uuids, null, function (err, ret) { + if (err) { console.error(err); } }); } }); - io.to(sid).emit('update-users',info); + io.to(sid).emit('update-users', info); }); }); @@ -103,80 +107,81 @@ function ioServer(io) { }); } - redis.set(uid,socket.id,null,function (err,ret) { - if(err){ + //将用户id和socket进行保定 + redis.set(uid, socket.id, 3600 * 3, function (err, ret) { + if (err) { console.error(err); } }); - redis.set(socket.id,uid,null,function (err,ret) { - if(err){ - console.error(err); - } - }); + // redis.set(socket.id,uid,null,function (err,ret) { + // if(err){ + // console.error(err); + // } + // }); }); //断开事件 - socket.on('disconnect', function() { + socket.on('disconnect', function () { console.log("与服务其断开"); _self.updateOnlieCount(false); - redis.get(socket.id,function (err,val) { - if(err){ + redis.get(socket.id, function (err, val) { + if (err) { console.error(err); } - redis.del(socket.id,function (err,ret) { - if(err){ + redis.del(socket.id, function (err, ret) { + if (err) { console.error(err); } }); - redis.del(val,function (err,ret) { - if(err){ + redis.del(val, function (err, ret) { + if (err) { console.error(err); } }); //通知用户下线 - if(val != AppConfig.KEFUUUID){ - redis.get(AppConfig.KEFUUUID,function (err,sid) { - if(err){ + if (val != AppConfig.KEFUUUID) { + redis.get(AppConfig.KEFUUUID, function (err, sid) { + if (err) { console.error(err); } - if(sid){ + if (sid) { var info = { - "uid":val, - "name":'客户下线', - "type":'offline' + "uid": val, + "name": '客户下线', + "type": 'offline' }; - io.to(sid).emit('update-users',info); + io.to(sid).emit('update-users', info); } }); - redis.get('user-uuids',function (err,uuids) { - if(err){ + redis.get('user-uuids', function (err, uuids) { + if (err) { console.error(err); } - if(uuids){ - uuids =JSON.parse(uuids); - }else{ + if (uuids) { + uuids = JSON.parse(uuids); + } else { uuids = []; } val = parseInt(val); var idx = __uuids.indexOf(val); - if( idx != -1){ + if (idx != -1) { __uuids.remove(val); //uuids.splice(idx,1); var tmp = []; uuids.forEach(function (user) { - if(user.uid != val){ + if (user.uid != val) { tmp.push(user); } }); uuids = JSON.stringify(tmp); - redis.set('user-uuids',uuids,null,function (err,ret) { - if(err){ + redis.set('user-uuids', uuids, null, function (err, ret) { + if (err) { console.error(err); } }); @@ -187,41 +192,42 @@ function ioServer(io) { }); //重连事件 - socket.on('reconnect', function() { + socket.on('reconnect', function () { console.log("重新连接到服务器"); }); //监听客户端发送的信息,实现消息转发到各个其他客户端 - socket.on('message',function(msg){ - msgModel.add(msg.from_uid,msg.uid,msg.content,msg.chat_type,msg.image,function (err) { - if(err){ - console.error(err); - } + socket.on('message', function (msg) { + //保存到数据库 + msgModel.add(msg.from_uid, msg.uid, msg.content, msg.chat_type, msg.image, function (err) { + if (err) { + console.error(err); + } }); - if(msg.type == msgType.messageType.public){ + if (msg.type == msgType.messageType.public) { var mg = { - "uid" : msg.from_uid , + "uid": msg.from_uid, "content": msg.content, - "chat_type" : msg.chat_type?msg.chat_type:'text', - "image":msg.image + "chat_type": msg.chat_type ? msg.chat_type : 'text', + "image": msg.image }; - socket.broadcast.emit("message",mg); - }else if(msg.type == msgType.messageType.private){ + socket.broadcast.emit("message", mg); + } else if (msg.type == msgType.messageType.private) { var uid = msg.uid; - redis.get(uid,function (err,sid) { - if(err){ - console.error(err); - } - if(sid){ - //给指定的客户端发送消息 - var mg = { - "uid" : msg.from_uid, - "content": msg.content, - "chat_type" : msg.chat_type?msg.chat_type:'text', - "image":msg.image - }; - io.to(sid).emit('message',mg); - } + redis.get(uid, function (err, sid) { + if (err) { + console.error(err); + } + if (sid) { + //给指定的客户端发送消息 + var mg = { + "uid": msg.from_uid, + "content": msg.content, + "chat_type": msg.chat_type ? msg.chat_type : 'text', + "image": msg.image + }; + io.to(sid).emit('message', mg); + } }); } @@ -230,30 +236,30 @@ function ioServer(io) { this.updateOnlieCount = function (isConnect) { //记录在线客户连接数 - redis.get('online_count',function (err,val) { - if(err){ + redis.get('online_count', function (err, val) { + if (err) { console.error(err); } - if(!val){ + if (!val) { val = 0; } - if(typeof val == 'string'){ + if (typeof val == 'string') { val = parseInt(val); } - if(isConnect){ + if (isConnect) { val += 1; - }else{ + } else { val -= 1; - if(val<=0){ + if (val <= 0) { val = 0; } } - console.log('当前在线人数:'+val); - io.sockets.emit('update_online_count', { online_count: val }); + console.log('当前在线人数:' + val); + io.sockets.emit('update_online_count', {online_count: val}); - redis.set('online_count',val,null,function (err,ret) { - if(err){ + redis.set('online_count', val, null, function (err, ret) { + if (err) { console.error(err); } }); diff --git a/public/js/client/client.js b/public/js/client/client.js index 1bad65b..7194e74 100644 --- a/public/js/client/client.js +++ b/public/js/client/client.js @@ -93,12 +93,13 @@ $(function(){ } + const gongHao = 10002; $("#btnSend").click(function(){ var msg = $("#textarea").val(); if(msg){ var msg_sender = { "type":'private', - "uid":'chat-kefu-admin', + "uid":'chat-admin-' + gongHao, "content":msg, "from_uid":uuid, "chat_type":'text' @@ -195,10 +196,11 @@ $(function(){ uuid = fp1.get(); console.log('连接成功...'+uuid); - var ip = $("#keleyivisitorip").html(); + var ip = $("#keleyivisitorip").text(); var msg = { "uid" : uuid, - "ip" : ip + "ip" : ip, + "gongHao":gongHao }; socket.emit('login', msg); get_message(uuid); diff --git a/public/js/server/index.js b/public/js/server/index.js index ac5ddb6..76476a2 100644 --- a/public/js/server/index.js +++ b/public/js/server/index.js @@ -322,6 +322,10 @@ layui.use(['layer', 'form', 'jquery'], function () { shade :0.7, value: '' }, function (username, index, elem) { + if(!username){ + showLoginView(); + return; + } $.post('/admin/login', {username}, (res) => { if (res.code != 200) { layer.msg(res.message); @@ -330,7 +334,7 @@ layui.use(['layer', 'form', 'jquery'], function () { layer.msg('登录成功'); layer.closeAll(); console.log(res); - // location.reload(); + location.reload(); }, 'json').fail(() => { layer.msg('登录异常请重试') }) @@ -339,6 +343,7 @@ layui.use(['layer', 'form', 'jquery'], function () { if (!data.username) { showLoginView(); } else { + socket.connect(); init(); get_users(); } diff --git a/routes/index.js b/routes/index.js index 46d9775..e2e8203 100644 --- a/routes/index.js +++ b/routes/index.js @@ -30,9 +30,18 @@ router.get('/admin/users', function (req, res, next) { res.render('./server/users'); }); -router.get('/admin/setup', function (req, res, next) { - let data = getViewAdmin(req) - res.render('./server/setup', data); +router.get('/admin/setup', async function (req, res, next) { + let data = getViewAdmin(req); + if(!data.username){ // 没有登录则 直接跳转到首页 + res.redirect('/admin'); + return; + } + let info = await userModel.findByUserName(data.username); + if(!info){ + res.redirect('/admin');return; + } + console.log(info); + res.render('./server/setup', info); }); router.post('/admin/update', async (req, res, next) => { diff --git a/utils/mongoose.js b/utils/mongoose.js index 3f1b1cd..08b71c2 100644 --- a/utils/mongoose.js +++ b/utils/mongoose.js @@ -1,5 +1,5 @@ var mongoose = require("mongoose"); -const DB_URL = 'mongodb://127.0.0.1:27017/kefu'; +const DB_URL = 'mongodb://192.168.10.80:27017/kefu'; mongoose.connect(DB_URL); /** diff --git a/views/server/footer.ejs b/views/server/footer.ejs index 5f27bb1..f3b76a1 100644 --- a/views/server/footer.ejs +++ b/views/server/footer.ejs @@ -5,8 +5,37 @@ @@ -67,6 +68,6 @@
  • 我的对话
  • 设置
  • -
  • 切换工号
  • +
  • 切换工号
  • \ No newline at end of file diff --git a/views/server/setup.ejs b/views/server/setup.ejs index 963d7d5..6238bf1 100644 --- a/views/server/setup.ejs +++ b/views/server/setup.ejs @@ -2,17 +2,24 @@
    +
    + +
    + <%=username%> +
    +
    - +
    - +