From 653870a9917afeb902e2cf6d6b31097bca83b08c Mon Sep 17 00:00:00 2001 From: callmeyan Date: Sun, 29 Sep 2019 21:48:33 +0800 Subject: [PATCH] aiya --- io/io.js | 28 ++++++++++++++---- model/session.js | 59 ++++++++++++++++++++++++++++++++++++++ public/js/client/client.js | 3 +- public/js/server/index.js | 3 +- routes/users.js | 24 +++++++--------- 5 files changed, 95 insertions(+), 22 deletions(-) create mode 100644 model/session.js diff --git a/io/io.js b/io/io.js index 937c99c..7749269 100644 --- a/io/io.js +++ b/io/io.js @@ -9,6 +9,7 @@ var ioSvc = require('./ioHelper').ioSvc; var AppConfig = require('../config'); var Common = require('../utils/common'); var msgModel = require('../model/message'); +const sessoionModel = require('../model/session'); //服务端连接 function ioServer(io) { @@ -38,20 +39,34 @@ function ioServer(io) { _self.updateOnlieCount(true); //用户与Socket进行绑定 - socket.on('login', function (msg) { + socket.on('login', async function (msg) { var uid = msg.uid; + let type = msg.type; // 获取用户类型 console.log(uid + '登录成功'); - //通知用户上线 - if (!uid.toString().startsWith(AppConfig.KF_PREFIX)) { + //如果不是客服登录 + if (type != 'kefu') { let gongHao = msg.gongHao; - console.log('gongHao=>',gongHao) + console.log('new customer login process gongHao=>', gongHao) + // 获取管理员的socket + try{ + //获取管理员 socket + let kefuData =await sessoionModel.find(gongHao); + if(kefuData){ // 找到客服数据 + + // 给管理员发送通知 + io.to(kefuData.socket).emit('update-users', info); + } + }catch (e) { + //TODO 失败重发机制 + console.log('给管理员发送通知失败'); + } // 给管理员发送通知 redis.get(AppConfig.KF_PREFIX + gongHao, function (err, sid) { if (err) { console.error(err); } - console.log('sid=>',sid) + console.log('sid=>', sid) if (sid) { redis.get('online_count', function (err, val) { if (err) { @@ -75,6 +90,7 @@ function ioServer(io) { "name": location + ' 客户', "type": 'online' }; + //将用户添加到 console.log(info); redis.get('user-uuids', function (err, uuids) { if (err) { @@ -98,7 +114,7 @@ function ioServer(io) { }); } }); - + //让客服更新用户 io.to(sid).emit('update-users', info); }); diff --git a/model/session.js b/model/session.js new file mode 100644 index 0000000..d6aab1c --- /dev/null +++ b/model/session.js @@ -0,0 +1,59 @@ +var mongoose = require('../utils/mongoose').mongoose; +var crypto = require('crypto'); + +var Schema = mongoose.Schema; + +// session模型 +var SessionSchema = new Schema({ + uid: {type: String, index: true}, + socket: {type: String}, + // 类型 + type: {type: String, default: "customer"}, + // 客服编号 + kefu_id: {type: String}, + nickname: {type: String}, + status: {type: Number}, + create_at: {type: Date, default: Date.now} +}); + +//session model instance +const sessionModel = mongoose.model("sessions", SessionSchema); + + +module.exports = { + find(uid) { + return new Promise((success, fail) => { + sessionModel.findOne({uid, status: 1}, (err, session) => { + if (err) fail(err); + else success(session) + }) + }); + }, + create(data) { + return new Promise((success, fail) => { + sessionModel.insert(data, (err, doc) => { + if (err) fail(err); + else success(doc); + }) + }); + }, + update(uid, data) { + return new Promise((success, fail) => { + sessionModel.findOneAndUpdate({uid}, data, success); + }); + }, + remove(uid) { + return new Promise((success, fail) => { + sessionModel.findOneAndRemove({uid}, data, success); + }); + }, + // 查询列表 + findByCondition(data) { + return new Promise((success, fail) => { + sessionModel.find(data, (err, doc) => { + if (err) fail(err); + else success(doc); + }) + }); + } +} \ No newline at end of file diff --git a/public/js/client/client.js b/public/js/client/client.js index 7194e74..750546c 100644 --- a/public/js/client/client.js +++ b/public/js/client/client.js @@ -200,7 +200,8 @@ $(function(){ var msg = { "uid" : uuid, "ip" : ip, - "gongHao":gongHao + "gongHao":gongHao, + type:'c' }; socket.emit('login', msg); get_message(uuid); diff --git a/public/js/server/index.js b/public/js/server/index.js index 76476a2..3d62d5f 100644 --- a/public/js/server/index.js +++ b/public/js/server/index.js @@ -246,7 +246,8 @@ layui.use(['layer', 'form', 'jquery'], function () { var ip = $("#keleyivisitorip").html(); var msg = { "uid": uuid, - "ip": ip + "ip": ip, + type:'kefu' }; socket.emit('login', msg); }); diff --git a/routes/users.js b/routes/users.js index 62bea67..144bd20 100644 --- a/routes/users.js +++ b/routes/users.js @@ -1,22 +1,18 @@ var express = require('express'); var router = express.Router(); var redis = require('../utils/redis'); +// import model from './../model/session' +const model = require('./../model/session'); + /* GET users listing. */ -router.get('/', function(req, res, next) { - redis.get('user-uuids',function (err,uuids) { - if(err){ - console.error(err); - return res.send({code:400,msg:'获取失败'}); - } - if(uuids){ - uuids =JSON.parse(uuids); - }else{ - uuids = []; - } - - return res.send({code:200,msg:'获取成功',data:uuids}); - }); +router.get('/', async function (req, res, next) { + try { + let data = await model.findByCondition({type: 'customer', kefu_id: req.cookies.username}); + return res.send({code: 200, msg: '获取成功', data: data ? data : []}); + } catch (e) { + return res.send({code: 400, msg: '获取失败'}); + } }); module.exports = router;