gatewayworker长连接下聊天页面之聊天记录初始化

gatewayworker长连接下聊天页面之聊天记录初始化
针对课程:https://study.163.com/course/courseLearn.htm?courseId=1005015012#/learn/video?lessonId=1051355043&courseId=1005015012

前端页面:

var fromid = { $fromid};
var toid = { $toid};
var API_URL = "/api/chat/";
var from_head = '';
var to_head = '';
var to_name='';
$(".send-btn").click(function(){ 
ver text = $(".send-input").val();

var message = '{"data":"'+text+'","type":"say","fromid":"'+fromid+'","toid":"'+toid+'"}';
$(".chat-content").append('<div><span style="background-image:url('+from_head+')"></span>'+text+'</div>');
ws.send(message);
$(".send-input").val("");
})


}

ws.onmessage = function(e){ 
var  message = eval("("+e.data+")"); //将客户端收到的json转换成js数据
switch(message.type){ 
case "init":
var bind = '{"type":'bind',"fromid":"'+fromid+'"}';
ws.send(bind);
get_head(fromid,toid);
get_name(toid);
message_load();//当页面加载的时候,执行。





ruturn;

case "text":

if(toid==message.fromid){ 
$(".chat-content").append('<div><span style="background-image:url('+to_head+')"></span>'+message.data+'</div>');
}
retrun;

case "save":
save_message(message);
return;

}
}

function save_message(message){ 
$.post(
API_URL+"save_message",
message,
function(){ },'json'
)
}

function  get_head(fromid,toid){ 
$.post(
API_URL+"get_head",
{ "fromid":fromid,"toid":toid},
function(e){ 
from_head = e.from_head;
to_head = e.from_head;
},'json'
);

}

function get_name(toid){ 
$.post(
API_URL+"get_name",
{ "uid"::toid},
function(e){ 
to_name = e.toname;
$(".shop-title").text("对方用户昵称是:"+toname);
console.log(e);
},'json'
);
}


function message_load()
{ 
$.post(
API_URL+"load",
{ "fromd":fromid,"toid":toid},
function(e){ 
//循环输出数据
$.each(e,function(index,content)){ 

//index是数据的下标
if(fromid==content.fromid){ 
//我发给对方的信息,要展示在右侧
$(".chat-content").append('<div><span style="background-image:url('+from_head+')"></span>'+content.content+'</div>');
}else{ 
//对方发送数据,展示在左侧
$(".chat-content").append('<div><span style="background-image:url('+to_head+')"></span>'+content.content+'</div>');
}
})

},'json'
);
};

API模块下,Chat控制器:

namespace app\api\controller;

class Chat extends Controller{ 

/*文本消息的数据持久化*/
public function save_message(){ 
if(Request::instance()->isAjax()){ 
$message = input("post.");
$datas['fromid'] = $message['fromid'];
$datas['fromname'] =$this->getName($datas['fromid']) ;
$datas['toid'] = $message['toid'];
$datas['toname'] =$this->getName($datas['toid']) ;
$datas['content'] = $message['data'];
$datas['time'] = $message['time'];
$datas['isread'] = $message['isread'];
$datas['type'] = 1 ;//文本为1,图片为2
Db::name("communication")->insert($datas);

}
}


/*根据用户id,返回用户的姓名*/
public function getName($uid){ 
$userinfo = Db::name("user")->where('id',$uid)->field('nickname')->find();
return $userinfo['nickname'];


/*获取用户头像*/

public function get_head(){ 
if(Request::instance()->isAjax()){ 
$fromid = input('fromid');
$toid = input('toid');
$frominfo =Db::name("user")->where('id',$fromid)->field('headimgurl')->find();
$toinfo =Db::name("user")->where('id',$toid)->field('headimgurl')->find();
return[
'from_head' =>$frominfo['headimgurl'],
'to_head' =>$toinfo['headimgurl']
];


}
}

/*获取用户名称*/

public function get_name(){ 

if(Request::instance()->isAjax()){ 
$uid = input('uid');

$toinfo =Db::name("user")->where('id',$uid)->field('nickname')->find();

return[
'to_name' =>$toinfo['nickname']
];
}
}


/*获取用户聊天记录*/

public function load(){ 

if(Request::instance()->isAjax()){ 
$fromid = input('fromid');
$toid = input('toid');

$count =Db::name("communication")->where('(fromid=:fromid and toid=:toid) || (fromid=:toid1 and toid=:fromid1)',['fromid'=>$fromid,'toid'=>$toid,'toid1'=>$toid,'fromid1'=>$fromid])->field('nickname')->count('id'); //:***的方式是占位符,参考http://blog.51cto.com/wujuxiang/403679

if($count>=10){ 
$message = Db::name("communication")->where('(fromid=:fromid and toid=:toid) || (fromid=:toid1 and toid=:fromid1)',['fromid'=>$fromid,'toid'=>$toid,'toid1'=>$toid,'fromid1'=>$fromid])->field('nickname')->limit($count-10,10)->order('id')->select(); //:***的方式是占位符,参考http://blog.51cto.com/wujuxiang/403679}
}else{ 
$message =Db::name("communication")->where('(fromid=:fromid and toid=:toid) || (fromid=:toid1 and toid=:fromid1)',['fromid'=>$fromid,'toid'=>$toid,'toid1'=>$toid,'fromid1'=>$fromid])->field('nickname')->order('id')-->select(); //:***的方式是占位符,参考http://blog.51cto.com/wujuxiang/403679}
return $message;
}
}


}
    原文作者:sinat_34469308
    原文地址: https://blog.csdn.net/sinat_34469308/article/details/84873054
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞