diff --git a/io/io.js b/io/io.js index 7749269..8361b48 100644 --- a/io/io.js +++ b/io/io.js @@ -53,7 +53,24 @@ function ioServer(io) { //获取管理员 socket let kefuData =await sessoionModel.find(gongHao); if(kefuData){ // 找到客服数据 + let location = Common.getIpLocation(msg.ip); + let socket = socket.id; + let type = 'customer'; + let kefu_id = gongHao; + let clientInfo = { + "uid": uid, + "name": location + ' 客户', + "type": 'online' + }; + // 添加客户到 对应的客服 + sessoionModel.create({ + uid, + socket:socket.id, + type:'customer', + kefu_id:gongHao, + nickname:clientInfo.name, + }) // 给管理员发送通知 io.to(kefuData.socket).emit('update-users', info); } @@ -61,74 +78,15 @@ function ioServer(io) { //TODO 失败重发机制 console.log('给管理员发送通知失败'); } - // 给管理员发送通知 - redis.get(AppConfig.KF_PREFIX + gongHao, function (err, sid) { - if (err) { - console.error(err); - } - console.log('sid=>', sid) - if (sid) { - redis.get('online_count', function (err, val) { - if (err) { - console.error(err); - } - if (!val) { - val = 0; - } - if (typeof val == 'string') { - val = parseInt(val); - } - //var ip = socket.request.connection.remoteAddress; - //此处获取IP可能会有延迟,建议改成自己的IP库 - Common.getIpLocation(msg.ip, function (err, location) { - if (err) { - location = ''; - } - var info = { - "uid": uid, - "name": location + ' 客户', - "type": 'online' - }; - //将用户添加到 - console.log(info); - redis.get('user-uuids', function (err, uuids) { - if (err) { - console.error(err); - } - if (uuids) { - uuids = JSON.parse(uuids); - } else { - uuids = []; - } - - if (__uuids.indexOf(uid) == -1) { - __uuids.push(uid); - 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) { - console.error(err); - } - }); - } - }); - //让客服更新用户 - io.to(sid).emit('update-users', info); - }); - - }); - } - }); } //将用户id和socket进行保定 - redis.set(uid, socket.id, 3600 * 3, function (err, ret) { - if (err) { - console.error(err); - } - }); + // 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){ @@ -142,7 +100,7 @@ function ioServer(io) { socket.on('disconnect', function () { console.log("与服务其断开"); - _self.updateOnlieCount(false); + _self.updateOnlineCount(false); redis.get(socket.id, function (err, val) { if (err) { @@ -250,7 +208,7 @@ function ioServer(io) { }); }); - this.updateOnlieCount = function (isConnect) { + this.updateOnlineCount = function (isConnect) { //记录在线客户连接数 redis.get('online_count', function (err, val) { if (err) { diff --git a/model/session.js b/model/session.js index d6aab1c..35994d0 100644 --- a/model/session.js +++ b/model/session.js @@ -12,7 +12,7 @@ var SessionSchema = new Schema({ // 客服编号 kefu_id: {type: String}, nickname: {type: String}, - status: {type: Number}, + status: {type: Number,default:1}, create_at: {type: Date, default: Date.now} }); diff --git a/public/js/server/index.js b/public/js/server/index.js index 3d62d5f..a4643ef 100644 --- a/public/js/server/index.js +++ b/public/js/server/index.js @@ -259,7 +259,7 @@ layui.use(['layer', 'form', 'jquery'], function () { msg_notification(msg); }); - //后端推送来消息时 + //后端推送来消息时,更新用户 socket.on('update-users', function (msg) { if (msg.type == 'offline') { //arrayRemove(uuids,msg.uid); diff --git a/utils/common.js b/utils/common.js index 8912b12..56f6c70 100644 --- a/utils/common.js +++ b/utils/common.js @@ -10,21 +10,13 @@ function getClientIp(req) { req.connection.socket.remoteAddress; }; -function getIpLocation(ip, callback) { - return setTimeout(() => { - try { - let data = qqwry.searchIP(ip) - if (!data) { - callback(new Error('get ip exception'), ip); - } else { - let area = data.Area.trim().toLowerCase().replace('cz88.net', ''); - callback(false, data.Country + ' ' + area); - } - } catch (e) { - callback(e, ip); - - } - }, 10); +function getIpLocation(ip) { + let data = qqwry.searchIP(ip) + if (!data) { + return ip; + } + let area = data.Area.trim().toLowerCase().replace('cz88.net', ''); + return data.Country + ' ' + area; } exports.getClientIp = getClientIp; diff --git a/utils/mongoose.js b/utils/mongoose.js index 08b71c2..3d7da3d 100644 --- a/utils/mongoose.js +++ b/utils/mongoose.js @@ -1,5 +1,5 @@ var mongoose = require("mongoose"); -const DB_URL = 'mongodb://192.168.10.80:27017/kefu'; +const DB_URL = 'mongodb://192.168.10.123:27017/kefu'; mongoose.connect(DB_URL); /** diff --git a/views/error.ejs b/views/error.ejs index f63440b..d761979 100644 --- a/views/error.ejs +++ b/views/error.ejs @@ -7,6 +7,8 @@ Document -

Error

+

<%= message %>

+

<%= error.status %>

+
<%= error.stack %>
\ No newline at end of file diff --git a/views/server/index.ejs b/views/server/index.ejs index 006d60d..a0c1985 100644 --- a/views/server/index.ejs +++ b/views/server/index.ejs @@ -22,11 +22,14 @@
- +
-
发送
+
+ 发送 +
@@ -43,6 +46,80 @@
  • 😊
  • 😋
  • 😌
  • +
  • 😍
  • +
  • 😎
  • +
  • 😏
  • +
  • + 😐 +
  • +
  • + 😑 +
  • +
  • + 😒 +
  • +
  • 😓 +
  • +
  • 😔 +
  • +
  • 😕 +
  • +
  • 😖 +
  • +
  • 😗
  • +
  • + 😘 +
  • +
  • 😙
  • +
  • + 😚 +
  • +
  • 😛 +
  • +
  • 😜 +
  • +
  • 😝 +
  • +
  • 😞 +
  • +
  • 😟 +
  • +
  • 😠 +
  • +
  • 😡 +
  • +
  • 😢 +
  • +
  • 😣 +
  • +
  • 😤 +
  • +
  • 😥 +
  • +
  • 😦 +
  • +
  • 😧 +
  • +
  • 😨 +
  • +
  • 😩 +
  • +
  • 😪 +
  • +
  • 😫 +
  • +
  • 😬 +
  • +
  • 😭 +
  • +
  • 😮 +
  • +
  • 😯
  • +
  • 😰
  • +
  • 😱
  • +
  • 😲
  • +
  • 😳
  • +
  • 😴
  • @@ -50,6 +127,6 @@ - + <% include footer.ejs %> \ No newline at end of file