Message.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * raingad IM [ThinkPHP6]
  4. * @author xiekunyu <raingad@foxmail.com>
  5. */
  6. namespace app\enterprise\model;
  7. use app\admin\model\KeywordLanguages;
  8. use app\admin\model\QuestionLanguages;
  9. use app\manage\model\Config;
  10. use app\BaseModel;
  11. use think\facade\Db;
  12. use think\facade\Cache;
  13. class Message extends BaseModel
  14. {
  15. protected $pk="msg_id";
  16. protected $json = ["extends"];
  17. protected $jsonAssoc = true;
  18. protected static $fileType=['file','image','video','voice','emoji'];
  19. // 添加聊天记录
  20. public static function addData($data){
  21. return Db::name('message')->insert($data);
  22. }
  23. // 更新消息状态
  24. public static function editData($update,$map){
  25. return Db::name('message')->where($map)->update($update);
  26. }
  27. // 查询聊天记录
  28. public static function getList($map,$where,$sort,$listRows,$pageSize){
  29. $list= Db::name('message')
  30. ->where($map)
  31. ->where($where)
  32. ->order($sort)
  33. ->paginate(['list_rows'=>$listRows,'page'=>$pageSize]);
  34. return $list;
  35. }
  36. // 发送消息
  37. public function sendMessage($param,$globalConfig=false){
  38. $is_group = $param['is_group'] ?? 0;
  39. $uid=self::$uid ? : ($param['user_id'] ?? 1);
  40. $is_robot = false; //是否给机器人发送消息
  41. if($param['toContactId']==-1){
  42. $is_group=0;
  43. }
  44. // 如果是系统账号,直接禁言
  45. if($is_group>1){
  46. $this->error=lang('im.forbidChat');
  47. return false;
  48. }
  49. $isForward=$param['is_forward'] ?? 0;
  50. $sendInterval = $globalConfig['chatInfo']['sendInterval'] ?? 0;
  51. // 如果设置了消息频率则验证,转发不收限制
  52. if ($sendInterval && !$isForward) {
  53. if (Cache::has('send_' . $uid)) {
  54. $this->error=lang('im.sendTimeLimit',['time'=>$sendInterval]);
  55. return false;
  56. }
  57. }
  58. if($param['type']=='text'){
  59. // 限制文字内容长度
  60. $text = strip_tags($param['content']);
  61. $textLen = mb_strlen($text);
  62. if ($textLen > 2048) {
  63. $this->error=lang('im.msgContentLimit') . $textLen;
  64. return false;
  65. }
  66. $param['content'] = preg_link($param['content']);
  67. // 接入聊天内容检测服务
  68. event('GreenText',['content'=>$param['content'],'service'=>"chat_detection"]);
  69. }
  70. $chatSetting = $globalConfig['chatInfo'];
  71. if($param['toContactId']!=-1){
  72. if ($is_group == 0) {
  73. $kefuUser=$chatSetting['autoAddUser']['user_ids'] ?? [];
  74. $manageUser=User::where([['status','=',1],['role','>',0]])->column('user_id');
  75. $kefu=array_unique(array_merge($kefuUser,$manageUser));
  76. $csUid = self::$userInfo['cs_uid'] ?? 0;
  77. $manage=false;
  78. // 发送者和接受者是客服或者管理员也可以发送消息
  79. if(in_array($uid,$kefu) || in_array($param['toContactId'],$kefu)){
  80. $manage=true;
  81. }
  82. if($chatSetting['simpleChat'] == 0 && !$manage){
  83. $this->error=lang('im.forbidChat');
  84. return false;
  85. }
  86. // 如果是单聊,并且是社区模式和不是自己的客服、需要判断是否是好友
  87. if ($globalConfig['sysInfo']['runMode'] == 2 && $csUid != $param['toContactId'] && !$manage) {
  88. // 判断我是不是对方的客服
  89. $cus = User::where(['user_id' => $param['toContactId']])->value('cs_uid');
  90. if ($cus != $uid) {
  91. $friend = Friend::where(['friend_user_id' => $uid, 'create_user' => $param['toContactId']])->find();
  92. if (!$friend) {
  93. $this->error=lang('im.notFriend');
  94. return false;
  95. }
  96. $otherFriend = Friend::where(['friend_user_id' => $param['toContactId'], 'create_user' => $uid])->find();
  97. if (!$otherFriend) {
  98. $this->error=lang('im.friendNot');
  99. return false;
  100. }
  101. }
  102. }
  103. //判断是否给机器人客服发送消息
  104. $autoTask = Config::autoTask();
  105. if (!empty($autoTask['user_id']) && $param['toContactId'] == $autoTask['user_id']) {
  106. $is_robot = true;
  107. }
  108. }else{
  109. // 群聊必须群成员才能发送消息
  110. $group_id = explode('-', $param['toContactId'])[1] ?? '';
  111. if(!$group_id){
  112. $this->error=lang('system.parameterError');
  113. return false;
  114. }
  115. if(!self::nospeak($group_id,$uid)){
  116. if($isForward){
  117. return false;
  118. }
  119. return shutdown(lang('group.notSpeak'));
  120. }
  121. // 群聊必须群成员才能发送消息
  122. $groupUser=GroupUser::where(['user_id'=>$uid,'status'=>1,'group_id'=>$group_id,'delete_time'=>0])->find();
  123. if(!$groupUser){
  124. $this->error = lang('group.notCustom');
  125. return false;
  126. }
  127. if($groupUser['no_speak_time']>time()){
  128. $this->error = lang('group.notSpeak',['time'=>date('Y-m-d H:i:s',$groupUser['no_speak_time'])]);
  129. return false;
  130. }
  131. }
  132. }
  133. if ($sendInterval) {
  134. Cache::set('send_' . $uid, time(), $sendInterval);
  135. }
  136. $data = self::sendMsg($param,$is_group, 0, $uid);
  137. // 机器人自动回复问题推送给用户
  138. if ($is_robot && $param['type'] == 'text') {
  139. //获取关键词匹配
  140. $keyword_ids = KeywordLanguages::getKeywordByContent($param['content']);
  141. $question = QuestionLanguages::getQuestion($keyword_ids, $param['content']);
  142. $data['question'] = $question;
  143. if ($question) {
  144. $param['type'] = 'list';
  145. $content = [
  146. 'title' => Config::where('field','reply_keyword')->value('val'),
  147. 'list' => $question,
  148. ];
  149. $param['content'] = json_encode($content);
  150. } else {
  151. $param['type'] = 'text';
  152. $param['content'] = Config::where('field','reply_keyword_math_fail')->value('val');
  153. }
  154. $fromUserId = $param['toContactId'];
  155. $param['toContactId'] = $param['fromUser']['id'];
  156. $param['user_id'] = $fromUserId;
  157. $param['fromUser'] = [
  158. 'id' => $fromUserId,
  159. 'displayName' => '88888',
  160. 'avatar' => '',
  161. 'account' => 'admin',
  162. ];
  163. $param['content'] = Config::where('field','reply_keyword_math_fail')->value('val');
  164. //$data2 = self::sendMsg($param,$is_group, 0, $param['user_id']);
  165. //机器人自动回复消息
  166. event('AutoReplyMessage', [$param, $globalConfig]);
  167. }
  168. return $data;
  169. }
  170. //实际发送消息
  171. public static function sendMsg($param,$is_group=0,$is_sys=0, $uid=0){
  172. $uid = $uid ?: ($param['user_id'] ?? 1);
  173. $toContactId=$param['toContactId'];
  174. $manage=[];
  175. // 重新建立会话,更新会话删除记录
  176. $isDelChat=ChatDelog::where(['user_id'=>$uid,'to_user'=>$toContactId])->find();
  177. if($isDelChat){
  178. ChatDelog::where(['user_id'=>$uid,'to_user'=>$toContactId])->delete();
  179. ChatDelog::updateCache($uid);
  180. }
  181. if($is_group==1){
  182. $group_id = explode('-', $param['toContactId'])[1] ?? '';
  183. $chat_identify=$toContactId;
  184. $toContactId=$group_id;
  185. $manage=GroupUser::getGroupManage($group_id);
  186. }else{
  187. $chat_identify=chat_identify($param['user_id'],$toContactId);
  188. }
  189. $fileSzie=isset($param['file_size'])?$param['file_size']:'';
  190. $fileName=isset($param['file_name'])?$param['file_name']:'';
  191. $ossUrl=getDiskUrl();
  192. // 如果是转发图片文件的消息,必须把域名去除掉
  193. $content=$param['content'];
  194. if(in_array($param['type'],self::$fileType)){
  195. if(strpos($param['content'],$ossUrl)!==false){
  196. $content=str_replace($ossUrl,'',$param['content']);
  197. }
  198. }
  199. $param['content']=$content;
  200. $atList=($param['at'] ?? null) ? array_map('intval', $param['at']): [];
  201. // 如果at里面有0,代表@所有人
  202. if($atList && in_array(0,$atList)){
  203. $atList=GroupUser::where([['group_id','=',$toContactId],['status','=',1],['user_id','<>',$param['user_id']]])->column('user_id');
  204. }
  205. $at=$atList ? implode(',',$atList) : null;
  206. $data=[
  207. 'from_user'=>$param['user_id'],
  208. 'to_user'=>$toContactId,
  209. 'id'=>$param['id'],
  210. 'content'=>$param['type'] != 'list' ? str_encipher($param['content'],true) : $param['content'],
  211. 'chat_identify'=>$chat_identify,
  212. 'create_time'=>time(),
  213. 'type'=>$param['type'],
  214. 'is_group'=>$toContactId==-1 ? 3 : $is_group,
  215. 'is_read'=>$is_group ? 1 : 0,
  216. 'file_id'=>$param['file_id'] ?? 0,
  217. "file_cate"=>$param['file_cate'] ?? 0,
  218. 'file_size'=>$fileSzie,
  219. 'file_name'=>$fileName,
  220. 'at'=>$at,
  221. 'pid'=>$param['pid'] ?? 0,
  222. 'extends'=>($param['extends'] ?? null) ? $param['extends'] : null,
  223. ];
  224. $message=new self();
  225. $message->update(['is_last'=>0],['chat_identify'=>$chat_identify]);
  226. $message->save($data);
  227. // 拼接消息推送
  228. $type=$is_group?'group':'simple';
  229. $sendData=$param;
  230. $sendData['status']='succeed';
  231. $sendData['at']=$atList;
  232. $sendData['msg_id']=$message->msg_id;
  233. $sendData['is_read']=0;
  234. $sendData['to_user']=$toContactId;
  235. $sendData['role']=$manage[self::$uid] ?? 3;
  236. $sendData['sendTime']=(int)$sendData['sendTime'];
  237. //这里单聊中发送对方的消息,对方是接受状态,自己是对方的联系人,要把发送对象设置为发送者的ID。
  238. if($is_group){
  239. $sendData['toContactId']=$param['toContactId'];
  240. // 将团队所有成员的未读状态+1
  241. GroupUser::editGroupUser([['group_id','=',$toContactId],['user_id','<>',$uid]],['unread'=>Db::raw('unread+1')]);
  242. }else{
  243. $sendData['toContactId']=$toContactId;//$uid;
  244. }
  245. $sendData['fromUser']['id']=(int)$sendData['fromUser']['id'];
  246. $sendData['fileSize']=$fileSzie;
  247. $sendData['fileName']=$fileName;
  248. if(in_array($sendData['type'],self::$fileType)){
  249. $sendData['content']=getFileUrl($sendData['content']);
  250. if($sendData['type']=='image'){
  251. $pre=1;
  252. }else{
  253. $pre=2;
  254. }
  255. $sendData['preview']=previewUrl($sendData['content'],$pre);
  256. $sendData['extUrl']=getExtUrl($sendData['content']);
  257. $sendData['download']= $sendData['file_id'] ? getMainHost().'/filedown/'.encryptIds($sendData['file_id']) : '';
  258. }
  259. $forContactId=$sendData['toContactId'];
  260. if($is_sys){
  261. $forContactId=$toContactId;
  262. }
  263. if($is_group==0){
  264. $toContactId=[$toContactId,$param['user_id']];
  265. }
  266. $sendData['toUser']=$param['toContactId'];
  267. $user=new User();
  268. // 将聊天窗口的联系人信息带上,方便临时会话
  269. $sendData['contactInfo']=$user->setContact($forContactId,$is_group,$sendData['type'],$sendData['content']);
  270. // 向发送方发送消息
  271. wsSendMsg($toContactId,$type,$sendData,$is_group);
  272. $sendData['toContactId']=$param['toContactId'];
  273. return $sendData;
  274. }
  275. // 群禁言
  276. public static function nospeak($group_id,$user_id){
  277. $group=Group::find($group_id);
  278. if($group->owner_id==$user_id){
  279. return true;
  280. }
  281. if($group->setting){
  282. $setting=json_decode($group->setting,true);
  283. $nospeak=isset($setting['nospeak'])?$setting['nospeak']:0;
  284. $role=GroupUser::where(['group_id'=>$group_id,'user_id'=>$user_id])->value('role');
  285. if($nospeak==1 && $role>2){
  286. return false;
  287. }elseif($nospeak==2 && $role!=1){
  288. return false;
  289. }
  290. }
  291. return true;
  292. }
  293. // 将消息中的@用户加入到atListQueue中
  294. public static function setAtread($messages,$user_id){
  295. foreach($messages as $k=>$v){
  296. if(!isset($v['at'])){
  297. continue;
  298. }
  299. if($v['at'] && in_array($user_id,$v['at'])){
  300. $atListQueue=Cache::get("atListQueue");
  301. $atListQueue[$v['msg_id']][]=$user_id;
  302. Cache::set("atListQueue",$atListQueue);
  303. }
  304. }
  305. }
  306. }