aiya
This commit is contained in:
parent
c8c614c47b
commit
653870a991
28
io/io.js
28
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);
|
||||
});
|
||||
|
||||
|
59
model/session.js
Normal file
59
model/session.js
Normal file
@ -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);
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
@ -200,7 +200,8 @@ $(function(){
|
||||
var msg = {
|
||||
"uid" : uuid,
|
||||
"ip" : ip,
|
||||
"gongHao":gongHao
|
||||
"gongHao":gongHao,
|
||||
type:'c'
|
||||
};
|
||||
socket.emit('login', msg);
|
||||
get_message(uuid);
|
||||
|
@ -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);
|
||||
});
|
||||
|
@ -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:'获取失败'});
|
||||
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: '获取失败'});
|
||||
}
|
||||
if(uuids){
|
||||
uuids =JSON.parse(uuids);
|
||||
}else{
|
||||
uuids = [];
|
||||
}
|
||||
|
||||
return res.send({code:200,msg:'获取成功',data:uuids});
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
Loading…
x
Reference in New Issue
Block a user