基础功能上传

This commit is contained in:
邰广银 2017-10-28 10:30:28 +08:00
parent 3c78c7f4c2
commit 27ecea41cb
9 changed files with 149 additions and 85 deletions

View File

@ -7,6 +7,7 @@ var redis = require('../utils/redis');
var msgType = require('./messageTpye');
var ioSvc = require('./ioHelper').ioSvc;
var AppConfig = require('../config');
var Common = require('../utils/common');
//服务端连接
function ioServer(io) {
@ -14,6 +15,8 @@ function ioServer(io) {
var _self = this;
ioSvc.setInstance(io);
var __uuids = [];
//初始化连接人数
redis.set('online_count',0,null,function (err,ret) {
if(err){
@ -34,7 +37,8 @@ function ioServer(io) {
_self.updateOnlieCount(true);
//用户与Socket进行绑定
socket.on('login', function (uid) {
socket.on('login', function (msg) {
var uid = msg.uid;
console.log(uid+'登录成功');
//通知用户上线
@ -54,36 +58,45 @@ function ioServer(io) {
if(typeof val == 'string'){
val = parseInt(val);
}
var info = {
"uid":uid,
"name":'客户'+val,
"type":'online'
};
io.to(sid).emit('update-users',info);
//var ip = socket.request.connection.remoteAddress;
//此处获取IP可能会有延迟建议改成自己的IP库
Common.getIpLocation(msg.ip,function (err,location) {
var 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) {
@ -146,9 +159,10 @@ function ioServer(io) {
}else{
uuids = [];
}
if(uuids.indexOf(val) != -1){
uuids.remove(val);
var idx = __uuids.indexOf(val);
if( idx != -1){
__uuids.remove(val);
uuids.splice(idx,1);
uuids = JSON.stringify(uuids);
redis.set('user-uuids',uuids,null,function (err,ret) {
if(err){

View File

@ -64,7 +64,14 @@ $(function(){
var fp1 = new Fingerprint();
uuid = fp1.get();
console.log('连接成功...'+uuid);
socket.emit('login', uuid);
var ip = $("#keleyivisitorip").html();
var msg = {
"uid" : uuid,
"ip" : ip
};
socket.emit('login', msg);
});
// /* 后端推送来消息时

View File

@ -1,30 +1,37 @@
//用于生成uuid
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
//用于生成uuid
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
return (S4()+S4()+"-"+S4()+S4()+S4());
}
function guid() {
return (S4()+S4()+"-"+S4()+S4()+S4());
}
function dateFormat(fmt,date) {
if(!fmt){
fmt = "yyyy-MM-dd hh:mm:ss";
}
if(!date){
date = new Date();
}
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
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)));
return fmt;
function dateFormat(fmt,date) {
if(!fmt){
fmt = "yyyy-MM-dd hh:mm:ss";
}
if(!date){
date = new Date();
}
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
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)));
return fmt;
}
function arrayRemove(array,val) {
var index = array.indexOf(val);
if (index > -1) {
array.splice(index, 1);
}
};

View File

@ -10,14 +10,6 @@ layui.use(['layer', 'form', 'jquery'], function () {
var uuids = [];
Array.prototype.remove = function(val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
//页面初始化函数
function init() {
$(".admin-index").addClass("layui-this");
@ -87,14 +79,14 @@ layui.use(['layer', 'form', 'jquery'], function () {
$('.chat-user').html('');
var data = data.data;
var count = 1;
data.forEach(function (uid) {
insert_user_html(uid,'客户' + count++);
data.forEach(function (user) {
insert_user_html(user.uid,user.name);
//创建聊天section
insert_section(uid);
insert_section(user.uid);
});
if(data.length > 0 && !currentUUID){
currentUUID = data[0];
currentUUID = data[0].uid;
}
$(".user-info").css("background","#ffffff");
@ -125,7 +117,12 @@ layui.use(['layer', 'form', 'jquery'], function () {
socket.on('connect', function () {
console.log('连接成功...');
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){
if(msg.type == 'offline'){
$("#"+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'){
if(!currentUUID){
currentUUID = msg.uid;
}
if(uuids.indexOf(msg.uid) == -1){
var index = uuids.indexOf(msg.uid);
if( index == -1){
uuids.push(msg.uid);
insert_user_html(msg.uid,msg.name);
insert_user_html(msg.uid,msg.name + '#'+ (uuids.length + 1));
//创建聊天section
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){
$(".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
View 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;

View File

@ -21,7 +21,7 @@
<!-- 标题栏 -->
<header class="bar bar-nav">
<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>
<!-- 这里是页面内容区 -->
<div class="content">
@ -40,7 +40,7 @@
</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/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>

View File

@ -137,7 +137,7 @@
<div class="friend">
<div class="friend-head">
<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 class="chat-user">
@ -160,5 +160,6 @@
<script src="/socket.io/socket.io.js"></script>
<script type='text/javascript' src='js/common.js' charset='utf-8'></script>
<script src="/js/server/index.js"> </script>
<script type="text/javascript" src="http://tool.keleyi.com/ip/visitoriphost/"></script>
<% include footer.ejs %>

View File

@ -1,7 +1,7 @@
<% include header.ejs %>
<div class="layui-container">
设置
<div class="layui-container" style="text-align: center;">
敬请期待
</div>
<script src="/layui/layui.js"></script>

View File

@ -1,7 +1,7 @@
<% include header.ejs %>
<div class="layui-container">
用户
<div class="layui-container" style="text-align: center;">
敬请期待
</div>
<script src="/layui/layui.js"></script>