|
|
@@ -1,5 +1,6 @@
|
|
|
<?php
|
|
|
namespace app\api\imsocket;
|
|
|
+use GatewayWorker\Lib\DbConnection;
|
|
|
use think\facade\Db;
|
|
|
use think\worker\Server;
|
|
|
use GatewayWorker\Lib\Gateway;
|
|
|
@@ -27,19 +28,97 @@ class imsocket extends Server
|
|
|
$connection->uid = $arr->uid;
|
|
|
//保存uid到connection的映射,实现针对特定uid的推送
|
|
|
$this->worker->uidConnections[$connection->uid]=$connection;
|
|
|
+ if($arr->school_id){
|
|
|
+ //如果是学校的话,返回一个老师的id给他。
|
|
|
+ $teacherList=Db::table('yoshop_user_info')
|
|
|
+ ->alias('i')
|
|
|
+ ->join('yoshop_user u','i.user_id = u.user_id')
|
|
|
+ ->where('u.user_type','=',3)
|
|
|
+ ->where('u.is_delete','=',0)
|
|
|
+ ->where('i.school_id','=',$arr->school_id)
|
|
|
+ ->where('i.status','=',1)
|
|
|
+ ->select();
|
|
|
+ $tempList=[];
|
|
|
+ foreach($teacherList as $list){
|
|
|
+
|
|
|
+ $temp['uid']= $list['user_id'];
|
|
|
+ $temp['uname']= $list['real_name'];
|
|
|
+ $temp['utype']= $list['user_type'];
|
|
|
+ if(isset($this->worker->uidConnections[$list['user_id']])){
|
|
|
+ //是否在线,在线加权重
|
|
|
+ $temp['w']=1;
|
|
|
+
|
|
|
+ }else{
|
|
|
+ $temp['w']=0;
|
|
|
+ }
|
|
|
+ $tempList[]=$temp;
|
|
|
+ }
|
|
|
+
|
|
|
+ //按权重随机返回
|
|
|
+ $weight = 0;
|
|
|
+ $tempdata = array ();
|
|
|
+ foreach ($tempList as $one) {
|
|
|
+ $weight += $one['w'];
|
|
|
+ for ($i = 0; $i < $one['w']; $i++) {
|
|
|
+ $tempdata[] = $one;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $use = rand(0, $weight -1);
|
|
|
+ $one = $tempdata[$use];
|
|
|
+
|
|
|
+
|
|
|
+ $msg=['type'=>'bind','fansinfo'=>['uid'=>$one['uid'],'uname'=>$one['uname']]];
|
|
|
+ $connection->send(json_encode($msg));
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
+
|
|
|
//聊天
|
|
|
if($arr->type=="text"){
|
|
|
+ $updata=[
|
|
|
+ 'from_user_id'=>$arr->from_user_id,
|
|
|
+ 'to_user_id'=>$arr->to_user_id,
|
|
|
+ 'message'=>$arr->msg,
|
|
|
+ 'school_id'=>$arr->school_id,
|
|
|
+ 'speciality_id'=>$arr->speciality_id,
|
|
|
+ 'sendtime'=>$arr->sendtime,
|
|
|
+ 'chat_key'=>$arr->chat_key
|
|
|
+ ];
|
|
|
if(isset($this->worker->uidConnections[$arr->to_user_id])){
|
|
|
$conn= $this->worker->uidConnections[$arr->to_user_id];
|
|
|
+ $updata['is_push']=0;
|
|
|
$conn->send($data);
|
|
|
}else{
|
|
|
- //先返回给发送者错误信息
|
|
|
- $conn2= $this->worker->uidConnections[$arr->from_user_id];
|
|
|
- $msg=['type'=>'error','msg'=>'对方不在线'];
|
|
|
- $conn2->send(json_encode($msg));
|
|
|
+// //先返回给发送者错误信息
|
|
|
+// $conn2= $this->worker->uidConnections[$arr->from_user_id];
|
|
|
+// $msg=['type'=>'error','msg'=>'对方不在线'];
|
|
|
+// $conn2->send(json_encode($msg));
|
|
|
+ $updata['is_push']=1;
|
|
|
+ }
|
|
|
+ //保存数据
|
|
|
+ Db::name('imchat')->insert($updata);
|
|
|
+
|
|
|
+ }
|
|
|
+ //获取聊天记录
|
|
|
+ if($arr->type=="loadHistory"){
|
|
|
+ $tfkey=$arr->tfkey;
|
|
|
+ $lastMessageTimeStamp=$arr->lastMessageTimeStamp;
|
|
|
+ $from_user_id=$arr->from_user_id;
|
|
|
+ $conn2= $this->worker->uidConnections[$from_user_id];
|
|
|
+
|
|
|
+
|
|
|
+ $msgList=Db::name('imchat')->where('chat_key','=',$tfkey)
|
|
|
+ ->where('sendtime','<',$lastMessageTimeStamp)
|
|
|
+ ->field('id,from_user_id,to_user_id,message,sendtime')
|
|
|
+ ->order('sendtime','desc')
|
|
|
+ ->limit(2)
|
|
|
+ ->select();
|
|
|
+ if($msgList){
|
|
|
+ $msg=['type'=>'loadHistory','mgList'=>$msgList];
|
|
|
+ }else{
|
|
|
+ $msg=['type'=>'loadHistory','mgList'=>''];
|
|
|
}
|
|
|
+ $conn2->send(json_encode($msg));
|
|
|
|
|
|
}
|
|
|
|