1. 程式人生 > >gatewayworker長連線下聊天頁面之聊天記錄初始化

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;
}
}


}