基础功能上传
This commit is contained in:
parent
3c78c7f4c2
commit
27ecea41cb
76
io/io.js
76
io/io.js
@ -7,6 +7,7 @@ var redis = require('../utils/redis');
|
|||||||
var msgType = require('./messageTpye');
|
var msgType = require('./messageTpye');
|
||||||
var ioSvc = require('./ioHelper').ioSvc;
|
var ioSvc = require('./ioHelper').ioSvc;
|
||||||
var AppConfig = require('../config');
|
var AppConfig = require('../config');
|
||||||
|
var Common = require('../utils/common');
|
||||||
|
|
||||||
//服务端连接
|
//服务端连接
|
||||||
function ioServer(io) {
|
function ioServer(io) {
|
||||||
@ -14,6 +15,8 @@ function ioServer(io) {
|
|||||||
var _self = this;
|
var _self = this;
|
||||||
ioSvc.setInstance(io);
|
ioSvc.setInstance(io);
|
||||||
|
|
||||||
|
var __uuids = [];
|
||||||
|
|
||||||
//初始化连接人数
|
//初始化连接人数
|
||||||
redis.set('online_count',0,null,function (err,ret) {
|
redis.set('online_count',0,null,function (err,ret) {
|
||||||
if(err){
|
if(err){
|
||||||
@ -34,7 +37,8 @@ function ioServer(io) {
|
|||||||
_self.updateOnlieCount(true);
|
_self.updateOnlieCount(true);
|
||||||
|
|
||||||
//用户与Socket进行绑定
|
//用户与Socket进行绑定
|
||||||
socket.on('login', function (uid) {
|
socket.on('login', function (msg) {
|
||||||
|
var uid = msg.uid;
|
||||||
console.log(uid+'登录成功');
|
console.log(uid+'登录成功');
|
||||||
|
|
||||||
//通知用户上线
|
//通知用户上线
|
||||||
@ -54,36 +58,45 @@ function ioServer(io) {
|
|||||||
if(typeof val == 'string'){
|
if(typeof val == 'string'){
|
||||||
val = parseInt(val);
|
val = parseInt(val);
|
||||||
}
|
}
|
||||||
var info = {
|
|
||||||
"uid":uid,
|
//var ip = socket.request.connection.remoteAddress;
|
||||||
"name":'客户'+val,
|
//此处获取IP可能会有延迟,建议改成自己的IP库
|
||||||
"type":'online'
|
Common.getIpLocation(msg.ip,function (err,location) {
|
||||||
};
|
var info = {
|
||||||
io.to(sid).emit('update-users',info);
|
"uid":uid,
|
||||||
|
"name":location + ' 客户',
|
||||||
|
"type":'online'
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
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);
|
|
||||||
uuids = JSON.stringify(uuids);
|
|
||||||
redis.set('user-uuids',uuids,null,function (err,ret) {
|
|
||||||
if(err){
|
|
||||||
console.error(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
redis.set(uid,socket.id,null,function (err,ret) {
|
redis.set(uid,socket.id,null,function (err,ret) {
|
||||||
@ -146,9 +159,10 @@ function ioServer(io) {
|
|||||||
}else{
|
}else{
|
||||||
uuids = [];
|
uuids = [];
|
||||||
}
|
}
|
||||||
|
var idx = __uuids.indexOf(val);
|
||||||
if(uuids.indexOf(val) != -1){
|
if( idx != -1){
|
||||||
uuids.remove(val);
|
__uuids.remove(val);
|
||||||
|
uuids.splice(idx,1);
|
||||||
uuids = JSON.stringify(uuids);
|
uuids = JSON.stringify(uuids);
|
||||||
redis.set('user-uuids',uuids,null,function (err,ret) {
|
redis.set('user-uuids',uuids,null,function (err,ret) {
|
||||||
if(err){
|
if(err){
|
||||||
|
@ -64,7 +64,14 @@ $(function(){
|
|||||||
var fp1 = new Fingerprint();
|
var fp1 = new Fingerprint();
|
||||||
uuid = fp1.get();
|
uuid = fp1.get();
|
||||||
console.log('连接成功...'+uuid);
|
console.log('连接成功...'+uuid);
|
||||||
socket.emit('login', uuid);
|
|
||||||
|
var ip = $("#keleyivisitorip").html();
|
||||||
|
var msg = {
|
||||||
|
"uid" : uuid,
|
||||||
|
"ip" : ip
|
||||||
|
};
|
||||||
|
socket.emit('login', msg);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// /* 后端推送来消息时
|
// /* 后端推送来消息时
|
||||||
|
@ -1,30 +1,37 @@
|
|||||||
//用于生成uuid
|
//用于生成uuid
|
||||||
function S4() {
|
function S4() {
|
||||||
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
|
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function guid() {
|
function guid() {
|
||||||
return (S4()+S4()+"-"+S4()+S4()+S4());
|
return (S4()+S4()+"-"+S4()+S4()+S4());
|
||||||
}
|
}
|
||||||
|
|
||||||
function dateFormat(fmt,date) {
|
function dateFormat(fmt,date) {
|
||||||
if(!fmt){
|
if(!fmt){
|
||||||
fmt = "yyyy-MM-dd hh:mm:ss";
|
fmt = "yyyy-MM-dd hh:mm:ss";
|
||||||
}
|
}
|
||||||
if(!date){
|
if(!date){
|
||||||
date = new Date();
|
date = new Date();
|
||||||
}
|
}
|
||||||
var o = {
|
var o = {
|
||||||
"M+": date.getMonth() + 1, //月份
|
"M+": date.getMonth() + 1, //月份
|
||||||
"d+": date.getDate(), //日
|
"d+": date.getDate(), //日
|
||||||
"h+": date.getHours(), //小时
|
"h+": date.getHours(), //小时
|
||||||
"m+": date.getMinutes(), //分
|
"m+": date.getMinutes(), //分
|
||||||
"s+": date.getSeconds(), //秒
|
"s+": date.getSeconds(), //秒
|
||||||
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
|
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
|
||||||
"S": date.getMilliseconds() //毫秒
|
"S": date.getMilliseconds() //毫秒
|
||||||
};
|
};
|
||||||
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
||||||
for (var k in o)
|
for (var k in o)
|
||||||
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
||||||
return fmt;
|
return fmt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function arrayRemove(array,val) {
|
||||||
|
var index = array.indexOf(val);
|
||||||
|
if (index > -1) {
|
||||||
|
array.splice(index, 1);
|
||||||
|
}
|
||||||
|
};
|
@ -10,14 +10,6 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
|||||||
|
|
||||||
var uuids = [];
|
var uuids = [];
|
||||||
|
|
||||||
Array.prototype.remove = function(val) {
|
|
||||||
var index = this.indexOf(val);
|
|
||||||
if (index > -1) {
|
|
||||||
this.splice(index, 1);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
//页面初始化函数
|
//页面初始化函数
|
||||||
function init() {
|
function init() {
|
||||||
$(".admin-index").addClass("layui-this");
|
$(".admin-index").addClass("layui-this");
|
||||||
@ -87,14 +79,14 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
|||||||
$('.chat-user').html('');
|
$('.chat-user').html('');
|
||||||
|
|
||||||
var data = data.data;
|
var data = data.data;
|
||||||
var count = 1;
|
|
||||||
data.forEach(function (uid) {
|
data.forEach(function (user) {
|
||||||
insert_user_html(uid,'客户' + count++);
|
insert_user_html(user.uid,user.name);
|
||||||
//创建聊天section
|
//创建聊天section
|
||||||
insert_section(uid);
|
insert_section(user.uid);
|
||||||
});
|
});
|
||||||
if(data.length > 0 && !currentUUID){
|
if(data.length > 0 && !currentUUID){
|
||||||
currentUUID = data[0];
|
currentUUID = data[0].uid;
|
||||||
}
|
}
|
||||||
|
|
||||||
$(".user-info").css("background","#ffffff");
|
$(".user-info").css("background","#ffffff");
|
||||||
@ -125,7 +117,12 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
|||||||
socket.on('connect', function () {
|
socket.on('connect', function () {
|
||||||
console.log('连接成功...');
|
console.log('连接成功...');
|
||||||
uuid = 'chat-kefu-admin';
|
uuid = 'chat-kefu-admin';
|
||||||
socket.emit('login', uuid);
|
var ip = $("#keleyivisitorip").html();
|
||||||
|
var msg = {
|
||||||
|
"uid" : uuid,
|
||||||
|
"ip" : ip
|
||||||
|
};
|
||||||
|
socket.emit('login', msg);
|
||||||
});
|
});
|
||||||
|
|
||||||
//后端推送来消息时
|
//后端推送来消息时
|
||||||
@ -138,23 +135,33 @@ layui.use(['layer', 'form', 'jquery'], function () {
|
|||||||
socket.on('update-users', function(msg){
|
socket.on('update-users', function(msg){
|
||||||
if(msg.type == 'offline'){
|
if(msg.type == 'offline'){
|
||||||
$("#"+msg.uid).remove();
|
$("#"+msg.uid).remove();
|
||||||
uuids.remove(msg.uid);
|
arrayRemove(uuids,msg.uid);
|
||||||
|
$("#section-" + msg.uid).remove();
|
||||||
|
$(".chat-user").find("#"+msg.uid).remove();
|
||||||
}else if(msg.type == 'online'){
|
}else if(msg.type == 'online'){
|
||||||
if(!currentUUID){
|
if(!currentUUID){
|
||||||
currentUUID = msg.uid;
|
currentUUID = msg.uid;
|
||||||
}
|
}
|
||||||
if(uuids.indexOf(msg.uid) == -1){
|
var index = uuids.indexOf(msg.uid);
|
||||||
|
if( index == -1){
|
||||||
uuids.push(msg.uid);
|
uuids.push(msg.uid);
|
||||||
insert_user_html(msg.uid,msg.name);
|
insert_user_html(msg.uid,msg.name + '#'+ (uuids.length + 1));
|
||||||
//创建聊天section
|
//创建聊天section
|
||||||
insert_section(msg.uid);
|
insert_section(msg.uid);
|
||||||
|
}else{
|
||||||
|
if($(".chat-user").find("#2316602733").length == 0){
|
||||||
|
insert_user_html(msg.uid,msg.name + '#'+ (uuids.length + 1));
|
||||||
|
//创建聊天section
|
||||||
|
insert_section(msg.uid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//更新用户在线数
|
//更新用户在线数
|
||||||
socket.on('update_online_count', function(msg){
|
socket.on('update_online_count', function(msg){
|
||||||
$(".friend-head-right").html( (msg.online_count - 1) + '人' );
|
var count = (msg.online_count - 1) >= 0 ? (msg.online_count - 1) : 0;
|
||||||
|
$(".friend-head-right").html( count + '人' );
|
||||||
});
|
});
|
||||||
|
|
||||||
//切换用户
|
//切换用户
|
||||||
|
28
utils/common.js
Normal file
28
utils/common.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
var http=require('http');
|
||||||
|
|
||||||
|
function getClientIp(req) {
|
||||||
|
return req.headers['x-forwarded-for'] ||
|
||||||
|
req.connection.remoteAddress ||
|
||||||
|
req.socket.remoteAddress ||
|
||||||
|
req.connection.socket.remoteAddress;
|
||||||
|
};
|
||||||
|
|
||||||
|
function getIpLocation(ip,callback) {
|
||||||
|
http.get('http://ip.taobao.com/service/getIpInfo.php?ip='+ip,function(req,res){
|
||||||
|
var html='';
|
||||||
|
req.on('data',function(data){
|
||||||
|
html+=data;
|
||||||
|
});
|
||||||
|
req.on('end',function(){
|
||||||
|
console.info(html);
|
||||||
|
var json = JSON.parse(html);
|
||||||
|
if(json.code == 0){
|
||||||
|
return callback(null,json.data.region + json.data.city);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.getClientIp = getClientIp;
|
||||||
|
exports.getIpLocation = getIpLocation;
|
@ -21,7 +21,7 @@
|
|||||||
<!-- 标题栏 -->
|
<!-- 标题栏 -->
|
||||||
<header class="bar bar-nav">
|
<header class="bar bar-nav">
|
||||||
<a class="icon icon-me pull-left open-panel"></a>
|
<a class="icon icon-me pull-left open-panel"></a>
|
||||||
<h1 class="title">客服系统</h1>
|
<h1 class="title">客服系统<span id="keleyivisitorip" style="display: none;"></span></h1>
|
||||||
</header>
|
</header>
|
||||||
<!-- 这里是页面内容区 -->
|
<!-- 这里是页面内容区 -->
|
||||||
<div class="content">
|
<div class="content">
|
||||||
@ -40,7 +40,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<script type="text/javascript" src="http://tool.keleyi.com/ip/visitoriphost/"></script>
|
||||||
<script type='text/javascript' src='//g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script>
|
<script type='text/javascript' src='//g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script>
|
||||||
<script type='text/javascript' src='//g.alicdn.com/msui/sm/0.6.2/js/sm.min.js' charset='utf-8'></script>
|
<script type='text/javascript' src='//g.alicdn.com/msui/sm/0.6.2/js/sm.min.js' charset='utf-8'></script>
|
||||||
<script type='text/javascript' src='//g.alicdn.com/msui/sm/0.6.2/js/sm-extend.min.js' charset='utf-8'></script>
|
<script type='text/javascript' src='//g.alicdn.com/msui/sm/0.6.2/js/sm-extend.min.js' charset='utf-8'></script>
|
||||||
|
@ -137,7 +137,7 @@
|
|||||||
<div class="friend">
|
<div class="friend">
|
||||||
<div class="friend-head">
|
<div class="friend-head">
|
||||||
<span class="friend-head-right">0人</span>
|
<span class="friend-head-right">0人</span>
|
||||||
<span class="help-my-chat">我的对话</span>
|
<span class="help-my-chat">我的对话<span id="keleyivisitorip" style="display: none;"></span></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="chat-user">
|
<div class="chat-user">
|
||||||
|
|
||||||
@ -160,5 +160,6 @@
|
|||||||
<script src="/socket.io/socket.io.js"></script>
|
<script src="/socket.io/socket.io.js"></script>
|
||||||
<script type='text/javascript' src='js/common.js' charset='utf-8'></script>
|
<script type='text/javascript' src='js/common.js' charset='utf-8'></script>
|
||||||
<script src="/js/server/index.js"> </script>
|
<script src="/js/server/index.js"> </script>
|
||||||
|
<script type="text/javascript" src="http://tool.keleyi.com/ip/visitoriphost/"></script>
|
||||||
|
|
||||||
<% include footer.ejs %>
|
<% include footer.ejs %>
|
@ -1,7 +1,7 @@
|
|||||||
<% include header.ejs %>
|
<% include header.ejs %>
|
||||||
|
|
||||||
<div class="layui-container">
|
<div class="layui-container" style="text-align: center;">
|
||||||
设置
|
敬请期待
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="/layui/layui.js"></script>
|
<script src="/layui/layui.js"></script>
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<% include header.ejs %>
|
<% include header.ejs %>
|
||||||
|
|
||||||
<div class="layui-container">
|
<div class="layui-container" style="text-align: center;">
|
||||||
用户
|
敬请期待
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="/layui/layui.js"></script>
|
<script src="/layui/layui.js"></script>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user